diff --git a/src/main/java/com/pluralsight/Main.java b/src/main/java/com/pluralsight/Main.java index ec5b029..079c837 100644 --- a/src/main/java/com/pluralsight/Main.java +++ b/src/main/java/com/pluralsight/Main.java @@ -1,7 +1,7 @@ package com.pluralsight; -import com.pluralsight.Utilities.Inputs; -import com.pluralsight.userInterfaces.HomeScreen; +import com.pluralsight.util.Inputs; +import com.pluralsight.ui.HomeScreen; public class Main { diff --git a/src/main/java/com/pluralsight/Constants/AdminPasswords.java b/src/main/java/com/pluralsight/constants/AdminPasswords.java similarity index 84% rename from src/main/java/com/pluralsight/Constants/AdminPasswords.java rename to src/main/java/com/pluralsight/constants/AdminPasswords.java index a84fde6..785456f 100644 --- a/src/main/java/com/pluralsight/Constants/AdminPasswords.java +++ b/src/main/java/com/pluralsight/constants/AdminPasswords.java @@ -1,4 +1,4 @@ -package com.pluralsight.Constants; +package com.pluralsight.constants; public enum AdminPasswords { RECEIPT_LOGS("receiptAdmin123!"); diff --git a/src/main/java/com/pluralsight/DataManagers/ReceiptManager.java b/src/main/java/com/pluralsight/dataManagers/ReceiptManager.java similarity index 91% rename from src/main/java/com/pluralsight/DataManagers/ReceiptManager.java rename to src/main/java/com/pluralsight/dataManagers/ReceiptManager.java index 843bfae..c82145a 100644 --- a/src/main/java/com/pluralsight/DataManagers/ReceiptManager.java +++ b/src/main/java/com/pluralsight/dataManagers/ReceiptManager.java @@ -1,8 +1,8 @@ -package com.pluralsight.DataManagers; +package com.pluralsight.dataManagers; -import com.pluralsight.Constants.AdminPasswords; -import com.pluralsight.Utilities.Inputs; -import com.pluralsight.Utilities.Utilities; +import com.pluralsight.constants.AdminPasswords; +import com.pluralsight.util.Inputs; +import com.pluralsight.util.Text; import java.io.*; import java.time.LocalDate; @@ -57,7 +57,7 @@ private static String viewReceiptLog(File file) { } private static boolean verifyPassword() { - Utilities.clearConsole(); + Text.clearConsole(); System.out.print("Enter Password: "); String userInput = Inputs.getString(); return userInput.equals(AdminPasswords.RECEIPT_LOGS.password); diff --git a/src/main/java/com/pluralsight/models/Chips.java b/src/main/java/com/pluralsight/models/Chips.java index a3bce3d..5a2ae4d 100644 --- a/src/main/java/com/pluralsight/models/Chips.java +++ b/src/main/java/com/pluralsight/models/Chips.java @@ -1,5 +1,7 @@ package com.pluralsight.models; +import com.pluralsight.models.abstractModel.Product; + public class Chips extends Product { private String name; diff --git a/src/main/java/com/pluralsight/models/Drinks.java b/src/main/java/com/pluralsight/models/Drinks.java index fe39e89..df524eb 100644 --- a/src/main/java/com/pluralsight/models/Drinks.java +++ b/src/main/java/com/pluralsight/models/Drinks.java @@ -1,5 +1,7 @@ package com.pluralsight.models; +import com.pluralsight.models.abstractModel.Product; + public class Drinks extends Product { private String brand; private String size; diff --git a/src/main/java/com/pluralsight/models/Order.java b/src/main/java/com/pluralsight/models/Order.java index 4e343e9..ba16d2e 100644 --- a/src/main/java/com/pluralsight/models/Order.java +++ b/src/main/java/com/pluralsight/models/Order.java @@ -1,6 +1,6 @@ package com.pluralsight.models; -import com.pluralsight.Utilities.Utilities; +import com.pluralsight.util.Text; import java.util.*; @@ -17,60 +17,65 @@ public Order() { this.sidesList = new ArrayList<>(); } - public final Map sides = new TreeMap<>(Map.of( - 1, "au jus", - 2, "sauce" + public final Map premiumToppings = new TreeMap<>(Map.of( + "1", "Steak", + "2", "Ham", + "3", "Salami", + "4", "Roast Beef", + "5", "Chicken", + "6", "Bacon" )); - public final Map premiumToppings = new TreeMap<>(Map.of( - 1, "Steak", - 2, "Ham", - 3, "Salami", - 4, "Roast Beef", - 5, "Chicken", - 6, "Bacon" + public final Map regularToppings = new TreeMap<>(Map.of( + "1", "Lettuce", + "2", "Peppers", + "3", "Onions", + "4", "Tomatoes", + "5", "Jalapenos", + "6", "Cucumbers", + "7", "Pickles", + "8", "Guacamole", + "9", "Mushrooms" )); - public final Map regularToppings = new TreeMap<>(Map.of( - 1, "Lettuce", - 2, "Peppers", - 3, "Onions", - 4, "Tomatoes", - 5, "Jalapenos", - 6, "Cucumbers", - 7, "Pickles", - 8, "Guacamole", - 9, "Mushrooms" + public final Map sauces = new TreeMap<>(Map.of( + "1", "Mayo", + "2", "Mustard", + "3", "Ketchup", + "4", "Ranch", + "5", "Thousand Islands", + "6", "Vinaigrette" + + )); + + public final Map drinksList = new TreeMap<>(Map.of( + "1", "Coke", + "2", "Root Beer", + "3", "Pepsi", + "4", "Dr. Pepper", + "5", "Fanta", + "6", "Lemonade" )); + + public final Map chipsList = new TreeMap<>(Map.of( + "1", "Kettle Cooked Jalapeno", + "2", "Lays Classic", + "3", "Cheetos", + "4", "Doritos Cool Ranch", + "5", "Doritos Nacho Cheese" + )); + + public final Map sides = new TreeMap<>(Map.of( + "1", "au jus", + "2", "sauce" + )); + public final Map cheeses = new TreeMap<>(Map.of( 1, "American", 2, "Provolone", 3, "Cheddar", 4, "Swiss" )); - public final Map sauces = new TreeMap<>(Map.of( - 1, "Mayo", - 2, "Mustard", - 3, "Ketchup", - 4, "Ranch", - 5, "Thousand Islands", - 6, "Vinaigrette" - )); - public final Map drinksList = new TreeMap<>(Map.of( - 1, "Coke", - 2, "Root Beer", - 3, "Pepsi", - 4, "Dr. Pepper", - 5, "Fanta", - 6, "Lemonade" - )); - public final Map chipsList = new TreeMap<>(Map.of( - 1, "Kettle Cooked Jalapeno", - 2, "Lays Classic", - 3, "Cheetos", - 4, "Doritos Cool Ranch", - 5, "Doritos Nacho Cheese" - )); //Methods @@ -125,7 +130,7 @@ public String toString() { } if (!this.drinks.isEmpty()) { - output.append(Utilities.createHeader("Drinks")); + output.append(Text.createHeader("Drinks")); String drinkAbbrev = ""; for (Drinks drink : drinks) { switch (drink.getSize()) { @@ -139,7 +144,7 @@ public String toString() { } if (!this.chips.isEmpty()) { - output.append((Utilities.createHeader("Chips"))); + output.append((Text.createHeader("Chips"))); for (Chips chips : chips) { double chipsCost = chips.getPrice(); subtotal += chipsCost; @@ -150,7 +155,7 @@ public String toString() { } if (!this.sidesList.isEmpty()) { - output.append(Utilities.createHeader("Sides")); + output.append(Text.createHeader("Sides")); sidesList.forEach(side -> output.append(String.format(" %s\n", side.getName()))); } diff --git a/src/main/java/com/pluralsight/models/Sandwich.java b/src/main/java/com/pluralsight/models/Sandwich.java index 4d141b3..39f47b8 100644 --- a/src/main/java/com/pluralsight/models/Sandwich.java +++ b/src/main/java/com/pluralsight/models/Sandwich.java @@ -1,7 +1,9 @@ package com.pluralsight.models; -import com.pluralsight.Utilities.Utilities; +import com.pluralsight.models.abstractModel.Product; +import com.pluralsight.util.Text; +import java.util.HashSet; import java.util.Set; public class Sandwich extends Product { @@ -10,11 +12,31 @@ public class Sandwich extends Product { private Set premiumToppingsList; private boolean extraMeat = false; private Set regularToppingsList; + private final Set toppingsToAdd = new HashSet<>(); private String cheese; private boolean extraCheese = false; private Set saucesList; + private final Set saucesToAdd = new HashSet<>(); private boolean toasted = false; + //METHODS + + public void addToppings(String topping) { + this.toppingsToAdd.add(topping); + } + + public void clearToppingsToAdd() { + this.toppingsToAdd.clear(); + } + + public void addSauces(String sauce) { + this.saucesToAdd.add(sauce); + } + + public void clearSaucesToAdd() { + this.saucesToAdd.clear(); + } + public double getPrice() { double finalPrice = 0; double premiumToppingCharge = 0; @@ -105,10 +127,18 @@ public void setRegularToppings(Set regularToppings) { this.regularToppingsList = regularToppings; } + public Set getToppingsToAdd() { + return this.toppingsToAdd; + } + public Set getSauces() { return saucesList; } + public Set getSaucesToAdd() { + return saucesToAdd; + } + public void setSauces(Set sauces) { this.saucesList = sauces; } @@ -155,7 +185,7 @@ public String toString() { String cheese = getCheese() != null ? getCheese() : "N/A"; String sandwichInfo = String.format("Sandwich [%s %s]", getSize(), getType()); - output.append(" ").append(Utilities.createHeader(sandwichInfo, getPrice())); + output.append(" ").append(Text.createHeader(sandwichInfo, getPrice())); output.append(String.format(""" Cheese: %s Toasted: %s | Extra Cheese: %s | Extra Meat: %s @@ -185,7 +215,7 @@ public String toString() { } public String displayCurrentSandwich() { - return Utilities.centerMessage("Your current sandwich", 45, '-') + + return Text.centerMessage("Your current sandwich", 45, '-') + "\n\n" + String.format("Bread Size: %s | Bread Type: %s\n", (getSize() == null ? "Required" : getSize()), (getType() == null ? "Required" : getType())) + "Meats: " + @@ -207,7 +237,7 @@ public String displayCurrentSandwichCompact(int sandwichNum) { String cheese = getCheese() != null ? getCheese() : "N/A"; String sandwichInfo = String.format("Sandwich %d [%s %s]", sandwichNum, getSize(), getType()); - output.append(" ").append(Utilities.createHeader(sandwichInfo, getPrice())); + output.append(" ").append(Text.createHeader(sandwichInfo, getPrice())); output.append(String.format(""" Cheese: %s Toasted: %s | Extra Cheese: %s | Extra Meat: %s @@ -235,4 +265,5 @@ public String displayCurrentSandwichCompact(int sandwichNum) { } return output.toString(); } + } diff --git a/src/main/java/com/pluralsight/models/Sides.java b/src/main/java/com/pluralsight/models/Sides.java index d9f9cec..0071ec0 100644 --- a/src/main/java/com/pluralsight/models/Sides.java +++ b/src/main/java/com/pluralsight/models/Sides.java @@ -1,5 +1,7 @@ package com.pluralsight.models; +import com.pluralsight.models.abstractModel.Product; + public class Sides extends Product { private String name; diff --git a/src/main/java/com/pluralsight/models/Product.java b/src/main/java/com/pluralsight/models/abstractModel/Product.java similarity index 65% rename from src/main/java/com/pluralsight/models/Product.java rename to src/main/java/com/pluralsight/models/abstractModel/Product.java index 09d9316..055b575 100644 --- a/src/main/java/com/pluralsight/models/Product.java +++ b/src/main/java/com/pluralsight/models/abstractModel/Product.java @@ -1,4 +1,4 @@ -package com.pluralsight.models; +package com.pluralsight.models.abstractModel; public abstract class Product { @Override diff --git a/src/main/java/com/pluralsight/ui/CreateSandwichScreen.java b/src/main/java/com/pluralsight/ui/CreateSandwichScreen.java new file mode 100644 index 0000000..9de5124 --- /dev/null +++ b/src/main/java/com/pluralsight/ui/CreateSandwichScreen.java @@ -0,0 +1,432 @@ +package com.pluralsight.ui; + +import com.pluralsight.util.Inputs; +import com.pluralsight.util.Text; +import com.pluralsight.models.BLT; +import com.pluralsight.models.Order; +import com.pluralsight.models.PhillyCheeseSteak; +import com.pluralsight.models.Sandwich; + +import java.util.*; + +public class CreateSandwichScreen { + private final Order userOrder; + + public CreateSandwichScreen(Order userOrder) { + this.userOrder = userOrder; + } + + //Methods + + //DISPLAYING MENU CHOICES + + public void sandwichCreationHomeScreen() { + Text.clearConsole(); + System.out.println(Text.centerMessage("Choose an option", 45, '-')); + System.out.print(""" + + [1] Create custom sandwich + [2] Choose a signature sandwich + + [x] Cancel sandwich + + Enter choice:\s"""); + + processSandwichHomeScreenMenuChoice(); + } + + public void sandwichEditScreen() { + Sandwich userSandwich = new Sandwich(); + Text.clearConsole(); + System.out.printf("\n%s\n", userSandwich.displayCurrentSandwich()); + System.out.println(Text.centerMessage("Customize your sandwich", 45, '-')); + System.out.print(""" + + [1] Choose bread size + [2] Choose bread type + [3] Choose meats (extra charge) + [4] Choose regular toppings + [5] Choose cheese (only 1 cheese allowed) + [6] Choose sauces + + [f] Finalize sandwich + [x] Cancel sandwich + + Enter choice:\s"""); + + if (processCustomSandwichCreationMenuChoice(userSandwich)) sandwichEditScreen(); + } + + public void sandwichEditScreen(Sandwich userSandwich) { + Text.clearConsole(); + System.out.printf("\n%s\n", userSandwich.displayCurrentSandwich()); + System.out.println(Text.centerMessage("Customize your sandwich", 45, '-')); + System.out.print(""" + + [1] Choose bread size + [2] Choose bread type + [3] Choose meats (extra charge) + [4] Choose regular toppings + [5] Choose cheese (only 1 cheese allowed) + [6] Choose sauces + + [f] Finalize sandwich + [x] Cancel sandwich + + Enter choice:\s"""); + + if (processCustomSandwichCreationMenuChoice(userSandwich)) sandwichEditScreen(userSandwich); + } + + private void chooseSignatureSandwichScreen() { + Text.clearConsole(); + System.out.println(""" + Which signature sandwich would you like? + + [1] BLT + [2] Philly Cheese Steak + + [x] Cancel signature sandwich + + Enter choice:\s"""); + + processSignatureSandwichMenuChoice(); + } + + + //PROCESSING MENU CHOICES + + private void processSandwichHomeScreenMenuChoice() { + String userChoice = Inputs.getString(); + + switch (userChoice) { + case "1": + sandwichEditScreen(); + break; + case "2": + chooseSignatureSandwichScreen(); + break; + case "X", "x": + break; + default: + System.out.println("This is not a valid choice, please try again."); + Inputs.awaitInput(); + break; + } + } + + private boolean processCustomSandwichCreationMenuChoice(Sandwich userSandwich) { + String userChoice = Inputs.getString(); + + switch (userChoice) { + case "1": + chooseBreadSize(userSandwich); + break; + case "2": + chooseBreadType(userSandwich); + break; + case "3": + chooseToppings(userSandwich, "premium"); + break; + case "4": + chooseToppings(userSandwich, "regular"); + break; + case "5": + chooseCheese(userSandwich); + break; + case "6": + chooseSauces(userSandwich); + break; + case "F", "f": + // first ask if they want toasted, extra cheese, or extra meat. + if (checkIfRequiredItemsAreChosen(userSandwich)) { + System.out.print("Would you like your sandwich toasted? (Y/N): "); + String toasted = Inputs.getString(); + if (toasted.equalsIgnoreCase("y")) { + userSandwich.setToasted(true); + } + + if (userSandwich.getCheese() != null) { + System.out.print("Would you like to make your sandwich extra cheesy? (Y/N): "); + String extraCheese = Inputs.getString(); + if (extraCheese.equalsIgnoreCase("y")) { + userSandwich.setExtraCheese(true); + } + } + + if (userSandwich.getPremiumToppings() != null) { + System.out.print("Would you like you sandwich to be extra meaty? (Y/N): "); + String extraMeat = Inputs.getString(); + if (extraMeat.equalsIgnoreCase("y")) { + userSandwich.setExtraMeat(true); + } + } + userOrder.addSandwich(userSandwich); + return false; + } else { + System.out.println("Sorry, it seems that you haven't chosen the required items (sandwich size / sandwich type)\nPlease select one of each to finalize your sandwich."); + Inputs.awaitInput(); + } + break; + case "X", "x": + return false; + default: + System.out.println("This is not a valid choice, please try again."); + break; + } + return true; + } + + private void processSignatureSandwichMenuChoice() { + String userChoice = Inputs.getString(); + + + Sandwich sandwich = null; + + switch (userChoice) { + case "1": + sandwich = new BLT(); + System.out.println(sandwich.displayCurrentSandwich()); + break; + case "2": + sandwich = new PhillyCheeseSteak(); + System.out.println(sandwich.displayCurrentSandwich()); + break; + case "X", "x": + break; + default: + System.out.println("That's not a valid choice"); + Inputs.awaitInput(); + break; + } + + if (sandwich != null) { + System.out.print("Would you like to edit your sandwich? (Y/N): "); + String editSandwich = Inputs.getString(); + if (editSandwich.equalsIgnoreCase("y")) sandwichEditScreen(sandwich); + else userOrder.addSandwich(sandwich); + } + } + + //CHOOSING SANDWICH OPTIONS + + private void chooseBreadSize(Sandwich userSandwich) { + Text.clearConsole(); + System.out.println(Text.centerMessage("Choosing Bread Size", 50, '=')); + System.out.print("\n"); + System.out.print(""" + [1] 4" ($5.50) + [2] 8" ($7.00) + [3] 12" ($8.50) + + Enter choice:\s"""); + int breadSizeChoice = Inputs.getInt(); + switch (breadSizeChoice) { + case 1 -> userSandwich.setSize("4\""); + case 2 -> userSandwich.setSize("8\""); + case 3 -> userSandwich.setSize("12\""); + default -> { + System.out.println("That is not a valid choice, please select a valid option..."); + Inputs.awaitInput(); + chooseBreadSize(userSandwich); + } + } + } + + private void chooseBreadType(Sandwich userSandwich) { + Text.clearConsole(); + System.out.println(Text.centerMessage("Choosing Bread Type", 50, '=')); + System.out.print("\n"); + + System.out.print(""" + [1] Wheat + [2] White + [3] Rye + [4] Wrap + + Enter choice:\s"""); + int breadChoice = Inputs.getInt(); + switch (breadChoice) { + case 1 -> userSandwich.setType("Wheat"); + case 2 -> userSandwich.setType("White"); + case 3 -> userSandwich.setType("Rye"); + case 4 -> userSandwich.setType("Wrap"); + default -> { + System.out.println("That is not a valid choice, please select a valid option..."); + chooseBreadType(userSandwich); + } + } + } + + private void chooseToppings(Sandwich userSandwich, String type) { + Map toppings = type.equals("premium") ? userOrder.premiumToppings : userOrder.regularToppings; + Set chosenToppings = checkIfHasToppings(userSandwich, type); + + Text.clearConsole(); + System.out.println(Text.centerMessage("Choosing toppings", 50, '=')); + System.out.printf("Current Toppings: %s\n\n", !chosenToppings.isEmpty() ? chosenToppings : "N/A"); + System.out.println("Please choose which toppings to add:"); + toppings.forEach((number, topping) -> System.out.printf("[%s] %s\n", number, topping)); + System.out.print(""" + + [D] Done + [x] Cancel choosing toppings... + + Enter choice:\s"""); + String userChoice = Inputs.getString(); + + if (toppings.containsKey(userChoice)) { + userSandwich.addToppings(toppings.get(userChoice)); + chooseToppings(userSandwich, type); + } + else { + switch (userChoice) { + case "D", "d": + System.out.printf(""" + Summary of %s toppings: + + """, type); + displaySummary(chosenToppings); + System.out.print("\nIs this correct? (Y/N): "); + String accepted = Inputs.getString(); + switch (accepted) { + case "y", "Y" -> { + if (type.equals("premium")) { + userSandwich.setPremiumToppings(chosenToppings); + } else if (type.equals("regular")) { + userSandwich.setRegularToppings(chosenToppings); + } + } + case "n", "N" -> { + System.out.println("Okay, let's try again..."); + userSandwich.clearToppingsToAdd(); + chooseToppings(userSandwich, type); + } + default -> { + System.out.println("That is not a valid choice, try again..."); + chooseToppings(userSandwich, type); + } + } + break; + case "X", "x": + userSandwich.clearToppingsToAdd(); + break; + default: + System.out.println("This is not a valid choice... Please enter a valid choice."); + chooseToppings(userSandwich, type); + break; + } + } + } + + private void chooseCheese(Sandwich userSandwich) { + Map cheeses = userOrder.cheeses; + boolean isChoosingCheeses = true; + while (isChoosingCheeses) { + Text.clearConsole(); + System.out.println(Text.centerMessage("Choosing cheese", 50, '=')); + System.out.print("\n"); + cheeses.forEach((number, cheese) -> System.out.printf("[%d] %s\n", number, cheese)); + System.out.println("\n[x] Cancel choosing cheese...\n\n"); + System.out.print("Enter choice: "); + + String userChoice = Inputs.getString(); + try { + int userIntChoice = Integer.parseInt(userChoice); + if (userIntChoice < 0 || userIntChoice > cheeses.size()) { + System.out.println("Please provide a valid choice..."); + } else { + userSandwich.setCheese(cheeses.get(userIntChoice)); + isChoosingCheeses = false; + } + } catch (NumberFormatException e) { + if (userChoice.equalsIgnoreCase("x")) { + System.out.println("Ok, no cheese..."); + isChoosingCheeses = false; + } else { + System.out.println("This is not a valid choice... Please enter a valid choice."); + } + } + } + } + + private void chooseSauces(Sandwich userSandwich) { + Map sauces = userOrder.sauces; + Set chosenSauces = checkIfHasSauces(userSandwich); + Text.clearConsole(); + System.out.println(Text.centerMessage("Choosing sauces", 50, '=')); + System.out.printf("Current Sauces: %s\n\n", !chosenSauces.isEmpty() ? chosenSauces : "N/A"); + System.out.print("\n"); + System.out.println("Please choose which sauces to add:"); + sauces.forEach((number, sauce) -> System.out.printf("[%s] %s\n", number, sauce)); + System.out.print(""" + + [d] Done + [x] Cancel choosing sauces + + Enter choice:\s"""); + String userChoice = Inputs.getString(); + + if (userOrder.sauces.containsKey(userChoice)) { + userSandwich.addSauces(userOrder.sauces.get(userChoice)); + chooseSauces(userSandwich); + } else { + switch (userChoice) { + case "D", "d": + System.out.println("\nSummary of sauces:\n"); + + displaySummary(chosenSauces); + System.out.print("\nIs this correct? (Y/N): "); + String accepted = Inputs.getString(); + switch (accepted) { + case "y", "Y" -> userSandwich.setSauces(chosenSauces); + case "n", "N" -> { + System.out.println("Okay, let's try again..."); + userSandwich.clearSaucesToAdd(); + chooseSauces(userSandwich); + } + } + break; + case "X", "x": + break; + default: + System.out.println("This is not a valid choice... Please enter a valid choice."); + userSandwich.clearSaucesToAdd(); + chooseSauces(userSandwich); + } + } + } + + private void displaySummary(Set chosenItems) { + chosenItems.forEach(System.out::println); + } + + //VALIDATION CHECKS + + private static Set checkIfHasToppings(Sandwich userSandwich, String type) { + Set chosenToppings = new HashSet<>(); + if (type.equalsIgnoreCase("premium")) { + if (userSandwich.getPremiumToppings() == null) return new HashSet<>(); + if (!userSandwich.getPremiumToppings().isEmpty()) chosenToppings = new HashSet<>(userSandwich.getPremiumToppings()); + } else if (type.equalsIgnoreCase("regular")) { + if (userSandwich.getRegularToppings() == null) return new HashSet<>(); + if (!userSandwich.getRegularToppings().isEmpty()) chosenToppings = new HashSet<>(userSandwich.getRegularToppings()); + } + + if (!userSandwich.getToppingsToAdd().isEmpty()) chosenToppings.addAll(userSandwich.getToppingsToAdd()); + return chosenToppings; + } + + private Set checkIfHasSauces(Sandwich userSandwich) { + Set sauces = new HashSet<>(); + if (userSandwich.getSauces() == null) return new HashSet<>(); + if (!userSandwich.getSauces().isEmpty()) sauces = new HashSet<>(userSandwich.getSauces()); + + if (!userSandwich.getSaucesToAdd().isEmpty()) sauces.addAll(userSandwich.getSaucesToAdd()); + return sauces; + } + + private boolean checkIfRequiredItemsAreChosen(Sandwich userSandwich) { + return userSandwich.getSize() != null && userSandwich.getType() != null; + } +} diff --git a/src/main/java/com/pluralsight/userInterfaces/HomeScreen.java b/src/main/java/com/pluralsight/ui/HomeScreen.java similarity index 80% rename from src/main/java/com/pluralsight/userInterfaces/HomeScreen.java rename to src/main/java/com/pluralsight/ui/HomeScreen.java index e6aa32a..279035a 100644 --- a/src/main/java/com/pluralsight/userInterfaces/HomeScreen.java +++ b/src/main/java/com/pluralsight/ui/HomeScreen.java @@ -1,18 +1,16 @@ -package com.pluralsight.userInterfaces; +package com.pluralsight.ui; -import com.pluralsight.DataManagers.ReceiptManager; -import com.pluralsight.Utilities.Inputs; +import com.pluralsight.dataManagers.ReceiptManager; +import com.pluralsight.util.Inputs; import java.util.*; public class HomeScreen { public static void displayHomeScreen() { - boolean usingApplication = true; - while (usingApplication) { - displayWelcomeMessage(); + displayWelcomeMessage(); - System.out.print(""" + System.out.print(""" [1] Create a new order [2] View receipts (Admin Only) @@ -21,8 +19,7 @@ public static void displayHomeScreen() { Enter choice:\s"""); - usingApplication = processHomeScreenChoice(); - } + if (processHomeScreenChoice()) displayHomeScreen(); } private static void displayWelcomeMessage() { diff --git a/src/main/java/com/pluralsight/ui/OrderScreen.java b/src/main/java/com/pluralsight/ui/OrderScreen.java new file mode 100644 index 0000000..1da6746 --- /dev/null +++ b/src/main/java/com/pluralsight/ui/OrderScreen.java @@ -0,0 +1,333 @@ +package com.pluralsight.ui; + +import com.pluralsight.models.abstractModel.Product; +import com.pluralsight.util.*; +import com.pluralsight.models.*; +import com.pluralsight.dataManagers.ReceiptManager; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class OrderScreen { + private final Order userOrder; + + public OrderScreen() { + this.userOrder = new Order(); + } + + //DISPLAYING MENUS + + public void homeScreen() { + Text.clearConsole(); + System.out.println(userOrder); + System.out.println("\n"); + System.out.print(""" + What would you like to add to your order? + + [1] Sandwich + [2] Drinks + [3] Chips + [4] Sides + + [e] Edit order + [f] Finalize order + + [x] Cancel order + + Enter choice:\s"""); + + if (processOrderMenuChoice()) homeScreen(); + } + + private void orderDrink() { + Text.clearConsole(); + System.out.print(""" + What size fountain drink would you like? + + [1] Small + [2] Medium + [3] Large + + [x] Cancel drink selection + + Enter choice:\s"""); + + if (processDrinkMenuChoice()) orderDrink(); + } + + private void addChips() { + Text.clearConsole(); + System.out.println(Text.centerMessage("Choosing chips", 50, '=')); + System.out.print("\n"); + userOrder.chipsList.forEach((number, chip) -> System.out.printf("[%s] %s\n", number, chip)); + System.out.println("\n[x] Cancel sides choice"); + System.out.print("Enter choice: "); + + if (processChipsMenuChoice()) addChips(); + } + + private void orderSides() { + Text.clearConsole(); + System.out.println(Text.centerMessage("Choosing sides", 50, '=')); + System.out.print("\n"); + userOrder.sides.forEach((number, side) -> System.out.printf("[%s] %s\n", number, side)); + System.out.println("\n[x] Cancel sides choice"); + System.out.print("Enter choice: "); + + if (processSidesMenuChoice()) orderSides(); + } + + //PROCESSING MENU CHOICES + + private boolean processOrderMenuChoice() { + String orderMenuChoice = Inputs.getString(); + + switch (orderMenuChoice) { + case "1" -> { + CreateSandwichScreen createSandwichScreen = new CreateSandwichScreen(userOrder); + createSandwichScreen.sandwichCreationHomeScreen(); + } + case "2" -> orderDrink(); + case "3" -> addChips(); + case "4" -> orderSides(); + case "E", "e" -> { + //Ask user what items they would like to edit + Text.clearConsole(); + + System.out.print(""" + What would you like to edit? + + [1] Sandwich + [2] Drinks + [3] Chips + [4] Sides + + [x] Cancel editing + + Enter choice:\s"""); + + processEditingMenuChoice(); + } + case "F", "f" -> { + // add items to receipt and finalize the order + Text.clearConsole(); + System.out.println(userOrder); + System.out.print("\n\n\n\nIs the order correct? (Y/N): "); + String orderFinished = Inputs.getString(); + + switch (orderFinished) { + case "Y", "y": + ReceiptManager.createReceipt(userOrder.toString()); + return false; + case "N", "n": + break; + default: + System.out.println("That's not a valid option."); + Inputs.awaitInput(); + break; + } + + } + case "X", "x" -> { + return false; + } + default -> { + System.out.println("This is not a valid choice"); + Inputs.awaitInput(); + } + } + return true; + } + + private boolean processDrinkMenuChoice() { + String userChoice = Inputs.getString(); + + Drinks drink = new Drinks(); + String size; + double price; + + switch (userChoice) { + case "1" -> { + size = "Small"; + price = 2.00; + } + case "2" -> { + size = "Medium"; + price = 2.50; + } + case "3" -> { + size = "Large"; + price = 3.00; + } + case "X", "x" -> { + return false; + } + default -> { + System.out.println("That's not a valid choice."); + Inputs.awaitInput(); + return true; + } + } + + drink.setSize(size); + drink.setPrice(price); + Text.clearConsole(); + System.out.println(""" + + What drink would you like? + """); + userOrder.drinksList.forEach((num, d) -> System.out.printf("[%s] %s\n", num, d)); + System.out.print("\n[x] Cancel drink choice\n\nEnter choice: "); + String drinkTypeChoice = Inputs.getString(); + + if (userOrder.drinksList.containsKey(drinkTypeChoice)) { + drink.setBrand(userOrder.drinksList.get(drinkTypeChoice)); + userOrder.addDrink(drink); + return false; + } else if (drinkTypeChoice.equalsIgnoreCase("x")) { + System.out.println("Cancelling drink choice..."); + Inputs.awaitInput(); + } else { + System.out.println("That's not a valid drink choice, please try again..."); + Inputs.awaitInput(); + } + return true; + } + + private boolean processChipsMenuChoice() { + String userChoice = Inputs.getString(); + + if (userOrder.chipsList.containsKey(userChoice)) { + Chips chip = new Chips(); + chip.setName(userOrder.chipsList.get(userChoice)); + userOrder.addChips(chip); + return false; + } else { + if (userChoice.equalsIgnoreCase("x")) { + return false; + } else { + System.out.println("This was not a valid sides choice. Please try again..."); + Inputs.awaitInput(); + } + } + return true; + } + + private boolean processSidesMenuChoice() { + String userChoice = Inputs.getString(); + + Sides side = new Sides(); + + if (userOrder.sides.containsKey(userChoice)) { + side.setName(userOrder.sides.get(userChoice)); + userOrder.addSide(side); + return false; + } else { + if (userChoice.equalsIgnoreCase("x")) { + return false; + } else { + System.out.println("This was not a valid sides choice. Please try again..."); + Inputs.awaitInput(); + } + } + return true; + } + + private void processEditingMenuChoice() { + String userChoice = Inputs.getString(); + + switch (userChoice) { + case "1": + selectWhichItemToEdit(userOrder.getSandwiches()); + break; + case "2": + selectWhichItemToEdit(userOrder.getDrinks()); + break; + case "3": + selectWhichItemToEdit(userOrder.getChips()); + break; + case "4": + selectWhichItemToEdit(userOrder.getSidesList()); + break; + case "X", "x": + break; + default: + System.out.println("This was not a valid menu choice. Please try again..."); + Inputs.awaitInput(); + break; + } + } + + private void selectWhichItemToEdit(List itemList) { + int originalItemListSize = itemList.size(); + Text.clearConsole(); + System.out.println(Text.centerMessage(" Choose item to edit ", 50, '=')); + System.out.println(); + Map itemsMap = new HashMap<>(); + int itemNum = 1; + if (!itemList.isEmpty()) { + for (Product item : itemList) { + itemsMap.put(itemNum, item); + if (item instanceof Sandwich) + System.out.printf("%s\n", ((Sandwich) item).displayCurrentSandwichCompact(itemNum)); + else System.out.printf("[%d] %s\n", itemNum, item); + itemNum++; + } + + System.out.print("\n\nWhich item would you like to edit?\n\nEnter choice: "); + + int itemEditChoice = Inputs.getInt(); + + if (itemEditChoice < 0 || itemEditChoice > itemsMap.size()) { + System.out.println("That's not a valid choice."); + Inputs.awaitInput(); + } else { + Product itemChoice = itemList.get(itemEditChoice - 1); + + if (itemChoice instanceof Drinks) { + orderDrink(); + if (validateItemEdited(originalItemListSize, itemChoice)) + userOrder.getDrinks().remove((Drinks) itemsMap.get(itemEditChoice)); + } else if (itemChoice instanceof Chips) { + addChips(); + userOrder.getChips().remove((Chips) itemsMap.get(itemEditChoice)); + } else if (itemChoice instanceof Sides) { + orderSides(); + userOrder.getSidesList().remove((Sides) itemsMap.get(itemEditChoice)); + } else if (itemChoice instanceof Sandwich) { + CreateSandwichScreen createSandwichScreen = new CreateSandwichScreen(userOrder); + createSandwichScreen.sandwichEditScreen((Sandwich) itemsMap.get(itemEditChoice)); + userOrder.getSandwiches().remove((Sandwich) itemsMap.get(itemEditChoice)); + } + } + } else { + System.out.println("You have nothing to edit..."); + Inputs.awaitInput(); + } + + } + + //VALIDATION CHECKS + + private boolean validateItemEdited(int originalListSize, Product itemChoice) { + List productList = null; + try { + if (itemChoice instanceof Drinks) { + productList = userOrder.getDrinks(); + } else if (itemChoice instanceof Chips) { + productList = userOrder.getChips(); + } else if (itemChoice instanceof Sides) { + productList = userOrder.getSidesList(); + } else if (itemChoice instanceof Sandwich) { + productList = userOrder.getSandwiches(); + } + assert productList != null; + if (productList.size() != originalListSize) return true; + } catch (IndexOutOfBoundsException e) { + return true; + } + return false; + } + +} diff --git a/src/main/java/com/pluralsight/userInterfaces/CreateSandwichScreen.java b/src/main/java/com/pluralsight/userInterfaces/CreateSandwichScreen.java deleted file mode 100644 index ba3765c..0000000 --- a/src/main/java/com/pluralsight/userInterfaces/CreateSandwichScreen.java +++ /dev/null @@ -1,452 +0,0 @@ -package com.pluralsight.userInterfaces; - -import com.pluralsight.Utilities.Inputs; -import com.pluralsight.Utilities.Utilities; -import com.pluralsight.models.BLT; -import com.pluralsight.models.Order; -import com.pluralsight.models.PhillyCheeseSteak; -import com.pluralsight.models.Sandwich; - -import java.util.*; - -public class CreateSandwichScreen{ - private final Order userOrder; - - public CreateSandwichScreen(Order userOrder) { - this.userOrder = userOrder; - } - - //Methods - - //DISPLAYING MENU CHOICES - - public void sandwichCreationHomeScreen() { - Utilities.clearConsole(); - System.out.println(Utilities.centerMessage("Choose an option", 45, '-')); - System.out.print(""" - - [1] Create custom sandwich - [2] Choose a signature sandwich - - [x] Cancel sandwich - - Enter choice:\s"""); - - processSandwichHomeScreenMenuChoice(); - } - - public void sandwichEditScreen() { - Sandwich userSandwich = new Sandwich(); - boolean isRunning = true; - while (isRunning) { - Utilities.clearConsole(); - System.out.printf("\n%s\n", userSandwich.displayCurrentSandwich()); - System.out.println(Utilities.centerMessage("Customize your sandwich", 45, '-')); - System.out.print(""" - - [1] Choose bread size - [2] Choose bread type - [3] Choose meats (extra charge) - [4] Choose regular toppings - [5] Choose cheese (only 1 cheese allowed) - [6] Choose sauces - - [f] Finalize sandwich - [x] Cancel sandwich - - Enter choice:\s"""); - - isRunning = processCustomSandwichCreationMenuChoice(userSandwich, isRunning); - } - } - - public void sandwichEditScreen(Sandwich userSandwich) { - boolean isRunning = true; - while (isRunning) { - Utilities.clearConsole(); - System.out.printf("\n%s\n", userSandwich.displayCurrentSandwich()); - System.out.println(Utilities.centerMessage("Customize your sandwich", 45, '-')); - System.out.print(""" - - [1] Choose bread size - [2] Choose bread type - [3] Choose meats (extra charge) - [4] Choose regular toppings - [5] Choose cheese (only 1 cheese allowed) - [6] Choose sauces - - [f] Finalize sandwich - [x] Cancel sandwich - - Enter choice:\s"""); - - isRunning = processCustomSandwichCreationMenuChoice(userSandwich, isRunning); - } - } - - private void chooseSignatureSandwichScreen() { - Utilities.clearConsole(); - System.out.println(""" - Which signature sandwich would you like? - - [1] BLT - [2] Philly Cheese Steak - - [x] Cancel signature sandwich - - Enter choice:\s"""); - - processSignatureSandwichMenuChoice(); - } - - - //PROCESSING MENU CHOICES - private void processSandwichHomeScreenMenuChoice() { - String userChoice = Inputs.getString(); - - try { - int userIntChoice = Integer.parseInt(userChoice); - switch (userIntChoice) { - case 1: - sandwichEditScreen(); - break; - case 2: - chooseSignatureSandwichScreen(); - break; - default: - System.out.println("This is not a valid choice, please try again."); - Inputs.awaitInput(); - break; - } - } catch (NumberFormatException e) { - switch (userChoice) { - case "X", "x" -> System.out.println(); - default -> System.out.println("This is not a valid choice, please try again."); - } - } - } - - private boolean processCustomSandwichCreationMenuChoice(Sandwich userSandwich, boolean isRunning) { - String userChoice = Inputs.getString(); - - try { - int userIntChoice = Integer.parseInt(userChoice); - switch (userIntChoice) { - case 1: - chooseBreadSize(userSandwich); - break; - case 2: - chooseBreadType(userSandwich); - break; - case 3: - chooseToppings(userSandwich, "premium"); - break; - case 4: - chooseToppings(userSandwich, "regular"); - break; - case 5: - chooseCheese(userSandwich); - break; - case 6: - chooseSauces(userSandwich); - break; - default: - System.out.println("This is not a valid choice, please try again."); - break; - } - } catch (NumberFormatException e) { - switch (userChoice) { - case "F", "f" -> { - // first ask if they want toasted, extra cheese, or extra meat. - if (checkIfRequiredItemsAreChosen(userSandwich)) { - System.out.print("Would you like your sandwich toasted? (Y/N): "); - String toasted = Inputs.getString(); - if (toasted.equalsIgnoreCase("y")) { - userSandwich.setToasted(true); - } - - if (userSandwich.getCheese() != null) { - System.out.print("Would you like to make your sandwich extra cheesy? (Y/N): "); - String extraCheese = Inputs.getString(); - if (extraCheese.equalsIgnoreCase("y")) { - userSandwich.setExtraCheese(true); - } - } - - if (userSandwich.getPremiumToppings() != null) { - System.out.print("Would you like you sandwich to be extra meaty? (Y/N): "); - String extraMeat = Inputs.getString(); - if (extraMeat.equalsIgnoreCase("y")) { - userSandwich.setExtraMeat(true); - } - } - userOrder.addSandwich(userSandwich); - isRunning = false; - } else { - System.out.println("Sorry, it seems that you haven't chosen the required items (sandwich size / sandwich type)\nPlease select one of each to finalize your sandwich."); - Inputs.awaitInput(); - } - } - case "X", "x" -> isRunning = false; - default -> System.out.println("This is not a valid choice, please try again."); - - } - } - return isRunning; - } - - private void processSignatureSandwichMenuChoice() { - String userChoice = Inputs.getString(); - - try { - int userIntChoice = Integer.parseInt(userChoice); - - Sandwich sandwich = null; - - switch (userIntChoice) { - case 1: - sandwich = new BLT(); - System.out.println(sandwich.displayCurrentSandwich()); - break; - case 2: - sandwich = new PhillyCheeseSteak(); - System.out.println(sandwich.displayCurrentSandwich()); - break; - default: - System.out.println("That's not a valid choice"); - Inputs.awaitInput(); - break; - } - - if (sandwich != null) { - System.out.print("Would you like to edit your sandwich? (Y/N): "); - String editSandwich = Inputs.getString(); - if (editSandwich.equalsIgnoreCase("y")) sandwichEditScreen(sandwich); - else userOrder.addSandwich(sandwich); - } - } catch (NumberFormatException e) { - if (userChoice.equalsIgnoreCase("x")) System.out.println(); - } - } - - - private void chooseBreadSize(Sandwich userSandwich) { - Utilities.clearConsole(); - System.out.println(Utilities.centerMessage("Choosing Bread Size", 50, '=')); - System.out.print("\n"); - System.out.print(""" - [1] 4" ($5.50) - [2] 8" ($7.00) - [3] 12" ($8.50) - - Enter choice:\s"""); - int breadSizeChoice = Inputs.getInt(); - switch (breadSizeChoice) { - case 1 -> userSandwich.setSize("4\""); - case 2 -> userSandwich.setSize("8\""); - case 3 -> userSandwich.setSize("12\""); - default -> { - System.out.println("That is not a valid choice, please select a valid option..."); - Inputs.awaitInput(); - chooseBreadSize(userSandwich); - } - } - } - - - private void chooseBreadType(Sandwich userSandwich) { - Utilities.clearConsole(); - System.out.println(Utilities.centerMessage("Choosing Bread Type", 50, '=')); - System.out.print("\n"); - - System.out.print(""" - [1] Wheat - [2] White - [3] Rye - [4] Wrap - - Enter choice:\s"""); - int breadChoice = Inputs.getInt(); - switch (breadChoice) { - case 1 -> userSandwich.setType("Wheat"); - case 2 -> userSandwich.setType("White"); - case 3 -> userSandwich.setType("Rye"); - case 4 -> userSandwich.setType("Wrap"); - default -> { - System.out.println("That is not a valid choice, please select a valid option..."); - chooseBreadType(userSandwich); - } - } - } - - private void chooseToppings(Sandwich userSandwich, String type) { - Map toppings = type.equals("premium") ? userOrder.premiumToppings : userOrder.regularToppings; - Set chosenToppings = checkIfHasToppings(userSandwich, type); - - boolean isChoosingToppings = true; - while (isChoosingToppings) { - Utilities.clearConsole(); - System.out.println(Utilities.centerMessage("Choosing toppings", 50, '=')); - System.out.printf("Current Toppings: %s\n\n", !chosenToppings.isEmpty() ? chosenToppings : "N/A"); - System.out.println("Please choose which toppings to add:"); - toppings.forEach((number, topping) -> System.out.printf("[%d] %s\n", number, topping)); - System.out.print(""" - - [D] Done - [x] Cancel choosing toppings... - - Enter choice:\s"""); - String userChoice = Inputs.getString(); - - try { - int userIntChoice = Integer.parseInt(userChoice); - if (userIntChoice < 0 || userIntChoice > toppings.size()) { - System.out.println("Please provide a valid choice..."); - } else { - chosenToppings.add(toppings.get(userIntChoice)); - } - } catch (NumberFormatException e) { - switch (userChoice) { - case "D", "d": - System.out.printf(""" - Summary of %s toppings: - - """, type); - displaySummary(chosenToppings); - System.out.print("\nIs this correct? (Y/N): "); - String accepted = Inputs.getString(); - switch (accepted) { - case "y", "Y" -> { - if (type.equals("premium")) { - userSandwich.setPremiumToppings(chosenToppings); - } else if (type.equals("regular")) { - userSandwich.setRegularToppings(chosenToppings); - } - isChoosingToppings = false; - } - case "n", "N" -> System.out.println("Okay, let's try again..."); - default -> System.out.println("That is not a valid choice, try again..."); - } - break; - case "X", "x": - isChoosingToppings = false; - break; - default: - System.out.println("This is not a valid choice... Please enter a valid choice."); - break; - } - } - } - } - - - private void chooseCheese(Sandwich userSandwich) { - Map cheeses = userOrder.cheeses; - boolean isChoosingCheeses = true; - while (isChoosingCheeses) { - Utilities.clearConsole(); - System.out.println(Utilities.centerMessage("Choosing cheese", 50, '=')); - System.out.print("\n"); - cheeses.forEach((number, cheese) -> System.out.printf("[%d] %s\n", number, cheese)); - System.out.println("\n[x] Cancel choosing cheese...\n\n"); - System.out.print("Enter choice: "); - - String userChoice = Inputs.getString(); - try { - int userIntChoice = Integer.parseInt(userChoice); - if (userIntChoice < 0 || userIntChoice > cheeses.size()) { - System.out.println("Please provide a valid choice..."); - } else { - userSandwich.setCheese(cheeses.get(userIntChoice)); - isChoosingCheeses = false; - } - } catch (NumberFormatException e) { - if (userChoice.equalsIgnoreCase("x")) { - System.out.println("Ok, no cheese..."); - isChoosingCheeses = false; - } else { - System.out.println("This is not a valid choice... Please enter a valid choice."); - } - } - } - } - - - private void chooseSauces(Sandwich userSandwich) { - Map sauces = userOrder.sauces; - Set chosenSauces = checkIfHasSauces(userSandwich); - boolean isChoosingSauces = true; - while (isChoosingSauces) { - Utilities.clearConsole(); - System.out.println(Utilities.centerMessage("Choosing sauces", 50, '=')); - System.out.printf("Current Sauces: %s\n\n", !chosenSauces.isEmpty() ? chosenSauces : "N/A"); - System.out.print("\n"); - System.out.println("Please choose which toppings to add:"); - sauces.forEach((number, sauce) -> System.out.printf("[%d] %s\n", number, sauce)); - System.out.print(""" - - [D] Done - - Enter choice:\s"""); - String userChoice = Inputs.getString(); - - try { - int userIntChoice = Integer.parseInt(userChoice); - if (userIntChoice < 0 || userIntChoice > sauces.size()) { - System.out.println("Please provide a valid choice..."); - } else if (userIntChoice == 0) { - break; - } else { - chosenSauces.add(sauces.get(userIntChoice)); - } - } catch (NumberFormatException e) { - if (userChoice.equalsIgnoreCase("d")) { - System.out.println("\nSummary of sauces:\n"); - - displaySummary(chosenSauces); - System.out.print("\nIs this correct? (Y/N): "); - String accepted = Inputs.getString(); - switch (accepted) { - case "y", "Y" -> { - userSandwich.setSauces(chosenSauces); - isChoosingSauces = false; - } - case "n", "N" -> System.out.println("Okay, let's try again..."); - } - } else { - System.out.println("This is not a valid choice... Please enter a valid choice."); - } - } - } - } - - private static Set checkIfHasToppings(Sandwich userSandwich, String type) { - Set chosenToppings = Set.of(); - if (type.equalsIgnoreCase("premium")) { - if (userSandwich.getPremiumToppings() == null) return new HashSet<>(); - if (!userSandwich.getPremiumToppings().isEmpty()) chosenToppings = userSandwich.getPremiumToppings(); - } - else if (type.equalsIgnoreCase("regular")) { - if (userSandwich.getRegularToppings() == null) return new HashSet<>(); - if (!userSandwich.getRegularToppings().isEmpty()) chosenToppings = userSandwich.getRegularToppings(); - } - return chosenToppings; - } - - private Set checkIfHasSauces(Sandwich userSandwich) { - Set sauces; - if (userSandwich.getSauces() == null) return new HashSet<>(); - if (!userSandwich.getSauces().isEmpty()) sauces = userSandwich.getSauces(); - else sauces = new HashSet<>(); - return sauces; - } - - private void displaySummary(Set chosenItems) { - chosenItems.forEach(System.out::println); - } - - - private boolean checkIfRequiredItemsAreChosen(Sandwich userSandwich) { - return userSandwich.getSize() != null && userSandwich.getType() != null; - } -} diff --git a/src/main/java/com/pluralsight/userInterfaces/OrderScreen.java b/src/main/java/com/pluralsight/userInterfaces/OrderScreen.java deleted file mode 100644 index a43f775..0000000 --- a/src/main/java/com/pluralsight/userInterfaces/OrderScreen.java +++ /dev/null @@ -1,363 +0,0 @@ -package com.pluralsight.userInterfaces; - -import com.pluralsight.Utilities.*; -import com.pluralsight.models.*; -import com.pluralsight.DataManagers.ReceiptManager; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class OrderScreen { - private final Order userOrder; - - public OrderScreen() { - this.userOrder = new Order(); - } - - //Menus - - public void homeScreen() { - Utilities.clearConsole(); - System.out.println(userOrder); - System.out.println("\n"); - System.out.print(""" - What would you like to add to your order? - - [1] Sandwich - [2] Drinks - [3] Chips - [4] Sides - - [e] Edit order - [f] Finalize order - - [x] Cancel order - - Enter choice:\s"""); - - if (processOrderMenuChoice()) homeScreen(); - } - - private void orderDrink() { - Utilities.clearConsole(); - System.out.print(""" - What size fountain drink would you like? - - [1] Small - [2] Medium - [3] Large - - [x] Cancel drink selection - - Enter choice:\s"""); - - if (processDrinkMenuChoice()) orderDrink(); - } - - private void addChips() { - Utilities.clearConsole(); - System.out.println(Utilities.centerMessage("Choosing chips", 50, '=')); - System.out.print("\n"); - userOrder.chipsList.forEach((number, chip) -> System.out.printf("[%d] %s\n", number, chip)); - System.out.println("\n[x] Cancel sides choice"); - System.out.print("Enter choice: "); - - if (processChipsMenuChoice()) addChips(); - } - - private void orderSides() { - Utilities.clearConsole(); - System.out.println(Utilities.centerMessage("Choosing sides", 50, '=')); - System.out.print("\n"); - userOrder.sides.forEach((number, side) -> System.out.printf("[%d] %s\n", number, side)); - System.out.println("\n[x] Cancel sides choice"); - System.out.print("Enter choice: "); - - if (processSidesMenuChoice()) orderSides(); - } - - //Processing Menu Choices - - private boolean processOrderMenuChoice() { - String orderMenuChoice = Inputs.getString(); - - try { - int userChoice = Integer.parseInt(orderMenuChoice); - switch (userChoice) { - case 1: - CreateSandwichScreen createSandwichScreen = new CreateSandwichScreen(userOrder); - createSandwichScreen.sandwichCreationHomeScreen(); - break; - case 2: - orderDrink(); - break; - case 3: - addChips(); - break; - case 4: - orderSides(); - break; - default: - System.out.println("This is not a valid choice"); - Inputs.awaitInput(); - break; - } - } catch (NumberFormatException e) { - switch (orderMenuChoice) { - case "E", "e" -> { - //Ask user what items they would like to edit - Utilities.clearConsole(); - - System.out.print(""" - What would you like to edit? - - [1] Sandwich - [2] Drinks - [3] Chips - [4] Sides - - [x] Cancel editing - - Enter choice:\s"""); - - processEditingMenuChoice(); - } - case "F", "f" -> { - // add items to receipt and finalize the order - Utilities.clearConsole(); - System.out.println(userOrder); - System.out.print("\n\n\n\nIs the order correct? (Y/N): "); - String orderFinished = Inputs.getString(); - - switch (orderFinished) { - case "Y", "y": - ReceiptManager.createReceipt(userOrder.toString()); - return false; - case "N", "n": - break; - default: - System.out.println("That's not a valid option."); - Inputs.awaitInput(); - break; - } - - } - case "X", "x" -> { - return false; - } - default -> { - System.out.print("This is not a valid choice."); - Inputs.awaitInput(); - } - } - } - return true; - } - - private boolean processDrinkMenuChoice() { - String userChoice = Inputs.getString(); - - try { - Drinks drink = new Drinks(); - int drinkChoice = Integer.parseInt(userChoice); - String size = ""; - double price = 0; - - if (drinkChoice < 0 || drinkChoice > 3) { - System.out.println("That's not a valid choice."); - Inputs.awaitInput(); - return true; - } - - switch (drinkChoice) { - case 1 -> { - size = "Small"; - price = 2.00; - } - case 2 -> { - size = "Medium"; - price = 2.50; - } - case 3 -> { - size = "Large"; - price = 3.00; - } - } - drink.setSize(size); - drink.setPrice(price); - Utilities.clearConsole(); - System.out.println(""" - - What drink would you like? - """); - userOrder.drinksList.forEach((num, d) -> System.out.printf("[%d] %s\n", num, d)); - System.out.print("\nEnter choice: "); - int drinkTypeChoice = Inputs.getInt(); - - if (validateUserChoice(drinkTypeChoice, userOrder.drinksList)) { - drink.setBrand(userOrder.drinksList.get(drinkTypeChoice)); - userOrder.addDrink(drink); - return false; - } - } catch (NumberFormatException e) { - if (userChoice.equalsIgnoreCase("x")) { - return false; - } else { - System.out.println("This is not a valid command, please try again."); - Inputs.awaitInput(); - } - } - return true; - } - - private boolean processChipsMenuChoice() { - String userChoice = Inputs.getString(); - try { - int userSidesChoice = Integer.parseInt(userChoice); - if (validateUserChoice(userSidesChoice, userOrder.chipsList)) { - Chips chip = new Chips(); - chip.setName(userOrder.chipsList.get(userSidesChoice)); - userOrder.addChips(chip); - return false; - } - } catch (NumberFormatException e) { - if (userChoice.equalsIgnoreCase("x")) { - return false; - } else { - System.out.println("This was not a valid sides choice. Please try again..."); - Inputs.awaitInput(); - } - } - return true; - } - - private boolean processSidesMenuChoice() { - String userChoice = Inputs.getString(); - try { - Sides side = new Sides(); - int userSidesChoice = Integer.parseInt(userChoice); - - if (validateUserChoice(userSidesChoice, userOrder.sides)) { - side.setName(userOrder.sides.get(userSidesChoice)); - userOrder.addSide(side); - return false; - } - } catch (NumberFormatException e) { - if (userChoice.equalsIgnoreCase("x")) { - return false; - } else { - System.out.println("This was not a valid sides choice. Please try again..."); - Inputs.awaitInput(); - } - } - return true; - } - - private void processEditingMenuChoice() { - String userChoice = Inputs.getString(); - - try { - int userIntChoice = Integer.parseInt(userChoice); - - switch (userIntChoice) { - case 1: - selectWhichItemToEdit(userOrder.getSandwiches()); - break; - case 2: - selectWhichItemToEdit(userOrder.getDrinks()); - break; - case 3: - selectWhichItemToEdit(userOrder.getChips()); - break; - case 4: - selectWhichItemToEdit(userOrder.getSidesList()); - break; - default: - break; - } - } catch (NumberFormatException e) { - if (userChoice.equalsIgnoreCase("x")) { - System.out.println(); - } else { - System.out.println("This was not a valid menu choice. Please try again..."); - Inputs.awaitInput(); - } - } - } - - private void selectWhichItemToEdit(List itemList) { - int originalItemListSize = itemList.size(); - Utilities.clearConsole(); - System.out.println(Utilities.centerMessage(" Choose item to edit ", 50, '=')); - System.out.println(); - Map itemsMap = new HashMap<>(); - int itemNum = 1; - if (!itemList.isEmpty()) { - for (Product item : itemList) { - itemsMap.put(itemNum, item); - if (item instanceof Sandwich) System.out.printf("%s\n", ((Sandwich) item).displayCurrentSandwichCompact(itemNum)); - else System.out.printf("[%d] %s\n", itemNum, item); - itemNum++; - } - - System.out.print("\n\nWhich item would you like to edit?\n\nEnter choice: "); - - int itemEditChoice = Inputs.getInt(); - - if (itemEditChoice < 0 || itemEditChoice > itemsMap.size()) { - System.out.println("That's not a valid choice."); - Inputs.awaitInput(); - } else { - Product itemChoice = itemList.get(itemEditChoice - 1); - - if (itemChoice instanceof Drinks) { - orderDrink(); - if (validateItemEdited(originalItemListSize, itemChoice)) userOrder.getDrinks().remove((Drinks) itemsMap.get(itemEditChoice)); - } else if (itemChoice instanceof Chips) { - addChips(); - userOrder.getChips().remove((Chips) itemsMap.get(itemEditChoice)); - } else if (itemChoice instanceof Sides) { - orderSides(); - userOrder.getSidesList().remove((Sides) itemsMap.get(itemEditChoice)); - } else if (itemChoice instanceof Sandwich) { - CreateSandwichScreen createSandwichScreen = new CreateSandwichScreen(userOrder); - createSandwichScreen.sandwichEditScreen((Sandwich) itemsMap.get(itemEditChoice)); - userOrder.getSandwiches().remove((Sandwich) itemsMap.get(itemEditChoice)); - } - } - } else { - System.out.println("You have nothing to edit..."); - Inputs.awaitInput(); - } - - } - - private boolean validateItemEdited(int originalListSize, Product itemChoice) { - List productList = null; - try { - if (itemChoice instanceof Drinks) { - productList = userOrder.getDrinks(); - } else if (itemChoice instanceof Chips) { - productList = userOrder.getChips(); - } else if (itemChoice instanceof Sides) { - productList = userOrder.getSidesList(); - } else if (itemChoice instanceof Sandwich) { - productList = userOrder.getSandwiches(); - } - assert productList != null; - if (productList.size() != originalListSize) return true; - } catch (IndexOutOfBoundsException e) { - return true; - } - return false; - } - private boolean validateUserChoice(int userChoice, Map itemsList) { - if (userChoice < 0 || userChoice > itemsList.size()) { - System.out.println("That's not a valid choice."); - Inputs.awaitInput(); - return false; - } - return true; - } -} diff --git a/src/main/java/com/pluralsight/Utilities/Inputs.java b/src/main/java/com/pluralsight/util/Inputs.java similarity index 75% rename from src/main/java/com/pluralsight/Utilities/Inputs.java rename to src/main/java/com/pluralsight/util/Inputs.java index a151713..bb0f33a 100644 --- a/src/main/java/com/pluralsight/Utilities/Inputs.java +++ b/src/main/java/com/pluralsight/util/Inputs.java @@ -1,4 +1,4 @@ -package com.pluralsight.Utilities; +package com.pluralsight.util; import java.util.Scanner; @@ -45,17 +45,6 @@ public static int getInt() { return input; } - public static double getDouble() { - ensureScannerIsOpen(); - while (!scanner.hasNextDouble()) { - System.out.println("That's not a valid double... Please enter a double:"); - scanner.next(); // Discard invalid input - } - double input = scanner.nextDouble(); - scanner.nextLine(); //eat CRLF - return input; - } - public static void awaitInput() { ensureScannerIsOpen(); System.out.print("\nPress ENTER to continue..."); diff --git a/src/main/java/com/pluralsight/Utilities/Utilities.java b/src/main/java/com/pluralsight/util/Text.java similarity index 82% rename from src/main/java/com/pluralsight/Utilities/Utilities.java rename to src/main/java/com/pluralsight/util/Text.java index 9cd2117..49d3662 100644 --- a/src/main/java/com/pluralsight/Utilities/Utilities.java +++ b/src/main/java/com/pluralsight/util/Text.java @@ -1,8 +1,8 @@ -package com.pluralsight.Utilities; +package com.pluralsight.util; -public class Utilities { +public class Text { - private Utilities() {} + private Text() {} //Text Utilities public static String centerMessage(String message, int width, char padChar) { @@ -18,26 +18,26 @@ public static String centerMessage(String message, int width, char padChar) { public static String createHeader(String header) { StringBuilder builder = new StringBuilder(); builder.append("\n"); - builder.append(Utilities.createInnerLine()).append("\n"); + builder.append(Text.createInnerLine()).append("\n"); builder.append(" "); builder.append(header); int spaces = 40 - header.length(); builder.append(" ".repeat(Math.max(0, spaces))); builder.append("||\n"); - builder.append(Utilities.createOuterLine()).append("\n"); + builder.append(Text.createOuterLine()).append("\n"); return builder.toString(); } public static String createHeader(String header, double price) { StringBuilder builder = new StringBuilder(); builder.append("\n"); - builder.append(Utilities.createInnerLine()).append("\n"); + builder.append(Text.createInnerLine()).append("\n"); builder.append(" "); builder.append(header); int spaces = 40 - header.length(); builder.append(" ".repeat(Math.max(0, spaces))); builder.append(String.format("|| $%.2f\n", price)); - builder.append(Utilities.createOuterLine()).append("\n"); + builder.append(Text.createOuterLine()).append("\n"); return builder.toString(); }