Q008 Shopping Cart
import java.util.ArrayList;
import java.util.Scanner;
class Item {
private int itemCode;
private String itemName;
private int itemQty;
public Item() {
public Item(int code, String name, int qty) {
this.itemCode = code;
this.itemName = name;
this.itemQty = qty;
public static void display(ArrayList<Item> itemArr) {
if (itemArr.isEmpty()) {
System.out.println("Cart is Empty");
} else {
for (Item item : itemArr) {
System.out.println("Item code: " + item.itemCode);
System.out.println("Item name: " + item.itemName);
System.out.println("Item quantity: " + item.itemQty);
}
public int getItemCode() {
return itemCode;
public class ShoppingCart {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Item> cart = new ArrayList<>();
int choice;
do {
System.out.println("Shopping cart menu");
System.out.println("1. Insert new item in cart");
System.out.println("2. Remove an item from cart");
System.out.println("3. View cart items");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter item code: ");
int code = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter item name: ");
String name = scanner.nextLine();
System.out.print("Enter item quantity: ");
int qty = scanner.nextInt();
cart.add(new Item(code, name, qty));
break;
case 2:
System.out.print("Enter the code of an item: ");
int removeCode = scanner.nextInt();
boolean found = false;
for (Item item : cart) {
if (item.getItemCode() == removeCode) {
cart.remove(item);
found = true;
System.out.println("Remaining items in cart..");
Item.display(cart);
break;
if (!found) {
System.out.println("Item not found");
break;
case 3:
Item.display(cart);
break;
case 4:
System.out.println("Thank you!!!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 4);
scanner.close();
Output:
Input and Output 1:
Shopping cart menu
1. Insert new item in cart
2. Remove an item from cart
3. View cart items
4. Exit
Enter your choice
1
Enter item code:
101
Enter item name:
ball
Enter item quantity:
4
Shopping cart menu
1. Insert new item in cart
2. Remove an item from cart
3. View cart items
4. Exit
Enter your choice
1
Enter item code:
102
Enter item name:
book
Enter item quantity:
3
Shopping cart menu
1. Insert new item in cart
2. Remove an item from cart
3. View cart items
4. Exit
Enter your choice
3
Item code: 101
Item name: ball
Item quantity: 4
Item code: 102
Item name: book
Item quantity: 3
Shopping cart menu
1. Insert new item in cart
2. Remove an item from cart
3. View cart items
4. Exit
Enter your choice
4
Thank you!!!
Input and Output 2:
Shopping cart menu
1. Insert new item in cart
2. Remove an item from cart
3. View cart items
4. Exit
Enter your choice
1
Enter item code:
101
Enter item name:
ball
Enter item quantity:
4
Shopping cart menu
1. Insert new item in cart
2. Remove an item from cart
3. View cart items
4. Exit
Enter your choice
1
Enter item code:
102
Enter item name:
book
Enter item quantity:
3
Shopping cart menu
1. Insert new item in cart
2. Remove an item from cart
3. View cart items
4. Exit
Enter your choice
3
Item code: 101
Item name: ball
Item quantity: 4
Item code: 102
Item name: book
Item quantity: 3
Shopping cart menu
1. Insert new item in cart
2. Remove an item from cart
3. View cart items
4. Exit
Enter your choice
2
Enter the code of an item:
102
Remaining items in cart..
Item code: 101
Item name: ball
Item quantity: 4
Shopping cart menu
1. Insert new item in cart
2. Remove an item from cart
3. View cart items
4. Exit
Enter your choice
4
Thank you!!!