Browse Source

output made pretty

Lauren Green 5 years ago
parent
commit
07e4fde229

+ 15
- 0
src/main/java/io/zipcoder/Item.java View File

@@ -45,4 +45,19 @@ public class Item {
45 45
     public String toString(){
46 46
         return "name:" + name + " price:" + price + " type:" + type + " expiration:" + expiration;
47 47
     }
48
+
49
+    @Override
50
+    public boolean equals(Object o) {
51
+        Item otherItem = (Item) o;
52
+        if(!this.getName().equalsIgnoreCase(((Item) o).getName()))
53
+            return false;
54
+        if(!this.getType().equalsIgnoreCase(((Item) o).getType()))
55
+            return false;
56
+        if(!this.getPrice().equals(((Item) o).getPrice()))
57
+            return false;
58
+        if(!this.getExpiration().equalsIgnoreCase(((Item) o).getExpiration()))
59
+            return false;
60
+
61
+        return true;
62
+    }
48 63
 }

+ 22
- 18
src/main/java/io/zipcoder/Items.java View File

@@ -64,31 +64,35 @@ public class Items extends ArrayList<Item>{
64 64
 
65 65
     public static String prettyOutput(Items milk, HashMap<Double, Integer> milkPrices, Items bread, HashMap<Double, Integer> breadPrices, Items cookies, HashMap<Double, Integer> cookiesPrices, Items apples, HashMap<Double, Integer> applesPrices) {
66 66
 
67
-        String result = "name: " + "Milk " + "seen: " + milk.size() + " times\n" +
68
-                "====================\n";
69
-            for(Double priceM : milkPrices.keySet()) {
70
-                result += "Price: " + priceM + " seen: " + milkPrices.get(priceM) + " times\n" +
71
-                "---------------------\n";
67
+        String result = String.format("%-8s %4s %12s %d %6s", "name:", "Milk", "seen:", milk.size(), "times\n") +
68
+                String.format("%12s %6s %13s", "=============", "", "=============\n");
69
+        for(Double priceM : milkPrices.keySet()) {
70
+                result += String.format("%-8s %.2f %12s %d %6s", "Price:", priceM, "seen:", milkPrices.get(priceM), "times\n") +
71
+                        String.format("%12s %6s %13s", "-------------", "", "-------------\n");
72 72
             }
73
-            result += "\nname: " + "Bread " + "seen: " + bread.size() + " times\n" +
74
-                    "====================\n";
73
+
74
+            result += String.format("%-8s %4s %12s %d %6s", "\nname:", "Bread", "seen:", bread.size(), "times\n") +
75
+                String.format("%12s %6s %13s", "=============", "", "=============\n");
75 76
         for(Double priceB : breadPrices.keySet()) {
76
-            result += "Price: " + priceB + " seen: " + breadPrices.get(priceB) + " times\n" +
77
-                    "---------------------\n";
77
+            result += String.format("%-8s %.2f %12s %d %6s", "Price:", priceB, "seen:", breadPrices.get(priceB), "times\n") +
78
+                    String.format("%12s %6s %13s", "-------------", "", "-------------\n");
78 79
         }
79
-        result += "\nname: " + "Cookies " + "seen: " + cookies.size() + " times\n" +
80
-                "====================\n";
80
+
81
+        result += String.format("%-5s %4s %12s %d %6s", "\nname:", "Cookies", "seen:", cookies.size(), "times\n") +
82
+                String.format("%12s %6s %13s", "=============", "", "=============\n");
81 83
         for(Double priceC : cookiesPrices.keySet()) {
82
-            result += "Price: " + priceC + " seen: " + cookiesPrices.get(priceC) + " times\n" +
83
-                    "---------------------\n";
84
+            result += String.format("%-8s %.2f %12s %d %6s", "Price:", priceC, "seen:", cookiesPrices.get(priceC), "times\n") +
85
+                    String.format("%12s %6s %13s", "-------------", "", "-------------\n");
84 86
         }
85
-        result += "\nname: " + "Apples " + "seen: " + apples.size() + " times\n" +
86
-                "====================\n";
87
+
88
+        result += String.format("%-7s %4s %12s %d %6s", "\nname:", "Apples", "seen:", apples.size(), "times\n") +
89
+                String.format("%12s %6s %13s", "=============", "", "=============\n");
87 90
         for(Double priceA : applesPrices.keySet()) {
88
-            result += "Price: " + priceA + " seen: " + applesPrices.get(priceA) + " times\n" +
89
-                    "---------------------\n";
91
+            result += String.format("%-8s %.2f %12s %d %6s", "Price:", priceA, "seen:", applesPrices.get(priceA), "times\n") +
92
+                    String.format("%12s %6s %13s", "-------------", "", "-------------\n");
90 93
         }
91
-        result += "\nErrors    seen: " + ItemParseException.getCount() + " times";
94
+
95
+        result += String.format("%-12s %14s %d %5s", "\nErrors", "seen:", ItemParseException.getCount(), "times");
92 96
 
93 97
         return result;
94 98
 

+ 2
- 2
src/test/java/io/zipcoder/ItemParserTest.java View File

@@ -36,10 +36,10 @@ public class ItemParserTest {
36 36
 
37 37
     @Test
38 38
     public void parseStringIntoItemTest() throws ItemParseException{
39
-        Item expected = new Item("milk", 3.23, "Food","1/25/2016");
39
+        Item expected = new Item("milk", 3.23, "food","1/25/2016");
40 40
         ArrayList<String> itemArray = itemParser.parseRawDataIntoStringArray(rawSingleItem);
41 41
         Item actual = itemParser.parseStringIntoItem(itemParser.mapKeysAndValuesOfItem(itemArray.get(0)));
42
-        assertEquals(expected.toString(), actual.toString());
42
+        assertEquals(expected, actual);
43 43
     }
44 44
 
45 45
     @Test(expected = ItemParseException.class)