任务是在创建银行账户时使用继承创建不同的类。然后我们存入,提取和报告余额。我有4个班级:
超类:BankAccount
子类:检查帐户
子类:储蓄账户
方法类:BankApp
BankAccount超类:
public class BankAccount {
String firstName;
String lastName;
String ssn;
protected float balance;
float withdraw;
float deposit;
long accountNumber;
BankAccount (){
}
BankAccount(String firstName, String lastName, String ssn, float balance){
this.firstName = firstName;
this.lastName = lastName;
this.ssn = ssn;
this.balance = balance;
}
long accountNumber() {
long accountNumber = (long) Math.floor(Math.random() * 9000000000L) + 1000000000L;
return accountNumber;
}
public void deposit(float amount) {
balance = balance + amount;
System.out.println(firstName + " " + lastName + " deposited $" + deposit + ". Current Balance $" + balance);
}
public void withdraw(float amount) {
if (balance >= withdraw) {
balance = balance - amount;
System.out.println(firstName + " " + lastName + " withdrew $" + withdraw + ". Current Balance $" + balance);
}
if (balance < withdraw) {
System.out.println("Unable to withdraw " + amount + " for " + firstName + " " + lastName + " due to insufficient funds.");
}
}
}
检查帐户子类:
public class CheckingAccount extends BankAccount {
float amtInterest;
float applyInterest;
String displayBalance;
public CheckingAccount() {
}
public CheckingAccount(String firstName, String lastName, String ssn, float balance) {
super(firstName, lastName, ssn, balance);
System.out.println("Successfully created account for " + firstName + " " + lastName + " " + accountNumber);
System.out.println(firstName + " " + lastName + ", Balance $" + balance);
}
float applyInterest () {
if (balance <= 10000) {
balance = balance * 0.1f;
}
if (balance > 10000) {
balance = 1000 + (balance * 0.02f);
}
return balance;
}
float displayBalance() {
return balance;
}
}
我省略了SavingsAccount SubClass,因为我可以在这两个类的帮助下调整该类。
输出:
Successfully created account for Alin Parker 0 //Not displaying a random account number (1)
Alin Parker, Balance $1000.0
Successfully created account for Mary Jones 0
Mary Jones, Balance $500.0
Successfully created account for John Smith 0
John Smith, Balance $200.0
Alin Parker deposited $0.0. Current Balance $23000.0 //Deposit being calculated but displayed as 0 (2)
Mary Jones deposited $0.0. Current Balance $12500.0
Alin Parker withdrew $0.0. Current Balance $21000.0 //Withdrawal being calculated but displayed as 0 (3)
Mary Jones withdrew $0.0. Current Balance $11500.0
Alin Parker withdrew $0.0. Current Balance $-28580.0 //Should not show negative balance and only notice below (4)
Unable to withdraw 30000.0 for Alin Parker due to insufficient funds
我在输出中概述了我的上述问题:
1)随机化的10位数帐号显示为0
2)存款金额被扣除,但显示为0
3)扣除金额,但显示为0
4)应显示“资金不足”的陈述,而不是负余额
5)主要方法具有未执行的功能:
public class BankApp {
public static void main(String[] args) {
CheckingAccount acct1 = new CheckingAccount("Alin", "Parker", "123-45-6789", 1000.0f);
CheckingAccount acct2 = new CheckingAccount("Mary", "Jones", "987-65-4321", 500.0f);
SavingsAccount acct3 = new SavingsAccount("John", "Smith", "1233-45-6789", 200.0f);
acct1.deposit(22000.00f);
acct2.deposit(12000.00f);
acct1.withdraw(2000.00f);
acct2.withdraw(1000.00f);
acct1.applyInterest(); //seems to skip
acct2.applyInterest(); //seems to skip
acct1.displayBalance(); //seems to skip
acct2.displayBalance(); //seems to skip
acct1.withdraw(30000.00f);
}
}
提前感谢您的帮助
答案 0 :(得分:0)
问题在于:
public void withdraw(float amount) {
if (balance >= withdraw) {
balance = balance - amount;
System.out.println(firstName + " " + lastName + " withdrew $" + withdraw + ". Current Balance $" + balance);
}
if (balance < withdraw) {
System.out.println("Unable to withdraw " + amount + " for " + firstName + " " + lastName + " due to insufficient funds.");
}
}
解决方案是:
public void withdraw(float amount) {
if (balance >= amount) {
balance = balance - amount;
System.out.println(firstName + " " + lastName + " withdrew $" + amount + ". Current Balance $" + balance);
}
if (balance < amount) {
System.out.println("Unable to withdraw " + amount + " for " + firstName + " " + lastName + " due to insufficient funds.");
}
}