From a7b20177d4348ca82eadf1997fe2393ab9f9e76c Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 2 Jul 2021 12:56:34 -0400 Subject: [PATCH 1/3] Added Coffee/Mugs models and services --- .idea/Product-Inventory-Lab.iml | 2 ++ .idea/compiler.xml | 1 + .idea/jarRepositories.xml | 20 +++++++++++ pom.xml | 33 +++++++++++++++++++ src/main/java/models/Coffee.java | 4 +++ src/main/java/models/Mugs.java | 4 +++ src/main/java/services/CoffeeService.java | 4 +++ src/main/java/services/MugsService.java | 4 +++ src/test/java/models/CoffeeTest.java | 9 +++++ src/test/java/models/MugsTest.java | 8 +++++ src/test/java/services/CoffeeServiceTest.java | 7 ++++ src/test/java/services/MugsServiceTest.java | 7 ++++ 12 files changed, 103 insertions(+) create mode 100644 .idea/Product-Inventory-Lab.iml create mode 100644 .idea/jarRepositories.xml create mode 100644 src/main/java/models/Coffee.java create mode 100644 src/main/java/models/Mugs.java create mode 100644 src/main/java/services/CoffeeService.java create mode 100644 src/main/java/services/MugsService.java create mode 100644 src/test/java/models/CoffeeTest.java create mode 100644 src/test/java/models/MugsTest.java create mode 100644 src/test/java/services/CoffeeServiceTest.java create mode 100644 src/test/java/services/MugsServiceTest.java 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..27003ae 100644 --- a/pom.xml +++ b/pom.xml @@ -22,4 +22,37 @@ + + + org.junit.jupiter + junit-jupiter-engine + 5.4.2 + test + + + org.junit.jupiter + junit-jupiter-api + 5.4.2 + test + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + junit + junit + RELEASE + test + + + \ No newline at end of file diff --git a/src/main/java/models/Coffee.java b/src/main/java/models/Coffee.java new file mode 100644 index 0000000..934bdc0 --- /dev/null +++ b/src/main/java/models/Coffee.java @@ -0,0 +1,4 @@ +package models; + +public class Coffee { +} diff --git a/src/main/java/models/Mugs.java b/src/main/java/models/Mugs.java new file mode 100644 index 0000000..4ed2a5a --- /dev/null +++ b/src/main/java/models/Mugs.java @@ -0,0 +1,4 @@ +package models; + +public class Mugs { +} diff --git a/src/main/java/services/CoffeeService.java b/src/main/java/services/CoffeeService.java new file mode 100644 index 0000000..b4d932e --- /dev/null +++ b/src/main/java/services/CoffeeService.java @@ -0,0 +1,4 @@ +package services; + +public class CoffeeService { +} diff --git a/src/main/java/services/MugsService.java b/src/main/java/services/MugsService.java new file mode 100644 index 0000000..243a11d --- /dev/null +++ b/src/main/java/services/MugsService.java @@ -0,0 +1,4 @@ +package services; + +public class MugsService { +} diff --git a/src/test/java/models/CoffeeTest.java b/src/test/java/models/CoffeeTest.java new file mode 100644 index 0000000..a52f6ed --- /dev/null +++ b/src/test/java/models/CoffeeTest.java @@ -0,0 +1,9 @@ +package models; + + +import org.junit.Test; + + +class CoffeeTest { + +} \ No newline at end of file diff --git a/src/test/java/models/MugsTest.java b/src/test/java/models/MugsTest.java new file mode 100644 index 0000000..dd4eafc --- /dev/null +++ b/src/test/java/models/MugsTest.java @@ -0,0 +1,8 @@ +package models; + + +import org.junit.Test; + +class MugsTest { + +} \ No newline at end of file diff --git a/src/test/java/services/CoffeeServiceTest.java b/src/test/java/services/CoffeeServiceTest.java new file mode 100644 index 0000000..e845264 --- /dev/null +++ b/src/test/java/services/CoffeeServiceTest.java @@ -0,0 +1,7 @@ +package services; + +import org.junit.Test; + +public class CoffeeServiceTest { + +} diff --git a/src/test/java/services/MugsServiceTest.java b/src/test/java/services/MugsServiceTest.java new file mode 100644 index 0000000..09c877e --- /dev/null +++ b/src/test/java/services/MugsServiceTest.java @@ -0,0 +1,7 @@ +package services; + +import org.junit.Test; + +public class MugsServiceTest { + +} From 6e303997f463395e24195b4583d13df1bae918e3 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 2 Jul 2021 14:44:04 -0400 Subject: [PATCH 2/3] Followed instructions for section 2 --- src/main/java/models/Coffee.java | 59 +++++++ src/main/java/services/CoffeeService.java | 29 ++++ src/test/java/models/CoffeeTest.java | 156 +++++++++++++++++- src/test/java/models/MugsTest.java | 2 +- src/test/java/services/CoffeeServiceTest.java | 28 ++++ 5 files changed, 271 insertions(+), 3 deletions(-) diff --git a/src/main/java/models/Coffee.java b/src/main/java/models/Coffee.java index 934bdc0..80f4058 100644 --- a/src/main/java/models/Coffee.java +++ b/src/main/java/models/Coffee.java @@ -1,4 +1,63 @@ package models; public class Coffee { + private Integer unitId; + private Integer amountInStock; + private String brand; + private String flavor; + private Double price; + private Double weight; + + public Coffee(Integer expectedUnitId, Integer expectedAmountInStock, String expectedBrand, String expectedFlavor, Double expectedPrice, Double expectedWeight) { + + } + + public Integer getUnitId() { + return unitId; + } + + public void setUnitId(Integer unitId) { + this.unitId = unitId; + } + + public Integer getAmountInStock() { + return amountInStock; + } + + public void setAmountInStock(Integer amountInStock) { + this.amountInStock = amountInStock; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getFlavor() { + return flavor; + } + + public void setFlavor(String flavor) { + this.flavor = flavor; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public Double getWeight() { + return weight; + } + + public void setWeight(Double weight) { + this.weight = weight; + } + } diff --git a/src/main/java/services/CoffeeService.java b/src/main/java/services/CoffeeService.java index b4d932e..eecf7e7 100644 --- a/src/main/java/services/CoffeeService.java +++ b/src/main/java/services/CoffeeService.java @@ -1,4 +1,33 @@ package services; +import models.Coffee; + +import java.util.ArrayList; + public class CoffeeService { + + private static Integer nextUnitId = 1; + private static ArrayList inventory = new ArrayList<>(); + + public static Coffee create(Integer unitId, Integer amountInStock, String brand, String flavor, Double price, Double weight) { + + Coffee createdCoffee = new Coffee(unitId++, amountInStock, brand, flavor, price, weight); + + inventory.add(createdCoffee); + + return createdCoffee; + } + + public Coffee findCoffee(Integer unitId) { + return inventory.get(unitId); + } + public Coffee[] findAll() { + return inventory.toArray(new Coffee[0]); + } + public boolean delete(Integer unitId) { + if (inventory.contains(unitId)) { + inventory.remove(unitId); + } + return false; + } } diff --git a/src/test/java/models/CoffeeTest.java b/src/test/java/models/CoffeeTest.java index a52f6ed..ec5efd2 100644 --- a/src/test/java/models/CoffeeTest.java +++ b/src/test/java/models/CoffeeTest.java @@ -1,9 +1,161 @@ package models; +import org.junit.Assert; +import org.junit.Before; import org.junit.Test; -class CoffeeTest { - +public class CoffeeTest { + + + @Test + public void constructorTest () { + //Given + Integer expectedUnitId = 203; + Integer expectedAmountInStock = 1000; + String expectedBrand = "Maxwell"; + String expectedFlavor = "Dark Roast"; + Double expectedPrice = 16.00; + Double expectedWeight = 12.0; + //When + Coffee testCoffee = new Coffee(expectedUnitId, expectedAmountInStock, expectedBrand, expectedFlavor + , expectedPrice, expectedWeight); + } + + @Test + public void getUnitIdTest () { + // given + Integer expectedUnitId = 100; + + // when + Coffee coffee = new Coffee(expectedUnitId, null, null , null + , null , null ); + coffee.setUnitId(expectedUnitId); + Integer actual = coffee.getUnitId(); + + // then + Assert.assertEquals(expectedUnitId, actual); + } + + @Test + public void setUnitIdTest () { + // given + Integer expectedUnitId = 100; + + // when + Coffee coffee = new Coffee(expectedUnitId, null, null , null + , null , null ); + coffee.setUnitId(expectedUnitId); + Integer actual = coffee.getUnitId(); + + // then + Assert.assertEquals(expectedUnitId, actual); + } + + @Test + public void getAmountInStock () { + // given + Integer expectedAmount = 1000; + // when + Coffee coffee = new Coffee(null, expectedAmount, null , null + , null , null ); + coffee.setAmountInStock(expectedAmount); + Integer actual = coffee.getAmountInStock(); + // then + Assert.assertEquals(expectedAmount, actual); + } + + @Test + public void setAmountInStock () { + // given + Integer expectedAmount = 1000; + // when + Coffee coffee = new Coffee(null, expectedAmount, null , null + , null , null ); + coffee.setAmountInStock(expectedAmount); + Integer actual = coffee.getAmountInStock(); + // then + Assert.assertEquals(expectedAmount, actual); + } + + @Test + public void getBrandTest () { + String expectedBrand = "Folgers"; + Coffee coffee = new Coffee(null, null, expectedBrand , null + , null , null ); + coffee.setBrand(expectedBrand); + String actual = coffee.getBrand(); + Assert.assertEquals(expectedBrand, actual); + } + + @Test + public void setBrandTest () { + String expectedBrand = "Folgers"; + Coffee coffee = new Coffee(null, null, expectedBrand , null + , null , null ); + coffee.setBrand(expectedBrand); + String actual = coffee.getBrand(); + Assert.assertEquals(expectedBrand, actual); + } + + @Test + public void getFlavorTest () { + String expectedFlavor = "Dark Roast"; + Coffee coffee = new Coffee(null, null, null, expectedFlavor + , null , null ); + coffee.setFlavor(expectedFlavor); + String actual = coffee.getFlavor(); + Assert.assertEquals(expectedFlavor, actual); + } + + @Test + public void setFlavorTest () { + String expectedFlavor = "Dark Roast"; + Coffee coffee = new Coffee(null, null, null, expectedFlavor + , null , null ); + coffee.setFlavor(expectedFlavor); + String actual = coffee.getFlavor(); + Assert.assertEquals(expectedFlavor, actual); + } + + @Test + public void setPriceTest () { + Double expectedPrice = 10.00; + Coffee coffee = new Coffee(null, null, null, null + , expectedPrice , null ); + coffee.setPrice(expectedPrice); + Double actual = coffee.getPrice(); + Assert.assertEquals(expectedPrice, actual); + } + + @Test + public void getPriceTest () { + Double expectedPrice = 10.00; + Coffee coffee = new Coffee(null, null, null, null + , expectedPrice , null ); + coffee.setPrice(expectedPrice); + Double actual = coffee.getPrice(); + Assert.assertEquals(expectedPrice, actual); + } + + @Test + public void setWeightTest () { + Double expectedWeight = 16.0; + Coffee coffee = new Coffee(null, null, null, null + , null , expectedWeight ); + coffee.setWeight(expectedWeight); + Double actual = coffee.getWeight(); + Assert.assertEquals(expectedWeight, actual); + } + + @Test + public void getWeightTest () { + Double expectedWeight = 16.0; + Coffee coffee = new Coffee(null, null, null, null + , null , expectedWeight ); + coffee.setWeight(expectedWeight); + Double actual = coffee.getWeight(); + Assert.assertEquals(expectedWeight, actual); + } } \ No newline at end of file diff --git a/src/test/java/models/MugsTest.java b/src/test/java/models/MugsTest.java index dd4eafc..fc55c1c 100644 --- a/src/test/java/models/MugsTest.java +++ b/src/test/java/models/MugsTest.java @@ -3,6 +3,6 @@ import org.junit.Test; -class MugsTest { +public class MugsTest { } \ No newline at end of file diff --git a/src/test/java/services/CoffeeServiceTest.java b/src/test/java/services/CoffeeServiceTest.java index e845264..a6348da 100644 --- a/src/test/java/services/CoffeeServiceTest.java +++ b/src/test/java/services/CoffeeServiceTest.java @@ -1,7 +1,35 @@ package services; +import models.Coffee; +import org.junit.Assert; import org.junit.Test; public class CoffeeServiceTest { + @Test + public void serviceUnitIdTest () { + Integer expectedUnitId = 4; + Integer expectedAmountInStock = 499; + String expectedBrand = "Maxwell"; + String expectedFlavor = "Light Roast"; + Double expectedPrice = 19.99; + Double expectedWeight = 16.0; + CoffeeService coffeeService = new CoffeeService(); + Coffee testCoffee = coffeeService.create(expectedUnitId, expectedAmountInStock, expectedBrand, expectedFlavor + , expectedPrice, expectedWeight); + + Integer actualUnitId = testCoffee.getUnitId(); + Integer actualAmountInStock = testCoffee.getAmountInStock(); + String actualBrand = testCoffee.getBrand(); + String actualFlavor = testCoffee.getFlavor(); + Double actualPrice = testCoffee.getPrice(); + Double actualWeight = testCoffee.getWeight(); + + Assert.assertEquals(Integer.class.getName(), new Integer(actualUnitId).getClass().getName()); + Assert.assertEquals(expectedAmountInStock, actualAmountInStock); + Assert.assertEquals(expectedBrand, actualBrand); + Assert.assertEquals(expectedFlavor, actualFlavor); + Assert.assertEquals(expectedPrice, actualPrice); + Assert.assertEquals(expectedWeight, actualWeight); + } } From a30cf1e2fcf55c4e3618e8cd26f680115734395f Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 5 Jul 2021 19:20:24 -0400 Subject: [PATCH 3/3] Completed barebonesof the lab --- pom.xml | 5 + src/main/java/models/Coffee.java | 26 +++- src/main/java/models/Console.java | 115 ++++++++++++++++++ src/main/java/models/Inventory.java | 35 ++++++ src/main/java/models/MainApplication.java | 23 ++++ src/main/java/services/CoffeeService.java | 88 +++++++++++++- src/main/java/utils/CSVUtils.java | 30 +++++ src/test/java/services/CoffeeServiceTest.java | 1 + 8 files changed, 314 insertions(+), 9 deletions(-) create mode 100644 src/main/java/models/Console.java create mode 100644 src/main/java/models/Inventory.java create mode 100644 src/main/java/models/MainApplication.java create mode 100644 src/main/java/utils/CSVUtils.java diff --git a/pom.xml b/pom.xml index 27003ae..1708df8 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,11 @@ + + com.fasterxml.jackson.core + jackson-databind + 2.10.1 + org.junit.jupiter junit-jupiter-engine diff --git a/src/main/java/models/Coffee.java b/src/main/java/models/Coffee.java index 80f4058..2f9e170 100644 --- a/src/main/java/models/Coffee.java +++ b/src/main/java/models/Coffee.java @@ -1,5 +1,6 @@ package models; + public class Coffee { private Integer unitId; private Integer amountInStock; @@ -8,8 +9,14 @@ public class Coffee { private Double price; private Double weight; - public Coffee(Integer expectedUnitId, Integer expectedAmountInStock, String expectedBrand, String expectedFlavor, Double expectedPrice, Double expectedWeight) { - + public Coffee(Integer expectedUnitId, Integer expectedAmountInStock, String expectedBrand, + String expectedFlavor, Double expectedPrice, Double expectedWeight) { + this.unitId = expectedUnitId; + this.amountInStock = expectedAmountInStock; + this.brand = expectedBrand; + this.flavor = expectedFlavor; + this.price = expectedPrice; + this.weight = expectedWeight; } public Integer getUnitId() { @@ -28,7 +35,7 @@ public void setAmountInStock(Integer amountInStock) { this.amountInStock = amountInStock; } - public String getBrand() { + public java.lang.String getBrand() { return brand; } @@ -36,7 +43,7 @@ public void setBrand(String brand) { this.brand = brand; } - public String getFlavor() { + public java.lang.String getFlavor() { return flavor; } @@ -60,4 +67,15 @@ public void setWeight(Double weight) { this.weight = weight; } + @Override + public String toString() { + String printingInventory = "Unit ID: " + getUnitId(); + printingInventory += "Amount in Stock: " + getAmountInStock(); + printingInventory += "Brand Name: " + getBrand(); + printingInventory += "Flavor Type: " + getFlavor(); + printingInventory += "Retail Price: " + getPrice(); + printingInventory += "Retail Weight: " + getWeight(); + + return printingInventory; + } } diff --git a/src/main/java/models/Console.java b/src/main/java/models/Console.java new file mode 100644 index 0000000..59717b8 --- /dev/null +++ b/src/main/java/models/Console.java @@ -0,0 +1,115 @@ +package models; + +import services.CoffeeService; +import services.MugsService; + +import java.io.Writer; +import java.lang.invoke.SwitchPoint; +import java.sql.SQLOutput; +import java.util.Scanner; + +import static services.CoffeeService.inventory; +import static services.CoffeeService.nextUnitId; + +public class Console { + private static Inventory inventory = new Inventory(); + + + public static void printWelcome() { + System.out.println("Welcome to Mugs N' Cof"); + } + + public static void initializeSampleInventory() { + CoffeeService coffeeService = inventory.getCoffeeService(); + MugsService mugsService = inventory.getMugsService(); + + coffeeService.create(100, 4000, "Folgers", + "Dark Roast", 9.99, 16.0); + coffeeService.create(69, 3500, "Maxwell", + "Light Roast", 14.99, 18.0); + coffeeService.create(4951, 30, "Starbucks", + "Medium Roast", 12.99, 24.00); + } + + public static void entryMenu () { + Scanner scanner = new Scanner(System.in); + print("1. Inventory"); + print("2. Update"); + print("3. Delete"); + print("4. Exit"); + int userInput = scanner.nextInt(); + switch (userInput) { + case 1 : + inventoryMenu(); + break; + case 2 : + updateMenu(); + break; + case 3 : + deleteMenu(); + break; + case 4 : + default: + print("See ya soon, buckaroon"); + } + + } + + public static void inventoryMenu() { + Scanner scanner = new Scanner(System.in); + print("What inventory would you like to view"); + print("1. Coffee"); + print("2. Mugs"); + int userInput = scanner.nextInt(); + switch (userInput) { + case 1 : + coffeeInventory(); + entryMenu(); + break; + case 2 : + // mugsInventory(); + break; + default: + } + + } + + public static void coffeeInventory () { + Integer option = 1; + + Coffee[] coffeeInventory = inventory.getCoffeeService().findAll(); + for (Coffee coffee : coffeeInventory) { + print(option++ + coffee.toString()); + } + } + + public static void updateMenu () { + coffeeInventory(); + Scanner scanner = new Scanner(System.in); + Coffee[] coffeeInventory = inventory.getCoffeeService().findAll(); + int userInput = scanner.nextInt(); + Coffee coffee = coffeeInventory[userInput - 1]; + print("Enter new number for amount"); + scanner.nextLine(); + userInput = scanner.nextInt(); + coffee.setAmountInStock(userInput); + entryMenu(); + } + + public static void deleteMenu () { + coffeeInventory(); + print("Enter numbered item for deletion"); + Scanner scanner = new Scanner(System.in); + Coffee[] coffeeInventory = inventory.getCoffeeService().findAll(); + int userInput = scanner.nextInt(); + Coffee coffee = coffeeInventory[userInput - 1]; + inventory.getCoffeeService().delete(coffee.getUnitId()); + entryMenu(); + } + + + public static void print(String input) { + System.out.println(input); + } + +} diff --git a/src/main/java/models/Inventory.java b/src/main/java/models/Inventory.java new file mode 100644 index 0000000..8c94795 --- /dev/null +++ b/src/main/java/models/Inventory.java @@ -0,0 +1,35 @@ +package models; + +import services.CoffeeService; +import services.MugsService; + +import java.util.Scanner; + +public class Inventory { + private CoffeeService coffeeService = new CoffeeService(); + private MugsService mugsService = new MugsService(); + + public CoffeeService getCoffeeService() { + return this.coffeeService; + } + + public MugsService getMugsService() { + return this.mugsService; + } + + public Integer coffeeInStock() { + Coffee[] allCoffee = this.coffeeService.findAll(); + Integer count = 0; + + for(Coffee index: allCoffee) { + count += index.getAmountInStock(); + } + return count; + } + + public Integer specificOrder() { + // return this.coffeeService.findCoffee(); + return null; + } + +} diff --git a/src/main/java/models/MainApplication.java b/src/main/java/models/MainApplication.java new file mode 100644 index 0000000..ed62a34 --- /dev/null +++ b/src/main/java/models/MainApplication.java @@ -0,0 +1,23 @@ +package models; + +import services.CoffeeService; + +import java.io.IOException; + +public class MainApplication { + + private CoffeeService coffeeService = new CoffeeService(); + + public static void main(String[] args) throws IOException { + MainApplication mainApp = new MainApplication(); + mainApp.init(); + } + + public void init() throws IOException { + Console.printWelcome(); + Console.initializeSampleInventory(); + //CoffeeService.loadData(); + CoffeeService.fileSaver(); + Console.entryMenu(); + } +} diff --git a/src/main/java/services/CoffeeService.java b/src/main/java/services/CoffeeService.java index eecf7e7..a46b54a 100644 --- a/src/main/java/services/CoffeeService.java +++ b/src/main/java/services/CoffeeService.java @@ -1,15 +1,25 @@ package services; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; + import models.Coffee; +import utils.CSVUtils; +import java.io.*; import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class CoffeeService { - private static Integer nextUnitId = 1; - private static ArrayList inventory = new ArrayList<>(); + public static Integer nextUnitId = 1; + public static ArrayList inventory = new ArrayList<>(); - public static Coffee create(Integer unitId, Integer amountInStock, String brand, String flavor, Double price, Double weight) { + public Coffee create(Integer unitId, Integer amountInStock, String brand, String flavor, Double price, Double weight) { Coffee createdCoffee = new Coffee(unitId++, amountInStock, brand, flavor, price, weight); @@ -25,9 +35,77 @@ public Coffee[] findAll() { return inventory.toArray(new Coffee[0]); } public boolean delete(Integer unitId) { - if (inventory.contains(unitId)) { - inventory.remove(unitId); + for(Coffee element: inventory){ + if(element.getUnitId() == unitId){ + inventory.remove(element); + return true; + } } return false; } + + + public static void fileSaver () throws IOException { + + + String csvFile = "/Users/nick/Desktop/Coffee.csv"; + FileWriter writer = new FileWriter(csvFile); //(1) + + CSVUtils.writeLine(writer, new ArrayList(Arrays.asList(String.valueOf(nextUnitId)))); // (2) + + for (Coffee s : inventory) { + List list = new ArrayList<>(); // (3) + list.add(String.valueOf(s.getUnitId())); + list.add(String.valueOf(s.getAmountInStock())); + list.add(s.getBrand()); + list.add(s.getFlavor()); + list.add(String.valueOf(s.getPrice())); + list.add(String.valueOf(s.getWeight())); + + CSVUtils.writeLine(writer, list); // (4) + } + +// (5) + writer.flush(); // nice + writer.close(); + + } + + + public static void loadData() { + + String csvFile = "/Users/nick/Desktop/Coffee.csv"; + String line = ""; + String csvSplitBy = ","; + + + + try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) { + nextUnitId = Integer.parseInt(br.readLine()); + + while ((line = br.readLine()) != null) { + + String[] coffee = line.split(csvSplitBy); + int nextUnitId = Integer.valueOf(coffee[0]); + int amountInStock = Integer.valueOf(coffee[1]); + String brand = coffee[2]; + String flavor = coffee[3]; + Double price = Double.valueOf(coffee[4]); + Double weight = Double.valueOf(coffee[5]); + + inventory.add(new Coffee(nextUnitId, amountInStock, brand, flavor, price, weight)); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + public static void writingJSON () throws IOException { + ObjectMapper mapper = new ObjectMapper(); + ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); + writer.writeValue(new File("sneaker.json"), inventory); + } + public static void readingJSON() throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + inventory = objectMapper.readValue(new File("sneaker.json"), new TypeReference>(){}); + } } diff --git a/src/main/java/utils/CSVUtils.java b/src/main/java/utils/CSVUtils.java new file mode 100644 index 0000000..cc309f6 --- /dev/null +++ b/src/main/java/utils/CSVUtils.java @@ -0,0 +1,30 @@ +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) + } +} + + diff --git a/src/test/java/services/CoffeeServiceTest.java b/src/test/java/services/CoffeeServiceTest.java index a6348da..ec6fc2d 100644 --- a/src/test/java/services/CoffeeServiceTest.java +++ b/src/test/java/services/CoffeeServiceTest.java @@ -1,5 +1,6 @@ package services; + import models.Coffee; import org.junit.Assert; import org.junit.Test;