Question: Hello, I need some help editing this code. It has to exactly match the picture I provided. Here is the code so far. DriverClass import
Hello, I need some help editing this code. It has to exactly match the picture I provided. Here is the code so far.
DriverClass
import java.util.Scanner;
public class GroceryCartDriver {
public static void main(String[] args) { Scanner scanner = null, scanner1 = null;
try { scanner = new Scanner(System.in); scanner1 = new Scanner(System.in); String items[] = { "Bread", "Banana", "Juice", "Milk", "Bottled Water" };
GroceryCart cart = new GroceryCart(); do { do { System.out.print("Welcome to Hit enter to calculate total or Enter the name of the item: ");
String name = scanner1.nextLine(); GroceryItem groceryItem = new GroceryItem(); if (name.length() != 0) {
do { boolean flag = false; ; for (int i = 0; i
} } if (flag) break; else { System.out.print("Invalid name. Enter a valid name:"); name = scanner1.nextLine();
} } while (true);
groceryItem.setName(name); System.out.print("Enter the quantity:"); int quantity = scanner.nextInt(); do { if (groceryItem.setQuantity(quantity)) break; else { System.out.print("Invalid quantity. Enter a valid quantity:"); quantity = scanner.nextInt();
}
} while (true); System.out.print("Enter the price :"); double price = scanner.nextDouble();
do { if (groceryItem.setPrice(price)) break; else { System.out.print("Invalid price, Enter a valid price:"); price = scanner.nextDouble();
}
} while (true);
cart.add(groceryItem); System.out.println(); } else { System.out.println(cart.toString()); System.out.println("Total :" + cart.total()); System.out.println("Tax :" + cart.getTax()); break;
}
} while (true);
System.out.println("Another customer: yeso"); char choice = scanner.next().charAt(0); if (choice == 'n') break; } while (true); System.out.println("Good Bye. COME BACK SOON"); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
Grocery cart
public class GroceryCart {
static final double TAX_RATE = .07;
GroceryItem[] list; int itemCount;
/** * */ public GroceryCart() { // TODO Auto-generated constructor stub itemCount = 0; list = new GroceryItem[10];
}
/** * @return */ public double total() { double total = 0.0d; for (int i = 0; i
total += list[i].getPrice();
} return total;
}
/** * @return the itemCount */ public int getItemCount() { return itemCount; }
/** * @param item */ public void add(GroceryItem item) {
if (itemCount
} }
public double getTax() { return total() * TAX_RATE;
}
/* * (non-Javadoc) * * @see java.lang.Object#toString() */ public String toString() {
String str = ""; for (int i = 0; i
str += (i + 1) + "\t" + list[i].getName() + "\t" + list[i].getQuantity() + "@\t" + list[i].getPrice() + " each\t" + list[i].getTotal() + " "; }
return str; }
}
Grocery item
public class GroceryItem {
String name; int quantity; double price;
public GroceryItem() { // TODO Auto-generated constructor stub }
/** * @param name * @param quantity * @param price */ public GroceryItem(String name, int quantity, double price) { this.name = name; this.quantity = quantity; this.price = price; }
/** * @return the name */ public String getName() { return name; }
/** * @param name * the name to set */ public boolean setName(String name) { this.name = name; return true; }
/** * @return the quantity */ public int getQuantity() { return quantity; }
/** * @param quantity * the quantity to set */ public boolean setQuantity(int quantity) { if (quantity > 0) { this.quantity = quantity; return true; } else return false; }
/** * @return the price */ public double getPrice() { return price; }
/** * @param price * the price to set */ public boolean setPrice(double price) { if (price
/* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return " name=" + name + ", quantity=" + quantity + ", price=" + price + ""; }
/* * (non-Javadoc) * * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { // TODO Auto-generated method stub if (obj instanceof GroceryItem) { GroceryItem groceryItem = (GroceryItem) obj; if (groceryItem.getName().equals(name)) return true; else return false; } else return false; }
/** * method calculates the total price for the item for the given number of * quantity */ public double getTotal() {
return quantity * price; } }
These are the codes. I need it to exactly match this photo below. Some issues I've notice below is that the tax is off. The spacings are off, it has to exactly match. The beginning when it welcomes is also only suppose to happen once but it keeps doing it cause of the loop. It needs to get rid of the welcome the second time it runs. Also when I start as a new customer the grocery list from the old one still pops up. The totals is also funky when starting as a new customer because it shows the previous items but doesn't add them up. Thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
