소스 검색

completed lab

Allison Ziegler 6 년 전
부모
커밋
39adafe90c
6개의 변경된 파일316개의 추가작업 그리고 8개의 파일을 삭제
  1. 63
    0
      Inventory.java
  2. 87
    0
      InventoryTest.java
  3. 45
    0
      MainApplication.java
  4. 27
    0
      Product.java
  5. 35
    0
      ProductTest.java
  6. 59
    8
      package.bluej

+ 63
- 0
Inventory.java 파일 보기

@@ -0,0 +1,63 @@
1
+
2
+import java.util.Hashtable;
3
+import java.util.Set;
4
+
5
+/**
6
+ * Write a description of class Inventory here.
7
+ *
8
+ * @author (your name)
9
+ * @version (a version number or a date)
10
+ */
11
+public class Inventory
12
+{
13
+    Hashtable<String, Product> productInventory;
14
+    
15
+    public Inventory() {
16
+        productInventory = new Hashtable<String, Product>();
17
+    }
18
+    
19
+    public void addItem(Product prod) {
20
+        productInventory.put(prod.getID(), prod);
21
+    }
22
+    
23
+    public void addItem(float price, int quantity, String id) {
24
+        Product newItem = new Product(price, quantity, id);
25
+        productInventory.put(id, newItem);
26
+    }
27
+    
28
+    public Product getItem(String id) {
29
+        return productInventory.get(id);
30
+    }
31
+    public int getOnHandQuantity(String id) {
32
+        return productInventory.get(id).getOnHandQuantity() ;
33
+    }
34
+    public float getPrice(String id) {
35
+        return productInventory.get(id).getPrice();
36
+    }
37
+    
38
+    public void updatePrice(String id, float price) {
39
+        productInventory.get(id).setPrice(price);
40
+    }
41
+    
42
+    public void addUnits(String id, int quantity) {
43
+        productInventory.get(id).addUnits(quantity);
44
+    }
45
+    
46
+    public void removeUnits(String id, int quantity) {
47
+        productInventory.get(id).removeUnits(quantity);
48
+    }
49
+    
50
+    public double getInventoryValue() {
51
+        double inventoryValue = 0;
52
+        Set<String> productIDs = productInventory.keySet();
53
+        
54
+        for (String id : productIDs) {
55
+            Product p = productInventory.get(id);
56
+            int quantity = p.getOnHandQuantity();
57
+            float price = p.getPrice();
58
+            inventoryValue += quantity * price;
59
+        }
60
+        
61
+        return inventoryValue;
62
+    }
63
+}

+ 87
- 0
InventoryTest.java 파일 보기

@@ -0,0 +1,87 @@
1
+
2
+
3
+import org.junit.Assert;
4
+import org.junit.After;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+/**
9
+ * The test class InventoryTest.
10
+ *
11
+ * @author  (your name)
12
+ * @version (a version number or a date)
13
+ */
14
+public class InventoryTest
15
+{
16
+    @Test
17
+    public void addItemTest1() {
18
+        String expected = "dragon";
19
+        
20
+        Inventory danysStuff = new Inventory();
21
+        danysStuff.addItem(new Product(25.00f, 3, "dragon"));
22
+        
23
+        Product dragon = danysStuff.getItem("dragon");
24
+        String actual = dragon.getID();
25
+        
26
+        Assert.assertEquals(actual, expected);
27
+    }
28
+    @Test
29
+    public void addItemTest2() {
30
+        String expected = "dragon";
31
+        
32
+        Inventory danysStuff = new Inventory();
33
+        danysStuff.addItem(25.00f, 3, "dragon");
34
+        
35
+        Product dragon = danysStuff.getItem("dragon");
36
+        String actual = dragon.getID();
37
+        
38
+        Assert.assertEquals(actual, expected);
39
+    }
40
+    @Test
41
+    public void addUnitsTest() {
42
+        int expected = 6;
43
+        
44
+        Inventory danysStuff = new Inventory();
45
+        danysStuff.addItem(new Product(25.00f, 3, "dragon"));
46
+        danysStuff.addUnits("dragon", 3);
47
+        
48
+        int actual = danysStuff.getOnHandQuantity("dragon");
49
+        
50
+        Assert.assertEquals(actual, expected);
51
+    }
52
+    @Test
53
+    public void removeUnitsTest() {
54
+        int expected = 2;
55
+        
56
+        Inventory danysStuff = new Inventory();
57
+        danysStuff.addItem(25.00f, 3, "dragon");
58
+        danysStuff.removeUnits("dragon", 1);
59
+        
60
+        int actual = danysStuff.getOnHandQuantity("dragon");
61
+        
62
+        Assert.assertEquals(actual, expected);
63
+    }
64
+    @Test
65
+    public void updatePriceTest() {
66
+        float expected = 516.99f;
67
+        
68
+        Inventory danysStuff = new Inventory();
69
+        danysStuff.addItem(25.00f, 3, "dragon");
70
+        danysStuff.updatePrice("dragon", 516.99f);
71
+        float actual = danysStuff.getPrice("dragon");
72
+        
73
+        Assert.assertEquals(actual, expected, 0.001f);
74
+    }
75
+    @Test
76
+    public void inventoryValueTest() {
77
+        double expected = 57.44;
78
+        
79
+        Inventory starkStuff = new Inventory();
80
+        starkStuff.addItem(3.00f, 9, "direwolf collar");
81
+        starkStuff.addItem(2.50f, 5, "direworlf treat");
82
+        starkStuff.addItem(2.99f, 6, "winterfell snowglobe");
83
+        double actual = starkStuff.getInventoryValue();
84
+        
85
+        Assert.assertEquals(actual, expected, 0.001f);
86
+    }
87
+}

+ 45
- 0
MainApplication.java 파일 보기

@@ -3,4 +3,49 @@
3 3
  * Created by leon on 1/10/18.
4 4
  */
5 5
 public class MainApplication {
6
+
7
+    public static void main(String[] args) {
8
+       System.out.println("This is a quick inventory demo!");
9
+       
10
+       Inventory starkStuff = new Inventory();
11
+       System.out.println("We can create a new inventory for Rickon Stark's new store, Stark Stuff!");
12
+       
13
+       starkStuff.addItem(3.00f, 9, "direwolf collar");
14
+       System.out.println("We added an invetory item called " + starkStuff.getItem("direwolf collar").getID());
15
+       System.out.println("The price is: " + starkStuff.getPrice("direwolf collar"));
16
+       System.out.println("The quanitity is: " + starkStuff.getOnHandQuantity("direwolf collar"));
17
+       
18
+       starkStuff.addItem(2.50f, 5, "direwolf treat");
19
+       starkStuff.addItem(2.99f, 6, "winterfell snowglobe");
20
+       
21
+       System.out.println(String.format("There are also %d direwolf treats that cost $%.2f.", 
22
+                          starkStuff.getOnHandQuantity("direwolf treat"), starkStuff.getPrice("direwolf treat"))); 
23
+           
24
+       System.out.println(String.format("There are also %d winterfell snowglobes that cost $%.2f.", 
25
+                          starkStuff.getOnHandQuantity("winterfell snowglobe"), starkStuff.getPrice("winterfell snowglobe")));
26
+       
27
+       starkStuff.updatePrice("winterfell snowglobe", 199.99f);
28
+       System.out.println("But wait!");
29
+       System.out.println("After Jon Snow returned to Winterfell, the demand for winterfell snowglobes shot way up!");
30
+       System.out.println("Rickon decides to up the price. (sadly, he must do this from beyond the grave)");
31
+       System.out.println(String.format("The new price of winterfell snowglobes is: $%.2f", starkStuff.getPrice("winterfell snowglobe"))); 
32
+       
33
+       starkStuff.removeUnits("direwolf treat", 3);
34
+       System.out.println("With Jon Snow's direwolf Ghost around, the direwolf treats are selling fast.");
35
+       System.out.println(String.format("The new on hand quantity for direwolf treats is: %d", starkStuff.getOnHandQuantity("direwolf treat")));
36
+       
37
+       starkStuff.addUnits("winterfell snowglobe", 15);
38
+       
39
+       System.out.println("Luckily there is a new shipment of winterfell snowglobe.");
40
+       System.out.println(String.format("The new on hand quantity for winterfell snowglobes is: %d", starkStuff.getOnHandQuantity("winterfell snowglobe")));
41
+       
42
+       double inVal = starkStuff.getInventoryValue();
43
+       
44
+       System.out.println(String.format("Now, the total value of the late Rickon Stark's inventory is: $%.2f", inVal));
45
+       
46
+    }
6 47
 }
48
+
49
+
50
+
51
+

+ 27
- 0
Product.java 파일 보기

@@ -4,4 +4,31 @@
4 4
  * Created by leon on 1/10/18.
5 5
  */
6 6
 public class Product {
7
+    private float price;
8
+    private int quantity;
9
+    final private String id;
10
+    
11
+    public Product(float price, int quantity, String id) {
12
+        this.price = price;
13
+        this.quantity = quantity;
14
+        this.id = id;
15
+    }
16
+    public void setPrice(float newPrice) {
17
+        this.price = newPrice;
18
+    }
19
+    public float getPrice() {
20
+        return price;
21
+    }
22
+    public void addUnits(int numUnits) {
23
+        quantity += numUnits;
24
+    }
25
+    public void removeUnits(int numUnits) {
26
+        quantity -= numUnits;
27
+    }
28
+    public int getOnHandQuantity() {
29
+        return quantity; 
30
+    }
31
+    public String getID() {
32
+        return id;
33
+    }
7 34
 }

+ 35
- 0
ProductTest.java 파일 보기

@@ -1,5 +1,40 @@
1
+
2
+import org.junit.Assert;
3
+import org.junit.Test;
4
+
1 5
 /**
2 6
  * Created by leon on 1/10/18.
3 7
  */
4 8
 public class ProductTest {
9
+    @Test
10
+    public void setPriceTest() {
11
+        float expected = 15.25f;
12
+        
13
+        Product dragons = new Product(25.00f, 3, "dragon");
14
+        dragons.setPrice(15.25f);
15
+        float actual = dragons.getPrice();
16
+        
17
+        Assert.assertEquals(actual, expected, 0.001f);
18
+    }
19
+    @Test
20
+    public void addUnitsTest() {
21
+        int expected = 10;
22
+        
23
+        Product dragons = new Product(25.00f, 3, "dragon");
24
+        dragons.addUnits(7);
25
+        int actual = dragons.getOnHandQuantity();
26
+        
27
+        Assert.assertEquals(actual, expected); 
28
+    }
29
+    @Test
30
+    public void removeUnitsTest() {
31
+        int expected = 2;
32
+        
33
+        Product dragons = new Product(25.00f, 3, "dragon");
34
+        dragons.removeUnits(1);
35
+        int actual = dragons.getOnHandQuantity();
36
+        
37
+        Assert.assertEquals(actual, expected);
38
+    }
39
+    
5 40
 }

+ 59
- 8
package.bluej 파일 보기

@@ -1,20 +1,35 @@
1 1
 #BlueJ package file
2
-editor.fx.0.height=722
3
-editor.fx.0.width=800
4
-editor.fx.0.x=560
5
-editor.fx.0.y=117
2
+dependency1.from=ProductTest
3
+dependency1.to=Product
4
+dependency1.type=UsesDependency
5
+dependency2.from=Inventory
6
+dependency2.to=Product
7
+dependency2.type=UsesDependency
8
+dependency3.from=InventoryTest
9
+dependency3.to=Inventory
10
+dependency3.type=UsesDependency
11
+dependency4.from=InventoryTest
12
+dependency4.to=Product
13
+dependency4.type=UsesDependency
14
+dependency5.from=MainApplication
15
+dependency5.to=Inventory
16
+dependency5.type=UsesDependency
17
+editor.fx.0.height=716
18
+editor.fx.0.width=1144
19
+editor.fx.0.x=50
20
+editor.fx.0.y=23
6 21
 objectbench.height=101
7 22
 objectbench.width=461
8 23
 package.divider.horizontal=0.6
9 24
 package.divider.vertical=0.8007380073800738
10 25
 package.editor.height=427
11 26
 package.editor.width=674
12
-package.editor.x=141
13
-package.editor.y=59
27
+package.editor.x=37
28
+package.editor.y=62
14 29
 package.frame.height=600
15 30
 package.frame.width=800
16
-package.numDependencies=0
17
-package.numTargets=0
31
+package.numDependencies=5
32
+package.numTargets=5
18 33
 package.showExtends=true
19 34
 package.showUses=true
20 35
 project.charset=UTF-8
@@ -23,3 +38,39 @@ readme.name=@README
23 38
 readme.width=47
24 39
 readme.x=10
25 40
 readme.y=10
41
+target1.height=50
42
+target1.name=ProductTest
43
+target1.showInterface=false
44
+target1.type=UnitTestTargetJunit4
45
+target1.width=100
46
+target1.x=120
47
+target1.y=20
48
+target2.height=50
49
+target2.name=Product
50
+target2.showInterface=false
51
+target2.type=ClassTarget
52
+target2.width=80
53
+target2.x=40
54
+target2.y=100
55
+target3.height=50
56
+target3.name=InventoryTest
57
+target3.showInterface=false
58
+target3.type=UnitTestTargetJunit4
59
+target3.width=80
60
+target3.x=150
61
+target3.y=180
62
+target4.association=InventoryTest
63
+target4.height=50
64
+target4.name=Inventory
65
+target4.showInterface=false
66
+target4.type=ClassTarget
67
+target4.width=80
68
+target4.x=120
69
+target4.y=210
70
+target5.height=50
71
+target5.name=MainApplication
72
+target5.showInterface=false
73
+target5.type=ClassTarget
74
+target5.width=120
75
+target5.x=270
76
+target5.y=100