Write a Java program to simulate an online store. The program should begin by displaying a list of products and their prices. There should be a minimum of 4 products offered. The program should ask the user to choose a product and then ask the user to enter the quantity they require of that product. The program should then allow the user to keep choosing more products and quantities until they enter something to indicate they want to end the program (e.g. a given number or ‘q’ or ‘exit’). The program should then tell the user the total amount for the products they have selected. Students must use array or arraylist to store the products and their prices.
SOLUTION TO THE ABOVE QUESTION
SOLUTION CODE
package com.company;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
//define a method that contains all the products in our shop with their prices
static void product()
{
System.out.println("Choose the product you want to buy: ");
System.out.println("1.product: bread price: ksh 50 per 1 bread");
System.out.println("2.product: sugar price: ksh 120 per Kilogram");
System.out.println("3.product: salt price: ksh 60 per Kilogram");
System.out.println("4.product: rice price: ksh 200 per kilogram");
System.out.println("5.product: maize_flour price: ksh 80 per 1 kilogram packet");
System.out.println("6.Exit buying products");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> my_total_price_array_list = new ArrayList<>();
System.out.println("Dear customer, welcome to our Dkay shop");
//Now lets have our loop
int terminate_condition = 1;
while (terminate_condition==1)
{
product();
System.out.println("Enter your option:");
int option = sc.nextInt();
if(option == 1)
{
System.out.println("Enter the number of breads you want to buy: ");
int bread_number = sc.nextInt();
//calculate the total price
int bread_total = bread_number * 50;
//add the the total to our array list
my_total_price_array_list.add(bread_total);
}
else if(option==2)
{
System.out.println("Enter the number of kilograms of sugar you want to buy: ");
int sugar_number_of_kgs = sc.nextInt();
//calculate the total price
int sugar_total = sugar_number_of_kgs * 120;
//add the the total to our array list
my_total_price_array_list.add(sugar_total);
}
else if(option==3)
{
System.out.println("Enter the number of kilograms of salt you want to buy: ");
int salt_number_of_kgs = sc.nextInt();
//calculate the total price
int salt_total = salt_number_of_kgs * 60;
//add the the total to our array list
my_total_price_array_list.add(salt_total);
}
else if(option==4)
{
System.out.println("Enter the number of kilograms of rice you want to buy: ");
int rice_number_of_kgs = sc.nextInt();
//calculate the total price
int rice_total = rice_number_of_kgs * 200;
//add the the total to our array list
my_total_price_array_list.add(rice_total);
}
else if(option==5)
{
System.out.println("Enter the number of packets of maize_flour you want to buy: ");
int maize_flour_number_of_pckts = sc.nextInt();
//calculate the total price
int maize_flour_total = maize_flour_number_of_pckts * 80;
//add the the total to our array list
my_total_price_array_list.add(maize_flour_total);
}
else if(option==6)
{
System.out.println("You are done buying your products, thank you for shopping at Dkay shop.");
terminate_condition = 0;
}
else
{
System.out.println("You have entered an invalid option, please try again");
}
}
//Now lets get the length of the array list
int array_list_length = my_total_price_array_list.size();
//declare total sum here
int sum = 0;
for(int i = 0; i<array_list_length; i++)
{
sum = sum + my_total_price_array_list.get(i);
}
//Now print the sum
System.out.println("Dear customer the products you have bought costs Ksh "+sum);
}
}
SAMPLE PROGRAM OUTPUT
Comments
thank you for the code and help me get 80/100 in assignment
Leave a comment