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
|
#include <iostream>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::ios;
using std::left;
using std::right;
using std::showpoint;
#include <fstream>
using std::ofstream;
using std::ostream;
using std::fstream;
#include <iomanip>
using std::setw;
using std::setprecision;
#include <cstdlib>
using std::exit;
#include "ClientData.h"
int enterChoice();
void createTextFile(fstream&);
void updateRecord(fstream&);
void newRecord(fstream&);
void deleteRecord(fstream&);
void outputLine(ostream&, const ClientData&);
int getAccount(const char * const);
enum Choices {PRINT = 1, UPDATE, NEW, DELETE, END};
int main()
{
fstream inOutCredit("client.dat",ios::app);
if (!inOutCredit)
{
cerr<<"File could not be opened, Please try again!"<<endl;
exit(1);
}
int choice;
while((choice = enterChoice()) != END)
{
switch (choice)
{
case PRINT:
createTextFile(inOutCredit);
break;
case UPDATE:
updateRecord(inOutCredit);
break;
case NEW:
newRecord(inOutCredit);
break;
case DELETE:
deleteRecord(inOutCredit);
break;
default:
cerr<<"Your input was unable to comply!"<<endl;
break;
}
inOutCredit.clear();
}
}
int enterChoice()
{
cout<<"\nEnter your selection:"<<endl
<<"1 - Create a database copy for printing purpose (ESRes_print.txt)"<<endl
<<"2 - Update a student record"<<endl
<<"3 - Add a new student record"<<endl
<<"4 - Delete a student record"<<endl
<<"5 - Terminate program\n";
int menuSelection;
cin>>menuSelection;
return menuSelection;
}
void createTextFile(fstream&readFromFile)
{
ofstream outPrintFile("ESRes_print.txt", ios::out);
if (!outPrintFile)
{
cerr<<"Your request was unable to comply, no print file created!"<<endl;
exit(1);
}
outPrintFile<<left<<setw(10)<<"Account"<<setw(16)<<"Last Name"<<setw(11)<<"First Name"<<right<<setw(10)<<"Balance"<<endl;
readFromFile.seekg(0);
ClientData client;
readFromFile.read(reinterpret_cast<char*>(&client),sizeof(ClientData));
while (!readFromFile.eof())
{
if (client.getAccountNumber() != 0)
outputLine(outPrintFile,client);
readFromFile.read(reinterpret_cast<char*>(&client),sizeof(ClientData));
}
}
void updateRecord(fstream&updateFile)
{
int accountNumber = getAccount("Enter account to update");
updateFile.seekg((accountNumber - 1)*sizeof(ClientData));
ClientData client;
updateFile.read(reinterpret_cast<char*>(&client),sizeof(ClientData));
if (client.getAccountNumber() != 0)
{
outputLine(cout, client);
cout<<"\nEnter charge (+) or payment (-) :";
double transaction;
cin>>transaction;
double oldBalance = client.getBalance();
client.setBalance(oldBalance + transaction);
outputLine(cout,client);
updateFile.seekp((accountNumber - 1)*sizeof(ClientData));
updateFile.write(reinterpret_cast<const char*>(&client),sizeof(ClientData));
}
else
cerr<<"Account #"<<accountNumber<<" has no information"<<endl;
}
void newRecord(fstream&insertInFile)
{
int accountNumber = getAccount("Enter new account number");
insertInFile.seekg((accountNumber - 1)*sizeof(ClientData));
ClientData client;
insertInFile.read(reinterpret_cast<char*>(&client),sizeof(ClientData));
if (client.getAccountNumber() == 0)
{
char lastName[15];
char firstName[10];
double balance;
cout<<"Enter lastname, firstname, balance\n?";
cin>>setw(15)>>lastName;
cin>>setw(10)>>firstName;
cin>>balance;
client.setLastName(lastName);
client.setFirstName(firstName);
client.setBalance(balance);
client.setAccountNumber(accountNumber);
insertInFile.seekp((accountNumber - 1)*sizeof(ClientData));
insertInFile.write(reinterpret_cast<const char*>(&client),sizeof(ClientData));
}
else
cerr<<"Account #"<<accountNumber<<" is already contains information."<<endl;
}
void deleteRecord(fstream&deleteFromFile)
{
int accountNumber = getAccount("Enter account to delete");
deleteFromFile.seekg((accountNumber - 1)*sizeof(ClientData));
ClientData client;
deleteFromFile.read(reinterpret_cast<char*>(&client),sizeof(ClientData));
if (client.getAccountNumber() != 0)
{
ClientData blankClient;
deleteFromFile.seekp((accountNumber - 1)*sizeof(ClientData));
deleteFromFile.write(reinterpret_cast<const char*>(&blankClient),sizeof(ClientData));
cout<<"Account #"<<accountNumber<<" deleted.\n";
}
}
void outputLine(ostream&output, const ClientData&record)
{
output<<left<<setw(10)<<record.getAccountNumber()<<setw(16)<<record.getLastName()<<setw(11)<<record.getFirstName()<<setw(10)<<setprecision(2)<<right<<fixed<<showpoint<<record.getBalance()<<endl;
}
int getAccount(const char* const prompt)
{
int accountNumber;
do
{
cout<<prompt<<"(1 - 100):";
cin>>accountNumber;
}while(accountNumber < 1 || accountNumber > 100);
return accountNumber;
}
|