2017-05-08 57 views
0

有兩件事我需要幫助。一個是在輸出中四捨五入的問題,另一個是找到一個更好的方式來編寫我的程序,如果有必要,輸出相同的結果。PiggyBank程序與構造函數,增變器和訪問器

什麼是寫這個程序最有效的方法?儘管它的工作原理應該如此,但我知道它並不是最好的。

package program2; 

import java.util.*; 

class PiggyBank 
{ 
    Scanner console = new Scanner(System.in); 

    private int numPennies, numNickles, numDimes, numQuarters; 
    private float total; 

    public PiggyBank(int pennies, int nickles, int dimes, int quarters) 
    { 
     numPennies = pennies; 
     numNickles = nickles; 
     numDimes = dimes; 
     numQuarters = quarters; 
     total = (float) 0.00; 
    } 

    public void addPennies(int pennies) 
    { 
     System.out.println("Have entered " + pennies + " pennies"); 

     if (pennies < 0) 
     { 
      System.out.println("No Pennies Added"); 
     } 
     else 
     { 
      numPennies = numPennies + pennies; 
      total = (float) (total + pennies * 0.01); 
     } 
    } 

    public void addNickles(int nickles) 
    { 
     System.out.println("Have entered " + nickles + " nickles"); 

     if (nickles < 0) 
     { 
      System.out.println("No Nickles Added"); 
     } 
     else 
     { 
      numNickles = numNickles + nickles; 
      total = (float) (total + nickles * 0.05); 
     } 
     System.out.println("Bank has $" + total + " in it"); 
     System.out.println(); 
    } 

    public void addDimes(int dimes) 
    { 
     System.out.println("Have entered " + dimes + " dimes"); 

     if (dimes < 0) 
     { 
      System.out.println("No Dimes Added"); 
     } 
     else 
     { 
      numDimes = numDimes + dimes; 
      total = (float) (total + dimes * 0.10); 
     } 
     System.out.println("Bank has $" + total + " in it"); 
     System.out.println(); 
    } 

    public void addQuarters(int quarters) 
    { 
     System.out.println("Have entered " + quarters + " quarters"); 

     if (quarters < 0) 
     { 
      System.out.println("No Quarters Added"); 
     } 
     else 
     { 
      numQuarters = numQuarters + quarters; 
      total = (float) (total + quarters * 0.25); 
     } 
    } 

    public float getContents() 
    { 
     return total; 
    } 

    public final int breakTheBank() 
    { 
     if (total >= 0) 
     { 
      total = 0; 
     } 

     return (int) total; 
    } 

} 

public class PiggyBankTester 
{ 

    public static void main(String[] args) 
    { 
     Scanner console = new Scanner(System.in); 

     System.out.print("Program By "); 
     String name = console.next(); 
     System.out.println(); 

     test(); 
    } 

    public static void test() 
    { 
     PiggyBank bank = new PiggyBank(0, 0, 0, 0); 

     bank.addNickles(3); 

     bank.addPennies(4); 
     System.out.println("Bank has $" + bank.getContents() + " in it \n"); 

     bank.addPennies(-18); 
     System.out.println("Bank has $" + bank.getContents() + " in it \n"); 

     bank.addDimes(2); 
     bank.addQuarters(3); 
     System.out.println("Bank has $" + bank.getContents() + " in it \n"); 

     bank.addQuarters(-3); 
     System.out.println("Bank has $" + bank.getContents() + " in it \n"); 

     System.out.println("Broke the bank and got $" + bank.getContents() + " from it \nBank has $" + bank.breakTheBank() + " in it"); 
    } 
} 

這是我的輸出示例。浮點數總計取得了一些結果,但我不知道如何讓它圍繞所有結果。

Program By JakeBrono46 

Have entered 3 nickles 
Bank has $0.15 in it 

Have entered 4 pennies 
Bank has $0.19000001 in it 

Have entered -18 pennies 
No Pennies Added 
Bank has $0.19000001 in it 

Have entered 2 dimes 
Bank has $0.39000002 in it 

Have entered 3 quarters 
Bank has $1.14 in it 

Have entered -3 quarters 
No Quarters Added 
Bank has $1.14 in it 

Broke the bank and got $1.14 from it 
Bank has $0 in it 

我沒有使用另一個站點來查找訪問器和增變器的結構。我不認爲我錯過了太重要的東西,但我現在無法想到我還需要做什麼。

+0

清理完代碼格式。主要是在那裏,只是錯過了一對「}」。小拼字。添加標點符號。刪除關於問題可以理解,不相關的行。 – mateuscb

回答

0

找到一個更好的方式來寫我的程序輸出相同的結果

import java.util.*; 
import java.text; 

class PiggyBank 
{ 
Scanner console = new Scanner(System.in); 
NumberFormat formatter = NumberFormat.getCurrencyInstance(); 

private int numPennies, numNickles, numDimes, numQuarters; 
private float total; 

public PiggyBank(int pennies, int nickles, int dimes, int quarters) 
{ 
    numPennies = pennies; 
    numNickles = nickles; 
    numDimes = dimes; 
    numQuarters = quarters;  
    total = 0.00; 
} 

我會使用NumberFormat類的文本包。通過這種方式,您可以將數字格式設置爲默認貨幣(在這種情況下爲美元)。自從您將其聲明爲浮點實例變量以來,您也不需要將浮點數轉換爲總變量。

bank.addPennies(4);  
System.out.println("Bank has" + formatter.format(bank.getContents()) + " in it \n"); 

您使用格式化像這樣打印的格式$#。##,讓您避免明確增加一個美元符號。

+0

感謝您的協助! – JakeBrono46

0
public final int breakTheBank()` 
{ 
if(total >= 0) 
{ 
    total = 0; 
} 

return (int) total; 
} 

我只想改變if語句if(total == 0),因爲你並不需要檢查它是否是0,如果你打算把它改爲0反正。除此之外,您可以做出的改變並不多。

+0

感謝您的協助! – JakeBrono46

0

一個與輸出

這裏舍入的一個問題是,我傾向於四捨五入十進制數字時使用,所以當你輸出總只是把它的方法。更多信息可以在這裏找到有關舍入數字: Round a double to 2 decimal places

public static double round(double value, int places) { 
    if (places < 0) throw new IllegalArgumentException(); 

    BigDecimal bd = new BigDecimal(value); 
    bd = bd.setScale(places, RoundingMode.HALF_UP); 
    return bd.doubleValue(); 
} 

找到一個更好的方式來寫我的程序輸出相同的結果

如果我的任務是要做到這一點我會使用枚舉,你可以爲每個硬幣創建一個枚舉值。如果你不確定如何使用枚舉來查看更多信息,https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

你想要在你的情況下做什麼是創建一個名爲Coins的枚舉,其中是一個浮點值那枚硬幣。然後爲實例addCoin創建另一個方法,其中需要枚舉枚舉和額外金額。然後通過訪問枚舉中硬幣的值並將其乘以所添加的金額來計算結果。

+0

謝謝您的保證 – JakeBrono46

相關問題