2015-12-14 32 views
2

我仍然无法弄清楚如何最有效的方式来做到这一点..基本上,我试图使帐户数组中创建的每个对象的平衡= 0。我尝试使用for循环,并为每个创建的帐户设置balance = 0,但由于平衡变量是在具有所有方法的类中创建的,因此我不确定如何使其工作。我一整天都在试图解决这个问题,但没有运气。谢谢。帐户类错误2

Main method: 
import java.util.Scanner; 
import java.text.NumberFormat; 
public class Account2 
{ 
    public static void main(String[] args) 
    { 
    Scanner scan = new Scanner(System.in); 
    Account[] acct = new Account[30]; 
    for (int count2; count2 < 30; count2++) 
    { 
     balance = 0; //Initial balance is always set to zero to be able to run fresh program every time 
    } 
    System.out.println("Enter your account number (1-30): "); 
    int key = scan.nextInt() - 1; 
    int reset = 0; 
    while (reset == 0) 
    { 
     System.out.println("Enter W for withdrawl; D for deposit; X to escape"); 
     char choice = scan.next().charAt(0); 

     if (choice == 'W' || choice == 'w' || choice == 'D' || choice == 'd' || choice == 'x' || choice == 'X') 
     { 
     if (choice == 'W' || choice == 'w') 
     { 
      System.out.println("Enter amount to withdraw: "); 
      Double withdraw1 = scan.nextDouble(); 
      if (withdraw1 <= acct[key].getBalance()) 
      { 
      acct[key].withdraw(withdraw1); 
      System.out.println("User # " + key++ + " funds after withdraw: " + acct[key].getBalance() + "$"); 
      System.out.println("User # " + key++ + " funds after interest: " + acct[key].addInterest() + "$"); 
      reset++; 
      } 
      else 
      System.out.println("Insufficient funds."); 
     } 

     if (choice == 'D' || choice == 'd') 
     { 
      System.out.println("Enter amount to deposit: "); 
      Double deposit1 = scan.nextDouble(); 
      if (deposit1 > 0) 
      { 
      acct[key].deposit(deposit1); 
      System.out.println("User # " + key++ + " funds after deposit: " + acct[key].getBalance() + "$"); 
      System.out.println("User # " + key++ + " funds after interest: " + acct[key].addInterest() + "$"); 
      reset++; 
      } 
      else 
      System.out.println("Use the withdrawl feature to withdrawl money."); 

     } 
     if (choice == 'x' || choice == 'X') 
      System.out.println("Thank You for using this bank."); 
      reset++; 
     } 
     else 
     { 
      System.out.println("Invalid entry, please try again"); 
      reset = 0; 
     } 
    } 
    } 
} 

支持类:

public class Account 
{ 
    private final double RATE = 0.03; //Interest is 3% 

    private int acctNumber; 
    private String name; 
    private balance; 

    //Defines owner, account number, and initial balance. 
    public Account(String owner, int account, double initial) 
    { 
    name = owner; 
    acctNumber = account; 
    balance = initial; 
    } 

    //deposits a specified amount and returns new balance 
    public double deposit(double amount) 
    { 
    balance = balance + amount; 
    return balance; 
    } 

    //withdraws the specified amount from the account and applies the fee 
    //             + returns balance 
    public double withdraw(double amount) 
    { 
    int fee = 1; 
    balance = balance - amount - fee; 
    return balance; 
    } 

    //Adds interest to the account 
    public double addInterest() 
    { 
    balance += (balance * RATE); 
    return balance; 
    } 
    public double getBalance() 
    { 
    return balance; 
    } 

    //returns a one line description of the account as a string 
    public String toString() 
    { 
    NumberFormat fmt = NumberFormat.getCurrencyInstance(); 
    return acctNumber + "/t" + name + "/t" + fmt.format(balance); 
    } 
} 

回答

1

从外部类,你只能Account在其可见的(例如公共)API曝光的方式进行交互。

在这种情况下,目前的方式做这将是withdraw()当前balance

acct[i].withdraw(acct[i].getBalance()); 

虽然这种特定情况下会把把资产负债底片,因为你收取费用,取款(其对调用类是隐藏的)。

如果你要在Account暴露出setBalance方法,你可以改为做

acct[i].setBalance(0); 

但是仔细一看,好像你有与实际初始化帐户的阵列什么麻烦。你可以这样做:

for (int count2; count2 < 30; count2++) 
{ 
    acct[count2] = new Account(owner, count2, 0); 
} 
+0

感谢您的帮助!虽然现在我得到了一个巨大的新错误流:(我想我会问我的教授关于它的明天。 – Submersed24