From 9ee820e242bc70d8cb91315388fcdf8a1c5bbfa5 Mon Sep 17 00:00:00 2001 From: sitarameez Date: Fri, 2 Jul 2021 23:40:29 -0400 Subject: [PATCH 01/14] add junit testing suite --- .idea/Product-Inventory-Lab.iml | 2 ++ .idea/compiler.xml | 1 + .idea/jarRepositories.xml | 20 ++++++++++++++++++++ pom.xml | 17 +++++++++++++++-- 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 .idea/Product-Inventory-Lab.iml create mode 100644 .idea/jarRepositories.xml diff --git a/.idea/Product-Inventory-Lab.iml b/.idea/Product-Inventory-Lab.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/.idea/Product-Inventory-Lab.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 7a4bf35..4f08829 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -6,6 +6,7 @@ + diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 43c1af2..2ccf8e9 100644 --- a/pom.xml +++ b/pom.xml @@ -21,5 +21,18 @@ - - \ No newline at end of file + + + org.junit.jupiter + junit-jupiter-engine + 5.4.2 + test + + + org.junit.jupiter + junit-jupiter-api + 5.4.2 + test + + + From 20f6f94386a26299a82ba6f66cab7039b06e2ec2 Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sat, 3 Jul 2021 11:55:21 -0400 Subject: [PATCH 02/14] add clothing/dosa models and services --- src/main/java/Models/Clothing.java | 4 ++++ src/main/java/Models/Dosa.java | 4 ++++ src/main/java/Services/ClothingService.java | 4 ++++ src/main/java/Services/DosaService.java | 4 ++++ src/test/java/Models/ClothingTest.java | 17 +++++++++++++++++ src/test/java/Models/DosaTest.java | 17 +++++++++++++++++ src/test/java/Services/ClothingServiceTest.java | 17 +++++++++++++++++ src/test/java/Services/DosaServiceTest.java | 17 +++++++++++++++++ 8 files changed, 84 insertions(+) create mode 100644 src/main/java/Models/Clothing.java create mode 100644 src/main/java/Models/Dosa.java create mode 100644 src/main/java/Services/ClothingService.java create mode 100644 src/main/java/Services/DosaService.java create mode 100644 src/test/java/Models/ClothingTest.java create mode 100644 src/test/java/Models/DosaTest.java create mode 100644 src/test/java/Services/ClothingServiceTest.java create mode 100644 src/test/java/Services/DosaServiceTest.java diff --git a/src/main/java/Models/Clothing.java b/src/main/java/Models/Clothing.java new file mode 100644 index 0000000..b5f3b02 --- /dev/null +++ b/src/main/java/Models/Clothing.java @@ -0,0 +1,4 @@ +package Models; + +public class Clothing { +} diff --git a/src/main/java/Models/Dosa.java b/src/main/java/Models/Dosa.java new file mode 100644 index 0000000..113a113 --- /dev/null +++ b/src/main/java/Models/Dosa.java @@ -0,0 +1,4 @@ +package Models; + +public class Dosa { +} diff --git a/src/main/java/Services/ClothingService.java b/src/main/java/Services/ClothingService.java new file mode 100644 index 0000000..c90f26c --- /dev/null +++ b/src/main/java/Services/ClothingService.java @@ -0,0 +1,4 @@ +package Services; + +public class ClothingService { +} diff --git a/src/main/java/Services/DosaService.java b/src/main/java/Services/DosaService.java new file mode 100644 index 0000000..2663da1 --- /dev/null +++ b/src/main/java/Services/DosaService.java @@ -0,0 +1,4 @@ +package Services; + +public class DosaService { +} diff --git a/src/test/java/Models/ClothingTest.java b/src/test/java/Models/ClothingTest.java new file mode 100644 index 0000000..3783913 --- /dev/null +++ b/src/test/java/Models/ClothingTest.java @@ -0,0 +1,17 @@ +package Models; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + +import static org.junit.jupiter.api.Assertions.*; + +class ClothingTest { + + @BeforeEach + void setUp() { + } + + @AfterEach + void tearDown() { + } +} \ No newline at end of file diff --git a/src/test/java/Models/DosaTest.java b/src/test/java/Models/DosaTest.java new file mode 100644 index 0000000..0e704cc --- /dev/null +++ b/src/test/java/Models/DosaTest.java @@ -0,0 +1,17 @@ +package Models; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + +import static org.junit.jupiter.api.Assertions.*; + +class DosaTest { + + @BeforeEach + void setUp() { + } + + @AfterEach + void tearDown() { + } +} \ No newline at end of file diff --git a/src/test/java/Services/ClothingServiceTest.java b/src/test/java/Services/ClothingServiceTest.java new file mode 100644 index 0000000..4e8baf4 --- /dev/null +++ b/src/test/java/Services/ClothingServiceTest.java @@ -0,0 +1,17 @@ +package Services; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + +import static org.junit.jupiter.api.Assertions.*; + +class ClothingServiceTest { + + @BeforeEach + void setUp() { + } + + @AfterEach + void tearDown() { + } +} \ No newline at end of file diff --git a/src/test/java/Services/DosaServiceTest.java b/src/test/java/Services/DosaServiceTest.java new file mode 100644 index 0000000..a491f80 --- /dev/null +++ b/src/test/java/Services/DosaServiceTest.java @@ -0,0 +1,17 @@ +package Services; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + +import static org.junit.jupiter.api.Assertions.*; + +class DosaServiceTest { + + @BeforeEach + void setUp() { + } + + @AfterEach + void tearDown() { + } +} \ No newline at end of file From 7517b7cd0aaf375df38d084fb58acdb8fa1d1983 Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sat, 3 Jul 2021 12:09:51 -0400 Subject: [PATCH 03/14] added properties to clothing class --- src/main/java/Models/Clothing.java | 6 ++++++ src/main/java/Models/Dosa.java | 1 + 2 files changed, 7 insertions(+) diff --git a/src/main/java/Models/Clothing.java b/src/main/java/Models/Clothing.java index b5f3b02..ee947d2 100644 --- a/src/main/java/Models/Clothing.java +++ b/src/main/java/Models/Clothing.java @@ -1,4 +1,10 @@ package Models; public class Clothing { + private int id; + private String name; + private String brand; + private int size; + private int quantity; + private float price; } diff --git a/src/main/java/Models/Dosa.java b/src/main/java/Models/Dosa.java index 113a113..3513e91 100644 --- a/src/main/java/Models/Dosa.java +++ b/src/main/java/Models/Dosa.java @@ -1,4 +1,5 @@ package Models; public class Dosa { + } From 6b5a579f289ff3a210695f017a909e34ffbc600e Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sat, 3 Jul 2021 13:23:38 -0400 Subject: [PATCH 04/14] added setters and getters --- src/main/java/Models/Chocolate.java | 9 +++ src/main/java/Models/Clothing.java | 63 +++++++++++++++++++ src/main/java/Models/Dosa.java | 5 -- src/main/java/Services/ChocolateService.java | 4 ++ src/main/java/Services/ClothingService.java | 1 + src/main/java/Services/DosaService.java | 4 -- .../{DosaTest.java => ChocolateTest.java} | 4 +- src/test/java/Models/ClothingTest.java | 41 +++++++++++- ...iceTest.java => ChocolateServiceTest.java} | 2 +- 9 files changed, 121 insertions(+), 12 deletions(-) create mode 100644 src/main/java/Models/Chocolate.java delete mode 100644 src/main/java/Models/Dosa.java create mode 100644 src/main/java/Services/ChocolateService.java delete mode 100644 src/main/java/Services/DosaService.java rename src/test/java/Models/{DosaTest.java => ChocolateTest.java} (90%) rename src/test/java/Services/{DosaServiceTest.java => ChocolateServiceTest.java} (89%) diff --git a/src/main/java/Models/Chocolate.java b/src/main/java/Models/Chocolate.java new file mode 100644 index 0000000..dae2d18 --- /dev/null +++ b/src/main/java/Models/Chocolate.java @@ -0,0 +1,9 @@ +package Models; + +public class Chocolate { +private String brand; +private String type; +private int quantity; +private int price; + +} diff --git a/src/main/java/Models/Clothing.java b/src/main/java/Models/Clothing.java index ee947d2..7ad6d0a 100644 --- a/src/main/java/Models/Clothing.java +++ b/src/main/java/Models/Clothing.java @@ -7,4 +7,67 @@ public class Clothing { private int size; private int quantity; private float price; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } + + public Clothing() + {} + + public Clothing(int id,String name,String brand,int Size,int quantity,float price){ + this.id=id; + this.name=name; + this.brand=brand; + this.size=size; + this.quantity=quantity; + this.price=price; + } + + +public String getName(){ + return name; + } + } + diff --git a/src/main/java/Models/Dosa.java b/src/main/java/Models/Dosa.java deleted file mode 100644 index 3513e91..0000000 --- a/src/main/java/Models/Dosa.java +++ /dev/null @@ -1,5 +0,0 @@ -package Models; - -public class Dosa { - -} diff --git a/src/main/java/Services/ChocolateService.java b/src/main/java/Services/ChocolateService.java new file mode 100644 index 0000000..586ab6f --- /dev/null +++ b/src/main/java/Services/ChocolateService.java @@ -0,0 +1,4 @@ +package Services; + +public class ChocolateService { +} diff --git a/src/main/java/Services/ClothingService.java b/src/main/java/Services/ClothingService.java index c90f26c..13affd5 100644 --- a/src/main/java/Services/ClothingService.java +++ b/src/main/java/Services/ClothingService.java @@ -1,4 +1,5 @@ package Services; public class ClothingService { + //public ClothingService } diff --git a/src/main/java/Services/DosaService.java b/src/main/java/Services/DosaService.java deleted file mode 100644 index 2663da1..0000000 --- a/src/main/java/Services/DosaService.java +++ /dev/null @@ -1,4 +0,0 @@ -package Services; - -public class DosaService { -} diff --git a/src/test/java/Models/DosaTest.java b/src/test/java/Models/ChocolateTest.java similarity index 90% rename from src/test/java/Models/DosaTest.java rename to src/test/java/Models/ChocolateTest.java index 0e704cc..1b63615 100644 --- a/src/test/java/Models/DosaTest.java +++ b/src/test/java/Models/ChocolateTest.java @@ -5,7 +5,7 @@ import static org.junit.jupiter.api.Assertions.*; -class DosaTest { +class ChocolateTest { @BeforeEach void setUp() { @@ -14,4 +14,6 @@ void setUp() { @AfterEach void tearDown() { } + + } \ No newline at end of file diff --git a/src/test/java/Models/ClothingTest.java b/src/test/java/Models/ClothingTest.java index 3783913..753647a 100644 --- a/src/test/java/Models/ClothingTest.java +++ b/src/test/java/Models/ClothingTest.java @@ -2,8 +2,10 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import sun.jvm.hotspot.utilities.Assert; -import static org.junit.jupiter.api.Assertions.*; class ClothingTest { @@ -13,5 +15,42 @@ void setUp() { @AfterEach void tearDown() { + } + + @Test + public void testSetName(){ + //Given + String expectedName ="Jean"; + //When + Clothing clothing =new Clothing(); + clothing.setName(expectedName); + //Then + Assertions.assertEquals(expectedName,clothing.getName()); + + + } + + @Test + public void testConstructor(){ + //Given + int id=112; + String expectedName ="Jean"; + String brand="Levis"; + int size=6; + int quantity=10; + float price=35; + //When + Clothing clothing =new Clothing(id,expectedName,brand,size,quantity,price); + //clothing.setName(expectedName); + //Then + Assertions.assertEquals(expectedName,clothing.getName()); + Assertions.assertEquals(expectedName,clothing.getId()); + Assertions.assertEquals(expectedName,clothing.getBrand()); + Assertions.assertEquals(expectedName,clothing.getSize()); + Assertions.assertEquals(expectedName,clothing.getQuantity()); + Assertions.assertEquals(expectedName,clothing.getPrice()); + + + } } \ No newline at end of file diff --git a/src/test/java/Services/DosaServiceTest.java b/src/test/java/Services/ChocolateServiceTest.java similarity index 89% rename from src/test/java/Services/DosaServiceTest.java rename to src/test/java/Services/ChocolateServiceTest.java index a491f80..7efa7bb 100644 --- a/src/test/java/Services/DosaServiceTest.java +++ b/src/test/java/Services/ChocolateServiceTest.java @@ -5,7 +5,7 @@ import static org.junit.jupiter.api.Assertions.*; -class DosaServiceTest { +class ChocolateServiceTest { @BeforeEach void setUp() { From e7795dfa37f10e683576c959aa6085db92989b50 Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sat, 3 Jul 2021 17:54:29 -0400 Subject: [PATCH 05/14] added clothing service methods --- src/main/java/Models/Chocolate.java | 43 ++++++++++++++++++- src/main/java/Services/ClothingService.java | 40 ++++++++++++++++- src/test/java/Models/ChocolateTest.java | 29 +++++++++++++ src/test/java/Models/ClothingTest.java | 10 ++--- .../java/Services/ClothingServiceTest.java | 31 +++++++++++++ 5 files changed, 146 insertions(+), 7 deletions(-) diff --git a/src/main/java/Models/Chocolate.java b/src/main/java/Models/Chocolate.java index dae2d18..5eb54c2 100644 --- a/src/main/java/Models/Chocolate.java +++ b/src/main/java/Models/Chocolate.java @@ -4,6 +4,47 @@ public class Chocolate { private String brand; private String type; private int quantity; -private int price; +private float price; +public Chocolate(){ +} + + public Chocolate(String brand,String type,int quantity,int price){ + this.brand=brand; + this.type=type; + this.quantity=quantity; + this.price=price; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public float getPrice() { + return price; + } + + public void setPrice(int price) { + this.price = price; + } } diff --git a/src/main/java/Services/ClothingService.java b/src/main/java/Services/ClothingService.java index 13affd5..9176454 100644 --- a/src/main/java/Services/ClothingService.java +++ b/src/main/java/Services/ClothingService.java @@ -1,5 +1,43 @@ package Services; +import Models.Clothing; + +import java.util.ArrayList; + public class ClothingService { - //public ClothingService + + private static int nextId=1; + private ArrayList inventory = new ArrayList<>(); + + + +public Clothing create(String name,String brand,int size,int quantity,float price){ + + Clothing createdClothing = new Clothing(nextId++,name,brand,size,quantity,price); + + inventory.add(createdClothing); + + return createdClothing; +} + +public Clothing findClothing(int id) { + for (Clothing findObject : inventory) { + if (findObject.getId() == id) { + return findObject; + } + } return null; +} + +public Clothing[] findAll(){ + ArrayList inventoryClone =(ArrayList) inventory.clone(); + return (Clothing[]) inventoryClone.toArray(); +} +public boolean delete(int id){ +for(Clothing deleteObject : inventory){ + if(deleteObject.getId()==id){ + return true; + } +} +return false; +} } diff --git a/src/test/java/Models/ChocolateTest.java b/src/test/java/Models/ChocolateTest.java index 1b63615..efec085 100644 --- a/src/test/java/Models/ChocolateTest.java +++ b/src/test/java/Models/ChocolateTest.java @@ -1,7 +1,9 @@ package Models; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -14,6 +16,33 @@ void setUp() { @AfterEach void tearDown() { } +@Test +public void testSetName(){ + //given + String expectedBrand="Munch"; + //when + Chocolate chocolate=new Chocolate(); + chocolate.setBrand(expectedBrand); + //then + Assertions.assertEquals(expectedBrand,chocolate.getBrand()); +} +@Test + public void testConstructor(){ + //given + String expectedBrand="Munch"; + String type="Wafer"; + int quantity=10; + float price=5; + + //when + Chocolate chocolate=new Chocolate(); + + //then + Assertions.assertEquals(expectedBrand,chocolate.getBrand()); + Assertions.assertEquals(type,chocolate.getType()); + Assertions.assertEquals(quantity,chocolate.getQuantity()); + Assertions.assertEquals(price,chocolate.getPrice()); +} } \ No newline at end of file diff --git a/src/test/java/Models/ClothingTest.java b/src/test/java/Models/ClothingTest.java index 753647a..cf06bab 100644 --- a/src/test/java/Models/ClothingTest.java +++ b/src/test/java/Models/ClothingTest.java @@ -44,11 +44,11 @@ public void testConstructor(){ //clothing.setName(expectedName); //Then Assertions.assertEquals(expectedName,clothing.getName()); - Assertions.assertEquals(expectedName,clothing.getId()); - Assertions.assertEquals(expectedName,clothing.getBrand()); - Assertions.assertEquals(expectedName,clothing.getSize()); - Assertions.assertEquals(expectedName,clothing.getQuantity()); - Assertions.assertEquals(expectedName,clothing.getPrice()); + Assertions.assertEquals(id,clothing.getId()); + Assertions.assertEquals(brand,clothing.getBrand()); + Assertions.assertEquals(size,clothing.getSize()); + Assertions.assertEquals(quantity,clothing.getQuantity()); + Assertions.assertEquals(price,clothing.getPrice()); diff --git a/src/test/java/Services/ClothingServiceTest.java b/src/test/java/Services/ClothingServiceTest.java index 4e8baf4..c69643f 100644 --- a/src/test/java/Services/ClothingServiceTest.java +++ b/src/test/java/Services/ClothingServiceTest.java @@ -1,7 +1,10 @@ package Services; +import Models.Clothing; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -14,4 +17,32 @@ void setUp() { @AfterEach void tearDown() { } + + @Test + public void createTest(){ + //int expectedId=112; + String expectedName ="Jean"; + String expectedBrand="Levis"; + int expectedSize=6; + int expectedQuantity=10; + float expectedPrice=35; + + ClothingService clothingService = new ClothingService(); + Clothing testClothing = clothingService.create(expectedName,expectedBrand,expectedSize,expectedQuantity,expectedPrice); + + int actualId = testClothing.getId(); + String actualName = testClothing.getName();; + String actualBrand = testClothing.getBrand();; + int actualSize = testClothing.getSize(); + int actualQuantity = testClothing.getQuantity(); + float actualPrice = testClothing.getPrice(); + + Assertions.assertEquals(Integer.class.getName(), new Integer(actualId).getClass().getName()); + Assertions.assertEquals(expectedName, actualName); + Assertions.assertEquals(expectedBrand, actualBrand); + Assertions.assertEquals(expectedSize, actualSize); + Assertions.assertEquals(expectedQuantity, actualQuantity); + Assertions.assertEquals(expectedPrice, actualPrice); + + } } \ No newline at end of file From df138b4a25457c810be2680a0483a0e300e31a28 Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sat, 3 Jul 2021 18:27:41 -0400 Subject: [PATCH 06/14] completed clothing section 2 --- src/main/java/Models/Clothing.java | 2 +- .../java/Services/ClothingServiceTest.java | 42 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/main/java/Models/Clothing.java b/src/main/java/Models/Clothing.java index 7ad6d0a..06e4863 100644 --- a/src/main/java/Models/Clothing.java +++ b/src/main/java/Models/Clothing.java @@ -55,7 +55,7 @@ public void setPrice(float price) { public Clothing() {} - public Clothing(int id,String name,String brand,int Size,int quantity,float price){ + public Clothing(int id,String name,String brand,int size,int quantity,float price){ this.id=id; this.name=name; this.brand=brand; diff --git a/src/test/java/Services/ClothingServiceTest.java b/src/test/java/Services/ClothingServiceTest.java index c69643f..1c3cdd0 100644 --- a/src/test/java/Services/ClothingServiceTest.java +++ b/src/test/java/Services/ClothingServiceTest.java @@ -37,7 +37,7 @@ public void createTest(){ int actualQuantity = testClothing.getQuantity(); float actualPrice = testClothing.getPrice(); - Assertions.assertEquals(Integer.class.getName(), new Integer(actualId).getClass().getName()); + //Assertions.assertEquals(Integer.class.getName(), new Integer(actualId).getClass().getName()); Assertions.assertEquals(expectedName, actualName); Assertions.assertEquals(expectedBrand, actualBrand); Assertions.assertEquals(expectedSize, actualSize); @@ -45,4 +45,44 @@ public void createTest(){ Assertions.assertEquals(expectedPrice, actualPrice); } + + @Test + void findClothing() { + //Given + ClothingService clothingService = new ClothingService(); + Clothing foundClothing = new Clothing(); + clothingService.create("Shirt","Arrow",32,10,30); + clothingService.create("Jeans","Levis",32,8,42); + //When + foundClothing = clothingService.findClothing(2); + //Then + Assertions.assertTrue(true); + + } + + @Test + void findAll() { + //Given + ClothingService clothingService = new ClothingService(); + //Clothing foundClothing = new Clothing(); + clothingService.create("Shirt","Arrow",32,10,30); + clothingService.create("Jeans","Levis",32,8,42); + //When + Clothing [] clothing = clothingService.findAll(); + //Then + Assertions.assertTrue(true); + } + + @Test + void delete() { + //Given + ClothingService clothingService = new ClothingService(); + Boolean isClotheDeleted = true; + clothingService.create("Shirt","Arrow",32,10,30); + clothingService.create("Jeans","Levis",32,8,42); + //When + isClotheDeleted = clothingService.delete(2); + //Then + Assertions.assertEquals(true, isClotheDeleted); + } } \ No newline at end of file From 01209733bc955d616e3ae7315850051456573968 Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sat, 3 Jul 2021 18:52:22 -0400 Subject: [PATCH 07/14] completed section 2 --- src/main/java/Models/Chocolate.java | 2 +- src/main/java/Services/ChocolateService.java | 38 ++++++++++ .../java/Services/ChocolateServiceTest.java | 70 +++++++++++++++++++ .../java/Services/ClothingServiceTest.java | 2 +- 4 files changed, 110 insertions(+), 2 deletions(-) diff --git a/src/main/java/Models/Chocolate.java b/src/main/java/Models/Chocolate.java index 5eb54c2..35c1eb8 100644 --- a/src/main/java/Models/Chocolate.java +++ b/src/main/java/Models/Chocolate.java @@ -9,7 +9,7 @@ public class Chocolate { public Chocolate(){ } - public Chocolate(String brand,String type,int quantity,int price){ + public Chocolate(String brand,String type,int quantity,float price){ this.brand=brand; this.type=type; this.quantity=quantity; diff --git a/src/main/java/Services/ChocolateService.java b/src/main/java/Services/ChocolateService.java index 586ab6f..d94eb41 100644 --- a/src/main/java/Services/ChocolateService.java +++ b/src/main/java/Services/ChocolateService.java @@ -1,4 +1,42 @@ package Services; +import Models.Chocolate; + +import java.util.ArrayList; + public class ChocolateService { + private static int nextId=1; + private ArrayList inventory = new ArrayList<>(); + + + + public Chocolate create(String brand,String type,int quantity,float price){ + + Chocolate createdChocolate = new Chocolate(brand,type,quantity,price); + + inventory.add(createdChocolate); + + return createdChocolate; + } + + public Chocolate findChocolate(String brand) { + for (Chocolate findObject : inventory) { + if (findObject.getBrand().equals(brand)) { + return findObject; + } + } return null; + } + + public Chocolate[] findAll(){ + ArrayList inventoryClone =(ArrayList) inventory.clone(); + return (Chocolate[]) inventoryClone.toArray(); + } + public boolean delete(String brand){ + for(Chocolate deleteObject : inventory){ + if(deleteObject.getBrand().equals(brand)){ + return true; + } + } + return false; + } } diff --git a/src/test/java/Services/ChocolateServiceTest.java b/src/test/java/Services/ChocolateServiceTest.java index 7efa7bb..a5ad472 100644 --- a/src/test/java/Services/ChocolateServiceTest.java +++ b/src/test/java/Services/ChocolateServiceTest.java @@ -1,7 +1,11 @@ package Services; +import Models.Chocolate; +import Models.Clothing; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -14,4 +18,70 @@ void setUp() { @AfterEach void tearDown() { } + + @Test + public void createTest(){ + + String expectedBrand="Kit Kat"; + String expectedType ="Bar"; + int expectedQuantity=10; + float expectedPrice=5; + + ChocolateService chocolateService = new ChocolateService(); + Chocolate testChocolate = chocolateService.create(expectedBrand,expectedType,expectedQuantity,expectedPrice); + + + String actualBrand = testChocolate.getBrand(); + String actualType = testChocolate.getType(); + int actualQuantity = testChocolate.getQuantity(); + float actualPrice = testChocolate.getPrice(); + + //Assertions.assertEquals(Integer.class.getName(), new Integer(actualId).getClass().getName()); + Assertions.assertEquals(expectedBrand, actualBrand); + Assertions.assertEquals(expectedType, actualType); + Assertions.assertEquals(expectedQuantity, actualQuantity); + Assertions.assertEquals(expectedPrice, actualPrice); + + } + + @Test + void findChocolate() { + //Given + ChocolateService chocolateService = new ChocolateService(); + Chocolate foundChocolate = new Chocolate(); + chocolateService.create("Munch","wafer",10,5); + chocolateService.create("Kit kat","bar",20,10); + //When + foundChocolate = chocolateService.findChocolate("Munch"); + + //Then + Assertions.assertEquals("Munch", foundChocolate.getBrand()); + + } + + @Test + void findAll() { + //Given + ChocolateService chocolateService = new ChocolateService(); + //Clothing foundClothing = new Clothing(); + chocolateService.create("Munch","wafer",10,5); + chocolateService.create("Kit kat","bar",20,10); + //When + Chocolate [] chocolates = chocolateService.findAll(); + //Then + Assertions.assertTrue(true); + } + + @Test + void delete() { + //Given + ChocolateService chocolateService = new ChocolateService(); + Boolean isChocolateDeleted = true; + chocolateService.create("Munch","wafer",10,5); + chocolateService.create("Kit kat","bar",20,10); + //When + isChocolateDeleted = chocolateService.delete("Munch"); + //Then + Assertions.assertEquals(true, isChocolateDeleted); + } } \ No newline at end of file diff --git a/src/test/java/Services/ClothingServiceTest.java b/src/test/java/Services/ClothingServiceTest.java index 1c3cdd0..503a3e8 100644 --- a/src/test/java/Services/ClothingServiceTest.java +++ b/src/test/java/Services/ClothingServiceTest.java @@ -56,7 +56,7 @@ void findClothing() { //When foundClothing = clothingService.findClothing(2); //Then - Assertions.assertTrue(true); + Assertions.assertEquals("Levis",foundClothing.getBrand()); } From c0647538b7aa508096432d713dee640c6ba98980 Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sat, 3 Jul 2021 19:24:30 -0400 Subject: [PATCH 08/14] methods and tests but still two errors in tests --- .../java/Services/ChocolateServiceTest.java | 19 +++++++++++-------- .../java/Services/ClothingServiceTest.java | 10 +++++----- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/test/java/Services/ChocolateServiceTest.java b/src/test/java/Services/ChocolateServiceTest.java index a5ad472..b486b72 100644 --- a/src/test/java/Services/ChocolateServiceTest.java +++ b/src/test/java/Services/ChocolateServiceTest.java @@ -7,6 +7,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.lang.reflect.Array; + import static org.junit.jupiter.api.Assertions.*; class ChocolateServiceTest { @@ -48,14 +50,14 @@ public void createTest(){ void findChocolate() { //Given ChocolateService chocolateService = new ChocolateService(); - Chocolate foundChocolate = new Chocolate(); + Chocolate chocolate = new Chocolate(); chocolateService.create("Munch","wafer",10,5); chocolateService.create("Kit kat","bar",20,10); //When - foundChocolate = chocolateService.findChocolate("Munch"); + chocolate = chocolateService.findChocolate("Munch"); //Then - Assertions.assertEquals("Munch", foundChocolate.getBrand()); + Assertions.assertEquals("Munch", chocolate.getBrand()); } @@ -67,21 +69,22 @@ void findAll() { chocolateService.create("Munch","wafer",10,5); chocolateService.create("Kit kat","bar",20,10); //When - Chocolate [] chocolates = chocolateService.findAll(); + Chocolate[] chocolates = chocolateService.findAll(); //Then - Assertions.assertTrue(true); + Assertions.assertEquals(2,chocolates.length); + //Assertions.assertTrue(true); } @Test void delete() { //Given ChocolateService chocolateService = new ChocolateService(); - Boolean isChocolateDeleted = true; + Boolean chocolateDeleted = true; chocolateService.create("Munch","wafer",10,5); chocolateService.create("Kit kat","bar",20,10); //When - isChocolateDeleted = chocolateService.delete("Munch"); + chocolateDeleted = chocolateService.delete("Munch"); //Then - Assertions.assertEquals(true, isChocolateDeleted); + Assertions.assertEquals(true, chocolateDeleted); } } \ No newline at end of file diff --git a/src/test/java/Services/ClothingServiceTest.java b/src/test/java/Services/ClothingServiceTest.java index 503a3e8..3ee2274 100644 --- a/src/test/java/Services/ClothingServiceTest.java +++ b/src/test/java/Services/ClothingServiceTest.java @@ -50,13 +50,13 @@ public void createTest(){ void findClothing() { //Given ClothingService clothingService = new ClothingService(); - Clothing foundClothing = new Clothing(); - clothingService.create("Shirt","Arrow",32,10,30); - clothingService.create("Jeans","Levis",32,8,42); + Clothing clothing = new Clothing(); + clothingService.create("Dress","Gucci",6,10,300); + clothingService.create("Jeans","Levis",10,8,45); //When - foundClothing = clothingService.findClothing(2); + clothing = clothingService.findClothing(2); //Then - Assertions.assertEquals("Levis",foundClothing.getBrand()); + Assertions.assertEquals("Levis",clothing.getBrand()); } From 2e8a2ae3f365938dbba20c5231b664a746beb174 Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sun, 4 Jul 2021 09:50:43 -0400 Subject: [PATCH 09/14] console and chocolate features for user added --- instructions/section-03.md | 14 +++++----- src/main/java/io/App.java | 51 +++++++++++++++++++++++++++++++++++ src/main/java/io/Console.java | 43 +++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 src/main/java/io/App.java create mode 100644 src/main/java/io/Console.java diff --git a/instructions/section-03.md b/instructions/section-03.md index 54e535e..cfed147 100644 --- a/instructions/section-03.md +++ b/instructions/section-03.md @@ -9,12 +9,12 @@ Now that we have production models and services to manage them we can pull every * Create a user interface to create, read, update and delete products ## Part 1 - Input Output -We will need to allow user input in order for the application to function. In order to do this we will use the Scanner class to accept input and methods from the System.out package to print to the console. In order to keep our code a bit cleaner as well as add a layer of abstraction we will use a Console class. +We will need to allow user input in order for the application to function. In order to do this we will use the Scanner class to accept input and methods from the System.out package to print to the console. In order to keep our code a bit cleaner as well as add a layer of abstraction we will use a io class. ``` package io; -public class Console { +public class io { } ``` @@ -24,7 +24,7 @@ Notice that I've added this to an _io_ package. Now we can keep repetitious System.out and Scanner calls out of the main application code. We can also use this class to print large output strings that would bloat our code. ``` -public class Console { +public class io { public static void printWelcome(){ System.out.println("" + "**************************************************" + @@ -40,10 +40,10 @@ public class Console { Having this print line string in the code is large and litters up the main code. Having it abstracted away alows us to keep the main code clean and easy to read. We will simply call the _printWelcome()_ behaviour wherever we want to call this code. ``` -Console.printWelcome() +io.printWelcome() ``` -Whenever we want to capture user input or display output we will create methods in the Console class to handle these behaviours. +Whenever we want to capture user input or display output we will create methods in the io class to handle these behaviours. ## Part 2 - Application class Now it is time to put all of these classes we have created to work. We will begin by creating a App class to initialize the application logic and initialize the services. This is the top most class and will start the program @@ -76,7 +76,7 @@ public class App { // (4) // application logic here // call methods to take user input and interface with services - Console.printWelcome(); + io.printWelcome(); } } ``` @@ -97,7 +97,7 @@ Now that we have a way to start the application, create the rest of the code to ## Conclusion -Creating a Console class allowed us to abstract away user interface components of the code. Now the main code isn't concerned with how UI is done, it is only concerned with what it does. We created an application class to house the elements of the program. Finally, creating a user interface to allow a user to interact (Create, Read, Update, and Delete) with items in the inventory. +Creating a io class allowed us to abstract away user interface components of the code. Now the main code isn't concerned with how UI is done, it is only concerned with what it does. We created an application class to house the elements of the program. Finally, creating a user interface to allow a user to interact (Create, Read, Update, and Delete) with items in the inventory. diff --git a/src/main/java/io/App.java b/src/main/java/io/App.java new file mode 100644 index 0000000..7819302 --- /dev/null +++ b/src/main/java/io/App.java @@ -0,0 +1,51 @@ +package io; + +import Services.ChocolateService; +import Services.ClothingService; + +public class App { + private ClothingService sneakerService = new ClothingService(); // (1) + private ChocolateService chocolateService =new ChocolateService(); + + public static void main(String... args){ + + + App application = new App(); // (2) + application.init(); // (3) + + + } + + public void init(){ + // (4) + // application logic here + // call methods to take user input and interface with services + Console.printWelcome(); + String options = Console.getStringInput("Choose an option\n" + + "1.Create chocolate\n"+ + "2.Read Inventory of \n"+ + "3.Update Product"+ + "4.Delete Product"+ + "5."); + switch (options) + { + case "1": + break; + } + Console.println("Enter Chocolate details:"); + String brand=""; + String type =""; + int quantity=0; + float price=0; + + brand = Console.getStringInput("Enter brand"); + type = Console.getStringInput("Enter type"); + quantity= Console.getIntegerInput("Enter quantity"); + price= Console.getIntegerInput("Enter price"); + + chocolateService.create(brand,type,quantity,price); + Console.println("Chocolate inventory"+brand+" created"); + + } + } + diff --git a/src/main/java/io/Console.java b/src/main/java/io/Console.java new file mode 100644 index 0000000..c5ab6a8 --- /dev/null +++ b/src/main/java/io/Console.java @@ -0,0 +1,43 @@ +package io; + +import java.util.Scanner; + +public class Console { + public static void printWelcome(){ + System.out.println("" + + "**************************************************" + + "*** Welcome and Bienvenue ***" + + "*** to ***" + + "*** ZipCo Inventory Manager ***" + + "**************************************************"); + } + public static void print(String output, Object... args) { + System.out.printf(output, args); + } + + public static void println(String output, Object... args) { + print(output + "\n", args); + } + + public static String getStringInput(String prompt) { + Scanner scanner = new Scanner(System.in); + println(prompt); + String userInput = scanner.nextLine(); + return userInput; + } + public static Integer getIntegerInput(String prompt) { + Scanner scanner = new Scanner(System.in); + println(prompt); + Integer userInput = 0; + try { + userInput = scanner.nextInt(); + } + catch (Exception e) + { + System.out.println("Enter a valid number!"); + } + return userInput; + } + + +} From 39eb8e882995def4f57083adc8ee664f475a3bea Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sun, 4 Jul 2021 11:33:43 -0400 Subject: [PATCH 10/14] section 3 completed --- src/main/java/Services/ChocolateService.java | 10 +++ src/main/java/io/App.java | 81 +++++++++++++++----- 2 files changed, 71 insertions(+), 20 deletions(-) diff --git a/src/main/java/Services/ChocolateService.java b/src/main/java/Services/ChocolateService.java index d94eb41..cc7580c 100644 --- a/src/main/java/Services/ChocolateService.java +++ b/src/main/java/Services/ChocolateService.java @@ -31,6 +31,16 @@ public Chocolate[] findAll(){ ArrayList inventoryClone =(ArrayList) inventory.clone(); return (Chocolate[]) inventoryClone.toArray(); } + + public Boolean update(String brand,Integer quantity){ + for(Chocolate updateQuantity:inventory){ + if(updateQuantity.getBrand().equals(brand)){ + updateQuantity.setQuantity(quantity); + return true; + } + } + return false; + } public boolean delete(String brand){ for(Chocolate deleteObject : inventory){ if(deleteObject.getBrand().equals(brand)){ diff --git a/src/main/java/io/App.java b/src/main/java/io/App.java index 7819302..3a44ec1 100644 --- a/src/main/java/io/App.java +++ b/src/main/java/io/App.java @@ -1,12 +1,15 @@ package io; +import Models.Chocolate; +import Models.Clothing; import Services.ChocolateService; import Services.ClothingService; public class App { - private ClothingService sneakerService = new ClothingService(); // (1) + private ClothingService clothingService = new ClothingService(); // (1) private ChocolateService chocolateService =new ChocolateService(); - + private Chocolate chocolate=new Chocolate(); + private Clothing clothing = new Clothing(); public static void main(String... args){ @@ -21,31 +24,69 @@ public void init(){ // application logic here // call methods to take user input and interface with services Console.printWelcome(); - String options = Console.getStringInput("Choose an option\n" + - "1.Create chocolate\n"+ - "2.Read Inventory of \n"+ - "3.Update Product"+ - "4.Delete Product"+ - "5."); - switch (options) - { - case "1": - break; - } - Console.println("Enter Chocolate details:"); String brand=""; String type =""; int quantity=0; float price=0; + Boolean exitProgram=false; + + do{ + String options = Console.getStringInput("Choose an option\n" + + "1.Create chocolate\n" + + "2.Read Inventory of \n" + + "3.Find all Product \n" + + "4.Delete Product \n" + + "5.Exit the program \n"); + + + switch (options) { + case "1": + Console.println("Enter Chocolate details:"); + - brand = Console.getStringInput("Enter brand"); - type = Console.getStringInput("Enter type"); - quantity= Console.getIntegerInput("Enter quantity"); - price= Console.getIntegerInput("Enter price"); + brand = Console.getStringInput("Enter brand"); + type = Console.getStringInput("Enter type"); + quantity = Console.getIntegerInput("Enter quantity"); + price = Console.getIntegerInput("Enter price"); - chocolateService.create(brand,type,quantity,price); - Console.println("Chocolate inventory"+brand+" created"); + chocolateService.create(brand, type, quantity, price); + Console.println("Chocolate inventory :" + brand + " created"); + break; + case "2": + brand = Console.getStringInput("Enter brand to find inventory of chocolate:"); + chocolate = chocolateService.findChocolate(brand); + Console.println("Product details: brand: "+chocolate.getBrand() + + " quantity: "+chocolate.getQuantity()+" price: "+chocolate.getPrice()); + break; + + case "3": + brand = Console.getStringInput("Enter brand to update inventory"); + quantity=Console.getIntegerInput("Enter the quantity you want to update:"); + Boolean updateQty = chocolateService.update(brand,quantity); + if(updateQty==true){ + Console.println("Quantity updated"); + } + else Console.println("Product not found"); + break; + + case "4": + brand = Console.getStringInput("Enter the brand of the product you want to delete:"); + Boolean checkDelete = chocolateService.delete(brand); + if(checkDelete==true){ + Console.println("Product Deleted"); + } + else Console.println("Product not found"); + break; + + case "5": + exitProgram=true; + Console.println("Program Exited:Goodbye"); + break; + + } + } + while(exitProgram==false); } } From 9035b2584e91f37e8524ea586399120823bbed36 Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sun, 4 Jul 2021 11:40:57 -0400 Subject: [PATCH 11/14] section 3 completed --- src/main/java/io/App.java | 2 +- src/test/java/Services/ChocolateServiceTest.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/App.java b/src/main/java/io/App.java index 3a44ec1..439fd7c 100644 --- a/src/main/java/io/App.java +++ b/src/main/java/io/App.java @@ -34,7 +34,7 @@ public void init(){ String options = Console.getStringInput("Choose an option\n" + "1.Create chocolate\n" + "2.Read Inventory of \n" + - "3.Find all Product \n" + + "3.Update Product \n" + "4.Delete Product \n" + "5.Exit the program \n"); diff --git a/src/test/java/Services/ChocolateServiceTest.java b/src/test/java/Services/ChocolateServiceTest.java index b486b72..19afc58 100644 --- a/src/test/java/Services/ChocolateServiceTest.java +++ b/src/test/java/Services/ChocolateServiceTest.java @@ -87,4 +87,18 @@ void delete() { //Then Assertions.assertEquals(true, chocolateDeleted); } + + @Test + void update() { + //given + ChocolateService chocolateService=new ChocolateService(); + Boolean chocolateUpdated = true; + chocolateService.create("cornetto","ice cream",5,5); + + //when + chocolateUpdated=chocolateService.update("cornetto",10); + + //then + Assertions.assertEquals(true,chocolateUpdated); + } } \ No newline at end of file From 8cf240da5dbd1b835ac2390d478199cd2cf517da Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sun, 4 Jul 2021 13:04:32 -0400 Subject: [PATCH 12/14] section 4 completed --- src/main/java/Models/Chocolate.java | 11 +++- src/main/java/Models/Clothing.java | 18 +++--- src/main/java/Services/ChocolateService.java | 65 +++++++++++++++++++- src/main/java/Services/ClothingService.java | 5 +- src/main/java/io/App.java | 1 + src/main/java/utils/CSVUtils.java | 29 +++++++++ 6 files changed, 115 insertions(+), 14 deletions(-) create mode 100644 src/main/java/utils/CSVUtils.java diff --git a/src/main/java/Models/Chocolate.java b/src/main/java/Models/Chocolate.java index 35c1eb8..03fd16b 100644 --- a/src/main/java/Models/Chocolate.java +++ b/src/main/java/Models/Chocolate.java @@ -1,6 +1,7 @@ package Models; public class Chocolate { + private int id; private String brand; private String type; private int quantity; @@ -9,12 +10,20 @@ public class Chocolate { public Chocolate(){ } - public Chocolate(String brand,String type,int quantity,float price){ + public Chocolate(int id,String brand,String type,int quantity,float price){ this.brand=brand; + this.id = id; this.type=type; this.quantity=quantity; this.price=price; } + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } public String getBrand() { return brand; diff --git a/src/main/java/Models/Clothing.java b/src/main/java/Models/Clothing.java index 06e4863..6106650 100644 --- a/src/main/java/Models/Clothing.java +++ b/src/main/java/Models/Clothing.java @@ -8,6 +8,15 @@ public class Clothing { private int quantity; private float price; + public Clothing(int id,String name,String brand,int size,int quantity,float price){ + this.id=id; + this.name=name; + this.brand=brand; + this.size=size; + this.quantity=quantity; + this.price=price; + } + public int getId() { return id; } @@ -55,14 +64,7 @@ public void setPrice(float price) { public Clothing() {} - public Clothing(int id,String name,String brand,int size,int quantity,float price){ - this.id=id; - this.name=name; - this.brand=brand; - this.size=size; - this.quantity=quantity; - this.price=price; - } + public String getName(){ diff --git a/src/main/java/Services/ChocolateService.java b/src/main/java/Services/ChocolateService.java index cc7580c..50766a7 100644 --- a/src/main/java/Services/ChocolateService.java +++ b/src/main/java/Services/ChocolateService.java @@ -1,8 +1,15 @@ package Services; import Models.Chocolate; +import utils.CSVUtils; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class ChocolateService { private static int nextId=1; @@ -12,7 +19,7 @@ public class ChocolateService { public Chocolate create(String brand,String type,int quantity,float price){ - Chocolate createdChocolate = new Chocolate(brand,type,quantity,price); + Chocolate createdChocolate = new Chocolate(nextId++,brand,type,quantity,price); inventory.add(createdChocolate); @@ -28,8 +35,9 @@ public Chocolate findChocolate(String brand) { } public Chocolate[] findAll(){ - ArrayList inventoryClone =(ArrayList) inventory.clone(); - return (Chocolate[]) inventoryClone.toArray(); +// ArrayList inventoryClone =(ArrayList) inventory.clone(); +// return (Chocolate[]) inventoryClone.toArray(); + return inventory.toArray(new Chocolate[0]); } public Boolean update(String brand,Integer quantity){ @@ -49,4 +57,55 @@ public boolean delete(String brand){ } return false; } + + public void writeCSVUtils() throws IOException { + String csvFile = "/Users/batman/Desktop/Chocolate.csv"; + FileWriter writer = new FileWriter(csvFile); //(1) + + CSVUtils.writeLine(writer, new ArrayList(Arrays.asList(String.valueOf(nextId)))); // (2) + + for (Chocolate s : inventory) { + List list = new ArrayList<>(); // (3) + // list.add(String.valueOf(s.getId())); + list.add(s.getBrand()); + list.add(s.getType()); + // list.add(s.getSport()); + list.add(String.valueOf(s.getQuantity())); + list.add(String.valueOf(s.getPrice())); + + CSVUtils.writeLine(writer, list); // (4) + } + writer.flush(); + writer.close(); + } + + private void loadData(){ + // (1) + String csvFile = "/Users/batman/Desktop/Chocolate.csv"; + String line = ""; + String csvSplitBy = ","; + + // (2) + try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) { + nextId = Integer.parseInt(br.readLine()); // (3) + + while ((line = br.readLine()) != null) { + // split line with comma + String[] candy = line.split(csvSplitBy); + + // (4) + int id = Integer.parseInt(candy[0]); + String type = candy[1]; + String brand = candy[2]; + // int quantity = candy[5]; + int quantity = Integer.parseInt(candy[3]); + float price = Float.parseFloat(candy[4]); + + // (5) + inventory.add(new Chocolate(id, type,brand, quantity, price)); + } + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/Services/ClothingService.java b/src/main/java/Services/ClothingService.java index 9176454..2098c7e 100644 --- a/src/main/java/Services/ClothingService.java +++ b/src/main/java/Services/ClothingService.java @@ -29,8 +29,9 @@ public Clothing findClothing(int id) { } public Clothing[] findAll(){ - ArrayList inventoryClone =(ArrayList) inventory.clone(); - return (Clothing[]) inventoryClone.toArray(); +// ArrayList inventoryClone =(ArrayList) inventory.clone(); +// return (Clothing[]) inventoryClone.toArray(); + return inventory.toArray(new Clothing[0]); } public boolean delete(int id){ for(Clothing deleteObject : inventory){ diff --git a/src/main/java/io/App.java b/src/main/java/io/App.java index 439fd7c..ac56b2b 100644 --- a/src/main/java/io/App.java +++ b/src/main/java/io/App.java @@ -10,6 +10,7 @@ public class App { private ChocolateService chocolateService =new ChocolateService(); private Chocolate chocolate=new Chocolate(); private Clothing clothing = new Clothing(); + public static void main(String... args){ diff --git a/src/main/java/utils/CSVUtils.java b/src/main/java/utils/CSVUtils.java new file mode 100644 index 0000000..36a5b94 --- /dev/null +++ b/src/main/java/utils/CSVUtils.java @@ -0,0 +1,29 @@ +package utils; + +import java.io.IOException; +import java.io.Writer; +import java.util.List; + +public class CSVUtils { + private static final char DEFAULT_SEPARATOR = ','; // (1) + + // (2) + public static void writeLine(Writer w, List values) throws IOException { + boolean first = true; + + StringBuilder sb = new StringBuilder(); + + // (3) + for (String value : values) { + if (!first) { + sb.append(DEFAULT_SEPARATOR); + } + sb.append(value); + first = false; + } + sb.append("\n"); + + w.append(sb.toString()); // (4) + } +} + From 2e905b13a9655a06a2ff47323eba79620781fe32 Mon Sep 17 00:00:00 2001 From: sitarameez Date: Sun, 4 Jul 2021 13:44:54 -0400 Subject: [PATCH 13/14] json completed --- chocolate.json | 7 +++++ pom.xml | 6 ++++ src/main/java/Services/ChocolateService.java | 20 +++++++++++--- src/main/java/io/App.java | 29 +++++++++++++++++--- 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 chocolate.json diff --git a/chocolate.json b/chocolate.json new file mode 100644 index 0000000..84793c3 --- /dev/null +++ b/chocolate.json @@ -0,0 +1,7 @@ +[ { + "id" : 1, + "brand" : "cornetto", + "type" : "ice cream", + "quantity" : 5, + "price" : 10.0 +} ] \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2ccf8e9..37f78ce 100644 --- a/pom.xml +++ b/pom.xml @@ -34,5 +34,11 @@ 5.4.2 test + + com.fasterxml.jackson.core + jackson-databind + 2.10.1 + + diff --git a/src/main/java/Services/ChocolateService.java b/src/main/java/Services/ChocolateService.java index 50766a7..606f6f5 100644 --- a/src/main/java/Services/ChocolateService.java +++ b/src/main/java/Services/ChocolateService.java @@ -1,12 +1,13 @@ package Services; import Models.Chocolate; +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectWriter; import utils.CSVUtils; -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; +import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -108,4 +109,15 @@ private void loadData(){ e.printStackTrace(); } } + + public ArrayList readJson() throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + this.inventory = objectMapper.readValue(new File("chocolate.json"), new TypeReference>(){}); + return this.inventory; + } + public void writeJson() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); + writer.writeValue(new File("chocolate.json"), inventory); + } } diff --git a/src/main/java/io/App.java b/src/main/java/io/App.java index ac56b2b..70adcee 100644 --- a/src/main/java/io/App.java +++ b/src/main/java/io/App.java @@ -5,13 +5,16 @@ import Services.ChocolateService; import Services.ClothingService; +import java.io.IOException; +import java.util.ArrayList; + public class App { private ClothingService clothingService = new ClothingService(); // (1) private ChocolateService chocolateService =new ChocolateService(); private Chocolate chocolate=new Chocolate(); private Clothing clothing = new Clothing(); - public static void main(String... args){ + public static void main(String... args) throws IOException { App application = new App(); // (2) @@ -20,7 +23,7 @@ public static void main(String... args){ } - public void init(){ + public void init() throws IOException { // (4) // application logic here // call methods to take user input and interface with services @@ -37,7 +40,9 @@ public void init(){ "2.Read Inventory of \n" + "3.Update Product \n" + "4.Delete Product \n" + - "5.Exit the program \n"); + "5.Save to Json \n"+ + "6.Read from Json \n"+ + "7.Exit the program \n"); switch (options) { @@ -79,8 +84,24 @@ public void init(){ } else Console.println("Product not found"); break; - case "5": + chocolateService.writeJson(); + Console.println("Value saved to chocolate.json"); + break; + case "6": + ArrayList chocolates = chocolateService.readJson(); + //chocolateService.in + for (Chocolate s:chocolates + ) { + Console.println("Brand:"+s.getBrand()+"\n"); + Console.println("Type:"+s.getType()+"\n"); + Console.println("Quantity:"+s.getQuantity()+"\n"); + Console.println("Price:"+s.getPrice()+"\n"); + } + + break; + + case "7": exitProgram=true; Console.println("Program Exited:Goodbye"); break; From 2e0262a12dc17d44468e679c6d53a090db909e58 Mon Sep 17 00:00:00 2001 From: sitarameez Date: Fri, 9 Jul 2021 17:50:05 -0400 Subject: [PATCH 14/14] updated --- src/main/java/Services/ClothingService.java | 2 ++ src/main/java/io/App.java | 1 + 2 files changed, 3 insertions(+) diff --git a/src/main/java/Services/ClothingService.java b/src/main/java/Services/ClothingService.java index 2098c7e..df1b8b9 100644 --- a/src/main/java/Services/ClothingService.java +++ b/src/main/java/Services/ClothingService.java @@ -29,9 +29,11 @@ public Clothing findClothing(int id) { } public Clothing[] findAll(){ + // ArrayList inventoryClone =(ArrayList) inventory.clone(); // return (Clothing[]) inventoryClone.toArray(); return inventory.toArray(new Clothing[0]); + } public boolean delete(int id){ for(Clothing deleteObject : inventory){ diff --git a/src/main/java/io/App.java b/src/main/java/io/App.java index 70adcee..c05da9e 100644 --- a/src/main/java/io/App.java +++ b/src/main/java/io/App.java @@ -9,6 +9,7 @@ import java.util.ArrayList; public class App { + //instantiation private ClothingService clothingService = new ClothingService(); // (1) private ChocolateService chocolateService =new ChocolateService(); private Chocolate chocolate=new Chocolate();