2017-01-24 198 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

感谢您的建议和帮助。我很感激! –

相关问题