java - 转账方法在银行程序中不起作用

标签 java oop debugging switch-statement

我目前在处理我的java银行问题的转账方法时遇到了一些麻烦。无论出于何种原因,当我将钱从一个帐户转移到另一个帐户时,它实际上并没有转移。我认为这可能是因为案例没有小写语句(例如案例“检查”而不是案例“检查”,但这并没有修复错误。但是,它确实阻止了打印默认案例说“您输入了无效的数字”。代码如下:

import java.io.InputStream;
import java.util.Scanner;

public class CustomerDemo
{
    public static void main(String[] args)
        {

            Scanner in = new Scanner(System.in);
            Customer customer = new Customer(System.in);
            int accountChoice; // show which account needs to be chosen.
            String cusSel; //for customer selection.
            double money;
            do
                {
                    System.out.println("main menu to be selected: " );
                    System.out.println("1.deposit " );
                    System.out.println("2.withdraw " );
                    System.out.println("3.transfer " );
                    System.out.println("4.print balance " );
                    System.out.println("q.quit " );
                    cusSel=in.next();
                    switch(cusSel.charAt(0))
                    {
                        case '1':
                            System.out.println("please select account: " );
                            System.out.println("1. Checking" );
                            System.out.println("2. Saving " );
                            accountChoice=in.nextInt();
                            if((accountChoice==1)||(accountChoice==2))
                                {
                                    System.out.println("please imput the deposit amount: " );
                                    money=in.nextDouble();
                                    if(accountChoice==1)
                                        {
                                            customer.deposit(money, "Checking");
                                        }
                                    else if(accountChoice==2)
                                        {
                                            customer.deposit(money, "Saving");
                                        }
                                }
                            else
                                {
                                    System.out.println("invalid choice. your choice does not exsist");
                                    break;
                                }
                            break;

                        case '2':

                            System.out.println("Please select account: " );
                            System.out.println("1. Checking" );
                            System.out.println("2. Saving " );

                            accountChoice=in.nextInt();
                            if((accountChoice==1)||(accountChoice==2))
                                {
                                    System.out.println("please input the withdraw amount: " );

                                    money=in.nextDouble();


                                    if(accountChoice==1)
                                        {
                                            customer.withdraw(money, "Checking");
                                        }
                                    else if(accountChoice==2)
                                        {
                                            customer.withdraw(money, "Saving");
                                        }
                                }
                            else
                                {
                                    System.out.println("invalid choice. your choice does not exsist");
                                    break;
                                }
                            break;

                        case '3':

                            System.out.println("please select an account to transfer from: " );
                            System.out.println("1. Checking" );
                            System.out.println("2. Saving " );

                            accountChoice=in.nextInt();
                            if((accountChoice==1)||(accountChoice==2))
                                {
                                    System.out.println("please input the transfer amount: " );

                                    money=in.nextDouble();

                                    if(accountChoice==1)
                                        {
                                            customer.transfer(money, "saving");
                                        }
                                    else if(accountChoice==2)
                                        {
                                            customer.transfer(money, "checking");
                                        }
                                }
                            else
                                {
                                    System.out.println("invalid choice. your choice does not exsist");
                                    break;
                                }
                            break;

                        case '4':
                            customer.printBalance();
                            break;
                        case 'q':
                            System.out.println("transaction complete, please have a nice day");
                            break;
                        default:
                            System.out.println("invalid choice.");

                    }

                } while (cusSel.charAt(0)!= 'q' && cusSel.charAt(0)!= 'Q');

        }
    private void getAccountChoice(int accountChoice) {
        // TODO Auto-generated method stub
    }

}

class Customer
{
    //two objects/ saving and checking
    Account Saving = new Account();
    Account Checking = new Account();
    public Customer(InputStream in)
        {
            // TODO Auto-generated constructor stub
        }
    boolean deposit(double amount, String acc)
        {
            double currentBalanceChecking, currentBalanceSaving;// this currentBalence is subtracted from and account.
            boolean retVal = false;
            switch (acc)
            {
                case "Checking": case "checking":
                    if(amount >=0)
                        {
                            currentBalanceChecking = this.Checking.deposit(amount);
                            retVal = true;
                        }
                    break;
                case "Saving": case "saving": 
                    if(amount >=0)
                        {
                            currentBalanceSaving = this.Saving.deposit(amount);
                            retVal = true;
                        }
                    break;
                default:
                    System.out.println("You entered an invalid number.");
            }
            return retVal;
        }
    boolean withdraw(double amount, String acc)
        {
            double currentBalanceChecking, currentBalanceSaving;// this currentBalence is subtracted from and account.
            boolean retVal = false;
            switch (acc)
            {
                case "Checking": case "checking":
                    if(amount <=Checking.getBalance())
                        {
                            currentBalanceChecking = this.Checking.withdraw(amount);
                            retVal = true;
                        }
                    break;
                case "Saving": case "saving": 
                    if(amount <=Saving.getBalance())
                        {
                            currentBalanceSaving = this.Saving.withdraw(amount);
                            retVal = true;
                        }
                    break;
                default:
                    System.out.println("You entered an invalid number.");
            }
            return retVal;
        }
    boolean transfer(double amount, String acc)
        {
            double currentBalanceChecking, currentBalanceSaving;// this currentBalence is subtracted from and account.
            boolean retVal = false;

            switch (acc)
            {
                case "Checking": case "checking":
                    if(amount <=Checking.getBalance())
                        {
                            currentBalanceChecking = this.Checking.withdraw(amount);
                            currentBalanceSaving = this.Saving.deposit(amount);
                            retVal = true;
                        }
                    break;
                case "Saving": case "saving": 
                    if(amount <=Saving.getBalance())
                        {
                            currentBalanceSaving = this.Saving.withdraw(amount);
                            currentBalanceChecking = this.Checking.deposit(amount);
                            retVal = true;
                        }
                    break;
                default:
                    System.out.println("You entered an invalid number.");
            }
            return retVal;
        }
    void printBalance()
        {
            System.out.println("The checking balance is $" + Checking.getBalance());
            System.out.println("The saving balance is $" + Saving.getBalance());
        }
}


class Account
{
    double balance;
    //the constructor tells the customer that there are zero dollars in the account.
    Account()
    {
        balance = 0;
    }
    //deposit money
    double deposit( double depAmount )
        {
            balance= balance + depAmount;//balance+ = depAmount
            return balance;
        }
    double withdraw( double withAmount )
        {
            balance= balance - withAmount;//balance- = withAmount
            return balance;
        }

    double getBalance()
        {
            return balance;
        }
}

最佳答案

您的帐户类别从 0 美元开始。当要转帐的金额大于帐户余额时,您的转帐方法不会提供输出。

关于java - 转账方法在银行程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26536188/

相关文章:

c++ - 我在 IDA 或 dbg 或 olly 上看到的内存是否与我在 RAM 上实时加载的内存相同?

c++ - 如何解释 .stackdump 文件?

java - 写入和复制文本文件

java - arraycopy() : Does Java ignore index arguments when length argument is 0?

java - 为什么java中的集合有int索引?

Matlab 删除我的值(value)观

Java套接字连接

c# - 使两个并行继承结构跨层交互的方法

php - 在类中包含类是不好的做法吗?

c++ - Linux,我可以重定向外部库(.so)的调试输出吗