调用时如何制作对象数组以创建新帐户? [英] How to make an array of objects that creates new accounts when called?

查看:57
本文介绍了调用时如何制作对象数组以创建新帐户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编程任务方面的帮助。 BankAccount类已经存在并且可以正常工作。我以前从未被要求将对象放入数组中,而且这样做很麻烦。

I need help on a programming assignment. The class of BankAccount already exists and works as it should. I have never been asked to place objects into arrays before, and am having trouble doing so.

我从以下内容开始:

   public class Bank
   {
   private BankAccount[] Bank;

   public Bank(BankAccount[] Bank)
   {
      BankAccount[] b1 = new BankAccount[10];
   }

尽管可以编译,但这是错误的。我不确定要去哪里。

Although it compiles, it is wrong. I am not sure where to go.

以下是我目前所坚持的代码要求。

The following are the requirements of the code that I am currently stuck on.


  • Bank类的对象最多可容纳10个BankAccount对象。

  • Bank对象的构造函数将创建一个最多容纳10个数组BankAccount对象。

以下代码是我们的教授在必须使用的作业中包含的测试程序:

The following code is the test program that our professor included with the assignment that we must use:

System.out.println("\nCreate bank1");
Bank bank1 = new Bank();
System.out.println("\nOne account");
BankAccount b1 = new BankAccount("Joe Mac", 1234);
b1.adjust(1000.0);
bank1.addAccount(b1);
bank1.printAccounts();
System.out.println("\nTwo accounts");
BankAccount b2 = new BankAccount("Sally Ride", 2345);
b2.adjust(2000.0);
bank1.addAccount(b2);
bank1.printAccounts();
System.out.println("\nThree accounts");
BankAccount b3 = new BankAccount("Pat Armstrong", 3456);
b3.adjust(3000.0);
bank1.addAccount(b3);
bank1.printAccounts();
System.out.println("\nMonthly Fee");
bank1.monthlyFee(fee);
bank1.printAccounts();
System.out.println("\nErrors:");

我们将不胜感激。谢谢。

Help would be immensely appreciated. Thank you.

推荐答案

您只需要添加一种方法来添加新帐户并测试它不会变得更大。小于10:

You just have to add a method to add a new account and test that it doesn't go more bigger than 10:

 public class Bank
   {
   private BankAccount[] accounts; //Don't name variables with uppercase
   private int accountsPointer; //This is going to keep track of how many accounts are there in the array

   public Bank() //The constructor doesn't need to accept any bank account since it'll start as empty
   {
    accounts = new BankAccount[10]; //Here we initialize the array
    accountsPointer = 0; //Here the pointer starts as 0 since the array is empty
   }
   public void addAccount(BankAccount account){
       accountsPointer++;
       if(accountsPointer<10){ //We test it here so it won't throw an out of bounds exeption
           accounts[accountsPointer-1]=account;//It assigns the account to the next empty space on the array
       }
   }

这篇关于调用时如何制作对象数组以创建新帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆