Você está na página 1de 4

import java.util.

Scanner;//name of the package(library)that includes the * class public class ATM{//name of the program public static Scanner kbd;//Scanner kbd function public static void main(String[] args) {// the main program with the wor d void doesn't let anything out String userAnswer = null;//declaring the string Scanner kbd = new Scanner(System.in); // declaring the function double accountbalance = 0, depositamount = 0, withdrawalbalance= 0;//declaring the variables, accountbalance, depositamount, withdrawalbalance, as double String acctNum,pwd;//declaring acctNum, pwd as string String acctres;//declaring acctres as string int counter=0;//declaring counter as int int Choice;//declaring Choice as int do//gives the do-while loop the condition to repeat the statemen t { System.out.println("Please enter your account number: ") ;//gives the system command to print acctNum = kbd.nextLine();//getting the inputs and alloca ting System.out.println("Please enter your account password: ");//gives the system command to print pwd = kbd.nextLine();//getting the inputs and allocating acctres=checkID(acctNum,pwd);//Initializing acctres if(!acctres.equals("error"))// checking for the situatio n of the acctres variable for error { accountbalance = Double.parseDouble(acctres);//I nitializing accountbalance do//gives the do-while loop the condition to rep eat the statement { Choice = Menu();//Initializing Menu() System.out.println(Choice);//gives the s ystem command to print if (Choice == 1)//checking for input 1 { System.out.println("Your Balance is: " + accountbalance + "$");//gives the system command to print } else if (Choice == 2)//checking for inpu t 2 { System.out.println("How much wou ld you like to deposit to your account? ");//gives the system command to print depositamount=kbd.nextDouble();/ /getting the inputs and allocating double calculatebalance = Deposi t(accountbalance, depositamount);//Initializing calculatebalance System.out.println("Your Balance with your Deposit now is: " + calculatebalance + "$");//gives the system comman d to print } else if (Choice == 3)//checking for inpu t 3 { System.out.println("How much wou ld you like to withdrawal from your account? ");//gives the system command to pr

int withdrawalbalance=kbd.nextDouble ();//getting the inputs and allocating double calculatebalance1 = Withd raw(accountbalance, withdrawalbalance);//Initializing calculatebalance1 System.out.println("You Balance now is: " + calculatebalance1 + "$");//gives the system command to print } else// checking for the other inputs exc ept the ones which were mentioned { System.out.println("Do you want to Log Out? (Y or N) ");//gives the system command to print userAnswer=kbd.next();//getting the inputs and allocating userAnswer=userAnswer.toUpperCas e();//to ignore the upper case or lower case input and count them as the same if (userAnswer.equals("Y"))//che cking for the situation of userAnswer for being equal Y which means yes { System.out.println("You are now logged out!");//gives the system command to print System.exit(0);// gives command for the if statement } } }while ((Choice <=4)|| (userAnswer.equals("N"))) ;//Checking input N } else// checking for other input except the ones that alr eady were mentioned { System.out.println("Error");//gives the system c ommand to print counter++;//Increasing the counter by 1 System.out.println(counter);//gives the system c ommand to print } }while(counter<3);//checking for the counter to be less than 3 o r not System.out.println("You have exceeded the required amount of tim es.");//gives the system command to print } public static String checkID(String acctNum, String pwd)//separating the part from the main program as a new function in the main function { String result = "error";//declaring the result as string // Strings a, b, and c contain the valid account numbers and pas swords. // For each string, the account number is listed first, followed by // a space, followed by the password for the account, followed b y a space, // followed by the current balance. String a = "44567-5 mypassword 520.36";//declaring string a whic h id equal 44567-5 mypassword 520.36 in this function String b = "12345-6 anotherpassword 48.20";//declaring string b which id equal 12345-6 anotherpassword 48.20 in this function String c = "54321-0 betterpassword 96.74";//declaring string c w hich id equal 54321-0 betterpassword 96.74 in this function

String d=a.substring(0,7);//declaring string d in this function String e=a.substring(8,18);//declaring string e in this function String eres=a.substring(19);//declaring string eres in this func tion String f=b.substring(0,7);//declaring string f in this function String g=b.substring(8,23);//declaring string g in this function String gres=b.substring(24);//declaring string gres in this func tion String h=c.substring(0,7);//declaring string h in this function String i=c.substring(8,22);//declaring string i in this function String ires=c.substring(23);//declaring string ires in this func tion if(acctNum.equals(d) && pwd.equals(e))//checking for account num ber and password { result=eres;//gives commands for the if statement } else if(acctNum.equals(f) && pwd.equals(g))//checking for accoun t number and password { result=gres;//gives commands for the if statement } else if(acctNum.equals(h) && pwd.equals(i))//checking for accoun t number and password { result=ires;//gives commands for the if statement } return result;//to get data out of the function and takes the va lue with it only once } public static double Displaybalance1(double accountbalance)//separating the part from the main program as a new function in the main function { return accountbalance;//to get data out of the function and take s the value with it only once } public static double Deposit(double accountbalance, double depositamount )//separating the part from the main program as a new function in the main funct ion { return accountbalance+ depositamount;//to get data out of the fu nction and takes the value with it only once } public static double Withdraw(double accountbalance, double withdrawalba lance)//separating the part from the main program as a new function in the main function { double newbalance1 = 0;//declaring the variables, newbalance1, a s double if (withdrawalbalance<=accountbalance)//checking the condition i n the parentheses(if withdrawalamount is less than or equal the accontbalance or not) { newbalance1 = accountbalance - withdrawalbalance;//gives the system command to print the calculated amount } else if (withdrawalbalance>accountbalance)//checking the conditi on in the parentheses(if withdrawalamount is more than the accontbalance or not) { newbalance1 = accountbalance;//declaring the newbalance1

as previous accountbalance without change System.out.println("error, that is impossible! your acco unt balance still is: " + newbalance1);//gives the system command to print the a ccountbalance without changing it } return newbalance1;//to get data out of the function and takes t he value with it only once } public static int Menu()//separating the part from the main program as a new function in the main function { int Option ;//declaring the variables, Size, as int do//gives the do-while loop the condition to repeat the statemen t { Scanner kbd1 = new Scanner(System.in);// declaring the f unction System.out.println("1. Display Balance ");//gives the s ystem command to print System.out.println("2. Deposit ");//gives the system co mmand to print System.out.println("3. Withdraw ");//gives the system c ommand to print System.out.println("4. Log Out ");//gives the system co mmand to print System.out.println("Enter your option");//gives the syst em command to print Option = kbd1.nextInt();//getting the inputs and allocat ing if ((Option<=0)&&(Option > 4))//checking for the stateme nt in parentheses(Smaller or equal 0 or bigger than 4 inputs) { System.out.println("Error! Choose again: ");//gi ves the system command to print } }while ((Option<=0)&&(Option > 4));//gives the while loop the co ndition to stop the repeating statement return Option;//to get data out of the function and takes the va lue with it only once } }

Você também pode gostar