hellionsoldier

Coding and a cup of Java Assignment Rev2

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