Skip to content

Commit

Permalink
Refactored more while loops to use recursion. Also got rid of all the…
Browse files Browse the repository at this point in the history
… try-catch blocks that were used in the wrong way.

- Reworked checkIfHasSauces and checkIfHasToppings to not reference the actual sets in the Sandwich, but to create a new HashSet of the respective sets.
  • Loading branch information
sekwanaa committed May 29, 2024
1 parent 4e9cb94 commit 859a62e
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 290 deletions.
91 changes: 48 additions & 43 deletions src/main/java/com/pluralsight/models/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,65 @@ public Order() {
this.sidesList = new ArrayList<>();
}

public final Map<Integer, String> sides = new TreeMap<>(Map.of(
1, "au jus",
2, "sauce"
public final Map<String, String> premiumToppings = new TreeMap<>(Map.of(
"1", "Steak",
"2", "Ham",
"3", "Salami",
"4", "Roast Beef",
"5", "Chicken",
"6", "Bacon"
));

public final Map<Integer, String> premiumToppings = new TreeMap<>(Map.of(
1, "Steak",
2, "Ham",
3, "Salami",
4, "Roast Beef",
5, "Chicken",
6, "Bacon"
public final Map<String, String> 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<Integer, String> 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<String, String> sauces = new TreeMap<>(Map.of(
"1", "Mayo",
"2", "Mustard",
"3", "Ketchup",
"4", "Ranch",
"5", "Thousand Islands",
"6", "Vinaigrette"

));

public final Map<String, String> drinksList = new TreeMap<>(Map.of(
"1", "Coke",
"2", "Root Beer",
"3", "Pepsi",
"4", "Dr. Pepper",
"5", "Fanta",
"6", "Lemonade"
));

public final Map<String, String> 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<String, String> sides = new TreeMap<>(Map.of(
"1", "au jus",
"2", "sauce"
));

public final Map<Integer, String> cheeses = new TreeMap<>(Map.of(
1, "American",
2, "Provolone",
3, "Cheddar",
4, "Swiss"
));
public final Map<Integer, String> sauces = new TreeMap<>(Map.of(
1, "Mayo",
2, "Mustard",
3, "Ketchup",
4, "Ranch",
5, "Thousand Islands",
6, "Vinaigrette"
));
public final Map<Integer, String> drinksList = new TreeMap<>(Map.of(
1, "Coke",
2, "Root Beer",
3, "Pepsi",
4, "Dr. Pepper",
5, "Fanta",
6, "Lemonade"
));
public final Map<Integer, String> chipsList = new TreeMap<>(Map.of(
1, "Kettle Cooked Jalapeno",
2, "Lays Classic",
3, "Cheetos",
4, "Doritos Cool Ranch",
5, "Doritos Nacho Cheese"
));


//Methods
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/pluralsight/models/Sandwich.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.pluralsight.models.abstractModel.Product;
import com.pluralsight.util.Text;

import java.util.HashSet;
import java.util.Set;

public class Sandwich extends Product {
Expand All @@ -11,11 +12,31 @@ public class Sandwich extends Product {
private Set<String> premiumToppingsList;
private boolean extraMeat = false;
private Set<String> regularToppingsList;
private final Set<String> toppingsToAdd = new HashSet<>();
private String cheese;
private boolean extraCheese = false;
private Set<String> saucesList;
private final Set<String> 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;
Expand Down Expand Up @@ -106,10 +127,18 @@ public void setRegularToppings(Set<String> regularToppings) {
this.regularToppingsList = regularToppings;
}

public Set<String> getToppingsToAdd() {
return this.toppingsToAdd;
}

public Set<String> getSauces() {
return saucesList;
}

public Set<String> getSaucesToAdd() {
return saucesToAdd;
}

public void setSauces(Set<String> sauces) {
this.saucesList = sauces;
}
Expand Down Expand Up @@ -236,4 +265,5 @@ public String displayCurrentSandwichCompact(int sandwichNum) {
}
return output.toString();
}

}
179 changes: 91 additions & 88 deletions src/main/java/com/pluralsight/ui/CreateSandwichScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,61 +259,62 @@ private void chooseBreadType(Sandwich userSandwich) {
}

private void chooseToppings(Sandwich userSandwich, String type) {
Map<Integer, String> toppings = type.equals("premium") ? userOrder.premiumToppings : userOrder.regularToppings;
Map<String, String> toppings = type.equals("premium") ? userOrder.premiumToppings : userOrder.regularToppings;
Set<String> chosenToppings = checkIfHasToppings(userSandwich, type);

boolean isChoosingToppings = true;
while (isChoosingToppings) {
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("[%d] %s\n", number, topping));
System.out.print("""
[D] Done
[x] Cancel choosing toppings...
Enter choice:\s""");
String userChoice = Inputs.getString();
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();

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;
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...");
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;
}
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;
}
}
}
Expand Down Expand Up @@ -350,49 +351,48 @@ private void chooseCheese(Sandwich userSandwich) {
}

private void chooseSauces(Sandwich userSandwich) {
Map<Integer, String> sauces = userOrder.sauces;
Map<String, String> sauces = userOrder.sauces;
Set<String> chosenSauces = checkIfHasSauces(userSandwich);
boolean isChoosingSauces = true;
while (isChoosingSauces) {
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 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();
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();

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")) {
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);
isChoosingSauces = false;
case "y", "Y" -> userSandwich.setSauces(chosenSauces);
case "n", "N" -> {
System.out.println("Okay, let's try again...");
userSandwich.clearSaucesToAdd();
chooseSauces(userSandwich);
}
case "n", "N" -> System.out.println("Okay, let's try again...");
}
} else {
break;
case "X", "x":
break;
default:
System.out.println("This is not a valid choice... Please enter a valid choice.");
}
userSandwich.clearSaucesToAdd();
chooseSauces(userSandwich);
}
}
}
Expand All @@ -404,22 +404,25 @@ private void displaySummary(Set<String> chosenItems) {
//VALIDATION CHECKS

private static Set<String> checkIfHasToppings(Sandwich userSandwich, String type) {
Set<String> chosenToppings = Set.of();
Set<String> chosenToppings = new HashSet<>();
if (type.equalsIgnoreCase("premium")) {
if (userSandwich.getPremiumToppings() == null) return new HashSet<>();
if (!userSandwich.getPremiumToppings().isEmpty()) chosenToppings = userSandwich.getPremiumToppings();
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 = userSandwich.getRegularToppings();
if (!userSandwich.getRegularToppings().isEmpty()) chosenToppings = new HashSet<>(userSandwich.getRegularToppings());
}

if (!userSandwich.getToppingsToAdd().isEmpty()) chosenToppings.addAll(userSandwich.getToppingsToAdd());
return chosenToppings;
}

private Set<String> checkIfHasSauces(Sandwich userSandwich) {
Set<String> sauces;
Set<String> sauces = new HashSet<>();
if (userSandwich.getSauces() == null) return new HashSet<>();
if (!userSandwich.getSauces().isEmpty()) sauces = userSandwich.getSauces();
else sauces = new HashSet<>();
if (!userSandwich.getSauces().isEmpty()) sauces = new HashSet<>(userSandwich.getSauces());

if (!userSandwich.getSaucesToAdd().isEmpty()) sauces.addAll(userSandwich.getSaucesToAdd());
return sauces;
}

Expand Down
Loading

0 comments on commit 859a62e

Please sign in to comment.