2016-10-24 56 views
-4

我難以理解如何格式化菜單。到目前爲止,我的工作並不成功,但我知道它可以更好地格式化和縮短。 This is the description of the assignment.你可以看到我的方法是不會工作,但我不知道如何改變我的方法...非gui結帳註冊菜單:如何格式化?

我很確定我需要使用數組,但我不知道如何存儲一個公共數組,所以任何方法都可以調用並存儲信息。

import java.util.Scanner; 

    /* 
    @author David Jacobsen 
    @version 10-23-16 
    */ 

    /* 
    CODE DESIGN: 
    -> Bring menu from basicMenu assingment  [X] 
    -> Adapt for this assignment     [X] 
    -> Add content for new menu style    [] 
    -> Adapt loop position from basicMenu   [] 
    -> Add interior loops       [] 
    -> Capitalize class name for *proper grammer* [X] 

    For Display Cart have three seperate totals for each item then divide by each total by its respective items total 
    to find the number of that item the customer has purchased and assign it to a varible to be added into menu print out. 

    For Print Recipt have the system call 'display cart' and add a subtotal, tax, and total printer to the end. 

    Loop with basic menu while looper. (method 'startOver'). 
    */ 
    public class CheckoutCounter { 
     public static void main(String[] args) { 
     while (startOver() == true) { 
      //Define Menu Bins 

      //Top Menu Choice Printer 
      System.out.println("1. Purchase Items"); 
      System.out.println("2. Display Cart"); 
      System.out.println("3. Print Your Recipt"); 
      System.out.println(" "); 
      //Choose 1, 2, or 3 Menu Input 
      double doubleA; 
      Scanner topMenu = new Scanner(System.in); 
      System.out.print("Please select and option, 1 - 3:"); 
      doubleA = topMenu.nextDouble(); 
      System.out.println(" "); 
      System.out.println("Good choice."); 
      System.out.println(" "); 
      //Method Chooser 
      //Menu Choice "Purchase Items" 
      if (doubleA <= 1) { 
      priceTotalDragonfruit = priceTotalDragonfruit + CheckoutCounter.purchaseItems(); 
      } 
      //Menu Choice "Print Recipt" 
      else if (doubleA >= 3) { 
      CheckoutCounter.printRecipt(); 
      } 
      //Menu Choice "Display Cart" 
      else { 
      CheckoutCounter.displayCart(); 
      } 
     } 
     } 
     //Purchase Items Method 
     public static double purchaseItems() { 
      //Define Variables and Initialize 
      double dragonfruit = 5.99; 
      double organicBlasphemy = 99.99; 
      double dayOldAnswerToTheUniverse = 100000; 
      double total = 0; 
      double multiplier; 
      //Define Total Containers and Initialize 
      double priceTotalDragonfruit = 0; 
      double priceTotalOrganicBlasphemy = 0; 
      double priceTotaldayOldAnswerToTheUniverse = 0; 
      //Top Menu Choice Printer 
      System.out.println("1. Dragonfruit ($5.99/lb)"); 
      System.out.println("2. Organic Blasphemy ($99.99 per comment)"); 
      System.out.println("3. 1 Day Old Answer to the Universe ($100,000 per request)"); 
      System.out.println(" "); 
      //Choose 1, 2, or 3 Menu Input 
      double doubleA; 
      Scanner topMenu = new Scanner(System.in); 
      System.out.print("Please select and option, 1 - 3:"); 
      doubleA = topMenu.nextDouble(); 
      System.out.println(" "); 
      System.out.println("Good choice."); 
      System.out.println(" "); 
      //Method Chooser 
      //Menu Choice "Dragonfruit" 
      if (doubleA <= 1) { 
       System.out.println("How much/many do you want?"); 
       Scanner multiplierScanner = new Scanner(System.in); 
       System.out.println("Enter amount here:"); 
       multiplier = multiplierScanner.nextDouble(); 
       System.out.println("We added your item(s) to your cart."); 
       priceTotalDragonfruit = total + (5.99 * multiplier); 
      } 
      //Menu Choice "1 Day Old Answer to the Universe" 
      else if (doubleA >= 3) { 
       System.out.println("How much/many do you want?"); 
       Scanner multiplierScanner = new Scanner(System.in); 
       System.out.println("Enter amount here:"); 
       multiplier = multiplierScanner.nextDouble(); 
       System.out.println("We added your item(s) to your cart."); 
       priceTotaldayOldAnswerToTheUniverse = total + (100000 * multiplier); 
      } 
      //Menu Choice "Organic Blasphemy" 
      else { 
       System.out.println("How much/many do you want?"); 
       Scanner multiplierScanner = new Scanner(System.in); 
       System.out.println("Enter amount here:"); 
       multiplier = multiplierScanner.nextDouble(); 
       System.out.println("We added your item(s) to your cart."); 
       priceTotalOrganicBlasphemy = total + (99.99 * multiplier); 
      } 
     } 
     //Display Cart Method 
     public static void displayCart() { 


     } 
     //Print Recipt/End Program Method 
     public static void printRecipt() { 

     } 
     //Start Over Loop Method 
     public static boolean startOver() { 
      Scanner inputScanner = new Scanner(System.in); 
      System.out.print("Please enter either 'True', to continue, or 'False', to stop: "); 
      Boolean userInputA; 
      userInputA = inputScanner.nextBoolean(); 
      boolean goAhead; 
      if (userInputA == false) { 
      goAhead = false; 
      } 
      else { 
      goAhead = true; 
      } 
      return goAhead; 
     } 
    } 
+1

哦哇。請使用封裝。這很難閱讀。 – nhouser9

+0

@ nhouser9:如果OP是新生,他的班級可能還沒有達到面向對象的編程。 (我社區學院的Java 101課程現在已經引入了這個概念。) –

+0

@ KevinJ.Chase你不需要OOP把它分解成方法雖然 – nhouser9

回答

0

對於像你這樣的學生來說,這實際上是一個很好的練習。我不會給你確切的答案(雖然有很多方法),但讓我幫你分析一下。

Java是一種OOP語言。首先要做的就是確定你的域對象是什麼。

根據您的要求,它需要一個菜單​​併爲它所做的每件事創建一個方法。 (聽起來像一個對象,是嗎?)

它看起來應該象下面這樣:

public class Menu{ 
    public void purchaseItems(){ // pass an argument or return something depending on what you need 
     // figure out how you purchase items 
    } 

    public void displayCurrentPurchases(){ 
     // figure out how you display the cart (your cart can be a List, Map, or even a class) 
    } 
    public void printReceipt(){ 
     // figure out how you print the receipt 
     //somewhere here you would need to call your computeTaxes method 
     double netAmount = computeTaxes(grossAmount); 
    } 

    /* It's a good idea to limit a method to doing only one thing as much as possible, so you might need to make private methods such as computing for taxes below */ 
    private double computeTaxes(double totalAmount){ 
     return total * 0.098; 
    } 

    // and so on... 
} 

現在,你有你的Menu對象集,您可以創建主類中實際創建的實例你的Menu

public class MainClass{ 
    public static void main(String[] args) { 
     Menu menu = new Menu(); 
     /* create your scanner here, and call the methods on the Menu depending on the user's choice */ 

     // a switch for the user's selection might be a good idea: 
     switch(selected){ 
      case 1: 
       menu.purchaseItems(); 
       break; 
      case 2: 
       // you get the point. I leave this to you. 
      default: 
       sysout("Invalid selection"); 
       break; 
     } 

    } 
} 
+0

謝謝你的建議,我可以看到我失蹤的地方封裝的概念。 –