我的程序有问题,这是我写的另一个程序的子类,叫做account。 我创建一个帐户(乔治)并为其设置值,一个名称,int id和双重余额的字符串。 我没有收到任何错误,但唯一正确的值是name的字符串。 我认为它必须在帐户类中使用我的Overridden 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 withdrawalCustomerAccount.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 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; } }Account.class:
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: "; } }I'm having issues with my program which is a subclass of another program I wrote called account. I create an account (george) and set values to it, a string for name, int id, and double balance. I'm not getting any errors but the only correct value is the string for name. I think it has to do something with my Overridden toString() method in the account class not grabbing the correct values.
My output:
Account holder name: George Account id: 0 Annual interest: 1.5 Monthly Interest Rate: 0.00125 Balance 109.0 Transaction:Correct ouput:
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 withdrawalCustomerAccount.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>(); }Transaction 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; } }Account.class :
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类中设置值,您需要调用
super(id, balance)在CustomerAccount构造函数中调用Account的构造函数
试试吧
To set the values in the Account class, you need to call
super(id, balance)in the CustomerAccount constructor to call the constructor for Account
Try that
更多推荐
发布评论