JAVA中的银行账户申请

Posted

技术标签:

【中文标题】JAVA中的银行账户申请【英文标题】:Bank Account Application in JAVA 【发布时间】:2014-10-10 10:05:05 【问题描述】:

任务是在创建银行帐户时使用继承创建不同的类。然后我们存款、取款和报告余额。我有 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 子类,因为我可以在你们在这两个类上给我的帮助下调整该类。

输出:

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) Main 方法具有未执行的功能:

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);
    

提前感谢您的帮助

【问题讨论】:

这到底是怎么回事,你使用了所有错误的变量,这就是为什么没有工作的原因。看看你的每一种方法,看看实际做了什么。我开始写一个 awnser,但你问的每个问题的 awnser 都是“你使用了错误的变量”。提示:将你的类变量设为私有,你也可以为类变量换一个不同的命名约定,它们都以下划线开头。所以就像_firstName 这样有助于更清楚地了解您使用的变量。 ***.com/questions/25336914/…的可能重复 该链接并未解决我的所有问题。作为一个n00b,我的声誉被否定是没有帮助的。我只是在寻求帮助。 【参考方案1】:

问题在于:

   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.");
        
    

【讨论】:

@vs-works 是我提到的错误之外出现的错误? @vs-works 这仍然是错误的,因为在提款后会再次检查余额。第二个条件必须替换为else

以上是关于JAVA中的银行账户申请的主要内容,如果未能解决你的问题,请参考以下文章

Java ArrayList银行账户

JAVA/JS企业对公账号如何校验正确性,方法或者正则表达式 强调一下,不是个人银行账户,是企业银行账户

领域驱动设计系列贫血模型和充血模型

Java基础知识综合练习_简单的银行账户操作系统

java 课程设计——银行管理系统

Java中在银行系统中有账户类型,进行基本存取款操作