我目前在Java银行问题的转帐方法上遇到麻烦。无论出于何种原因,当我将资金从一个帐户转移到另一个帐户时,它实际上都不会转移。我认为这可能是因为案例不包含小写语句(例如,案例“ checking”相对于案例“ Checking”,但这并不能解决错误。但是,它确实避免了打印默认案例) “您输入的号码无效”。代码如下:
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开始。当要转帐的金额大于帐户余额时,您的转帐方法不提供输出。