JAVA中的银行账户申请
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25347274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bank Account Application in JAVA
提问by vs-works
The task is to create different classes using inheritance in creating bank accounts. We then deposit, withdraw and report balances. I have 4 classes:
任务是在创建银行账户时使用继承创建不同的类。然后我们存款、取款和报告余额。我有4个班级:
Superclass: BankAccount
Subclass: Checking Account
Subclass: Savings Account
Method class: BankApp
超类:BankAccount
子类:支票账户
子类:储蓄账户
方法类:BankApp
BankAccount Superclass:
银行账户超类:
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.");
}
}
}
Checking Account Subclass:
支票账户子类:
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;
}
}
I omitted the SavingsAccount SubClass because I can adjust that class with the help you guys can give me on these two classes.
我省略了 SavingsAccount 子类,因为我可以在你们在这两个类上给我的帮助下调整该类。
Output:
输出:
Successfully created account for Alin Parker 0 //Not displaying a random account number (1)
Alin Parker, Balance 00.0
Successfully created account for Mary Jones 0
Mary Jones, Balance 0.0
Successfully created account for John Smith 0
John Smith, Balance 0.0
Alin Parker deposited 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. Current Balance 000.0 //Deposit being calculated but displayed as 0 (2)
Mary Jones deposited 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.");
}
}
.0. Current Balance 500.0
Alin Parker withdrew 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.");
}
}
.0. Current Balance 000.0 //Withdrawal being calculated but displayed as 0 (3)
Mary Jones withdrew ##代码##.0. Current Balance 500.0
Alin Parker withdrew ##代码##.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
I have outlined my questions above in the output:
1) Randomized 10 digit account number is displaying as 0
2) Deposit amount is being deducted but displaying as 0
3) Withdrawal amount is being deducted but displaying as 0
4) The "insufficient funds" statement should display instead of a negative balance
5) Main method has functions that are not being carried out:
我在输出中概述了上面的问题:
1) 随机 10 位帐号显示为 0
2) 存款金额被扣除但显示为 0
3) 提款金额被扣除但显示为 0
4) “资金不足”语句应显示而不是负余额
5) Main 方法具有未执行的功能:
Thank you in advance for your help
预先感谢您的帮助
回答by Sagar D
The problem is with :
问题在于:
##代码##The solution is :
解决办法是:
##代码##