I am having trouble with case 4, i want it to search for an item in the arrayList and say it is in the cart or not in the cart but every time i run the program it says an item in the cart is actually in the cart and an item i did not add is in the cart. how do i declare it to say i do have the item the user typed in and say you don't have that item the user typed in.
public static void main(String[] args) {
String nameOfItem = " ";
double priceOfItem = 0.0;
int quantityOfItem = 0;
String choice = "y";
String choice2 = " ";
Scanner keyboard = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.###");
ArrayList<Item> cart1 = new ArrayList<Item>();
while (choice.equalsIgnoreCase("y")) {
System.out.print("What is the item you want? ");
nameOfItem = keyboard.next();
System.out.print("What is the price? ");
priceOfItem = keyboard.nextDouble();
System.out.print("How many do you want? ");
quantityOfItem = keyboard.nextInt();
keyboard.nextLine();
System.out.print("Do you wish to continue?(y/n) ");
choice = keyboard.nextLine();
cart1.add(new Item(nameOfItem, quantityOfItem, priceOfItem));
System.out.println();
{
while (choice.equalsIgnoreCase("n")) {
System.out.println();
System.out.println("1. Display the contents of the cart\n" + "2. Add another item to the cart\n"
+ "3. Remove an item from the cart\n" + "4. Search an item in the cart\n"
+ "5. If done shopping");
choice2 = keyboard.nextLine();
switch (choice2) {
case "1":
double total = 0;
for (Item item : cart1) {
System.out.println(item);
total += item.getPrice() * item.getQuantity();
}
System.out.println("Total of your items: " + df.format(total));
break;
case "2":
System.out.print("What item do you want? ");
nameOfItem = keyboard.nextLine();
System.out.print("What is the price? ");
priceOfItem = keyboard.nextDouble();
System.out.print("How many do you want? ");
quantityOfItem = keyboard.nextInt();
keyboard.nextLine();
cart1.add(new Item(nameOfItem, quantityOfItem, priceOfItem));
System.out.println();
break;
case "3":
for (Item item : cart1) {
System.out.println(item);
}
ArrayList<Item> cart2 = new ArrayList<Item>();
System.out.println(
"\nWhat item would you like to delete?\nAll quantities of the item will be deleted");
nameOfItem = keyboard.nextLine();
cart2.add(new Item(nameOfItem, quantityOfItem, priceOfItem));
for (Item item : cart1) {
if (item.getProduct().equals(nameOfItem)) {
cart2.add(item);
}
}
cart1.removeAll(cart2);
System.out.println();
break;
case "4":
System.out.println("What item are you looking for?");
nameOfItem = keyboard.nextLine();
for (Item item : cart1) {
if (cart1.contains(item))
{
System.out.println("You do have that item in your cart!");
} else {
System.out.println("You do not have that item in your cart, press\n2 on the "
+ "next screen to add it to your cart");
}
}
System.out.println();
break;
case "5":
System.exit(0);
break;
default:
System.out.println("Invalid choice, pick again!!!!");
System.out.println("What item are you looking for?");
nameOfItem = keyboard.nextLine();
boolean found = false;
for (Item item : cart1) {
if (item.getName().equals(nameOfItem)) {
found = true;
}
}
if (found) {
System.out.println("You do have that item in your cart!");
} else {
System.out.println("You do not have that item in your cart, press\n2 on the "
+ "next screen to add it to your cart");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.