hellionsoldier

Coding and a cup of Java Assignment

Dec 26th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.11 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     static int[] shoppingBasket = {0,0,0,0,0};
  5.     // Add names in order of values of store items
  6.     static final String[] ITEMNAMES = {"Broken Computer", "Cookie", "Unknown Video Game", "Modern Art", "Useless Button"};
  7.     // Add values in order of store items
  8.     static final int[] ITEMCOSTS = {40,3,25,80,10};
  9.  
  10.     static boolean inStore = true;
  11.     static int customerCash = 100;
  12.  
  13.     public static void main(String[] args) {
  14.         // Loop while we're still in the store
  15.         while (inStore) {
  16.             mainMenu();
  17.             switch(inputHandler(3)) {
  18.                 case 1: // I'm going shopping!
  19.                     shopMenu();
  20.                     break;
  21.                 case 2: // Whats in the basket?
  22.                     displayBasket(shoppingBasket);
  23.                     break;
  24.                 case 3: // Exact change only please...
  25.                     payAndExit();
  26.                     break;
  27.             }
  28.         }
  29.     }
  30.  
  31.     public static int inputHandler(int n) {
  32.         // Got really sick of putting scanners everywhere.
  33.         Scanner scanner = new Scanner(System.in);
  34.         int choice = scanner.nextInt();
  35.         // Need a way to use this without it range checking.
  36.         if (n == 0) {
  37.             return choice;
  38.         } else {
  39.             while (choice < 1 || choice > n) {
  40.                 System.out.println("Option out of range please try again.");
  41.                 choice = scanner.nextInt();
  42.             }
  43.         }
  44.         return choice;
  45.     }
  46.  
  47.     public static void mainMenu() {
  48.         // tl;dr wall o text
  49.         System.out.println("\n\nWelcome Customer: You currently have " + itemsInBasket(shoppingBasket) + " items in your shopping basket.");
  50.         System.out.println("What would you like to do?");
  51.         System.out.println("1. Add an item to the shopping basket");
  52.         System.out.println("2. View the content of the shopping basket");
  53.         System.out.println("3. Pay and exit the shop");
  54.     }
  55.  
  56.     public static void shopMenu() {
  57.         System.out.println("What would you like to add to your shopping basket?");
  58.         System.out.println("1. Broken Computer $40");
  59.         System.out.println("2. Cookie $30");
  60.         System.out.println("3. Unknown video game $25");
  61.         System.out.println("4. Modern art $80");
  62.         System.out.println("5. Useless button $10");
  63.         System.out.println("6. Go Back");
  64.         // switch case to add items
  65.         switch(inputHandler(6)){
  66.             case 1:
  67.                 addItem(0);
  68.                 break;
  69.             case 2:
  70.                 addItem(1);
  71.                 break;
  72.             case 3:
  73.                 addItem(2);
  74.                 break;
  75.             case 4:
  76.                 addItem(3);
  77.                 break;
  78.             case 5:
  79.                 addItem(4);
  80.                 break;
  81.             case 6:
  82.                 break;
  83.         }
  84.     }
  85.  
  86.     public static void displayBasket(int[] basket) {
  87.         if (itemsInBasket(shoppingBasket) == 0) {
  88.             System.out.println("Your shopping basket is empty.");
  89.         } else {
  90.             for (int x = 0; x < basket.length; x++) {
  91.                 System.out.printf("Quantity of %s:  [%d]%n", ITEMNAMES[x], shoppingBasket[x]);
  92.             }
  93.             System.out.println("What would you like to do?");
  94.             System.out.println("1. Remove Item");
  95.             System.out.println("2. Go back");
  96.  
  97.             switch(inputHandler(2)) {
  98.                 case 1:
  99.                     // whats in the basket to see what can be removed
  100.                     System.out.println("What item would you like to remove?");
  101.                     for (int x = 0; x < basket.length; x++) {
  102.                         System.out.printf((x+1) + ". Quantity of %s:  [%d]%n", ITEMNAMES[x], shoppingBasket[x]);
  103.                     }
  104.                     System.out.println("6. Go Back");
  105.                     // switch case to remove items
  106.                     switch(inputHandler(6)) {
  107.                         case 1:
  108.                             removeItem(0);
  109.                             break;
  110.                         case 2:
  111.                             removeItem(1);
  112.                             break;
  113.                         case 3:
  114.                             removeItem(2);
  115.                             break;
  116.                         case 4:
  117.                             removeItem(3);
  118.                             break;
  119.                         case 5:
  120.                             removeItem(4);
  121.                             break;
  122.                         case 6:
  123.                             break;
  124.                     }
  125.                     break;
  126.                 case 2:
  127.                     break;
  128.             }
  129.         }
  130.     }
  131.  
  132.     public static void payAndExit() {
  133.         // everything i needed, time to go
  134.         int total = 0;
  135.         for ( int x = 0; x < shoppingBasket.length; x++) {
  136.             total = total + (shoppingBasket[x] * ITEMCOSTS[x]);
  137.         }
  138.         if (total > customerCash) {
  139.             System.out.println("Sorry, cannot complete transaction: Insufficient funds.");
  140.             System.out.println("Try removing some items and try again.");
  141.         } else {
  142.             System.out.println("Your Total is: " + total);
  143.             System.out.println("Your change is: " + (customerCash - total));
  144.             System.out.println("Have a nice day!");
  145.             inStore = false;
  146.         }
  147.     }
  148.  
  149.     public static int itemsInBasket(int[] cart){
  150.         // just a quick sum
  151.         int total = 0;
  152.         for (int item : cart) {
  153.             total = total + item;
  154.         }
  155.         return total;
  156.     }
  157.  
  158.     public static void addItem(int item) {
  159.         // what i want, and how  many
  160.         System.out.println(ITEMNAMES[item] + " costs " + ITEMCOSTS[item] + ". How many would you like?");
  161.         shoppingBasket[item] = shoppingBasket[item] + inputHandler(0);
  162.     }
  163.  
  164.     public static void removeItem(int item) {
  165.         // take out this many
  166.         System.out.println("Remove how many?");
  167.         shoppingBasket[item] = shoppingBasket[item] - inputHandler(0);
  168.     }
  169.  
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment