Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * BASIC text-based web store on your computer. Web store has two menus:
- * Main menu and Items menu. Items include name and price.
- * Keep track what the user has bought.
- * Finally print cart contents and their price.
- *
- *
- * @author Martin Silén
- * @version W.4.3.
- */
- import java.util.*;
- public class WebStore {
- static Scanner reader=new Scanner(System.in);
- static int choice;
- static ArrayList <Item> currentCart= new ArrayList<Item>();
- static ArrayList <Item> clearCart= new ArrayList<Item>();
- public static void main(String[] args) {
- //Introprompt.
- System.out.println("Welcome to the web store!");
- System.out.println("Currently you have in your cart:\n\n");
- //Main menu
- String mainMenu = "Main Menu"
- + "\nWhat do you want to do:"
- +"\n1. Add items to the cart"
- +"\n2. Clear the cart"
- +"\n3. Leave";
- mainMenu : while (true){
- System.out.println(mainMenu);
- choice=reader.nextInt();
- switch (choice){
- case 1:
- chooseItem(); //Prompts Item method and menu
- break;
- case 2: System.out.println("\nYou have emptied your cart"+clearCart());
- break;
- case 3: System.out.println("\nYou have bought the following items today: "+checkCart()+"\nExiting..."); //ending message
- break mainMenu;
- default: System.out.println("Please try again."); //if they punch the wrong number
- break;
- }
- }
- }
- public static void chooseItem(){
- String itemMenu = "\nItems menu"
- + "\nWhat item would you like to add to your cart?"
- + "\n1: Newspaper (20€)"
- + "\n2: Course book (40€)"
- + "\n3: Laptop (400€)"
- + ""
- + "\n4: Back to main menu";
- itemMenu : while (true){
- System.out.println(itemMenu);
- choice=reader.nextInt();
- switch (choice){
- case 1: System.out.println("\nYou have added a Newspaper (20€)");
- addNewspaper();
- System.out.println("Currently you have in your cart"+checkCart());
- break;
- case 2: System.out.println("\nYou added a Course book 40€");
- addCourseBook();
- System.out.println("Your cart now contains"+checkCart());
- break;
- case 3: System.out.println("\nYou added a Laptop (400€)");
- addLaptop();
- System.out.println("Your cart now contains"+checkCart());
- break;
- case 4:
- break itemMenu;
- default: System.out.println("\nPlease try again");
- break;
- }
- }
- }
- public static String checkCart(){
- return currentCart.toString();
- }
- public static String clearCart(){
- currentCart=clearCart;
- return clearCart.toString();
- }
- public static ArrayList addNewspaper(){
- Item nyGrej=new Item("Newspaper subscription", 20);
- currentCart.add(nyGrej);
- return currentCart;
- }
- public static ArrayList addCourseBook(){
- Item nyGrej=new Item("Course Book", 40);
- currentCart.add(nyGrej);
- return currentCart;
- }
- public static ArrayList addLaptop(){
- Item nyGrej=new Item("Laptop", 400);
- currentCart.add(nyGrej);
- return currentCart;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement