gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:11:43 25 4
gpt4 key购买 nike

我的程序有问题,该程序是我编写的另一个名为 account 的程序的子类。我创建一个帐户 (george) 并为其设置值、名称字符串、int id 和双倍余额。我没有收到任何错误,但唯一正确的值是名称字符串。我认为它必须对帐户类中的 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 withdrawal

客户帐户.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;
}
}

帐户.类:

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 的构造函数

试试这个

关于java - 创建帐户(对象)时输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46496838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com