I'm trying to make a simple billing system for a restaurant. This shows the menu to the user and lets them choose, add an Item or quit. In the end, it prints the bill.
The issue I'm facing is, I want the user to be able to add an item again with the item's number instead of overwriting the value.
Below code is my attempt which doesn't return anything.
public void addOrder(String meal, int quantity,
String[] dish, double[] cost) {
for (int i = 0; i < orderedFood.size(); i ) {
// look if food item already exists, update quantity of it by
// adding the previous value of the item to the new amount
if (orderedFood.get(i).contains(meal)) {
int oldQuantity = orderedQuantity.get(i);
orderedQuantity.set(i, oldQuantity quantity);
break;
} else {
// if theres no item of this type yet, create a new one
orderedFood.add(meal);
orderedQuantity.add(quantity);
}
}
The code below shows how the food object gets created, and how the program works.
public static void Order() {
String[] dish = {"Sandwich", "Coffee", "Salad"};
double[] cost = {6.5, 3.2, 4.0};
for (int i = 0; i < dish.length; i ) {
System.out.println("\n" dish[i] ": " cost[i] "€.");
}
System.out.println("\nWhat would you like to order? \n\n");
Scanner myObj = new Scanner(System.in);
List<String> dishList = new ArrayList<>(Arrays.asList(dish));
// check if item exists
String menuItemTemp = myObj.nextLine();
String menuItem = "";
if (dishList.contains(menuItemTemp)) {
System.out.println("\nOkay.\n");
menuItem = menuItemTemp;
} else {
System.out.println("Error 404 Not Found.");
Order();
}
System.out.println("\nHow many? \n");
int userQuant = myObj.nextInt();
Bill myBill = new Bill();
myBill.addOrder(menuItem, userQuant, dish, cost);
System.out.println("\nOrder more? 1 - Yes. 2 - No. \n");
Scanner menuScan = new Scanner(System.in);
int menuScanner = menuScan.nextInt();
switch (menuScanner) {
// if another order is to be made
case 1:
System.out.println("\nOkay.");
Order();
break;
// output and end of program
case 2:
myBill.getOrder();
System.out.println(Math.round(myBill.getTotal() * 100.00) / 100.00 "€\n");
System.out.println("\nThanks for ordering!\n");
// close scanners
menuScan.close();
myObj.close();
// end
System.exit(1);
}
Any nudge in the right direction would help a lot.
CodePudding user response:
So I would've left a comment if could've however I don't have a enough reputation for that yet. I'm not sure what your myBill.getTotalOrder() function is. My first through here without seeing more of your source code to understand your design would be to just make a Meal object that tracks it's name, how much it is, and how many times it was ordered. Then during your the getTotalOrder() you could just loop through each meal and add up the total.
CodePudding user response:
The error is the premature else. When not already present, you need to walk the entire for loop before knowing that there is no match.
If you do return
instead of break
you can treat the not-found case after the for.
public void addOrder(String meal, int quantity,
String[] dish, double[] cost) {
for (int i = 0; i < orderedFood.size(); i ) {
// look if food item already exists, update quantity of it by
// adding the previous value of the item to the new amount
if (orderedFood.get(i).contains(meal)) {
int oldQuantity = orderedQuantity.get(i);
orderedQuantity.set(i, oldQuantity quantity);
return;
}
}
// if theres no item of this type yet, create a new one
orderedFood.add(meal);
orderedQuantity.add(quantity);
}
Of course
for
orderedFood.get(i).contains(meal)
int oldQuantity = orderedQuantity.get(i);
orderedQuantity.set(i, oldQuantity quantity);
hints that an other data structure might be better:
Map<String, Integer> ordered = new HashMap<>(); // Meal to quantity.
public void addOrder(String meal, int quantity,
String[] dish, double[] cost) {
ordered.merge(meal, quantity, Integer::sum);
}
That is map the meal names to the total quantity. Indices are irrelevant.
Map.merge
works as follows:
merge(K key, V value, (Value oldv, Value newv) -> resultv)
If newv is null, a remove is done. Otherwise:
The lambda is called with an (possibly accumulating) old value, and the passed value.
if (oldv == null)
put(key, newv);
else
put(key, oldv newv); // newv == value
Integer::sum
is the same as (x, y) -> x y
.