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/.idea/misc.xml b/.idea/misc.xml index 4b661a5..df00c07 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 43c1af2..03895eb 100644 --- a/pom.xml +++ b/pom.xml @@ -21,5 +21,30 @@ + + + org.junit.jupiter + junit-jupiter-engine + 5.4.2 + test + + + org.junit.jupiter + junit-jupiter-api + 5.4.2 + test + + + junit + junit + RELEASE + test + + + com.fasterxml.jackson.core + jackson-databind + 2.10.1 + + \ No newline at end of file diff --git a/src/main/java/models/App.java b/src/main/java/models/App.java new file mode 100644 index 0000000..e4ccdb2 --- /dev/null +++ b/src/main/java/models/App.java @@ -0,0 +1,183 @@ +package models; + + +import services.SneakerService; + +import java.awt.*; +import java.io.IOException; +import java.sql.SQLOutput; +import java.util.InputMismatchException; +import java.util.Scanner; + +public class App { + private SneakerService sneakerService = new SneakerService(); // (1) + Scanner scanner = new Scanner(System.in); + private Sneaker sneaker = new Sneaker(); + + public static void main(String[] args) { + + App application = new App(); // (2) + application.init(); // (3) + + + + }//main + + + public void init(){ + SneakerService.loadData(); + Console.printWelcome(); + + // (4) + // application logic here + // call methods to take user input and interface with services + boolean flag = true; + while (flag) { + System.out.println(ColorEnum.CYAN.formatString("📝Main Menu")); + System.out.print(ColorEnum.BLUE.formatString("➕Create\t")); + System.out.print(ColorEnum.BLUE.formatString("📖Read\t ")); + System.out.print(ColorEnum.BLUE.formatString("🖌Update\t")); + System.out.print(ColorEnum.BLUE.formatString("⌦ Delete\t")); + System.out.print(ColorEnum.BLUE.formatString("🔍Find\t")); + System.out.println(ColorEnum.BLUE.formatString("❗️Exit\t")); + String menu = scanner.next(); + + if(menu.equalsIgnoreCase("Create")){ + add(); + try { + SneakerService.savaDataToCsv(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + else if(menu.equalsIgnoreCase("Read")){ + System.out.println("Please enter product ID:"); + int id = scanner.nextInt(); + read(id); + } + else if(menu.equalsIgnoreCase("Update")){ + System.out.println("Please enter product id"); + int id = scanner.nextInt(); + update(id); + + } + else if(menu.equalsIgnoreCase("Delete")){ + System.out.println("Please enter product id"); + int id = scanner.nextInt(); + sneaker.delete(id); + } + else if(menu.equalsIgnoreCase("Find")){ + System.out.println("Please enter product id"); + int id = scanner.nextInt(); + Sneaker sneaker1 = sneaker.findSneaker(id); + if(sneaker1!=null){ + System.out.println(sneaker1.toString()); + } + + } + + else if(menu.equalsIgnoreCase("exit")){ + break; + } + } + + } + + /**Create different products to be added to inventory + Read from existing products + Update products + Delete products + Get different reports about products + Exit the program*/ + + public void add(){ + Scanner scanner = new Scanner(System.in); + System.out.println("Please enter product name:"); + String name = scanner.nextLine(); + System.out.println("Please enter brand name:"); + String brand = scanner.nextLine(); + System.out.println("Please enter sport name:"); + String sport = scanner.nextLine(); + System.out.println("Please enter product size:"); + float size = scanner.nextFloat(); + System.out.println("Please enter product quantity:"); + int qty = scanner.nextInt(); + System.out.println("Please enter product price:"); + float price = scanner.nextFloat(); + sneaker = SneakerService.create(name,brand,sport,size,qty,price); + System.out.println("Product created and product ID: " + sneaker.getId()); + } + + public void update(int id){ + for (Sneaker s:SneakerService.getInventory()){ + if(s.getId() == id){ + System.out.println("Please choice valid option"); + System.out.println(ColorEnum.CYAN.formatString("📝Options")); + System.out.print(ColorEnum.BLUE.formatString("Name\t")); + System.out.print(ColorEnum.BLUE.formatString("Brand\t ")); + System.out.print(ColorEnum.BLUE.formatString("Sport\t")); + System.out.print(ColorEnum.BLUE.formatString("Size\t")); + System.out.println(ColorEnum.BLUE.formatString("Quantity\t")); + System.out.println(ColorEnum.BLUE.formatString("Price\t")); + String menu1 = scanner.next(); + if(menu1.equalsIgnoreCase("name")){ + System.out.println("Please enter new Product name:"); + String input = scanner.next(); + s.setName(input); + System.out.println("Name updated to: "+s.getName()); + } + else if(menu1.equalsIgnoreCase("brand")){ + System.out.println("Please enter new Brand name:"); + String input = scanner.next(); + s.setBrand(input); + System.out.println("Brand updated to: "+s.getBrand()); + } + else if(menu1.equalsIgnoreCase("sport")){ + System.out.println("Please enter new Sport name:"); + String input = scanner.next(); + s.setSport(input); + System.out.println("Sport name updated to "+s.getSport()); + } + else if(menu1.equalsIgnoreCase("size")){ + System.out.println("Please enter new size:"); + float input = scanner.nextFloat(); + s.setSize(input); + System.out.println("Size updated to "+s.getSize()); + } + else if(menu1.equalsIgnoreCase("quantity")){ + System.out.println("Please enter new size:"); + int input = scanner.nextInt(); + s.setSize(input); + System.out.println("Quantity updated to "+s.getId()); + + } + else if(menu1.equalsIgnoreCase("price")){ + System.out.println("Please enter new size:"); + int input = scanner.nextInt(); + s.setSize(input); + System.out.println("Price updated to "+ s.getPrice()); + } + + } + } + + } + + public void read(int id){ + boolean flag = true; + for (Sneaker s:SneakerService.getInventory()){ + if(s.getId() == id){ + System.out.println(s.toString()); + flag = false; + break; + } + } + if(flag) { + System.out.println(ColorEnum.RED.formatString("Product not found in the inventory!")); + } + } + + + + +}//class diff --git a/src/main/java/models/ColorEnum.java b/src/main/java/models/ColorEnum.java new file mode 100644 index 0000000..93bd0c4 --- /dev/null +++ b/src/main/java/models/ColorEnum.java @@ -0,0 +1,28 @@ +package models; + +public enum ColorEnum { + RED("\u001B[31m"), + GREEN("\u001B[32m"), + BLUE("\u001B[34m"), + YELLOW("\u001B[33m"), + CYAN("\u001B[36m"), + MAGENTA("\u001B[35m"), + WHITE("\u001B[37m"), + BLACK("\u001B[30m"); + + private final String code; + + ColorEnum(String code) { + this.code = code; + } + + public String getCode() { + return code; + } + + public String formatString(String text) { + return code + text + "\u001B[0m"; // Reset color to default after the text + } + + +} diff --git a/src/main/java/models/Console.java b/src/main/java/models/Console.java new file mode 100644 index 0000000..e2cfc17 --- /dev/null +++ b/src/main/java/models/Console.java @@ -0,0 +1,15 @@ +package models; + +import java.awt.*; + +public class Console { + public static void printWelcome(){ + System.out.println(""+ + "✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎" + "\n" + + "✴︎⭐️ Welcome and Bienvenue ⭐️✴︎" + "\n" + + "✴︎⭐️ to ⭐️✴︎" + "\n" + + "✴︎⭐️ ZipCo Inventory Manager ⭐️✴︎" + "\n" + + "✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎✴︎"); + } + +} diff --git a/src/main/java/models/Sneaker.java b/src/main/java/models/Sneaker.java new file mode 100644 index 0000000..16fe50e --- /dev/null +++ b/src/main/java/models/Sneaker.java @@ -0,0 +1,149 @@ +package models; + +import services.SneakerService; + +import java.io.IOException; +import java.util.Arrays; + +public class Sneaker { + private int id; + private String name; + private String brand; + private String sport; + private float size; + private int qty; + private float price; + SneakerService sneakerService = new SneakerService(); + + public Sneaker(){ + + } + + + public Sneaker(int id, String name, String brand, String sport, float size, int qty, float price) { + this.id = id; + this.name = name; + this.brand = brand; + this.sport = sport; + this.size = size; + this.qty = qty; + this.price = price; + } + + public Sneaker(int id, String name, String brand, String sport, int qty, float price) { + this.id = id; + this.name = name; + this.brand = brand; + this.sport = sport; + this.qty = qty; + this.price = price; + } + + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getSport() { + return sport; + } + + public void setSport(String sport) { + this.sport = sport; + } + + public float getSize() { + return size; + } + + public void setSize(float size) { + this.size = size; + } + + public int getQty() { + return qty; + } + + public void setQty(int qty) { + this.qty = qty; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } + + //read + public Sneaker findSneaker(int id) { + // should take an int and return an object with that id, if exists + boolean flag = true; + Sneaker sn = null; + for (Sneaker s:SneakerService.getInventory()){ + if(s.getId() == id){ + //System.out.println(s.toString()); + sn = s; + flag = false; + break; + } + } + if(flag) { + System.out.println(ColorEnum.RED.formatString("Product not found in the inventory!")); + } + return sn; + } + + //read all + public Sneaker[] findAll() { + // should return a basic array copy of the ArrayList + return SneakerService.getInventory().toArray(new Sneaker[0]); + } + + //delete + public boolean delete(int id) { + // should remove the object with this id from the ArrayList if exits and return true. + // Otherwise return false + boolean flag = true; + for (Sneaker s:SneakerService.getInventory()){ + if(s.getId() == id){ + SneakerService.getInventory().remove(s); + flag = false; + System.out.println("Product ID "+s.getId()+" removed from inventory list"); + break; + } + } + if(flag) { + System.out.println(ColorEnum.RED.formatString("Product not found in the inventory!")); + } + + return flag; + } + + @Override + public String toString() { + return "ID 🟰 "+getId() + " \nProduct Name 🟰 " + getName() + " \nBrand Name 🟰 " + getBrand() + " \nSport 🟰 " + +getSport()+" \nSize 🟰 "+getSize()+" \nQuantity 🟰 "+getQty() + " \nPrice 🟰 "+getPrice(); + } +} diff --git a/src/main/java/models/Whiskey.java b/src/main/java/models/Whiskey.java new file mode 100644 index 0000000..bb9e5d5 --- /dev/null +++ b/src/main/java/models/Whiskey.java @@ -0,0 +1,4 @@ +package models; + +public class Whiskey { +} diff --git a/src/main/java/services/CSVUtils.java b/src/main/java/services/CSVUtils.java new file mode 100644 index 0000000..2fe2dfc --- /dev/null +++ b/src/main/java/services/CSVUtils.java @@ -0,0 +1,28 @@ +package services; + +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/main/java/services/Sneaker.csv b/src/main/java/services/Sneaker.csv new file mode 100644 index 0000000..e6129de --- /dev/null +++ b/src/main/java/services/Sneaker.csv @@ -0,0 +1,5 @@ +5 +1,a,a,a,1,1.0 +2,b,b,b,2,2.0 +3,c,c,c,3,3.0 +4,q,q,q,5,5.0 diff --git a/src/main/java/services/SneakerService.java b/src/main/java/services/SneakerService.java new file mode 100644 index 0000000..a9442fe --- /dev/null +++ b/src/main/java/services/SneakerService.java @@ -0,0 +1,96 @@ +package services; + +import models.Sneaker; + +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 SneakerService { + + private static int nextId = 1; // (1) + + private static List inventory = new ArrayList<>(); // (2) + + + public static Sneaker create(String name, String brand,String sport,float size,int qty,float price){ + Sneaker createdSneaker = new Sneaker(nextId,name,brand,sport,size,qty,price); + inventory.add(createdSneaker); + nextId++; + return createdSneaker; + } + + + + public Sneaker find(Sneaker sneaker){ + return inventory.get(nextId); + } + + + public static List getInventory() { + return inventory; + } + + public static int getNextId() { + return nextId; + } + + + public static void savaDataToCsv() throws IOException { + String csvFile = "/Users/asan/Desktop/Projects/Product-Inventory-Lab/src/main/java/services/Sneaker.csv"; + FileWriter writer = new FileWriter(csvFile); //(1) + + CSVUtils.writeLine(writer, new ArrayList<>(Arrays.asList(String.valueOf(nextId)))); // (2) + + for (Sneaker s : inventory) { + List list = new ArrayList<>(); // (3) + list.add(String.valueOf(s.getId())); + list.add(s.getName()); + list.add(s.getBrand()); + list.add(s.getSport()); + list.add(String.valueOf(s.getQty())); + list.add(String.valueOf(s.getPrice())); + CSVUtils.writeLine(writer, list); // (4) + } + +// (5) + writer.flush(); + writer.close(); + } + + + + public static void loadData(){ + // (1) + String csvFile = "src/main/java/services/Sneaker.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[] beer = line.split(csvSplitBy); + + // (4) + int id = Integer.parseInt(beer[0]); + String name = beer[1]; + String brand = beer[2]; + String sport = beer[3]; + int qty = Integer.parseInt(beer[4]); + float price = Float.parseFloat(beer[5]); + + // (5) + inventory.add(new Sneaker(id, name, brand, sport, qty, price)); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/services/WhiskeyService.java b/src/main/java/services/WhiskeyService.java new file mode 100644 index 0000000..1307c24 --- /dev/null +++ b/src/main/java/services/WhiskeyService.java @@ -0,0 +1,4 @@ +package services; + +public class WhiskeyService { +} diff --git a/src/main/java/services/sneaker.json b/src/main/java/services/sneaker.json new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/models/SneakerTest.java b/src/test/java/models/SneakerTest.java new file mode 100644 index 0000000..2d8a8cd --- /dev/null +++ b/src/test/java/models/SneakerTest.java @@ -0,0 +1,80 @@ +package models; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import services.SneakerService; + +import java.util.ArrayList; +import java.util.List; + +public class SneakerTest { + @Test + public void setNameTest() { + Sneaker testSneaker1 = new Sneaker(); + // given (1) + String expected = "OZWEEGO"; + + // when (2) + testSneaker1 = new Sneaker(); + testSneaker1.setName(expected); + + // then (3) + Assertions.assertEquals(expected, testSneaker1.getName()); + } + + + @Test // (1) + public void constructorTest() { + + // (2) + int expectedId = 6; + String expectedName = "Stan Smith"; + String expectedBrand = "Adidas"; + String expectedSport = "Tennnis"; + int expectedQty = 10; + float expectedPrice = 80.00f; + + // (3) + Sneaker testSneaker = new Sneaker(expectedId, expectedName, expectedBrand, expectedSport, expectedQty, expectedPrice); + + // (4) + Assertions.assertEquals(expectedId, testSneaker.getId()); + Assertions.assertEquals(expectedName, testSneaker.getName()); + Assertions.assertEquals(expectedBrand, testSneaker.getBrand()); + Assertions.assertEquals(expectedSport, testSneaker.getSport()); + Assertions.assertEquals(expectedQty, testSneaker.getQty()); + Assertions.assertEquals(expectedPrice, testSneaker.getPrice()); + } + + + @Test + public void findSneaker() { + Sneaker sneaker = SneakerService.create("Jogger", "Adidas", "Run", 10.5f, 1, 115); + int id = sneaker.getId(); + int expected = 1; + Assert.assertEquals(expected, id); + } + + @Test + public void findAll() { + Sneaker sneaker = SneakerService.create("Jogger", "Adidas", "Run", 10.5f, 1, 115f); + String expectedName = "Jogger"; + String expectedBrand = "Adidas"; + String expectedSportName = "Run"; + float expectedSize = 10.5f; + int expectedQty = 1; + float expectedPrice = 115f; + //copy of the inventory list + Sneaker[] sneakers = SneakerService.getInventory().toArray(new Sneaker[0]); + Assert.assertEquals(expectedName,sneakers[0].getName()); + Assert.assertEquals(expectedBrand,sneakers[0].getBrand()); + Assert.assertEquals(expectedSportName,sneakers[0].getSport()); + Assert.assertEquals(expectedSize,sneakers[0].getSize(),0.01); + Assert.assertEquals(expectedQty,sneakers[0].getQty()); + Assert.assertEquals(expectedPrice,sneakers[0].getPrice(),0.01); + } + + +} + diff --git a/src/test/java/models/WhiskeyTest.java b/src/test/java/models/WhiskeyTest.java new file mode 100644 index 0000000..81c7672 --- /dev/null +++ b/src/test/java/models/WhiskeyTest.java @@ -0,0 +1,4 @@ +package models; + +public class WhiskeyTest { +} diff --git a/src/test/java/services/SneakerServiceTest.java b/src/test/java/services/SneakerServiceTest.java new file mode 100644 index 0000000..d0e7c5c --- /dev/null +++ b/src/test/java/services/SneakerServiceTest.java @@ -0,0 +1,44 @@ +package services; + +import models.Sneaker; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +public class SneakerServiceTest { + @Test + public void createTest(){ + + // (1) + String expectedName = "Stan Smith"; + String expectedBrand = "Adidas"; + String expectedSport = "Tennis"; + float expectedSize = 10.5f; + int expectedQty = 10; + float expectedPrice = 80.00f; + + // (2) + SneakerService sneakerService = new SneakerService(); + Sneaker testSneaker = SneakerService.create(expectedName, expectedBrand, + expectedSport, expectedSize, expectedQty, expectedPrice); + + // (3) + int actualId = testSneaker.getId(); + String actualName = testSneaker.getName(); + String actualBrand = testSneaker.getBrand(); + String actualSport = testSneaker.getSport(); + float actualSize = testSneaker.getSize(); + int actualQty = testSneaker.getQty(); + float actualPrice = testSneaker.getPrice(); + + // (4) + Assertions.assertEquals(Integer.class.getName(), new Integer(actualId).getClass().getName()); + Assertions.assertEquals(expectedName, actualName); + Assertions.assertEquals(expectedBrand, actualBrand); + Assertions.assertEquals(expectedSport, actualSport); + Assertions.assertEquals(expectedSize, actualSize); + Assertions.assertEquals(expectedQty, actualQty); + Assertions.assertEquals(expectedPrice, actualPrice); + + } + } + diff --git a/src/test/java/services/WhiskeyServiceTest.java b/src/test/java/services/WhiskeyServiceTest.java new file mode 100644 index 0000000..cb856f0 --- /dev/null +++ b/src/test/java/services/WhiskeyServiceTest.java @@ -0,0 +1,4 @@ +package services; + +public class WhiskeyServiceTest { +}