Bitcoin

Bitcoin
Bitcoin

ATM-CODE

import java.util.*;

public class ATM {

    public static Scanner kbd = new Scanner(System.in);
    // The checkID method determines if acctNum is a valid account number
    // and pwd is the correct password for the account.  If the account information
    // is valid, the method returns the current account balance, as a string.
    // If the account information is invalid, the method returns the string "error".
    public static String checkID(String acctNum, String pwd)
    {
        String result = "error";

        // Strings a, b, and c contain the valid account numbers and passwords.
        // For each string, the account number is listed first, followed by
        // a space, followed by the password for the account, followed by a space,
        // followed by the current balance.
        String a = "44567-5 mypassword 520.36";
        String b = "1234567-6 anotherpassword 48.20";
        String c = "4321-0 betterpassword 96.74";

        if (acctNum.equals(a.substring(0, a.indexOf(" "))) && 
                pwd.equals(a.substring(a.indexOf(" ")+1,a.lastIndexOf(" "))))
            return result = a.substring(a.lastIndexOf(" ") + 1);

        if (acctNum.equals(b.substring(0, b.indexOf(" "))) && 
                pwd.equals(b.substring(b.indexOf(" ")+1,b.lastIndexOf(" "))))
            return result = b.substring(b.lastIndexOf(" ") + 1);

        if (acctNum.equals(c.substring(0, c.indexOf(" "))) && 
                pwd.equals(c.substring(c.indexOf(" ") + 1,c.lastIndexOf(" "))))
            return result = c.substring(c.lastIndexOf(" ") + 1);

        return result;
    }
public static int menu()
    {
        int menuChoice;
        do
        { 
            System.out.print("\nPlease Choose From the Following Options:"
                    + "\n 1. Display Balance \n 2. Deposit"
                    + "\n 3. Withdraw\n 4. Log Out\n\n");

            menuChoice = kbd.nextInt();

            if (menuChoice < 1 || menuChoice > 4){
                System.out.println("error");
            }

        }while (menuChoice < 1 || menuChoice > 4);

        return menuChoice;
    }

    public static void displayBalance(double x)
    {
        System.out.printf("\nYour Current Balance is $%.2f\n", x);
    }

    public static double deposit(double x, double y)
    {
        double depositAmt = y, currentBal = x;
        double newBalance = depositAmt + currentBal;

        System.out.printf("Your New Balance is $%.2f\n",  newBalance);

        return newBalance;
    }
 public static double withdraw(double x, double y)
    {
        double withdrawAmt = y, currentBal = x, newBalance;

        newBalance = currentBal - withdrawAmt;
        System.out.printf("Your New Balance is %.2f\n",newBalance);

        return newBalance;  
    }

    public static void main(String[] args) {

        String accNum, pass, origBal = "error";
        int count = 0, menuOption = 0;
        double depositAmt = 0, withdrawAmt = 0, currentBal=0; 
        boolean  foundNonDigit;
        //loop that will count the number of login attempts
        //you make and will exit program if it is more than 3.
        //as long as oriBal equals an error.  
        do{
            foundNonDigit = false;
            System.out.println("Please Enter Your Account Number: ");
            accNum = kbd.next();

            System.out.println("Enter Your Password: ");
            pass = kbd.next();

            origBal = checkID(accNum, pass);

            count++;

            if (count >= 3 && origBal.equals("error")){
                System.out.print("Maximum Login Attempts Reached.");
                System.exit(0);
}
if (!(origBal.equals("error"))){
                System.out.println("\nYour New Balance is: $ "+ origBal);
            }
            else
                System.out.println(origBal);


        }while(origBal.equals("error"));

        currentBal=Double.parseDouble(origBal);
        //this loop will keep track of the options that 
        //the user inputs in for the menu. and will 
        //give the option of deposit, withdraw, or logout.

        while (menuOption != 4)
        { 
            menuOption=menu();
            switch (menuOption)
            {
            case 1:
                displayBalance(currentBal);
                break;
            case 2:
                System.out.print("\nEnter Amount You Wish to Deposit: $ ");
                depositAmt = kbd.nextDouble();
                currentBal=deposit(depositAmt, currentBal);
                break;
            case 3:
                System.out.print("\nEnter Amount You Wish to Withdrawl: $ ");
                withdrawAmt = kbd.nextDouble();

                while(withdrawAmt>currentBal){
                    System.out.print("ERROR: INSUFFICIENT FUNDS!! "
                            + "PLEASE ENTER A DIFFERENT AMOUNT: $");
withdrawAmt = kbd.nextDouble();
        }

                currentBal = withdraw(currentBal, withdrawAmt);
                break;
            case 4:
                System.out.print("\nThank For Using My ATM.  Have a Nice Day.  Good-Bye!");
                System.exit(0);
                break;
            }
        }
}
ANOTHER ATM PROGRAM CODE
  1. import java.util.Scanner;
  2. public class ATM_Transaction
  3. {
  4.     public static void main(String args[] )
  5.     { 
  6.         int balance = 5000, withdraw, deposit;
  7.         Scanner s = new Scanner(System.in);
  8.         while(true)
  9.         {
  10.             System.out.println("Automated Teller Machine");
  11.             System.out.println("Choose 1 for Withdraw");
  12.             System.out.println("Choose 2 for Deposit");
  13.             System.out.println("Choose 3 for Check Balance");
  14.             System.out.println("Choose 4 for EXIT");
  15.             System.out.print("Choose the operation you want to perform:");
  16.             int n = s.nextInt();
  17.             switch(n)
  18.             {
  19.                 case 1:
  20.                 System.out.print("Enter money to be withdrawn:");
  21.                 withdraw = s.nextInt();
  22.                 if(balance >= withdraw)
  23.                 {
  24.                     balance = balance - withdraw;
  25.                     System.out.println("Please collect your money");
  26.                 }
  27.                 else
  28.                 {
  29.                     System.out.println("Insufficient Balance");
  30.                 }
  31.                 System.out.println("");
  32.                 break;
  33.  
  34.                 case 2:
  35.                 System.out.print("Enter money to be deposited:");
  36.                 deposit = s.nextInt();
  37.                 balance = balance + deposit;
  38.                 System.out.println("Your Money has been successfully depsited");
  39.                 System.out.println("");
  40.                 break;
  41.  
  42.                 case 3:
  43.                 System.out.println("Balance : "+balance);
  44.                 System.out.println("");
  45.                 break;
  46.  
  47.                 case 4:
  48.                 System.exit(0);
  49.             }
  50.         }
  51.     }
  52. }

No comments:

Post a Comment

Facebook