【问题标题】:3 cheque accounts 2 constructors for cust without overdraft but cust who does not want overdraft is getting one3 个支票账户 2 个没有透支但不想要透支的 cust 的构造函数得到一个
【发布时间】:2019-11-15 16:39:31
【问题描述】:

ChequeAccount 类扩展了 Account 类。 Account 类包含 ID、名称和余额属性。 ChequesAccount 类有 overdraftLimit、amtOverdrawn 和 transactionNo 以及来自 Account 的 super(ID, name, balance)。我在一个数组中创建了 3 个 chqAccount 对象并打印了它们的详细信息。 Ringo 在他的 chq 帐户中选择退出透支功能,因此对他来说是独立的构造函数。但是,当我打印所有支票账户的详细信息时,他是唯一获得透支工具的人。它让我头疼,请帮助

public class TestAccounts6
{
   private static ChequeAccount[] chqAccount = new ChequeAccount[5];
   private static int indexNo = 0;

   public static void main(String[] args)
   {
      ChequeAccount c1 = new ChequeAccount("S1111", "Paul", 1245.00, 0, 0, 0);
      ChequeAccount c2 = new ChequeAccount("S2222", "Ringo", 2500.00);
      ChequeAccount c3 = new ChequeAccount("S3333", "John", 1575.00, 0, 0, 0);
      chqAccount[0] = c1;
      chqAccount[1] = c2;
      chqAccount[2] = c3;
      indexNo = 3;
      System.out.printf("%-10s%-10s%-10s%-10s%-10s%-10s%n", "ID", "Name", "Balance",
                        "Overdraft", "Amount", "No of");
      System.out.printf("%-10s%-10s%-10s%-10s%-10s%-10s%n", "", "", "",
                        "Limit", "Overdrawn", "Transactions\n");
      for (int i = 0; i < indexNo; i++)
      {
         chqAccount[i].print();
      }

   }

}
public class ChequeAccount extends Account
{
   protected double overdraftLimit = 10000;
   protected double amtOverdrawn = 0;
   protected int transactionNo = 0;

   // constructor
   public ChequeAccount(String ID, String name, double balance,
                        double overdraftLimit, double amtOverdrawn,
                        int transactionNo)
   {
      super(ID, name, balance);
      this.overdraftLimit = overdraftLimit;
      this.amtOverdrawn = amtOverdrawn;
      this.transactionNo = transactionNo;
   }

   public ChequeAccount(String ID, String name, double balance)
   {
      super(ID, name, balance);
   }

   public void print()
   {

      System.out.printf("%-10s%-10s$%-9.2f$%-9.2f$%-9.2f%-10d%n", ID, name,
                        balance, overdraftLimit, amtOverdrawn, transactionNo);
   }

}
public class Account
{
   protected String ID;
   protected String name;
   protected double balance;

   // Constructor
   public Account(String ID, String name, double balance)
   {
      this.ID = ID;
      this.name = name;
      this.balance = balance;
   }

预计林戈不会获得透支便利,而约翰和保罗会。与预期相反

【问题讨论】:

    标签: java inheritance constructor superclass


    【解决方案1】:

    这是代码的预期行为。

    public class ChequeAccount extends Account
    {
       protected double overdraftLimit = 10000;
       protected double amtOverdrawn = 0;
       protected int transactionNo = 0;
       ...
    }
    

    您已为这些变量分配了默认值。当您调用 Ringo 的构造函数时,这些变量不会更新,因此将使用您分配的默认值。

    使用 PaulJohn 您正在为这些变量分配新值(全为 0),以便将它们打印出来。

    ChequeAccount c1 = new ChequeAccount("S1111", "Paul", 1245.00, 10000, 0, 0);
    ChequeAccount c2 = new ChequeAccount("S2222", "Ringo", 2500.00);
    ChequeAccount c3 = new ChequeAccount("S3333", "John", 1575.00, 10000, 0, 0); 
    
    public class ChequeAccount extends Account
    {
       protected double overdraftLimit = 0;
       protected double amtOverdrawn = 0;
       protected int transactionNo = 0;
       ...
    }
    

    使用上述更改修改您的代码现在将显示 PaulJohn 的透支限制大于零,同时使 Ringo 的 限制为 0 .

    【讨论】:

      【解决方案2】:

      它的行为正确。因为

      1. 您已经创建了 3 个 chqAccount 对象

      ChequeAccount c1 = new ChequeAccount("S1111", "Paul", 1245.00, 0, 0, 0);

      ChequeAccount c2 = new ChequeAccount("S2222", "Ringo", 2500.00);

      ChequeAccount c3 = new ChequeAccount("S3333", "John", 1575.00, 0, 0, 0);

      1. 当您尝试打印此对象的值时。 它正在按以下方式打印。

      ID名称余额透支金额
      限制透支交易

      S1111 保罗 $1245.00 $0.00 $0.00 0 S2222
      林戈 $2500.00 $10000.00 $0.00 0 S3333 约翰
      $1575.00 $0.00 $0.00 0

      当我们观察上述情况并检查 Ringo 的详细信息时。它的余额为:$10000.00,因为您没有为 Ringo 分配任何值。但在 ChequeAccount 中如下所示。

        protected double overdraftLimit = 10000;
        protected double amtOverdrawn = 0;
        protected int transactionNo = 0;
      

      它有默认值。以便在打印时采用默认值。

      【讨论】:

      • 感谢 Raju,所以我会使用评估员为个人客户设置透支限额
      猜你喜欢
      • 2015-09-01
      • 1970-01-01
      • 2021-07-03
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多