我難以理解如何格式化菜單。到目前爲止,我的工作並不成功,但我知道它可以更好地格式化和縮短。 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;
}
}
哦哇。請使用封裝。這很難閱讀。 – nhouser9
@ nhouser9:如果OP是新生,他的班級可能還沒有達到面向對象的編程。 (我社區學院的Java 101課程現在已經引入了這個概念。) –
@ KevinJ.Chase你不需要OOP把它分解成方法雖然 – nhouser9