Demetrius Murray 6 years ago
parent
commit
25801f593f

+ 2
- 0
.idea/compiler.xml View File

@@ -7,10 +7,12 @@
7 7
         <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
8 8
         <outputRelativeToContentRoot value="true" />
9 9
         <module name="bankaccountlab" />
10
+        <module name="productmanager" />
10 11
       </profile>
11 12
     </annotationProcessing>
12 13
     <bytecodeTargetLevel target="1.8">
13 14
       <module name="bankaccountlab" target="1.5" />
15
+      <module name="productmanager" target="1.5" />
14 16
     </bytecodeTargetLevel>
15 17
   </component>
16 18
 </project>

+ 13
- 0
.idea/libraries/Maven__junit_junit_4_12.xml View File

@@ -0,0 +1,13 @@
1
+<component name="libraryTable">
2
+  <library name="Maven: junit:junit:4.12">
3
+    <CLASSES>
4
+      <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
5
+    </CLASSES>
6
+    <JAVADOC>
7
+      <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12-javadoc.jar!/" />
8
+    </JAVADOC>
9
+    <SOURCES>
10
+      <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12-sources.jar!/" />
11
+    </SOURCES>
12
+  </library>
13
+</component>

+ 13
- 0
.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml View File

@@ -0,0 +1,13 @@
1
+<component name="libraryTable">
2
+  <library name="Maven: org.hamcrest:hamcrest-core:1.3">
3
+    <CLASSES>
4
+      <root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
5
+    </CLASSES>
6
+    <JAVADOC>
7
+      <root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3-javadoc.jar!/" />
8
+    </JAVADOC>
9
+    <SOURCES>
10
+      <root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3-sources.jar!/" />
11
+    </SOURCES>
12
+  </library>
13
+</component>

+ 1
- 0
.idea/modules.xml View File

@@ -3,6 +3,7 @@
3 3
   <component name="ProjectModuleManager">
4 4
     <modules>
5 5
       <module fileurl="file://$PROJECT_DIR$/bankaccountlab.iml" filepath="$PROJECT_DIR$/bankaccountlab.iml" />
6
+      <module fileurl="file://$PROJECT_DIR$/productmanager.iml" filepath="$PROJECT_DIR$/productmanager.iml" />
6 7
     </modules>
7 8
   </component>
8 9
 </project>

+ 494
- 376
.idea/workspace.xml
File diff suppressed because it is too large
View File


+ 8
- 0
pom.xml View File

@@ -7,6 +7,14 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>productmanager</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>4.12</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
 
11 19
 
12 20
 </project>

+ 101
- 0
src/main/java/com/zipcodewilmington/productmanager/Inventory.java View File

@@ -0,0 +1,101 @@
1
+package com.zipcodewilmington.productmanager;
2
+
3
+import javax.sound.midi.Soundbank;
4
+import java.util.ArrayList;
5
+
6
+public class Inventory {
7
+    ArrayList<Product> inventory = new ArrayList<Product>();
8
+
9
+    public void addProduct(String name, int id, double price, int quantity){
10
+        if (!inventory.contains(name)) {
11
+            inventory.add(new Product(name, id, price, quantity));
12
+            System.out.printf("New item added | name:" + name + " | id: %03d | Price: $%3.2f | Quantity: %s\n", id, price, quantity);
13
+        } else {
14
+            System.out.printf(name + " already exists. " + "id: %03d | Price: $%3.2f | Quantity: %s\n", getIdByName(name), getPriceByName(name), getQuantByName(name));
15
+        }
16
+    }
17
+
18
+    public void removeProduct(String name, int id){
19
+        if(getIdByName(name) == id){
20
+            inventory.remove(findProduct(id));
21
+            System.out.printf(name + "(id: %03d) has been removed from inventory\n", id);
22
+        } else {
23
+            System.out.printf("The item \'" + name + "\' and id \'%03d\' + you entered do not match", id);
24
+        }
25
+    }
26
+
27
+    public Product findProduct(String name){
28
+        Product result = null;
29
+        for(Product product : inventory){
30
+            if (product.getName().equals(name)){
31
+                result = product;
32
+                break;
33
+            } else if (!inventory.contains(product)){
34
+                System.out.println("Can't find product by name given");
35
+            }
36
+        }
37
+        return result;
38
+    }
39
+
40
+    public Product findProduct(int id){
41
+        Product result = null;
42
+        for(Product product : inventory){
43
+            if (product.getId() == id){
44
+                result = product;
45
+            } else if (!inventory.contains(product)){
46
+                System.out.println("Can't find product by id given");
47
+            }
48
+        }
49
+        return result;
50
+    }
51
+
52
+    public int getIdByName(String name){
53
+        if (findProduct(name) != null) {
54
+            return findProduct(name).getId();
55
+        } else {
56
+            System.out.println(name + "Does not exist with name given");
57
+            return 0;
58
+        }
59
+    }
60
+
61
+    public double getPriceByName(String name){
62
+        if (findProduct(name) != null) {
63
+            return findProduct(name).getPrice();
64
+        } else {
65
+            System.out.println("Price for " + name + "does not exist with name given");
66
+            return 0;
67
+        }
68
+    }
69
+
70
+    public int getQuantByName(String name){
71
+        if (findProduct(name) != null) {
72
+            return findProduct(name).getQuantity();
73
+        } else {
74
+            System.out.println("Quantity for " + name + "does not exist with name given");
75
+            return 0;
76
+        }
77
+    }
78
+
79
+    public String getNameById(int id) {
80
+        if (findProduct(id) != null) {
81
+            return findProduct(id).getName();
82
+        } else {
83
+            System.out.printf("Product for \'id:%03d\' + does not exist with id given\n", id);
84
+            return "";
85
+        }
86
+    }
87
+
88
+    public int countItems(){
89
+        int sum = 0;
90
+        for(Product product : inventory){
91
+            sum+=product.getQuantity();
92
+        }
93
+        System.out.println(sum + " total items in store.");
94
+        return sum;
95
+    }
96
+
97
+    public int countProducts(){
98
+        System.out.println(inventory.size() + " unique products in store.");
99
+        return inventory.size();
100
+    }
101
+}

+ 18
- 0
src/main/java/com/zipcodewilmington/productmanager/MainApplication.java View File

@@ -4,4 +4,22 @@ package com.zipcodewilmington.productmanager;
4 4
  * Created by leon on 1/10/18.
5 5
  */
6 6
 public class MainApplication {
7
+    public static void main(String[] args){
8
+        Inventory manager = new Inventory();
9
+        manager.addProduct("Sneakers", 001, 75,100);
10
+        manager.addProduct("Socks",002,10,200);
11
+        manager.addProduct("Shorts",003,50,300);
12
+
13
+        manager.countItems();
14
+        manager.countProducts();
15
+
16
+        System.out.println("Sneakers cost: " + manager.getPriceByName("Sneakers"));
17
+        System.out.println("There are " + manager.getQuantByName("Sneakers") + " " + manager.getNameById(001) + " in the store remaining");
18
+
19
+        manager.removeProduct("Sneakers",001);
20
+        manager.inventory.size();
21
+
22
+        //try to find something that doesn't exist
23
+        manager.getNameById(004);
24
+    }
7 25
 }

+ 43
- 0
src/main/java/com/zipcodewilmington/productmanager/Product.java View File

@@ -4,4 +4,47 @@ package com.zipcodewilmington.productmanager;
4 4
  * Created by leon on 1/10/18.
5 5
  */
6 6
 public class Product {
7
+    private String name;
8
+    private double price;
9
+    private int id;
10
+    private int quantity;
11
+
12
+    public Product(String name, int id, double price, int quantity){
13
+        this.name = name;
14
+        this.price = price;
15
+        this.id = id;
16
+        this.quantity = quantity;
17
+    }
18
+
19
+    public void setName(String name){
20
+        this.name = name;
21
+    }
22
+
23
+    public String getName(){
24
+        return name;
25
+    }
26
+
27
+    public void setPrice(double price){
28
+        this.price = price;
29
+    }
30
+
31
+    public double getPrice(){
32
+        return price;
33
+    }
34
+
35
+    public void setId(int id){
36
+        this.id = id;
37
+    }
38
+
39
+    public int getId(){
40
+        return id;
41
+    }
42
+
43
+    public void setQuantity(int quantity){
44
+        this.quantity = quantity;
45
+    }
46
+
47
+    public int getQuantity(){
48
+        return quantity;
49
+    }
7 50
 }

+ 111
- 0
src/test/java/com/zipcodewilmington/productmanager/InventoryTest.java View File

@@ -0,0 +1,111 @@
1
+package com.zipcodewilmington.productmanager;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class InventoryTest {
7
+    Inventory inv = new Inventory();
8
+    @Test
9
+    public void addProductTest(){
10
+        int preAdd = inv.inventory.size();
11
+        inv.addProduct("Computer",100, 5.00,50);
12
+        int postAdd = inv.inventory.size();
13
+
14
+        int actual = postAdd - preAdd;
15
+        int expected = 1;
16
+
17
+        Assert.assertEquals(expected, actual);
18
+    }
19
+
20
+    @Test
21
+    public void removeProductTest(){
22
+        inv.addProduct("Computer",100,5.00,50);
23
+        int preRem = inv.inventory.size();
24
+        inv.removeProduct("Computer", 100);
25
+        int postRem = inv.inventory.size();
26
+
27
+        int actual = postRem - preRem;
28
+        int expected = -1;
29
+
30
+        Assert.assertEquals(expected,actual);
31
+    }
32
+
33
+    @Test
34
+    public void findProductByNameTest(){
35
+        inv.addProduct("Notebook", 101,1.00,30);
36
+        inv.addProduct("Computer",100,5.00,50);
37
+        String actual = inv.findProduct("Notebook").getName();
38
+        //using getName() of Product class since it works. Don't know how to represent product since didn't instantiate it with a name
39
+        String expected ="Notebook";
40
+
41
+        Assert.assertEquals(expected,actual);
42
+    }
43
+
44
+    @Test
45
+    public void findProductByIdTest(){
46
+        inv.addProduct("Notebook", 101,1.00,30);
47
+        inv.addProduct("Computer",100,5.00,50);
48
+        //using getName() of Product class since it works. Don't know how to represent product since didn't instantiate it with a name
49
+        String actual = inv.findProduct(101).getName();
50
+        String expected ="Notebook";
51
+
52
+        Assert.assertEquals(expected,actual);
53
+    }
54
+
55
+    @Test
56
+    public void getIdByNameTest(){
57
+        inv.addProduct("Stapler", 103,2,20);
58
+        int actual = inv.getIdByName("Stapler");
59
+        int expected = 103;
60
+
61
+        Assert.assertEquals(expected,actual);
62
+    }
63
+
64
+    @Test
65
+    public void getPriceByNameTest(){
66
+        inv.addProduct("Stapler", 103,2,20);
67
+        double actual = inv.getPriceByName("Stapler");
68
+        double expected = 2;
69
+
70
+        Assert.assertEquals(expected,actual,0.0099);
71
+    }
72
+
73
+    @Test
74
+    public void getQuantByNameTest(){
75
+        inv.addProduct("Stapler", 103,2,20);
76
+        int actual = inv.getQuantByName("Stapler");
77
+        int expected = 20;
78
+
79
+        Assert.assertEquals(expected,actual);
80
+    }
81
+
82
+    @Test
83
+    public void getNamebyIdTest(){
84
+        inv.addProduct("Stapler", 103,2,20);
85
+        String actual = inv.getNameById(103);
86
+        String expected = "Stapler";
87
+
88
+        Assert.assertEquals(expected,actual);
89
+    }
90
+
91
+    @Test
92
+    public void countItemsTest(){
93
+        inv.addProduct("Notebook", 101,1.00,30);
94
+        inv.addProduct("Computer",100,5.00,50);
95
+        int actual = inv.countItems();
96
+        int expected = 80;
97
+
98
+        Assert.assertEquals(expected,actual);
99
+    }
100
+
101
+    @Test
102
+    public void countProductsTest(){
103
+        inv.addProduct("Notebook", 101,1.00,30);
104
+        inv.addProduct("Computer",100,5.00,50);
105
+        int actual = inv.countProducts();
106
+        int expected = 2;
107
+
108
+        Assert.assertEquals(expected,actual);
109
+    }
110
+
111
+}

+ 70
- 0
src/test/java/com/zipcodewilmington/productmanager/ProductTest.java View File

@@ -1,7 +1,77 @@
1 1
 package com.zipcodewilmington.productmanager;
2 2
 
3
+import org.junit.Test;
4
+import org.junit.Assert;
5
+
3 6
 /**
4 7
  * Created by leon on 1/10/18.
5 8
  */
6 9
 public class ProductTest {
10
+    Product prod = new Product("Computer", 100,5.00, 50);
11
+
12
+    @Test
13
+    public void getName(){
14
+        String actual = prod.getName();
15
+        String expected = "Computer";
16
+
17
+        Assert.assertEquals(expected,actual);
18
+    }
19
+
20
+    @Test
21
+    public void setName(){
22
+        prod.setName("Laptop");
23
+        String actual = prod.getName();
24
+        String expected = "Laptop";
25
+
26
+        Assert.assertEquals(expected,actual);
27
+    }
28
+    @Test
29
+    public void getPriceTest(){
30
+        double actual = prod.getPrice();
31
+        double expected = 5.00;
32
+
33
+        Assert.assertEquals(expected, actual, 0.1);
34
+    }
35
+
36
+    @Test
37
+    public void setPriceTest(){
38
+        prod.setPrice(6.00);
39
+        double actual = prod.getPrice();
40
+        double expected = 6.00;
41
+
42
+        Assert.assertEquals(expected,actual,.1);
43
+    }
44
+
45
+    @Test
46
+    public void getIdTest(){
47
+        int actual = prod.getId();
48
+        int expected = 100;
49
+
50
+        Assert.assertEquals(expected, actual);
51
+    }
52
+
53
+    @Test
54
+    public void setIdTest(){
55
+        prod.setId(002);
56
+        int actual = prod.getId();
57
+        int expected = 002;
58
+
59
+        Assert.assertEquals(expected,actual);
60
+    }
61
+    @Test
62
+    public void getQuantityTest(){
63
+        int actual = prod.getQuantity();
64
+        int expected = 50;
65
+
66
+        Assert.assertEquals(expected, actual);
67
+    }
68
+
69
+    @Test
70
+    public void setQuantityTest(){
71
+        prod.setQuantity(60);
72
+        int actual = prod.getQuantity();
73
+        int expected = 60;
74
+
75
+        Assert.assertEquals(expected,actual);
76
+    }
7 77
 }