2

Hey guys I have been working on this program for class the past couple hours and just cant seem to get these last 2 issues resolved. Its basically a slightly modified CashRegister class with basic functions through a menu. The problems I am having are:

1) After the user makes a menu selection for the first time, every subsequent time the menu shows up in console twice and I cant seem to find a fix for this.

2) Also whenever I choose to display the content of my CashRegister, the first line is always output as 0.00 no matter the input.

Here is my CashRegister class followed by my tester:

import java.util.ArrayList;

/** * */

/** * @author Cole * */ public class CashRegister {

   private double dailyTotal;
   private double totalPrice;
   ArrayList<Double> items;

   /**
      Constructs a cash register with cleared item count and total.

   */
   public CashRegister()
   {
      items = new ArrayList<Double>();
      dailyTotal = 0;
      totalPrice= 0;
   }

   /**
      Adds an item to this cash register.
      @param price the price of this item

   */
   public void addItem(double price)
   {
     items.add(price);
     dailyTotal = dailyTotal + price;

   }

   /**
      Gets the price of all items in the current sale.
      @return the total amount
   */
   public double getTotal()
   {
       for(int x=0; x<items.size(); x++){
          totalPrice = totalPrice + items.get(x);
      }


       return totalPrice;
   }

   /**
      Gets the number of items in the current sale.
      @return the item count
   */
   public int getCount()
   {
      return items.size(); 
   }

   /**
      Clears the item count and the total.
   */
   public void clear()
   {
      items.clear();
      totalPrice = 0;
   }

   public void display(){
       for(int x=0; x<items.size(); x++){
           System.out.printf("%10.2f%n", items.get(x));

       }
       System.out.println("------------------------------");
   }

   public double getDailyTotal(){
       dailyTotal = dailyTotal + totalPrice;
       return dailyTotal;


   }

}

import java.util.ArrayList;

import java.util.Scanner;

/** * */

/** * @author Cole * */ public class Prog2 {

/**
 * @param args
 */

public static final String MENU = "******************************************\n" +
              "* Enter \"n\" to start a new Cash Register.          *\n" +
              "* Enter \"a\" to add an item to the current Cash Register.    *\n" +
              "* Enter \"d\" to display the total of the current Cash Register.    *\n" +
              "* Enter \"e\" to exit the program.   *\n" +
              "******************************************";

    public static final String NEW_CUSTOMER = "n";
    public static final String ADD_ITEM = "a";
    public static final String DISPLAY = "d";
    public static final String EXIT = "e";



public static void main(String[] args) {
    // TODO Auto-generated method stub

    CashRegister register = new CashRegister();
    Scanner keyboard = new Scanner(System.in);
    String input;
    double userInput = 0;

    do {                                                
        input = printMenu(keyboard);                    


        if(input.equals(NEW_CUSTOMER)){
            register.getDailyTotal();
            register.clear();
        }

        else if(input.equals(ADD_ITEM)){
            System.out.println("Please enter the price of the item: ");
            register.addItem(userInput);
            userInput = keyboard.nextDouble();
        }

        else if(input.equalsIgnoreCase(DISPLAY)){

            register.display();
            System.out.println("Total: " + register.getTotal());
            System.out.println("Item Count: " +register.getCount());
        }


        else if(input.equalsIgnoreCase(EXIT)){
            System.out.printf("Daily Sales Total: " + "%.2f%n",register.getDailyTotal());
            System.out.println("Program Ended...");
            break;
            }
    }while(input != EXIT);

}

public static String printMenu(Scanner input){      //this method displays the menu for the user
    String response = "no reponse yet";

    System.out.println(MENU);
    response = input.nextLine();

    return response;        //response returned based on the users input

}

}

8
  • Can you tell us the sequence of inputs which leads to the menj showing up twice? Commented Feb 7, 2017 at 0:07
  • 1
    you can't comapre strings with == or !=, use !input.equals(EXIT) in your while logic Commented Feb 7, 2017 at 0:07
  • @RAZ_Muh_Taz Nice catch, missed that. Commented Feb 7, 2017 at 0:08
  • its whenever i consecutively try to add items to the cart Commented Feb 7, 2017 at 0:09
  • so I just threw a keyboard.nextLine(); in after userInput = keyboard.nextDouble(): and that fixed the menu problem. Now i just cant seem to find out why when i display all of the values the first one is always 0.00 Commented Feb 7, 2017 at 0:12

1 Answer 1

1

You need to get input from the user before you add the item, that's why you are getting a 0 for your first item. Since your value for userInput is set to 0 at the beginning and your statements are switched you will always initialy create an item with 0.0 for it's value and all the other values will be one step behind the actual inputs.

else if(input.equals(ADD_ITEM)){
     System.out.println("Please enter the price of the item: ");
     userInput = keyboard.nextDouble();
     register.addItem(userInput);    
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much.
feel free to mark as answer if it indeed solve your problem that way it closes the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.