转账方法不适用于银行计划

时间:2014-10-23 19:46:49

标签: java oop debugging switch-statement

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

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

1 个答案:

答案 0 :(得分:1)

您的帐户课程以$ 0开头。当您要转账的金额大于账户余额时,您的转账方式不提供输出。