Advertisement
Jycko

Untitled

Nov 25th, 2022
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.Scanner;
  4.  
  5. public class leonardshop {
  6.     static HashMap<Integer, leonarditem> leoitems = new HashMap<Integer, leonarditem>();
  7.    
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         loadVariables();
  11.         showMenu();
  12.     }
  13.     // leonarditem & amount
  14.     static Scanner input = new Scanner(System.in);
  15.  
  16.     private static HashMap<leonarditem, Integer> shoppingcart = new HashMap<leonarditem, Integer>();
  17.     private static void showMenu() {
  18.         System.out.println("0. CLEAR CART");
  19.         System.out.println("1. SHOW CART CONTENTS");
  20.  
  21.         System.out.println("2.  Pensil     : RP.100.000");
  22.         System.out.println("3. eraser       : RP.500");
  23.         System.out.println("4. iphone       : RP.3000");
  24.         System.out.println("5. microphone : RP.50.000");
  25.         System.out.print("What would you like to purchase? (1-4): ");
  26.         String inputid = input.next();
  27.         int itemcode = -1;
  28.         try {
  29.             itemcode = Integer.parseInt(inputid);
  30.         } catch (NumberFormatException e) {
  31.             System.out.println("Incorrect id.");
  32.             showMenu();
  33.             return;
  34.         }
  35.         // wrong code
  36.         if (itemcode != 0 && itemcode != 1) {
  37.         if (!leoitems.containsKey(itemcode) || itemcode == -1) {
  38.             System.out.println("That item does not exist.");
  39.             showMenu();
  40.             return;
  41.         }
  42.         }
  43.        
  44.         // RESET CART
  45.         if (itemcode == 0) {
  46.             shoppingcart.clear();
  47.             System.out.println("Cart cleared.");
  48.             showMenu();
  49.             return;
  50.         }
  51.        
  52.         // SHOW CART CONTENT
  53.         if (itemcode == 1) {
  54.             System.out.println("-=== SHOPPING CART CONTENTS ===-");
  55.             int i = 1;
  56.             int total = 0;
  57.             for (leonarditem car : shoppingcart.keySet()) {
  58.                 int amount = shoppingcart.get(car);
  59.                 total = total + car.price * amount;
  60.                 System.out.println(i + ". '" + car.itemname + "' x" + amount + "");
  61.                 i++;
  62.             }
  63.             System.out.println("YOUR TOTAL PRICE:");
  64.             System.out.println("Rp. " + total);
  65.             System.out.println(" ");
  66.  
  67.             showMenu();
  68.             return;
  69.  
  70.         }
  71.        
  72.         leonarditem item = leoitems.get(itemcode);
  73.         System.out.print("[LEO SHOP] Chosen item: " + item.itemname + " Rp. " + item.price);
  74.         System.out.print("\n [LEO SHOP] How many would you like to buy?");
  75.  
  76.         int amount = input.nextInt();
  77.         shoppingcart.put(item, amount);
  78.         System.out.print("[LEO SHOP] Added " + amount + "x of " + item.itemname + " to your shopping cart. \n");
  79.         showMenu();
  80.         return;
  81.  
  82.     }
  83.     //int price, String itemname, int itemcode) {
  84.     private final static void loadVariables() {
  85.        
  86.         ArrayList<leonarditem> items = new ArrayList<leonarditem>();
  87.         items.add(new leonarditem(100000,"jordy PENCIL", 2));
  88.         items.add(new leonarditem(500, "jordy ERASER", 3));
  89.         items.add(new leonarditem(3000, "jordy iPHONE", 4));
  90.         items.add(new leonarditem(50000, "jordy microphone", 5));
  91.         for (leonarditem item : items) {
  92.             leoitems.put(item.itemcode, item);
  93.         }
  94.     }
  95. }
  96.  
  97. // main class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement