📄 atm.java
字号:
switch( option )
{
case 1:
withDrawCash( myAccount );
break;
case 2:
transferCash( myAccount );
break;
case 3:
depositCash( myAccount );
break;
case 4:
displayBalance( myAccount );
break;
case 5:
return;
default:
System.out.println("\nINVALID OPTION! Enter option no. again\n");
}
}
}
private void transferCash( Account myAccount )
{
System.out.println("\n***** Transfer Cash Menu *****\n");
Scanner input = new Scanner( System.in );
int amount = 0;
int receivingAccount = 0;
while( true )
{
System.out.print("\nEnter the amount in multiples of 500: ");
amount = input.nextInt();
if( (amount % 500) == 0 )
{
if( myAccount.getBalance() >= amount )
break;
else
System.out.print("\nERROR! Your account doesnot contain that" +
"much amount.\n\n");
}
}
while(true)
{
System.out.print("\nEnter the account number to which you want to transfer: ");
receivingAccount = input.nextInt();
if( receivingAccount > 0 )
{
if( isAccount( receivingAccount ) )
break;
else
System.out.println("\nERROR! Given account doesnot exist.\n");
}
}
String ownerName = getOwnerName(receivingAccount);
System.out.print("\nYou wish to deposit Rs. " + amount + " in account held by " +
ownerName + " ; \nIf this information is correct please re-enter the account number: " );
if( input.nextInt() == receivingAccount )
{
depositCash( receivingAccount, amount );
System.out.print("\nTransaction Confirmed.\n");
myAccount.setBalance( myAccount.getBalance() - amount );
while( true )
{
Scanner input2 = new Scanner( System.in );
System.out.print("\nDo you wish to print a receipt(Y/N)? ");
String ans = input2.nextLine();
if( ans.equalsIgnoreCase("y") )
{
System.out.println("Account no.: " + myAccount.getAccountNo() );
System.out.println("Ammount deposited: " + amount );
System.out.println("Balance: " + myAccount.getBalance() );
break;
}
else if( ans.equalsIgnoreCase("n") )
{
break;
}
}
}
else
{
System.out.println("\nTRANSACTION NOT CONFIRMED! Re-entered account doesnot" +
"matched with the previous one.");
}
}
private int showCustomerMenu()
{
System.out.println("\n\n***** Customer Menu *****\n");
System.out.println("Press 1 to WITHDRAW cash.");
System.out.println("Press 2 to TRANSFER cash.");
System.out.println("Press 3 to DEPOSIT cash.");
System.out.println("Press 4 to DISPLAY balance.");
System.out.println("Press 5 to EXIT.\n");
Scanner input = new Scanner( System.in );
int numb = input.nextInt();
return numb;
}
public boolean isAccount( int accountNo )
{
for( int i = 0; i < accountsList.size(); i++ )
{
Object o = accountsList.get(i);
Account c = (Account)o;
if( c.getAccountNo() == accountNo )
return true;
}
return false;
}
public String getOwnerName( int accountNo )
{
for( int i = 0; i < accountsList.size(); i++ )
{
Object o = accountsList.get(i);
Account c = (Account)o;
if( c.getAccountNo() == accountNo )
return c.getName();
}
return null;
}
public boolean depositCash( int accountNo, double amount )
{
for( int i = 0; i < accountsList.size(); i++ )
{
Object o = accountsList.get(i);
Account c = (Account)o;
if( c.getAccountNo() == accountNo )
{
c.setBalance( c.getBalance() + amount );
return true;
}
}
return false;
}
private void depositCash( Account myAccount )
{
System.out.println("\n***** Deposit Cash *****\n");
Scanner input = new Scanner( System.in );
double amount;
while( true )
{
System.out.print("\nEnter the amount to be Deposited: ");
amount = input.nextDouble();
if( amount < 0 )
System.out.println("\nERROR! Invalid amount entered.\n");
else
break;
}
myAccount.setBalance( myAccount.getBalance() + amount );
System.out.println("\nCash Deposited Successfully.");
while( true )
{
Scanner input2 = new Scanner( System.in );
System.out.print("Do you wish to print a receipt(Y/N)? ");
String ans = input2.nextLine();
if( ans.equalsIgnoreCase("y") )
{
System.out.println("Account no.: " + myAccount.getAccountNo() );
System.out.println("Ammount Deposited: " + amount );
System.out.println("Balance: " + myAccount.getBalance() );
break;
}
else if( ans.equalsIgnoreCase("n") )
{
break;
}
}
}
private void displayBalance( Account myAccount )
{
System.out.println( "\nAccount No.: " + myAccount.getAccountNo() );
System.out.println( "\nBalance: " + myAccount.getBalance() );
}
private void showAccountWelcomeMessage()
{
System.out.println("\nCONGATULATIONS! Your Account has been successfully created.\n");
}
public void openExistingAccount()
{
Scanner input = new Scanner( System.in );
while( true )
{
System.out.println("To access your account: ");
System.out.print("Enter Login: ");
String login = input.nextLine();
System.out.print("Enter Pin code: ");
int pinCode = input.nextInt();
if( isValidCustomer( login, pinCode ) )
{
enterAccount( returnValidAccount( login, pinCode ) );
return;
}
else
{
System.out.println("\nACCESS DENIED: Either the login or Pin code " +
"is incorrect\n");
return;
}
}
}
private boolean isValidCustomer( String login, int pinCode )
{
for( int i = 0; i < accountsList.size(); i++ )
{
Object o = accountsList.get(i);
Account c = (Account)o;
if( c.getLoginId().equals( login ) )
{
if( c.getPinCode() == pinCode )
return true;
}
}
return false;
}
private Account returnValidAccount( String login, int pinCode )
{
for( int i = 0; i < accountsList.size(); i++ )
{
Object o = accountsList.get(i);
Account c = (Account)o;
if( c.getLoginId().equals( login ) )
{
if( c.getPinCode() == pinCode )
return c;
}
}
return null;
}
}// end of class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -