David Jacobsen David Jacobsen - 1 month ago 8
Java Question

Non-gui checkout register menu: how to format it?

I'm stumped on how to format my menu. What I have so far isn't working but I know it could be better formatted and shorter. This is the description of the assignment. You can see that my approach isn't going to work but I don't know how to change my approach...

I'm pretty sure I need to use arrays but I'm not sure how to store a public array so any method can call and store information to it.

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;
}
}

Answer

This is actually a good exercise for a student like you. I wont give you the exact answer (there are many ways though) but let me help you analyze this.

Java is an OOP language. The first thing to do is always determine what your domain objects would be.

Based on your requirement, it is asking for a Menu and create a method for each of the things it does. (sounds like an Object, yes?)

It should look something like below:

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...
}

Now that you've got your Menu object set, you can create your Main class to actually create an instance of your 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;
       }

   }
}
Comments