帐户类错误2

时间:2015-12-14 03:29:01

标签: java arrays

我仍然无法确定最有效的方法是什么。基本上,我正在尝试为创建的Account数组中的每个对象设置balance = 0。我尝试使用for循环并为每个创建的帐户设置balance = 0,但我不确定如何使这个工作,因为在具有所有方法的类中创建了balance变量。我整天都在尝试这个问题,但没有运气。感谢。

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 个答案:

答案 0 :(得分:1)

在外部课程中,您只能以其可见(例如公开)API中公开的方式与Try this: document.getElementsByTagName("body ").addEventListener("click", function(){ gameOver(); }); function gameOver() { alert("Game Over!"); } 进行互动。

在这种情况下,目前执行此操作的方法是Account当前withdraw()

balance

虽然这个具体案例会使余额成为负数,因为你收取了提款费用(对于通话类别是隐藏的)。

如果您要在acct[i].withdraw(acct[i].getBalance()); 上公开setBalance方法,则可以改为

Account

然而仔细看,似乎你遇到的麻烦实际上是初始化帐户数组。你可以这样做:

acct[i].setBalance(0);