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

Java 创建帐户(对象)时输出错误,java,Java,我的程序有问题,它是我写的另一个程序account的子类。我创建了一个帐户(george)并为其设置了值,一个用于名称、int id和双余额的字符串。我没有得到任何错误,但唯一正确的值是name的字符串。我认为这与account类中被重写的toString()方法没有获取正确的值有关 我的输出: Account holder name: George Account id: 0 Annual interest: 1.5 Monthly Interest Rate: 0.00125 Balance

我的程序有问题,它是我写的另一个程序account的子类。我创建了一个帐户(george)并为其设置了值,一个用于名称、int id和双余额的字符串。我没有得到任何错误,但唯一正确的值是name的字符串。我认为这与account类中被重写的toString()方法没有获取正确的值有关

我的输出:

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

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 
CustomerAccount.class:

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>();
}
class Account {
    private int id = 0;
    private double balance = 0.0;
    private double annualInterestRate = 0.0;
    private java.util.Date dateCreated;

// set dateCreated for the time and date the account was created
Account() {
    dateCreated = new java.util.Date();
}

// Set accounts id and balance equal to this objects
Account(int id, double balance){
    this();
    this.id = id;
    this.balance = balance;
}

// Getters and Setters manipulating variables to be set to the created account
// Setters have no return value and set itself equal to the variable and getters 
// grab the variables and return them.
public int getId() {
    return this.id;
}

public double getBalance() {
    return this.balance;
}

public double getannualInterestRate() {
    return this.annualInterestRate;
}

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

public void setId(int id) {
    this.id = id;
}

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

public void setannualInterest(double annualInterestRate) {
    this.annualInterestRate = annualInterestRate;
}

public double getMonthlyInterestRate() {
    return (annualInterestRate / 100) / 12;
}

public double getMonthlyInterest() {
    return balance * getMonthlyInterestRate();
}

// set balance of withdraw to balance - amount = balance
public void withdraw (double amount) {
    if(amount < balance) {
        balance -= amount;
    }
}

// set balance of deposit to balance + amount = balance
public void deposit(double amount) {
    balance += amount;
    //Transaction.add(new Transaction('D', amount, balance, "Deposit to account"));
}   

@Override
public String toString() {
    return "Account holder name: " + CustomerAccount.name + 
            "\nAccount id: " + id + 
            "\nAnnual interest: " + this.getannualInterestRate() + 
            "\nMonthly Interest Rate: " + this.getMonthlyInterestRate() + 
            "\nBalance " + this.getBalance() +
            "\nTransaction: ";
}
}
Account.class:

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>();
}
class Account {
    private int id = 0;
    private double balance = 0.0;
    private double annualInterestRate = 0.0;
    private java.util.Date dateCreated;

// set dateCreated for the time and date the account was created
Account() {
    dateCreated = new java.util.Date();
}

// Set accounts id and balance equal to this objects
Account(int id, double balance){
    this();
    this.id = id;
    this.balance = balance;
}

// Getters and Setters manipulating variables to be set to the created account
// Setters have no return value and set itself equal to the variable and getters 
// grab the variables and return them.
public int getId() {
    return this.id;
}

public double getBalance() {
    return this.balance;
}

public double getannualInterestRate() {
    return this.annualInterestRate;
}

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

public void setId(int id) {
    this.id = id;
}

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

public void setannualInterest(double annualInterestRate) {
    this.annualInterestRate = annualInterestRate;
}

public double getMonthlyInterestRate() {
    return (annualInterestRate / 100) / 12;
}

public double getMonthlyInterest() {
    return balance * getMonthlyInterestRate();
}

// set balance of withdraw to balance - amount = balance
public void withdraw (double amount) {
    if(amount < balance) {
        balance -= amount;
    }
}

// set balance of deposit to balance + amount = balance
public void deposit(double amount) {
    balance += amount;
    //Transaction.add(new Transaction('D', amount, balance, "Deposit to account"));
}   

@Override
public String toString() {
    return "Account holder name: " + CustomerAccount.name + 
            "\nAccount id: " + id + 
            "\nAnnual interest: " + this.getannualInterestRate() + 
            "\nMonthly Interest Rate: " + this.getMonthlyInterestRate() + 
            "\nBalance " + this.getBalance() +
            "\nTransaction: ";
}
}
类帐户{
私有int id=0;
私人双余额=0.0;
私人双年度利率=0.0;
private java.util.Date创建日期;
//设置创建帐户的时间和日期的创建日期
账户(){
dateCreated=new java.util.Date();
}
//将帐户id和余额设置为等于此对象
账户(int id,双倍余额){
这个();
this.id=id;
这个平衡=平衡;
}
//操作要设置到已创建帐户的变量的getter和setter
//setter没有返回值,将自身设置为等于变量和getter
//获取变量并返回它们。
公共int getId(){
返回此.id;
}
公共双getBalance(){
还这个。结余;
}
公共双getAnnualInterestate(){
返回此.annualInterestate;
}
公共字符串getDateCreated(){
返回此.dateCreated.toString();
}
公共无效集合id(内部id){
this.id=id;
}
公共余额(双倍余额){
这个平衡=平衡;
}
公共无效设置年度利益(双年度利益){
this.annualInterestate=annualInterestate;
}
公共双GetMonthlyInterestate(){
回报(年度利率/100)/12;
}
公共双getMonthlyInterest(){
返回余额*GetMonthlyInterestate();
}
//将提取的余额设置为余额-金额=余额
公开作废取款(双倍金额){
如果(金额<余额){
余额-=金额;
}
}
//将存款余额设置为余额+金额=余额
公共作废保证金(双倍金额){
余额+=金额;
//交易。添加(新交易('D',金额,余额,“账户存款”);
}   
@凌驾
公共字符串toString(){
return“账户持有人名称:”+CustomerAccount.name+
“\n计数id:”+id+
“\n年度利息:”+this.getAnnualInterestate()+
“\n月利率:”+this.getMonthlyInterestate()+
“\n平衡”+此.getBalance()+
“\n交易:”;
}
}

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

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


试试看

正确的输出应该是什么?您在这里发布了很多代码。您应该努力将其减少到a。当您在main中将AnnualInterestate设置为1.5时,为什么预期输出为0.015?如果您希望它以百分比而不是十进制显示,那么在getterYeah中不需要这样做。我需要找到一种方法,在每次激活提款/存款方法时将事务添加到数组中。然后我计划在以后使用for循环打印这些内容,但我希望在继续之前获得上述所有事务的正确输出。尝试Account类中受保护的更改,并让我知道这是否修复了您的输出。因此,将它们更改为Account类中受保护的对我的输出没有影响,仍然是相同的值。