2017-02-06 9 views
2

こんにちは私は過去数時間のこのクラスでこのプログラムに取り組んでいて、最後の2つの問題が解決されたようです。その基本的には、基本的な機能をメニューからわずかに変更したCashRegisterクラスです。私が抱えている問題は次のとおりです。入力後にJavaメニューが2回表示される

1)ユーザーが最初にメニューを選択した後、メニューがコンソールに2回表示されたたびに、これに対して修正が見つかるようです。

2)また、自分のCashRegisterのコンテンツを表示するように選択すると、最初の行は入力に関係なく常に0.00として出力されます。ここで

は私のテスターに​​続く私のCashRegisterクラスです:

*
import java.util.ArrayList; 

/** */

/** *コール @author * */ パブリッククラス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 * */ パブリッククラス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 

} 

}

+0

menjが2回表示される入力順序を教えてください。 –

+1

あなたは==や!=で文字列を完成させることはできません。あなたのwhileロジックで!input.equals(EXIT)を使用してください。 –

+0

@RAZ_Muh_Taz –

答えて

1

あなたは取得する必要がありますアイテムを追加する前にユーザーから入力してください。そのため、最初のアイテムに0が表示されます。最初はuserInputの値が0に設定されており、ステートメントが切り替わると、値が0.0のアイテムが常に最初に作成され、他のすべての値は実際の入力より1ステップ遅れて表示されます。

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

ありがとうそんなに。 – Strayfire

+0

本当にあなたの問題を解決する場合は、質問を閉じるように答えとしてマークしてください –

関連する問題