Question

In: Computer Science

Practice Coding Task C++ ATM Machine with Some Classes Create an ATM machine in C++ with...

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.

Solutions

Expert Solution

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



Related Solutions

C++ Assignment 1: Make two classes practice For each of the two classes you will create...
C++ Assignment 1: Make two classes practice For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. Class #1 Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc Take your chosen class from...
Exceptional task in c++ OOP: 1. Create three exceptions classes / structures, for division by zero,...
Exceptional task in c++ OOP: 1. Create three exceptions classes / structures, for division by zero, first second for out of range, third for reading file that doesn't exist. Secure given code with self-made exceptions
Task use c++ and Create Base class task with virtual method Create a pair of derivative...
Task use c++ and Create Base class task with virtual method Create a pair of derivative classes of architecture, scientist, economist - Define a salary in each of these classes and create a method that prints the salary for each job Create a working object Use dynamic_cast Use typeid Create an object for each job that it will include the number of employees of that type and the method of printing these numbers
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this uml as a reference.
(MUST BE DONE IN C (NOT C++)) In this task, you will create a structure with...
(MUST BE DONE IN C (NOT C++)) In this task, you will create a structure with arrays. You will have to create your own structure. However, make sure to meet these guidelines: - Give the structure whichever name you want. - It must have at least 3 members. - Two of the members must be arrays. - Your members should be of at least two different data-types. In other words, your members cannot be integers only (or floats, or doubles…)....
Task 1. Create class "Vehicle" with variables "company" and "year". LM 2. Create 3 derived classes:...
Task 1. Create class "Vehicle" with variables "company" and "year". LM 2. Create 3 derived classes: "Bus", "Car", and "Motorcycle". They should contain a variable storing number of seats (bus) / weight (car) / number of tires (motorcycle), constructor, destructor and a print_info function that will print all the information about the vehicle. PK 3. The constructors should take as parameters: company, year and number of seats / weight / number of tires. Inside it assign the company name and...
In this task, you will create a Python script in which you will practice reading files...
In this task, you will create a Python script in which you will practice reading files in Python and writing them to a new output file. Construct a text file called Py4_Task3_input.txt that has the following lines: 4 Sandwiches 04 July 2020 Pasta 31 October 2014 Hot Dogs 15 November 2005 Tacos 25 December 1986 The first line of the file represents the number of lines in the data file. Write a loop that reads in each additional line (one...
Simulate an ATM machine. Create ten accounts in an array with id 0, 1, . ....
Simulate an ATM machine. Create ten accounts in an array with id 0, 1, . . . , 9, and initial balance $100. The system prompts the user to enter an id. If the id is entered incorrectly, ask the user to enter a correct id. Once an id is accepted, the main menu is displayed as shown in the sample run. You can enter a choice 1 for viewing the current balance, 2 for withdrawing money, 3 for depositing...
This question has been answered already however, I'd like some new examples. Task: In C#, create...
This question has been answered already however, I'd like some new examples. Task: In C#, create a minimum of three try/catch statements that would handle potential input errors. Thank you.
Create this C++ program using classes 1. Create a file text file with a string on...
Create this C++ program using classes 1. Create a file text file with a string on it 2. Check the frecuency of every letter, number and symbol (including caps) 3. Use heapsort to sort the frecuencys found 4. Use huffman code on the letters, symbols or numbers that have frecuencys I created the file, and the frecuency part but i'm having trouble with the huffman and heapsort implementation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT