创建帐户时输出错误(对象)

时间:2017-09-29 20:59:44

标签: java

我的程序有问题,这是我写的另一个程序的子类,叫做account。我创建一个帐户(乔治)并为其设置值,一个名称,int id和双重余额的字符串。我没有收到任何错误,但唯一正确的值是name的字符串。我认为它必须在帐户类中使用我的Overridden toString()方法,而不是抓取正确的值。

我的输出:

B  b1    b2      b3 
    X  Y  X   Y   X  Y        
A                      
a1  1  2  3   4   5   6
a2  7  8  9  10  11  12

正确的输出:

Account holder name: George
Account id: 0
Annual interest: 1.5
Monthly Interest Rate: 0.00125
Balance 109.0
Transaction: 

CustomerAccount.class:

Account holder name: George
Account id: 1122
Annual interest: 0.015
Monthly Interest Rate: 0.013862499999999998
balance: 1109.0
Transaction:
type: d amount: 30.0 balance: 1030.0 deposit
type: d amount: 40.0 balance: 1070.0 deposit
type: d amount: 50.0 balance: 1120.0 deposit
type: w amount: 5.0 balance: 1115.0 withdrawal
type: w amount: 4.0 balance: 1111.0 withdrawal
type: w amount: 2.0 balance: 1109.0 withdrawal 

交易类:

public class CustomerAccount extends Account {

public static String name;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    CustomerAccount george = new CustomerAccount("George", 1122, 1000.0);
    george.setannualInterest(1.5);
    Account c1 = george;

    george.deposit(30);
    george.deposit(40);
    george.deposit(50);

    george.withdraw(5);
    george.withdraw(4);
    george.withdraw(2);

    System.out.println(c1.toString());

    CustomerAccount john = new CustomerAccount("John", 1123, 500);
    john.setannualInterest(2.5);
    Account c2 = john;

    ArrayList<CustomerAccount> sort = new ArrayList<CustomerAccount>();
    //sort.add(c1);
    //sort.addAll(c2);

}

    CustomerAccount(String name, int id, double balance) {
        this.name = name;
        id = getId();
        balance = getBalance();
}

   ArrayList<Transaction> transactions = new ArrayList<Transaction>();
}

Account.class:

class Transaction extends Account {
    private java.util.Date dateCreated;
    private char type;
    private double amount;
    private double balance;
    private String description;
    private double transaction;

Transaction() {

}

Transaction(char type, double amount, double balance, String description) {
    dateCreated = new java.util.Date();
    this.type = type;
    this.amount = amount;
    this.balance = balance;
    this.description = description;
}

public String getDateCreated() {
    return dateCreated.toString();
}

public char getType() {
    return type;
}

public double getAmount() {
    return amount;
}

public double getBalance() {
    return balance;
}

public String getDescription() {
    return description;
}

public double getTransaction() {
    return this.transaction;
}

public void setType(char type) {
    this.type = type;
}

public void setAmount(double amount) {
    this.amount = amount;
}

public void setBalance(double balance) {
    this.balance = balance;
}

public void setDescription(String description) {
    this.description = description;
}

public void setTransaction(double amount) {
    this.transaction = amount;
}
}

1 个答案:

答案 0 :(得分:1)

要设置Account类中的值,您需要调用

super(id, balance)
在CustomerAccount构造函数中调用Account

的构造函数

试试