0% found this document useful (0 votes)
46 views3 pages

Module 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views3 pages

Module 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

*;

public class CartFinal {

public static void main( String[] args ) {

String owner,keepShopping="y", prompt;


List<String> items= new ArrayList<>();
List<Integer>no_items = new ArrayList<>();
List<Float> price = new ArrayList<>();
float total=0;

Scanner input = new Scanner(System.in);


do
{

System.out.print( "Enter the name of the item: " );


items.add(input.next());
System.out.print( "Enter the quantity : " );
no_items.add(input.nextInt());
System.out.print( "Enter the unit price: " );
price.add(input.nextFloat());

System.out.print( "Continue shopping (y/n)? " );


keepShopping = input.next();

}
while ( keepShopping.equalsIgnoreCase( "y" ) );

getPrompt(items,no_items,price,total);
}

static void getTotal(List<String> items, List<Integer> no_items, List<Float>


price, float total) {
boolean is_paid;
Scanner input = new Scanner(System.in);

System.out.println("Products\t\tQuantity\t\tPrice");
for(int i=0;i<items.size();i++) {
System.out.println(i+". "+items.get(i) + "\t\t"+no_items.get(i)
+"\t\t\t"+price.get(i));
total +=no_items.get(i)*price.get(i);
}
System.out.println("The total cost is : "+total);
System.out.print("Do you want to pruchase the products (true/false)?
");
is_paid = input.nextBoolean();

if(is_paid == true) {
System.out.println("Purchase successful.");
}else {
System.out.println("Purchase denied");
}
}

static void getRemove(List<String> items, List<Integer> no_items,


List<Float> price, float total) {
int remove;
Scanner input = new Scanner(System.in);

System.out.println("Products\t\tQuantity\t\tPrice");
for(int i=0;i<items.size();i++) {
System.out.println(i+". "+items.get(i) + "\t\t"+no_items.get(i)
+"\t\t\t"+price.get(i));
}
System.out.print("Enter the index you want to remove : ");
remove = input.nextInt();

items.remove(remove);
no_items.remove(remove);
price.remove(remove);

getPrompt(items,no_items,price,total);
}

static void getAdd(List<String> items, List<Integer> no_items, List<Float>


price, float total) {
Scanner input = new Scanner(System.in);
String prompt;

System.out.print( "Enter the name of the item: " );


items.add(input.next());
System.out.print( "Enter the quantity : " );
no_items.add(input.nextInt());
System.out.print( "Enter the unit price: " );
price.add(input.nextFloat());

getPrompt(items,no_items,price,total);
}
static void getUpdate(List<String> items, List<Integer> no_items,
List<Float> price, float total) {
Scanner input = new Scanner(System.in);
String Items;
float Price;
int update, No_item;

System.out.print("Enter the index you want to update : ");


update = input.nextInt();
System.out.print( "Enter the name of the item : " );
Items = input.next();
System.out.print( "Enter the quantity : " );
No_item = input.nextInt();
System.out.print( "Enter the unit price: " );
Price = input.nextFloat();

items.set(update, Items);
no_items.set(update, No_item);
price.set(update, Price);
getPrompt(items,no_items,price,total);
}
static void getPrompt(List<String> items, List<Integer> no_items,
List<Float> price, float total) {

Scanner input = new Scanner(System.in);


String prompt;
System.out.println("Products\t\tQuantity\t\tPrice");
for(int i=0;i<items.size();i++) {
System.out.println(i+". "+items.get(i) + "\t\t"+no_items.get(i)
+"\t\t\t"+price.get(i));
}
System.out.print("\nEnter add to add item, remove to remove
item,\nupdate if you want to change some products, done to finish shopping : ");
prompt = input.next();

if(prompt.equals("add")) {
getAdd(items,no_items,price,total);
}else if(prompt.equals("remove")) {
getRemove(items,no_items,price,total);
}else if(prompt.equals("done")){
getTotal(items,no_items,price,total);
}else if(prompt.equals("update")) {
getUpdate(items,no_items,price,total);
}

}
}

You might also like