I already wrote an ATM program which did store only three account number, password and balance but now I want to get these information from a file which would be read to the program and by entering the information the program should search the file and find the line that fit the information that I entered first for account number then for password and if all match one of 50000 account in the file show the balance of that account and also if I need to deposit or withdraw from one of the account it would save the new balance and next time if I sign in with that account it would show me the new balance which I changed by adding deposit to it or withdraw mony from it!
I know that some how my program reads the file and copy it to another file and also in another method erase the copy file that ?I have and should replace it with the new information(balance) but I don't know how to do it! would someone please help me and walk me through it.
This is the one that I wrote with 3 accounts information stored in the program itself:
Please please someone help me as soon as possible!
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 word 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 statement
{
System.out.println("Please enter your account number: ");//gives the system command to print
acctNum = kbd.nextLine();//getting the inputs and allocating
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 situation of the acctres variable for error
{
accountbalance = Double.parseDouble(acctres);//Initializing accountbalance
do//gives the do-while loop the condition to repeat the statement
{
Choice = Menu();//Initializing Menu()
System.out.println(Choice);//gives the system 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 input 2
{
System.out.println("How much would you like to deposit to your account? ");//gives the system command to print
depositamount=kbd.nextDouble();//getting the inputs and allocating
double calculatebalance = Deposit(accountbalance, depositamount);//Initializing calculatebalance
System.out.println("Your Balance with your Deposit now is: " + calculatebalance + "$");//gives the system command to print
}
else if (Choice == 3)//checking for input 3
{
System.out.println("How much would you like to withdrawal from your account? ");//gives the system command to print
withdrawalbalance=kbd.nextDouble();//getting the inputs and allocating
double calculatebalance1 = Withdraw(accountbalance, withdrawalbalance);//Initializing calculatebalance1
System.out.println("You Balance now is: " + calculatebalance1 + "$");//gives the system command to print
}
else// checking for the other inputs except 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.toUpperCase();//to ignore the upper case or lower case input and count them as the same
if (userAnswer.equals("Y"))//checking 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 already were mentioned
{
System.out.println("Error");//gives the system command to print
counter++;//Increasing the counter by 1
System.out.println(counter);//gives the system command to print
}
}while(counter<3);//checking for the counter to be less than 3 or not
System.out.println("You have exceeded the required amount of times.");//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 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";//declaring string a which 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 which 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 function
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 function
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 function
if(acctNum.equals(d) && pwd.equals(e))//checking for account number and password
{
result=eres;//gives commands for the if statement
}
else if(acctNum.equals(f) && pwd.equals(g))//checking for account number and password
{
result=gres;//gives commands for the if statement
}
else if(acctNum.equals(h) && pwd.equals(i))//checking for account number and password
{
result=ires;//gives commands for the if statement
}
return result;//to get data out of the function and takes the value 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 takes 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 function
{
return accountbalance+ depositamount;//to get data out of the function and takes the value with it only once
}
public static double Withdraw(double accountbalance, double withdrawalbalance)//separating the part from the main program as a new function in the main function
{
double newbalance1 = 0;//declaring the variables, newbalance1, as double
if (withdrawalbalance<=accountbalance)//checking the condition in 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 condition 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 account balance still is: " + newbalance1);//gives the system command to print the accountbalance without changing it
}
return newbalance1;//to get data out of the function and takes the 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 statement
{
Scanner kbd1 = new Scanner(System.in);// declaring the function
System.out.println("1. Display Balance ");//gives the system command to print
System.out.println("2. Deposit ");//gives the system command to print
System.out.println("3. Withdraw ");//gives the system command to print
System.out.println("4. Log Out ");//gives the system command to print
System.out.println("Enter your option");//gives the system command to print
Option = kbd1.nextInt();//getting the inputs and allocating
if ((Option<=0)&&(Option > 4))//checking for the statement in parentheses(Smaller or equal 0 or bigger than 4 inputs)
{
System.out.println("Error! Choose again: ");//gives the system command to print
}
}while ((Option<=0)&&(Option > 4));//gives the while loop the condition to stop the repeating statement
return Option;//to get data out of the function and takes the value with it only once
}
}