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/instructions/section-01.md b/instructions/section-01.md index ab6c191..8e22efe 100644 --- a/instructions/section-01.md +++ b/instructions/section-01.md @@ -10,6 +10,7 @@ In this section we start with an empty Java project and start to setup our envir * Import a testing framework to support Test Driven Development(TDD) * Creating classes stubs for our products + ## Part 1 - Repository 1. Log into github.com diff --git a/pom.xml b/pom.xml index 43c1af2..5c25a7e 100644 --- a/pom.xml +++ b/pom.xml @@ -21,5 +21,23 @@ - + + + com.fasterxml.jackson.core + jackson-databind + 2.10.1 + + + org.junit.jupiter + junit-jupiter-engine + 5.4.2 + test + + + org.junit.jupiter + junit-jupiter-api + 5.4.2 + test + + \ No newline at end of file diff --git a/src/main/java/io/App.java b/src/main/java/io/App.java new file mode 100644 index 0000000..5d45483 --- /dev/null +++ b/src/main/java/io/App.java @@ -0,0 +1,26 @@ +package io; +import services.SneakerService; + +import java.io.IOException; + +public class App { + public static void main(String... args) throws IOException { + + App application = new App(); + Inventory.putSomeItemsInInventory(); + application.init(); + + // Creating a new CSV file + //SneakerService.makeCSVfile(); + + } + private void init() throws IOException { + Console.printWelcome(); + Console.printOptions(); + Console.options(); + //SneakerService.makeCSVfile(); + SneakerService.loadData(); + + } + +} diff --git a/src/main/java/io/Console.java b/src/main/java/io/Console.java new file mode 100644 index 0000000..3a96da4 --- /dev/null +++ b/src/main/java/io/Console.java @@ -0,0 +1,82 @@ +package io; + +import java.util.Scanner; + +public class Console { + + public static void printWelcome(){ + System.out.println("" + + "**************************************************" + "\n" + + "*** Welcome and Bienvenue ***" + "\n" + + "*** to ***" + "\n" + + "*** Nathan's Inventory Manager ***" + "\n" + + "**************************************************"); + } + //Prints out the options for app + public static void printOptions() { + System.out.println("" + + "*****************************************" + "\n" + + "*** 1. Create new product ***" + "\n" + + "*** 2. View current inventory ***" + "\n" + + "*** 3. Update product ***" + "\n" + + "*** 4. View product reports ***" + "\n" + + "*** 5. Exit ***" + "\n" + + "*****************************************"); + } + + 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); + return scanner.nextLine(); + } + + public static Integer getIntegerInput(String prompt) { + Scanner scanner = new Scanner(System.in); + println(prompt); + return Integer.parseInt(scanner.nextLine()); + } + + public static Float getFloatInput(String prompt) { + Scanner scanner = new Scanner(System.in); + println(String.valueOf(prompt)); + return Float.parseFloat((scanner.nextLine())); + } + + public static void options() { + boolean exitNow = false; + while(exitNow == false){ + Integer option = Console.getIntegerInput("Type number to choose option:"); + switch (option) { + case 1: + Inventory.makeNewProduct(); + break; + case 2: + //View current inventory + Inventory.viewInventory(); + break; + case 3: + //Update product - delete product + Inventory.removeProduct(); + break; + case 4: + //View product reports + Inventory.viewProduct(); + break; + case 5: + //Exit + exitNow = true; + break; + } + } + } + + +} diff --git a/src/main/java/io/Inventory.java b/src/main/java/io/Inventory.java new file mode 100644 index 0000000..d7f1b7f --- /dev/null +++ b/src/main/java/io/Inventory.java @@ -0,0 +1,95 @@ +package io; + +import models.Shirt; +import models.Sneaker; +import services.CSVUtils; +import services.ShirtService; +import services.SneakerService; + +import java.io.FileWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Locale; + +public class Inventory { + static SneakerService sneakerService = new SneakerService(); + static ShirtService shirtService = new ShirtService(); + + static void putSomeItemsInInventory() { + sneakerService.create("NitroBalance", "Nike", "Running", 8, 60.00f); + sneakerService.create("RainbowFeet", "Adidas", "Soccer", 7, 55.00f); + sneakerService.create("BubbleGum", "Puma", "Dance", 6, 70.00f); + shirtService.create("Vneck", "Jcrew", "Cotton", 25, 20.00f); + shirtService.create("Button-Down", "FruitDeLoom", "Cotton", 50, 10.00f); + shirtService.create("Sweater", "Nautica", "Wool", 15, 45.00f); + } + + + static void viewInventory(){ + ArrayList wholeInventory = new ArrayList<>(); + for (Shirt shirt: shirtService.findAll()) { + wholeInventory.add(shirt.getShirtType()); + } + for (Sneaker sneaker: sneakerService.findAll()) { + wholeInventory.add(sneaker.getName()); + } + String readOut = wholeInventory.toString(); + Console.println("The current inventory has: " + readOut.substring(1, readOut.length()-1 )); + } + + static void viewProduct() { + String productType = Console.getStringInput("Shirts or Sneakers?"); + switch (productType.toLowerCase(Locale.ROOT)) { + case "shirts": + Integer viewThisShirt = Console.getIntegerInput("Enter item ID to view"); + Console.println("" + shirtService.find(viewThisShirt).toString()); + break; + case "sneaker": + Integer viewThisSneaker = Console.getIntegerInput("Enter item ID to view"); + Console.println("" + sneakerService.find(viewThisSneaker).toString()); + break; + } + } + + static void removeProduct() { + String productType = Console.getStringInput("Shirts or Sneakers?"); + switch (productType.toLowerCase(Locale.ROOT)){ + case "shirts": + Integer deleteThisShirt = Console.getIntegerInput("Enter item ID to delete"); + shirtService.delete(deleteThisShirt); + break; + case "sneakers": + Integer deleteThisSneaker = Console.getIntegerInput("Enter item ID to delete"); + sneakerService.delete(deleteThisSneaker); + break; + } + } + + public static void makeNewProduct(){ + String productType = Console.getStringInput("Choose shirt or sneaker").toLowerCase(Locale.ROOT); + switch(productType) { + case "shirt": + //Male new shirt + String shirtType = Console.getStringInput("Enter shirt type:"); + String shirtBrand = Console.getStringInput("Enter shirt brand:"); + String shirtMaterial = Console.getStringInput("Enter shirt material:"); + int shirtQuantity = Console.getIntegerInput("Enter quantity:"); + float shirtPrice = Console.getFloatInput("Enter price"); + shirtService.create(shirtType, shirtBrand, shirtMaterial, shirtQuantity, shirtPrice); + + break; + case "sneaker": + //Make new sneaker + String sneakerName = Console.getStringInput("Enter sneaker name:"); + String sneakerBrand = Console.getStringInput("Enter sneaker brand:"); + String sneakerSport = Console.getStringInput("Enter sneaker sport:"); + int sneakerSize = Console.getIntegerInput("Enter sneaker size:"); + int sneakerQty = Console.getIntegerInput("Enter quantity:"); + float sneakerPrice = Console.getFloatInput("Enter price:"); + sneakerService.create(sneakerName, sneakerBrand, sneakerSport, sneakerQty, sneakerPrice); + break; + } + } + +} diff --git a/src/main/java/models/Shirt.java b/src/main/java/models/Shirt.java new file mode 100644 index 0000000..a5d7463 --- /dev/null +++ b/src/main/java/models/Shirt.java @@ -0,0 +1,71 @@ +package models; + +public class Shirt { + private int id; + private String shirtType; + private String brand; + private String material; + private int quantity; + private float price; + + public Shirt(){ + } + + public Shirt(int id, String shirtType, String brand, String material, int quantity, float price) { + this.id = id; + this.shirtType = shirtType; + this.brand = brand; + this.material = material; + this.quantity = quantity; + this.price = price; + } + + public void setShirtType(String type) { + this.shirtType = type; + } + + public String getShirtType() { + return this.shirtType; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getBrand() { + return this.brand; + } + + public void setMaterial(String material) { + this.material = material; + } + + public String getMaterial() { + return this.material; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public int getQuantity() { + return this.quantity; + } + + public void setPrice(float price) { + this.price = price; + } + + public float getPrice() { + return this.price; + } + + public void setId(int id) { + this.id = id; + } + + public int getId() { + return this.id; + } + +} diff --git a/src/main/java/models/Sneaker.java b/src/main/java/models/Sneaker.java new file mode 100644 index 0000000..e26f567 --- /dev/null +++ b/src/main/java/models/Sneaker.java @@ -0,0 +1,81 @@ +package models; + +public class Sneaker { + private int id; + private String name; + private String brand; + private String sport; + private int size; + private int quantity; + private float price; + + public Sneaker(){ + } + + public Sneaker(int id, String name, String brand, String sport, int qyt, float price) { + this.id = id; + this.name = name; + this.brand = brand; + this.sport = sport; + this.quantity = qyt; + this.price = price; + } + + + public void setName(String name){ + this.name = name; + } + + public String getName(){ + return this.name; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getBrand() { + return this.brand; + } + + public void setSport(String sport) { + this.sport = sport; + } + + public String getSport() { + return this.sport; + } + + public void setSize(int size) { + this.size = size; + } + + public int getSize() { + return this.size; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public int getQuantity() { + return this.quantity; + } + + public void setPrice(float price) { + this.price = price; + } + + public float getPrice() { + return this.price; + } + + public int getId() { + return this.id; + } + + public void setID(int id){ + this.id = id; + } + +} diff --git a/src/main/java/services/CSVUtils.java b/src/main/java/services/CSVUtils.java new file mode 100644 index 0000000..9d40053 --- /dev/null +++ b/src/main/java/services/CSVUtils.java @@ -0,0 +1,29 @@ +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/ShirtService.java b/src/main/java/services/ShirtService.java new file mode 100644 index 0000000..1fea93a --- /dev/null +++ b/src/main/java/services/ShirtService.java @@ -0,0 +1,46 @@ +package services; + +import models.Shirt; +import models.Sneaker; + +import java.util.ArrayList; + +public class ShirtService { + //ID for each shirt + private static int nextID = 1; + //ArrayList 'inventory' to hold all shirts + private static ArrayList inventory = new ArrayList<>(); + + //Method to create a new shirt and add to inventory + public Shirt create(String shirtType, String brand, String material, int quantity, float price) { + //instantiate a new shirt object + Shirt newShirt = new Shirt(nextID++, shirtType, brand, material, quantity, price); + //Add new shirt object to inventory + inventory.add(newShirt); + return newShirt; + } + + + public Object find(int id) { + for (Shirt shirt: inventory) { + if(shirt.getId() == id){ + return shirt; + } + } + return null; + } + + public Shirt[] findAll() { + return inventory.toArray(new Shirt[0]); + } + + public boolean delete(int id) { + for(Shirt shirt: inventory) { + if(shirt.getId() == id){ + inventory.remove(shirt); + return true; + } + } + return false; + } +} diff --git a/src/main/java/services/SneakerService.java b/src/main/java/services/SneakerService.java new file mode 100644 index 0000000..12824dc --- /dev/null +++ b/src/main/java/services/SneakerService.java @@ -0,0 +1,121 @@ +package services; + +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +import jdk.internal.org.objectweb.asm.TypeReference; +import models.Sneaker; + +import java.io.*; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class SneakerService { + //Generate an int ID for the each sneaker + public static int nextID = 1; + //Create an inventory arrayList for sneakers + private static ArrayList inventory = new ArrayList<>(); + + //Creates a new sneaker object and add it to the inventory. + public Sneaker create(String name, String brand, String sport, int qty, float price) { + //2 + Sneaker createdSneaker = new Sneaker(nextID++, name, brand, sport, qty, price); + //3 + inventory.add(createdSneaker); + //4 + return createdSneaker; + + } + + //Finds a particular sneaker object in inventory using a provided id. + public Object find(int id) { + for(Sneaker sneaker : inventory) { + if(sneaker.getId() == id){ + return sneaker; + } + } + return null; + } + + public Sneaker[] findAll() { + return inventory.toArray(new Sneaker[0]); + } + + public boolean delete(int id) { + for(Sneaker sneaker: inventory) { + if(sneaker.getId() == id){ + inventory.remove(sneaker); + return true; + } + } + return false; + } + + public static void makeCSVfile() throws IOException { + String csvFile = "/Users/nathan/Desktop/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.getQuantity())); + list.add(String.valueOf(s.getPrice())); + + CSVUtils.writeLine(writer, list); // (4) + } + + // (5) + writer.flush(); + writer.close(); + } + + public static void loadData(){ + // (1) + String csvFile = "/Users/nathan/Desktop/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(); + } + } + + public void readInJSON(){ + ObjectMapper objectMapper = new ObjectMapper(); + inventory = objectMapper.readValue(new File("sneaker.json"), new TypeReference>(){}); + } + + public void writeInJSON() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); + writer.writeValue(new File("sneaker.json"), inventory); + } + + +} diff --git a/src/test/java/io/AppTest.java b/src/test/java/io/AppTest.java new file mode 100644 index 0000000..e29a925 --- /dev/null +++ b/src/test/java/io/AppTest.java @@ -0,0 +1,4 @@ +package io; + +public class AppTest { +} diff --git a/src/test/java/io/ConsoleTest.java b/src/test/java/io/ConsoleTest.java new file mode 100644 index 0000000..a5ea791 --- /dev/null +++ b/src/test/java/io/ConsoleTest.java @@ -0,0 +1,4 @@ +package io; + +public class ConsoleTest { +} diff --git a/src/test/java/models/ShirtTest.java b/src/test/java/models/ShirtTest.java new file mode 100644 index 0000000..eb13b83 --- /dev/null +++ b/src/test/java/models/ShirtTest.java @@ -0,0 +1,88 @@ +package models; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ShirtTest { + Shirt shirt = new Shirt(); + + @Test + public void getTypeTest(){ + //Given + String expected = "Button down"; + shirt.setShirtType(expected); + //When + String actual = shirt.getShirtType(); + //Then + Assertions.assertEquals(expected, actual); + } + @Test + public void getBrandTest(){ + //Given + String expected = "Gucci"; + shirt.setBrand(expected); + //When + String actual = shirt.getBrand(); + //Then + Assertions.assertEquals(expected, actual); + } + @Test + public void getMaterialTest(){ + //Given + String expected = "Cotton"; + shirt.setMaterial(expected); + //When + String actual = shirt.getMaterial(); + //Then + Assertions.assertEquals(expected, actual); + } + @Test + public void getQuantityTest(){ + //Given + int expected = 25; + shirt.setQuantity(expected); + //When + int actual = shirt.getQuantity(); + //Then + Assertions.assertEquals(expected, actual, 0); + } + @Test + public void getPriceTest(){ + //Given + float expected = 25.99f; + shirt.setPrice(expected); + //When + double actual = shirt.getPrice(); + //Then + Assertions.assertEquals(expected, actual); + } + @Test + public void getIdTest(){ + //Given + int expectedId = 1; + shirt.setId(expectedId); + //When + int actualId = shirt.getId(); + //Then + Assertions.assertEquals(expectedId, actualId); + } + @Test + public void shirtConstructorTest(){ + //Given + int idExpected = 1; + String shirtTypeExpected = "Crew Neck" ; + String brandExpected = "Produce of the Factory"; + String materialExpected = "Cotton"; + int quantityExpected = 50; + float priceExpected = 5.99f; + //When + Shirt testShirt = new Shirt(idExpected, shirtTypeExpected, brandExpected, materialExpected, quantityExpected, priceExpected); + //Then + Assertions.assertEquals(shirtTypeExpected, testShirt.getShirtType()); + Assertions.assertEquals(brandExpected, testShirt.getBrand()); + Assertions.assertEquals(materialExpected, testShirt.getMaterial()); + Assertions.assertEquals(quantityExpected, testShirt.getQuantity()); + Assertions.assertEquals(priceExpected, testShirt.getPrice()); + } + +} diff --git a/src/test/java/models/SneakerTest.java b/src/test/java/models/SneakerTest.java new file mode 100644 index 0000000..1271030 --- /dev/null +++ b/src/test/java/models/SneakerTest.java @@ -0,0 +1,89 @@ +package models; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class SneakerTest { + @Test + public void setNameTest(){ + //Given + String expected = "OZWEEGO"; + //When + Sneaker testSneaker = new Sneaker(); + testSneaker.setName(expected); + //Then + Assertions.assertEquals(expected,testSneaker.getName()); + } + @Test + public void setBrandTest(){ + //Given + String expected = "Nike"; + //When + Sneaker testSneaker = new Sneaker(); + testSneaker.setBrand(expected); + //Then + Assertions.assertEquals(expected, testSneaker.getBrand()); + } + @Test + public void setSportTest(){ + //Given + String expected = "Running"; + //When + Sneaker testSneaker = new Sneaker(); + testSneaker.setSport(expected); + //Then + Assertions.assertEquals(expected,testSneaker.getSport()); + } + @Test + public void setSizeTest(){ + //Given + int expected = 7; + //When + Sneaker testSneaker = new Sneaker(); + testSneaker.setSize(expected); + //Then + Assertions.assertEquals(expected,testSneaker.getSize()); + } + @Test + public void setQuantityTest(){ + //Given + int expected = 50; + //When + Sneaker testSneaker = new Sneaker(); + testSneaker.setQuantity(expected); + //Then + Assertions.assertEquals(expected,testSneaker.getQuantity()); + } + @Test + public void setPriceTest(){ + //Given + float expected = 99.99f; + //When + Sneaker testSneaker = new Sneaker(); + testSneaker.setPrice(expected); + //Then + Assertions.assertEquals(expected,testSneaker.getPrice()); + } + + @Test + public void constructorTest() { + //Given + int expectedID = 01; + String expectedName = "SpeedForce"; + String expectedBrand = "Sketchers"; + String expectedSport = "Training"; + int expectedQuantity = 26; + float expectedPrice = 78.99f; + //When + Sneaker testSneaker = new Sneaker(expectedID,expectedName, expectedBrand, expectedSport, expectedQuantity, expectedPrice); + //Then + Assertions.assertEquals(expectedID, testSneaker.getId()); + Assertions.assertEquals(expectedName, testSneaker.getName()); + Assertions.assertEquals(expectedBrand, testSneaker.getBrand()); + Assertions.assertEquals(expectedSport, testSneaker.getSport()); + Assertions.assertEquals(expectedQuantity, testSneaker.getQuantity()); + Assertions.assertEquals(expectedPrice, testSneaker.getPrice()); + + + } +} diff --git a/src/test/java/services/ShirtServiceTest.java b/src/test/java/services/ShirtServiceTest.java new file mode 100644 index 0000000..398bdb6 --- /dev/null +++ b/src/test/java/services/ShirtServiceTest.java @@ -0,0 +1,118 @@ +package services; + +import models.Shirt; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +public class ShirtServiceTest { + @Test + public void createShirtTest(){ + //1 + int expectedId = 1; + String expectedShirtType = "V-neck"; + String expectedBrand = "J crew"; + String expectedMaterial = "Polyester"; + int expectedQuantity = 10; + float expectedPrice = 25.00f; + //2 + ShirtService newShirtService = new ShirtService(); + Shirt testShirt = newShirtService.create(expectedShirtType, expectedBrand, expectedMaterial, expectedQuantity, expectedPrice); + //3 + int actualId = testShirt.getId(); + String actualShirtType = testShirt.getShirtType(); + String actualBrand = testShirt.getBrand(); + String actualMaterial = testShirt.getMaterial(); + int actualQuantity = testShirt.getQuantity(); + float actualPrice = testShirt.getPrice(); + //4 + Assertions.assertEquals(expectedId, actualId); + Assertions.assertEquals(expectedShirtType, actualShirtType); + Assertions.assertEquals(expectedBrand, actualBrand); + Assertions.assertEquals(expectedMaterial, actualMaterial); + Assertions.assertEquals(expectedQuantity, actualQuantity); + Assertions.assertEquals(expectedPrice, actualPrice); + } + + @Test + public void findShirtTest(){ + //Given + int expId = 1; + String expShirtType = "Blouse"; + String expBrand = "Zara"; + String expMaterial = "Wool"; + int expQuantity = 15; + float expPrice = 50.00f; + ShirtService newShirtService1 = new ShirtService(); + Shirt newBlouse = newShirtService1.create(expShirtType, expBrand, expMaterial, expQuantity, expPrice); + //When + + //Then + Assertions.assertEquals(newBlouse, newShirtService1.find(expId)); + + + } + + @Test + public void findAllShirtTest(){ + //1 + //Shirt 1 + int expId = 1; + String expShirtType = "Button-Down"; + String expBrand = "Puma"; + String expMaterial = "Cotton"; + int expQuantity = 25; + float expPrice = 10.00f; + //Shirt 2 + int expId2 = 2; + String expShirtType2 = "Rain coat"; + String expBrand2 = "AE"; + String expMaterial2 = "Plastic"; + int expQuantity2 = 5; + float expPrice2 = 60.00f; + //2 + ShirtService newShirtService2 = new ShirtService(); + Shirt newButtonDown = newShirtService2.create(expShirtType, expBrand, expMaterial, expQuantity, expPrice); + Shirt newRainCoat = newShirtService2.create(expShirtType2, expBrand2, expMaterial2, expQuantity2, expPrice2); + //3 + Object[] actualShirtArray = new Shirt[2]; + actualShirtArray[0] = newButtonDown; + actualShirtArray[1] = newRainCoat; + //4 + Assertions.assertArrayEquals(actualShirtArray, newShirtService2.findAll()); + + } + + @Test + public void deleteShirtTest(){ + //1 + //Shirt 1 + int expId = 1; + String expShirtType = "Button-Up"; + String expBrand = "Happy"; + String expMaterial = "Silk"; + int expQuantity = 10; + float expPrice = 100.00f; + //Shirt 2 + int expId2 = 2; + String expShirtType2 = "Winter coat"; + String expBrand2 = "Panda"; + String expMaterial2 = "Feather"; + int expQuantity2 = 9; + float expPrice2 = 160.00f; + //2 + ShirtService newShirtService2 = new ShirtService(); + Shirt newButtonUp = newShirtService2.create(expShirtType, expBrand, expMaterial, expQuantity, expPrice); + Shirt newWinterCoat = newShirtService2.create(expShirtType2, expBrand2, expMaterial2, expQuantity2, expPrice2); + //3 + //Test 1 + boolean deleteResult = newShirtService2.delete(expId2); + Assertions.assertTrue(deleteResult); + + //Test 2 + Assertions.assertFalse(newShirtService2.delete(expId2)); + + + } +} diff --git a/src/test/java/services/SneakerServiceTest.java b/src/test/java/services/SneakerServiceTest.java new file mode 100644 index 0000000..12a320d --- /dev/null +++ b/src/test/java/services/SneakerServiceTest.java @@ -0,0 +1,139 @@ +package services; + +import models.Sneaker; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +public class SneakerServiceTest { + @Test + public void createTest(){ + //1 + String expectedName = "NitroBalance"; + String expectedBrand = "Nike"; + String expectedSport = "Basket Ball"; + int expectedSize = 11; + int expectedQty = 10; + float expectedPrice = 75.00f; + + //2 + SneakerService newSneakerService = new SneakerService(); + Sneaker testSneaker = newSneakerService.create(expectedName, expectedBrand, expectedSport, expectedQty, expectedPrice); + //3 + int actualId = testSneaker.getId(); + String actualName = testSneaker.getName(); + String actualBrand = testSneaker.getBrand(); + String actualSport = testSneaker.getSport(); + int actualSize = testSneaker.getSize(); + int actualQty = testSneaker.getQuantity(); + 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(expectedQty, actualQty); + Assertions.assertEquals(expectedPrice, actualPrice); + } + + /** + * should take an int and return an object with that id, if it exists. + */ + @Test + public void findTest(){ + //1 + //Sneaker 1 + int expectedID = 1; + String expectedName = "NitroBalance"; + String expectedBrand = "Nike"; + String expectedSport = "Basket Ball"; + int expectedQty = 10; + float expectedPrice = 75.00f; + //Sneaker2 + int expectedID2 = 2; + String expectedName2 = "RocketFeet"; + String expectedBrand2 = "NewBalance"; + String expectedSport2 = "Running"; + int expectedQty2 = 20; + float expectedPrice2 = 90.00f; + + //2 + SneakerService newSneakerService1 = new SneakerService(); + //Making sneaker1 + Sneaker newSneaker = newSneakerService1.create(expectedName, expectedBrand, expectedSport, expectedQty, expectedPrice); + //Making sneaker2 + Sneaker newSneaker2 = newSneakerService1.create(expectedName2, expectedBrand2, expectedSport2, expectedQty2, expectedPrice2); + //3 + //4 + Assertions.assertEquals(newSneaker2,newSneakerService1.find(expectedID2)); + } + + /** + * //read all + * public Sneaker[] findAll() { + * // should return a basic array copy of the ArrayList + * } + */ + @Test + public void sneakerServiceFindAllTest() { + //1 + int expectedID = 1; + String expectedName = "NitroBalance"; + String expectedBrand = "Nike"; + String expectedSport = "Basket Ball"; + int expectedQty = 10; + float expectedPrice = 75.00f; + //2 + SneakerService newSneakerService = new SneakerService(); + Sneaker newSneaker = newSneakerService.create(expectedName, expectedBrand, expectedSport, expectedQty, expectedPrice); + //3 + Object[] actualSneakerArray = new Sneaker[1]; + actualSneakerArray[0] = newSneaker; + //4 + Assertions.assertEquals(true, Arrays.equals(actualSneakerArray,newSneakerService.findAll())); + } + + /** + * //delete + * public boolean delete(int id) { + * // should remove the object with this id from the ArrayList if exits and return true. + * // Otherwise return false + * } + */ + @Test + public void SneakerServiceDeleteTest(){ + //1 + int expectedID1 = 1; + String expectedName = "NitroBalance"; + String expectedBrand = "Nike"; + String expectedSport = "Basket Ball"; + int expectedQty = 10; + float expectedPrice = 75.00f; + + int expectedID2 = 2; + String expectedName2 = "BubbleGum"; + String expectedBrand2 = "Adidas"; + String expectedSport2 = "Tennis"; + int expectedQty2 = 20; + float expectedPrice2 = 80.00f; + + //2 + SneakerService newSneakerService = new SneakerService(); + Sneaker newSneaker1 = newSneakerService.create(expectedName, expectedBrand, expectedSport, expectedQty, expectedPrice); + Sneaker newSneaker2 = newSneakerService.create(expectedName2, expectedBrand2, expectedSport2, expectedQty2, expectedPrice2); + + //Test 1 + boolean deleteResult = newSneakerService.delete(expectedID2); + Assertions.assertEquals(true, deleteResult); + + //Test 2 + boolean deleteResult2 = newSneakerService.delete(expectedID2); + Assertions.assertEquals(false, deleteResult2); + + + + + } +} +