【问题标题】:Java Bank Program - How to deduct from the balanceJava银行程序-如何从余额中扣除
【发布时间】:2021-01-15 02:35:22
【问题描述】:

我有一个简单的银行程序,您可以在其中查看余额、存款和取款。顺便说一下,这是一个循环,因此您可以进行多次交易

char anotherTransact, option;
    int balance;
    double deposit, withdraw;
    
    
    do {
    System.out.println("\nWelcome to ABC BANK \n");
    
    System.out.println("B - Check for Balance");
    System.out.println("D - Make Deposit");
    System.out.println("W - Make Withdraw");
    System.out.println("Q - Quit");
    
    
        System.out.print("\nSelect an option : ");
        option = scan.next().charAt(0);
        
        
        balance = 100000; 
        if ((option == 'B') || (option == 'b')) {
            System.out.println("\nYour current balance is " +balance);
        }
        
        else if ((option == 'D') || (option == 'd')) {
            System.out.print("\nEnter amount to Deposit : ");
            deposit = scan.nextDouble();
            if (deposit > 1) {
                System.out.println("Deposit Transaction is successfully completed.");
                double newBalance = 100000 + deposit; 
            }
            else if (deposit > 500000) {
                System.out.println("Deposit Amount must not be greater than 500,000");
            }
            else {
                System.out.println("Deposit must be greater than zero");
            }
        }
        
        else if ((option == 'W') || (option == 'w')) {
            System.out.print("\nEnter amount to Withdraw : ");
            withdraw = scan.nextDouble();
            if (withdraw % 100 == 0) {
                System.out.println("Withdrawal Transaction is successfully completed.");
                
            }
            else if (withdraw > 150000) {
                System.out.println("Withdrawal Amount must not be greater then 150,000");
            }
            else {
                System.out.println("Withdrawal Amount must be greater than zero");
            }
        }
        
        else {
            System.out.println("\nInvalid entry, enter any valid option : (B/D/W/Q)");
        }
        
        System.out.print("\nWant to Transact another (Y/N?) ");
        anotherTransact = scan.next().charAt(0);        
    }
    
    while ((anotherTransact == 'Y') || (anotherTransact =='y'));

问题是当我选择提款选项时,我不知道它应该如何在下一次银行交易中扣除余额并在余额中添加存款。

例如,我会在银行存入 40 美元。然后进行一项新交易以打开我的余额,该余额应将我存入的 40 美元加起来。我怎样才能在我的余额中添加存入的钱?还有提现的时候怎么扣?

【问题讨论】:

  • 您没有正确更新balance -= withdraw; 和`余额+=存款;`。对这种类型的用例使用 switch case。最好在那里使用一个类和定义的方法。在进行交易之前显示成功消息是错误的。此外,缺少一些边缘情况,您也可以包括这些情况。

标签: java if-statement


【解决方案1】:

在会计中,您不会跟踪任何余额值,因为它始终是动态计算的。
在资产负债表(通常是数据库表)。如果你使用类似intdouble 的数据类型......你只能代表美分;通常BigDecimal 用于表示货币价值。

如果您想让这个程序以某种方式运行,请将所有事务推送到ArrayList。我的意思是,一个愚蠢的问题会是:您打算如何在不跟踪所有交易的情况下签发银行对帐单?

【讨论】:

    【解决方案2】:

    您需要更新 balance 变量。您在循环的每次迭代中都将 balance 设置为 100000。另外,我将其更改为 scan.nextInt() 因为余额是整数。如果要处理小数,请将 balance 更改为 double 并将 nextInt 更改回 nextDouble。

    import java.util.Scanner;
    
    class Main {  
      public static void main(String args[]) { 
        char anotherTransact, option;
        int balance = 100000;
        double deposit, withdraw;
        Scanner scan = new Scanner(System.in);
        
        do {
        System.out.println("\nWelcome to ABC BANK \n");
        
        System.out.println("B - Check for Balance");
        System.out.println("D - Make Deposit");
        System.out.println("W - Make Withdraw");
        System.out.println("Q - Quit");
        
        
            System.out.print("\nSelect an option : ");
            option = scan.next().charAt(0);
            
            
            if ((option == 'B') || (option == 'b')) {
                System.out.println("\nYour current balance is " +balance);
            }
            
            else if ((option == 'D') || (option == 'd')) {
                System.out.print("\nEnter amount to Deposit : ");
                deposit = scan.nextInt();
                if (deposit > 1) {
                    System.out.println("Deposit Transaction is successfully completed.");
                    balance += deposit; 
                }
                else if (deposit > 500000) {
                    System.out.println("Deposit Amount must not be greater than 500,000");
                }
                else {
                    System.out.println("Deposit must be greater than zero");
                }
            }
            
            else if ((option == 'W') || (option == 'w')) {
                System.out.print("\nEnter amount to Withdraw : ");
                withdraw = scan.nextInt();
                if (withdraw % 100 == 0) {
                    balance -= withdraw;
                    System.out.println("Withdrawal Transaction is successfully completed.");
                    
                }
                else if (withdraw > 150000) {
                    System.out.println("Withdrawal Amount must not be greater then 150,000");
                }
                else {
                    System.out.println("Withdrawal Amount must be greater than zero");
                }
            }
            
            else {
                System.out.println("\nInvalid entry, enter any valid option : (B/D/W/Q)");
            }
            
            System.out.print("\nWant to Transact another (Y/N?) ");
            anotherTransact = scan.next().charAt(0);        
        }
        
        while ((anotherTransact == 'Y') || (anotherTransact =='y')); 
      } 
    }
    

    【讨论】:

    • deposit > 1 应该是 deposit > 1 && deposit < 500000。还应该支持浮点交易。 withdraw % 100 == 0 应该是 withdraw % 100 == 0 && withdraw < balance 等等。
    • @roottraveller 是的,但这不是 OP 所要求的,我想一旦他们知道如何更新变量,他们就可以自己找出排除案例。而且由于这家银行只允许以百元钞票取款,我认为他们不接受硬币存款
    【解决方案3】:

    首先,从余额中减去balance -= withdrawal,与balance = balance - withdraw相同,这将更新变量balance。当您想打印当前余额时,只需调用此变量即可。
    double newBalance = 100000 + deposit; 不会更新您的余额,因为您只打印 balance 而不是 newBalance
    其次,balance 不应该是 int 变量。
    而且您的某些交易规则不正确。取款时,请检查余额是否大于取款金额,取款金额是否大于0(balance > withdraw && withdraw > 0)。另外,withdraw % 100 == 0 对我来说真的没有意义,它不是"Withdrawal Amount must be greater than zero" 的条件

    【讨论】:

    • 取款 % 100 == 0 实际上意味着取款应该能被 100 整除。我不知道这是否是正确的表达方式
    • @Liyanna 如果您希望提款能被 100 整除,那么这是正确的
    猜你喜欢
    • 2021-07-05
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2015-02-18
    • 2019-02-20
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多