You are on page 1of 7

Members:

John Christian Asentista


May Jun Macua
Flobert Apus
Program name: Grocery list calculator
Program description: This program allows users to add grocery items with their name, price,
and quantity to the shop, then displays the items and calculates the total revenue.
Algorithm:
Step1. Start
Step2. Display “Welcome to the Grocery Shop!”
Step3. Initialize an empty list to store grocery items
Step4. Initialize a Scanner object to get user input
Step5. While true:
a. Prompt the user to enter the item name, price, and quantity
b. Create a new GroceryItem object with the provided information
c. Add the item to the list of grocery items
d. Ask the user if they want to add another item
e. If the user does not want to add another item, break the loop
Step6. Display the list of added grocery items
Step7. Calculate the total revenue by iterating over the list of items and summing up the prices
multiplied by quantities
Step8. Display the total revenue
Step9. End

Flowchart:
Source code:
Import java.util.ArrayList;
Import java.util.Scanner;

Class GroceryItem {
Private String name;
Private double price;
Private int quantity;

Public GroceryItem(String name, double price, int quantity) {


This.name = name;
This.price = price;
This.quantity = quantity;
}

Public String getName() {


Return name;
}

Public double getPrice() {


Return price;
}

Public int getQuantity() {


Return quantity;
}
Public void setQuantity(int quantity) {
This.quantity = quantity;
}

Public double calculateTotal() {


Return price * quantity;
}
}

Public class Groceryshop {


Private ArrayList<GroceryItem> items;

Public Groceryshop() {
Items = new ArrayList<>();
}

Public void addItem(GroceryItem item) {


Items.add(item);
}

Public void displayItems() {


System.out.println(“Grocery Shop Items:”);
For (GroceryItem item : items) {
System.out.println(“Name: “ + item.getName() + “, Price: $” + item.getPrice() + “,
Quantity: “ + item.getQuantity());
}
}
Public double calculateTotalRevenue() {
Double totalRevenue = 0;
For (GroceryItem item : items) {
totalRevenue += item.calculateTotal();
}
Return totalRevenue;
}

Public static void main(String[] args) {


Groceryshop shop = new Groceryshop();
Scanner scanner = new Scanner(System.in);

System.out.println(“Welcome to the Grocery Shop!”);


Char choice;
Do {
System.out.print(“Enter item name: “);
String name = scanner.nextLine();
System.out.print(“Enter item price: “);
Double price = scanner.nextDouble();
System.out.print(“Enter item quantity: “);
Int quantity = scanner.nextInt();
Scanner.nextLine(); // Consume newline

GroceryItem item = new GroceryItem(name, price, quantity);


Shop.addItem(item);
System.out.print(“Do you want to add another item? (y/n): “);
Choice = scanner.next().charAt(0);
Scanner.nextLine(); // Consume newline
} while (choice == ‘y’ || choice == ‘Y’);

Shop.displayItems();
System.out.println(“Total revenue: $” + shop.calculateTotalRevenue());

Scanner.close();
}
}

Test:

You might also like