Java: Write an application that simulates a simple Automatic Teller Machine. You
ID: 3720125 • Letter: J
Question
Java: Write an application that simulates a simple Automatic Teller Machine. Your application must incorporate multiple classes, arrays, and file input. Your ATM application must first read existing client information from an input text file after which you will startup the ATM which will allow authenticated clients to view their balance, make deposits, or withdraw cash, while maintaining their updated balance. This is a perpetual ATM and cannot be shutdown.
The CLIENT class must consist of:
1. Four private instance variables: pin, first and last name, and balance
2. Constructor with values to initialize the values of the four instance variables
3. Methods to set and retrieve the pin, full name, and balance
4. Method that deposits or adds a valid amount to the balance
5. Method that withdraws or subtracts a valid amount from the balance
The ATMTest class must do the following
1. Use a method to read the pin, first and last name, and balance of 10 customers from the given input text file (clients.txt) into an array of objects of the CLIENT class. Output the same from main to demonstrate the successful execution of this method and the start of your perpetual Automatic Teller Machine
2. Display a welcome screen, then prompt and read the user entered pin number.
3. Use a method to match the entered pin with one of the customers pins. Return the index of the matching customer or -1 if there is no match.
4. If the entered information matches that of one of the customers in the array: a. Welcome the customer by full name. b. Display the following choices: i. Withdrawal ii. Deposit iii. Current balance iv. Exit
5. Should the entered information not match that of any of your customers, inform the user that the entered pin does not match any existing customer’s and to try again. Follow this by displaying of the original welcome screen (step 2).
6. If the Withdrawal option is selected, prompt and read the withdrawal amount, ensure the amount is valid, update and display the latest balance, and then prompt the user to enter M or m to view menu in step 4b or X or x to exit.
7. If the Deposit option is selected, prompt and read the deposit amount, ensure the deposit amount is valid, update and display the latest balance, and then prompt the user to enter M or m to view menu in step 4b or X or x to exit.
8. The Current balance option must display the current balance followed by a prompt to enter M or m to view menu in step 4b or X or x to exit. 9. Upon selecting Exit, the original login screen (step2) must display.
Explanation / Answer
Its a big project .. according to the Chegg guidelines i have tried to solve atleast the bare minimum and more.. youll understand and can do the rest
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package atm;
/**
*
* @author
*/
public class Client {
private int pin;
private String fName;
private String lName;
private int bal;
public Client(int pin,String fName,String lName,int bal){
this.pin=pin;
this.fName=fName;
this.lName=lName;
this.bal=bal;
}
public void Depoist(int amt){
this.bal+=amt;
}
public void Withdraw(int amt){
this.bal-=amt;
}
public int getPin() {
return pin;
}
public String getfName() {
return fName;
}
public String getlName() {
return lName;
}
public int getBal() {
return bal;
}
public void setPin(int pin) {
this.pin = pin;
}
public void setfName(String fName) {
this.fName = fName;
}
public void setlName(String lName) {
this.lName = lName;
}
public void setBal(int bal) {
this.bal = bal;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package atm;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author Faizan
*/
public class ATM {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException{
// TODO code application logic here
Client[] cl = new Client[10];
int ind=0;
// The name of the file to open.
String fileName = "C:\Users\Faizan\Documents\NetBeansProjects\ATM\src\atm\Clients.txt";
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
String[] temp ;
Client client;
while((line = bufferedReader.readLine()) != null) {
temp=line.split(" ");
client=new Client(Integer.parseInt(temp[0]), temp[1], temp[2], Integer.parseInt(temp[3]));
cl[ind]= client;
ind++;
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "' " + ex.getMessage());
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
for (int i = 0; i < ind; i++) {
System.out.print(cl[i].getPin() + " " + cl[i].getfName()+" " + cl[i].getlName()+ " " + cl[i].getBal());
System.out.println(".");
}
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
// Reading data using readLine
String ans = "";
Client client;
boolean first=true,second=true;
while (first==true) {
System.out.println("Welconme to ATM");
System.out.println("Enter pin");
ans= reader.readLine();
client=GetClientByPin(cl,ans);
if (client==null) {
System.out.println("Client not found. enter pin again");
second=false;
}else{
second=true;
}
int a=0;
while (second==true) {
System.out.println("Welcome " + client.getfName()+client.getlName());
System.out.println("1.Withdraw");
System.out.println("2.Deposit");
System.out.println("3.Balance");
System.out.println("4.Exit");
System.out.println("Enter Choice");
ans=reader.readLine();
a= Integer.parseInt(ans);
switch(a){
case 1:System.out.println("Enter amt to withdraw");
ans=reader.readLine();
a=Integer.parseInt(ans);
if (a<client.getBal()) {
client.Withdraw(a);
}else{
System.out.println("Insufficient balance");
}
break;
case 2:System.out.println("Enter amt to deposit");
ans=reader.readLine();
a=Integer.parseInt(ans);
client.Depoist(a);
break;
case 3:System.out.println("Your balance is " + client.getBal());
break;
case 4:
break;
}
}
}
}
private static Client GetClientByPin(Client[] cl, String ans) {
for (int i = 0; i < cl.length; i++) {
if ( cl[i].getPin() == Integer.parseInt(ans)) {
return cl[i];
}
}
return null;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.