In: Computer Science
Practice Coding Task
C++ ATM Machine with Some Classes
Create an ATM machine in C++ with a few classes.
Requirements:
Automated Teller Machine (ATM) simulationGiven 3 trials, the user
is able to see his balance by entering a four-digit pin that must
NEVER be displayed on screen but masked by the Asterix (*)
character. A list of pins stored on the file system must be loaded
to verify the pin. The user should be able to withdrawfundsbelow a
set limit and cannot redraw more than 3 times within a space of 5
minutes. The balance from each pin should be determined each time
the program is run and is a random value between 0 and 100. The
user should also be allowed to change his pin at any time which
should be reflected in the file system.
SOURCE CODE
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <conio.h>
#include <ctime>
using namespace std;
//defining user class
class user
{
//data members
int pin;
float amount;
int limit = 3;
public:
//setter functions
void set_pin(int pin){ this->pin = pin; }
void set_amount(float amount){ this->amount = amount; }
//getter functions
int get_pin() { return pin; }
float get_amount(){ return amount; }
int get_limit(){ return limit; }
//utility function declaration
void update_amount(float a);
};
//this function reads a file containing pins
//and loads them into user array and sets random
//balance for each user
//returns the number of pins read from file
int load_pins(user *A)
{
ifstream f;
//open file
f.open("pins.txt");
int pin, i = 0;
//read pins one by one
while(f >> pin)
{
//set pin
A[i].set_pin(pin);
//set balance
A[i].set_amount(rand() % 101);
i++;
}
//close file
f.close();
//return i
return i;
}
//this function updates the user's balance
//also checks the validity of input
void user::update_amount(float a)
{
if(a > 100 || a < 1) cout << "\n Invalid
amount\n";
else if(a > amount) cout << "\nInsufficient
balance\n";
else
{
amount -= a;
cout << "\nRemaining balance: " << amount <<
"\n";
}
limit--;
}
//this function is used to enter a pin
//without disclosing the user input
//it returns the entered pin
int enter_pin()
{
string pass;
char ch;
int pin, i;
//loop to secretly enter pin
while((ch = getch()) != 13)
{
putchar('*');
pass += ch;
}
//convert string password to integer pin
pin = atoi(pass.c_str());
//return pin
return pin;
}
//check the existence of a pin
//if it exists, return the index
//else return -1
int check_pin(int pin, user *arr, int count)
{
for(int i = 0; i < count; i++)
{
if(arr[i].get_pin() == pin)
return i;
}
return -1;
}
int main()
{
//seed the random function
srand(time(0));
//declare an array of atm users
user arr[100];
int count, pin;
//load pins
count = load_pins(arr);
char choice = ' ', ch;
//menu loop
while(choice != 'x')
{
cout << "e: enter pin\n";
cout << "c: change pin\n";
cout << "x: exit\n";
cin >> choice;
switch(choice)
{
case 'e':
{
pin = enter_pin();
int i = check_pin(pin, arr, count);
float amount;
//check if pin exists
if(i != -1)
{
//check if 3 withdrawals have been made
if(arr[i].get_limit() > 0)
{
cout << "\nEnter amount to withdraw: ";
cin >> amount;
arr[i].update_amount(amount);
}
else
{
cout << "Withdrawal limit reached\n";
}
}
else
{
cout << "\nPin does not exist\n";
}
}
break;
case 'c':
{
cout << "Enter old pin: ";
pin = enter_pin();
int i = check_pin(pin, arr, count);
//check if pin exists
if(i != -1)
{
cout << "\nEnter new pin: ";
cin >> pin;
//change pin
arr[i].set_pin(pin);
cout << "\nPIN changed\n";
//update to file
ofstream f;
f.open("pins.txt");
for(int j = 0; j < count; j++) f << arr[j].get_pin() << endl;
//close file
f.close();
}
}
break;
case 'x':
cout << "\nProgram Terminated";
break;
default:
cout << "\nInvalid option\n";
break;
}
}
return 0;
}
Contents of pins.txt before program start
Contents of pins.txt after program termination
OUTPUT