Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. /**
  2.  * BASIC text-based web store on your computer. Web store has two menus:
  3.  * Main menu and Items menu. Items include name and price.
  4.  * Keep track what the user has bought.
  5.  * Finally print cart contents and their price.
  6.  *
  7.  *
  8.  * @author Martin Silén
  9.  * @version W.4.3.
  10.  */
  11. import java.util.*;
  12. public class WebStore {
  13.     static Scanner reader=new Scanner(System.in);
  14.     static int choice;
  15.     static ArrayList <Item> currentCart= new ArrayList<Item>();
  16.     static ArrayList <Item> clearCart= new ArrayList<Item>();
  17.     public static void main(String[] args) {
  18.         //Introprompt.
  19.         System.out.println("Welcome to the web store!");
  20.         System.out.println("Currently you have in your cart:\n\n");
  21.         //Main menu
  22.         String mainMenu = "Main Menu"
  23.                         + "\nWhat do you want to do:"
  24.                         +"\n1. Add items to the  cart"
  25.                         +"\n2. Clear the cart"
  26.                         +"\n3. Leave";
  27.         mainMenu : while (true){
  28.                 System.out.println(mainMenu);
  29.                 choice=reader.nextInt();
  30.                 switch (choice){
  31.                     case 1:
  32.                         chooseItem(); //Prompts Item method and menu
  33.                         break;
  34.                     case 2: System.out.println("\nYou have emptied your cart"+clearCart());
  35.                         break;
  36.                     case 3: System.out.println("\nYou have bought the following items today: "+checkCart()+"\nExiting..."); //ending message
  37.                         break mainMenu;
  38.                     default: System.out.println("Please try again."); //if they punch the wrong number
  39.                         break;
  40.             }
  41.         }
  42.     }
  43.         public static void chooseItem(){
  44.             String itemMenu = "\nItems menu"
  45.                         + "\nWhat item would you like to add to your cart?"
  46.                         + "\n1: Newspaper (20€)"
  47.                         + "\n2: Course book (40€)"
  48.                         + "\n3: Laptop (400€)"
  49.                         + ""
  50.                         + "\n4: Back to main menu";
  51.         itemMenu : while (true){
  52.             System.out.println(itemMenu);
  53.             choice=reader.nextInt();
  54.                 switch (choice){
  55.                     case 1: System.out.println("\nYou have added a Newspaper (20€)");
  56.                         addNewspaper();
  57.                         System.out.println("Currently you have in your cart"+checkCart());
  58.                         break;
  59.                     case 2: System.out.println("\nYou added a Course book 40€");
  60.                         addCourseBook();
  61.                         System.out.println("Your cart now contains"+checkCart());
  62.                         break;
  63.                     case 3: System.out.println("\nYou added a Laptop (400€)");
  64.                         addLaptop();
  65.                         System.out.println("Your cart now contains"+checkCart());
  66.                         break;
  67.                     case 4:
  68.                         break itemMenu;
  69.                     default: System.out.println("\nPlease try again");
  70.                         break;
  71.                 }
  72.             }
  73.         }
  74.         public static String checkCart(){
  75.            return currentCart.toString();
  76.         }
  77.         public static String clearCart(){
  78.             currentCart=clearCart;
  79.             return clearCart.toString();
  80.         }
  81.         public static ArrayList addNewspaper(){
  82.             Item nyGrej=new Item("Newspaper subscription", 20);
  83.             currentCart.add(nyGrej);
  84.                 return currentCart;
  85.         }
  86.         public static ArrayList addCourseBook(){
  87.             Item nyGrej=new Item("Course Book", 40);
  88.             currentCart.add(nyGrej);
  89.                 return currentCart;
  90.         }
  91.         public static ArrayList addLaptop(){
  92.             Item nyGrej=new Item("Laptop", 400);
  93.             currentCart.add(nyGrej);
  94.                 return currentCart;
  95.         }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement