1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
#include "ATM.h"
#include <iostream>
using std::cout;//needed for printing to the screen
using std::cin;//needed for reading from the keyboard
using std::endl;//needed to create a newline
#include <cstdlib>
#include<string>
using std::getline;//function getline comes from the string library
using std::string;//needed to declare string variables/objects
ATM::ATM(int pinNum)
{
if(!isValidPin(pinNum))//calling function isValidPin and passing it pinNum
{
exit(-1);/*function exit() comes from library cstdlib causes any program to quit immediately*/
}
else
pin = pinNum;/*if we get here, then the pinNum was valid and we assign it to
our data member, pin */
cout<<"Please enter your date of birth in the format: dd/mm/yyyy ";
/*function getline grabs an entire line of text INCLUDING spaces;
stops reading when the user hits enter or when it reaches the end of line;
getline grabs text using cin which as you know, is connected to the keyboard;
getline saves the text it grabs inside of a string variable, in our case,
inside of the variable, dateOfBirth.
*/
string dateOfBirth;//we need to declare a string to store the date of birth
getline(cin, dateOfBirth);//grabs a string in the format dd/mm/yyyy
parseDOB(dateOfBirth);
string fullName;
cout<<"\nPlease enter your full name: ";
getline(cin, fullName);//grabs a first and last name separated by a space and stores it in fullName
parseName(fullName);//passes fullname to function parseName() that will separate and assign to firstName and lastName respectively
cout<<"\nPlease enter your address. It must not exceed 25 characters.\n";
getline(cin, address);//grabs the full address from the keyboard, including spaces up until enter is hit or newline encountered
setAddress(address);//passing address to function verifyAddress()
balance = 0.0;//initializing the balance to zero
loggedIn = false;//User must login using their new pin before they can do anything
}
ATM::ATM(int pinNum, string dob, string name, string add)
{
pin = pinNum;
ATM::parseDOB(dob);
ATM::parseName(name);
address = add;
balance = 0.0;
}
ATM::ATM(int pinNum, string dob, string name)
{
pin = pinNum;
ATM::parseDOB(dob);
ATM::parseName(name);
}
ATM::ATM(int pinNum, string dob, string name, double startBal)
{
pin = pinNum;
ATM::parseDOB(dob);
ATM::parseName(name);
if (startBal<0){
balance = 0.0;
}
else{
balance = startBal;
}
}
void ATM::printStatement()
{
cout<<"Bank of the People"<<endl<<ATM::getFirstName()<<ATM::getLastName()<<endl<<ATM::getDay()<<" "<<ATM::getMonth()<<" "<<ATM::getYear()<<endl<<balance<<endl;
}
void ATM::setAddress(string add)
{
if(add.length() > 25)//checking if the address received is longer than 25 characters
{
cout<<add<<" exceeds 25 characters! Only 25 characters will be used\n";
string tempaddress = add.substr(0,25);
setAddress(tempaddress);//assigns the shortened address to data member, address
}
else//this code only runs if the condition above fails
{
setAddress(add);
}
}
string ATM::getAddress()
{
return address;
}
//prints customer's name, dob, address and current balance
void ATM::deposit(double amount)
{if (loggedIn == 0){
cout<<"Error. You are not logged in."<<endl;
}
else if (amount<0){
cout<<"Error: Amount entered is negative."<<endl;
}
else {
balance = balance + amount;
}
}
void ATM::printWelcomeScreen()
{
cout<<"Welcome to the Bank of the People"<<endl;
}
void ATM::setDay(int d)
{
if ((d<=31)&&(d>0)){
day = d;
}
else {
day = 1;
}
}
void ATM::setMonth(int m)
{
if ((m<=12)&&(m>0)){
month = m;
}
else {
month = 1;
}
}
void ATM::setYear(int y)
{
if ((y>0)&&(y<=2015)){
year = y;
}
else{
year = 2015;
}
}
int ATM::getDay()
{
return day;
}
int ATM::getMonth()
{
return month;
}
int ATM::getYear()
{
return year;
}
void ATM::setFirstName(string fname)
{
firstName = fname;
}
void ATM::setLastName(string lname)
{
lastName = lname;
}
string ATM::getFirstName()
{
return firstName;
}
string ATM::getLastName()
{
return lastName;
}
bool ATM::isLoggedIn()
{
return loggedIn;
}
bool ATM::withdraw(double amount)
{
ATM::isLoggedIn();
if (loggedIn == 0){
cout<<"Error. You are not logged in."<<endl;
return 0;
}
else if (balance<amount){
cout<<"Error: Insufficient funds"<<endl;
return 0;
}
else if(amount<=0){
cout<<"Error: amount entered is negative"<<endl;
return 0;
}
else {
balance = balance - amount;
return 1;
}
}
void ATM::login(int pinNum)
{
if (pinNum == pin){
loggedIn = 1;
cout<<"You are now logged in"<<endl;
}
else{
loggedIn = 0;
cout<<"Status: login failed"<<endl;
}
}
bool ATM::changePin(int newPin)
{
ATM::isLoggedIn();
if (loggedIn == 0){
cout<<"Error. You are not logged in."<<endl;
return 0;
}
else {
pin = newPin;
return 1;
}
}
bool ATM::isValidPin(int pinNum)
{
if(pinNum < 1000 || pinNum > 9999)
{
cout<<"Pin number must be 4 digits long.";
return false;//returning false means that pinNum's value was inappropriate
}
else //now check whether all 4 digits are unique
{
short int firstDigit = pinNum/1000;//int divided by int returns an int
pinNum = pinNum%1000;//pinNum will now get the remainder from the division
short int secondDigit = pinNum/100;
pinNum = pinNum%100;
short int thirdDigit = pinNum/10;
short int fourthDigit = pinNum%10;
return(firstDigit != secondDigit && firstDigit!=thirdDigit && firstDigit!=fourthDigit && secondDigit!=thirdDigit && secondDigit!=fourthDigit && thirdDigit!=fourthDigit);
}
}
void ATM::parseDOB(string dob)
{
string day = dob.substr(0,2);
int d = stoi(day);
ATM::setDay(d);
string month = dob.substr(3,2);
int m = stoi(month);
ATM::setMonth(m);
string year = dob.substr(6,4);
int y = stoi(year);
ATM::setYear(y);
}
void ATM::parseName(string fullName)
{
int position = fullName.find(" ");
int position2 = fullName.find("\n");
string firstname = fullName.substr(0,position);
ATM::setFirstName(firstname);
string lastname = fullName.substr(position,position2);
ATM::setLastName(lastname);
}
|