2017-01-24 241 views
-3

我有糟糕的編碼習慣,我從糟糕的代碼中挑選出來的,但是我正在嘗試解決這些問題。我的主要問題是試圖通過構造函數傳遞最初的參數,我沒有在Java中編寫一年的代碼,所以讓我知道一切都是錯誤的!將參數傳遞給構造函數

public class AccountHolder { 

    public static void main(String args[]) 
    { 
     //Introduce scanner 
      Scanner sc = new Scanner(System.in); //Used to take input from user 
      System.out.println("Welcome to the bank program! Can you tell me your current balance?"); 

     input = sc.nextDouble(); 
     AccountHolder(input); 
     } 

     // Introduce private field members 
     private static double annualInterestRate; //Constant to hold annual interest rate 
     private static double fee; // Constant to hold the withdrawal fee 
     private double balance; // variable to hold the balance 
     private double rateUpdate; // variable to hold the value to update the rate 
     private static double input; // variable to hold user input 
     private double test; // variable to test whether or not user will drop below $100. 

     // introduce a DecimalFormat object 
     DecimalFormat twoPlace = new DecimalFormat("0.00"); // Used to keep values to 2 significant figures. 

     // Introduce public methods 

     public AccountHolder(double input) 
     { 
      balance = input; 
     } 

     public void deposit(double input) 
     { 
      balance = balance + input; 
      System.out.println("Your new balance is: $" + twoPlace.format(balance)); 
     } 

     public void withdrawl(double input) 
     { 
      test = balance; 
      balance = balance - input; 

      if (balance < 100.0) 
      { 
       balance = balance + input; 
       System.out.println("Your balance is not allowed to drop below $100.00. Please try again when you have more funds."); 
      } 

      if (test >= 500 && balance < 500) 
      { 
       balance = balance - fee; 
       System.out.println("You have been charged an additional $50 for dropping below $500.00."); 
      } 

      System.out.println("Your new balance is: $" + twoPlace.format(balance)); 
     } 



     public void monthlyInterest() 
     { 
      balance += balance * (annualInterestRate/12.0); 
     } 

     public static void modifyMonthlyInterest(double rateUpdate) 
     { 
      annualInterestRate = rateUpdate; 
      while (annualInterestRate <= 0 || annualInterestRate >= 1.0) 
      { 
       System.out.println("Error! Interest rates must be between 0 and 1. We need to keep our money!"); 
       annualInterestRate = sc.nextDouble(); 
      } 

     } 

     public String toString() 
     { 
      return String.format("$%.2f", balance); 

     } 



} 
+0

您應該將此問題移至http://codereview.stackexchange.com/並更準確地說明您遇到的問題。 –

回答

1

這是你的構造:

public AccountHolder(double input) { 
     balance = input; 
} 

和要傳遞的參數,如:

AccountHolder(input); 

你缺少使用關鍵字實際創建一個新的實例的那類...

like

AccountHolder myHolder = new AccountHolder(input); 
+0

此外,您應該考慮使用整數變量來跟蹤美分數量,而不是使用雙變量來跟蹤美元數量(或您使用的任何貨幣單位)。你會發現使用雙打後會導致大量的舍入和截斷頭痛。 – FredK

+0

謝謝....可能是,我沒有做代碼檢查,只專注於構造相關的問題... –

+0

謝謝你的幫助和建議!我非常感激。 此外,我們必須使用雙重的這個項目,但我一定會牢記個人編程。 –

2

代替

AccountHolder(input); 

你需要做的

new AccountHolder(input); 

當你缺乏 「新的」,它被解釋爲一個方法調用。用「新」它被解釋爲對構造函數的調用。 PS:我想建議你看看變量的範圍。例如。你可以在「main」方法中定義「輸入」變量,而不是靜態類變量。這可以提高代碼的可讀性。

+0

感謝您的建議和幫助。我很感激! –

相關問題