⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atm.java

📁 this is a relatively simple program.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

import java.util.ArrayList;
import java.util.Scanner;

public class ATM
{
    private ArrayList accountsList;
    int noOfAccountsCreated; // it will represent unique account number
    
    public ATM( )
    {
        accountsList = new ArrayList();
        noOfAccountsCreated = 0;
    }
    
    public void createAccount( String loginId, int pinCode, double balance, String name,
            String address, String email, String phoneNo )
    {
        noOfAccountsCreated++;
        Account newAccount = new Account( noOfAccountsCreated,loginId,pinCode,balance,name,
                address,email,phoneNo );
        
        accountsList.add( newAccount );        
        showAccountWelcomeMessage();
        
        enterAccount( newAccount );
    }
    
    private void withDrawCash( Account myAccount )
    {
        int option = showWithDrawCashMenu();
        if( option == 1 )
            fastCashWithDraw( myAccount );
        else
            normalCashWithDraw( myAccount );
    }
    
    private void fastCashWithDraw( Account myAccount )
    {
        int option = showFastCashMenu();
        /*
         * 1----500
         * 2----1000
         * 3----2000
         * 4----5000
         * 5----10000
         * 6----15000
         * 7----20000
         */
        
        double balance = myAccount.getBalance();
        boolean error = false;
        int amountWithdrawn = 0;
        
        switch( option )
        {
            case 1:
                if( balance >= 500 )
                {
                    myAccount.setBalance( balance - 500 );
                    amountWithdrawn = 500;
                    System.out.print("\nCash withdrawn successfully\n");
                }
                else
                    System.out.println("\nERROR! Amount withdrawn is greater than " +
                            "original balance\n");
                break;
                
            case 2:
                if( balance >= 1000 )
                {
                    myAccount.setBalance( balance - 1000 );
                    amountWithdrawn = 1000;
                    System.out.print("\nCash withdrawn successfully\n");
                }
                else
                    System.out.println("\nERROR! Amount withdrawn is greater than " +
                            "original balance\n");
                break;
            
            case 3:
                if( balance >= 2000 )
                {
                    myAccount.setBalance( balance - 2000 );
                    amountWithdrawn = 2000;
                    System.out.print("\nCash withdrawn successfully\n");
                }
                else
                    System.out.println("\nERROR! Amount withdrawn is greater than " +
                            "original balance\n");
                break;
                
            case 4:
                if( balance >= 5000 )
                {   myAccount.setBalance( balance - 5000 );
                    amountWithdrawn = 5000;
                    System.out.print("\nCash withdrawn successfully\n");
                }
                else
                    System.out.println("\nERROR! Amount withdrawn is greater than " +
                            "original balance\n");
                break;
                
            case 5:
                if( balance >= 10000 )
                {   myAccount.setBalance( balance - 10000 );
                    amountWithdrawn = 10000;
                    System.out.print("\nCash withdrawn successfully\n");
                }
                else
                    System.out.println("\nERROR! Amount withdrawn is greater than " +
                            "original balance\n");
                break;
                
            case 6:
                if( balance >= 15000 )
                {   myAccount.setBalance( balance - 15000 );
                    amountWithdrawn = 15000;
                    System.out.print("\nCash withdrawn successfully\n");
                }
                else
                    System.out.println("\nERROR! Amount withdrawn is greater than " +
                            "original balance\n");
                break;
            
            case 7:
                if( balance >= 20000 )
                {   myAccount.setBalance( balance - 20000 );
                    amountWithdrawn = 20000;
                    System.out.print("\nCash withdrawn successfully\n");
                }
                else
                    System.out.println("\nERROR! Amount withdrawn is greater than " +
                            "original balance\n");
                break;
        }
        
        if( !error )
        {
            Scanner input = new Scanner( System.in );            
            
            while( true )
            {
                System.out.print("\nDo you wish to print a receipt(Y/N)?  ");
                String ans = input.nextLine();
                if( ans.equalsIgnoreCase("y") )
                {
                   System.out.println("Account no.: " + myAccount.getAccountNo() ); 
                   System.out.println("Ammount withdrawn: " + amountWithdrawn );
                   System.out.println("Balance: " + myAccount.getBalance() ); 
                   break;
                }
                else if( ans.equalsIgnoreCase("n") )
                {
                    break;
                }
            }
        }
}
    
    private int showFastCashMenu()
    {
        Scanner input = new Scanner( System.in );
        int option = 1;
        
        while( true )
        {        
            System.out.println("\n***** Fast Cash Withdraw possible choices *****\n");
            System.out.println("Inorder to withdraw cash, enter appropriate choice: \n");
            System.out.println("Press 1 for Rs. 500/-");
            System.out.println("Press 2 for Rs. 1000/-");
            System.out.println("Press 3 for Rs. 2000/-");
            System.out.println("Press 4 for Rs. 5000/-");
            System.out.println("Press 5 for Rs. 10000/-");
            System.out.println("Press 6 for Rs. 15000/-");
            System.out.println("Press 7 for Rs. 20000/-\n");
            
            option = input.nextInt();
            if( option >= 1 || option <= 7 )
            {
                break;
            }
            else
                System.out.println("INVALID OPTION! Enter option again.");
        }
                
        return option;
    }
    
    private void normalCashWithDraw( Account myAccount )
    {
        System.out.println("\n***** Normal Cash Withdrawn *****\n ");
        Scanner input = new Scanner( System.in );
        double amount; 
        
        while( true )
        {
            System.out.println("\nEnter the amount to be withdrawn: ");
            amount = input.nextDouble();    
            if( amount < 0 )
                System.out.println("\nERROR! Invalid amount entered.\n");
            else
                break;
        }
        
        boolean error = false;
        if( myAccount.getBalance() >= amount )
        {
            myAccount.setBalance( myAccount.getBalance() - amount );
        }
        else
        {
            System.out.println("ERROR! Amount withdrawn is greater than the original amount.");
            error = true;
        }
        
        if( !error )
        {
        System.out.print("\nCash withdrawn successfully\n");
            
            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 withdrawn: " + amount );
                   System.out.println("Balance: " + myAccount.getBalance() ); 
                   break;
                }
                else if( ans.equalsIgnoreCase("n") )
                {
                    break;
                }
            }
        }
        
    }  
    
    private int showWithDrawCashMenu()
    {
        int option = 1;
        Scanner input = new Scanner( System.in );
        
        while( true )
        {
            System.out.println("\n***** Withdraw Cash Menu *****\n");
            System.out.println("Inorder to withdraw cash enter an appropriate choice: \n");
            System.out.println("Press 1 for FAST cash.");
            System.out.println("Press 2 for NORMAL cash.\n");
        
            option = input.nextInt();
            
            if( option == 1 || option == 2 )
            {
                break;
            }
            else
                System.out.println("\nINVALID OPTION! Enter option again.\n");                
        }
                
        return option;
    }
    
    private void enterAccount( Account myAccount )
    {
    int option = 0;
    while( option != 5 )
    {
        option = showCustomerMenu();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -