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
|
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
class Iesnc {
public:
Iesnc(int = 0, const std::string & = "", const std::string & = "", double = 0.0);
void setAccountNumber(int);
int getAccountNumber() const;
void setLastName(const std::string&);
std::string getLastName() const;
void setFirstName(const std::string&);
std::string getFirstName() const;
void setBalance(double);
double getBalance() const;
private:
int accountNumber {};
char lastName[15] {};
char firstName[10] {};
double balance {};
};
Iesnc::Iesnc(int accountNumberValue, const std::string& lastNameValue, const std::string& firstNameValue, double balanceValue)
: accountNumber(accountNumberValue), balance(balanceValue) {
setLastName(lastNameValue);
setFirstName(firstNameValue);
}
int Iesnc::getAccountNumber() const {
return accountNumber;
}
void Iesnc::setAccountNumber(int accountNumberValue) {
accountNumber = accountNumberValue;
}
std::string Iesnc::getLastName() const {
return lastName;
}
void Iesnc::setLastName(const std::string& lastNameString) {
auto length {lastNameString.size()};
length = (length < 15 ? length : 14);
lastNameString.copy(lastName, length);
lastName[length] = '\0';
}
std::string Iesnc::getFirstName() const {
return firstName;
}
void Iesnc::setFirstName(const std::string& firstNameString) {
auto length {firstNameString.size()};
length = (length < 10 ? length : 9);
firstNameString.copy(firstName, length);
firstName[length] = '\0';
}
double Iesnc::getBalance() const {
return balance;
}
void Iesnc::setBalance(double balanceValue) {
balance = balanceValue;
}
void outputLine(std::ostream&, const Iesnc&);
void writeToDat(std::fstream&);
void readFromDat(std::fstream&);
//void saveDatabase(std::fstream&);
int main() {
std::fstream masterStream("master.dat", std::ios::in | std::ios::out | std::ios::binary);
std::fstream saveLoadDatabase("databaseCopy.dat", std::ios::in | std::ios::out | std::ios::binary);
if (!masterStream || !saveLoadDatabase)
return (std::cout << "Cannot open files\n"), 1;
for (int key {-1}; key != 0; ) {
std::cout << "\n1 >> write\n";
std::cout << "2 >> read\n";
std::cout << "3 >> refresh\n";
std::cout << "4 >> save database\n";
std::cout << "0 >> exit\n";
std::cout << "Enter option: ";
std::cin >> key;
switch (key) {
case 1:
writeToDat(masterStream);
break;
case 2:
readFromDat(masterStream);
break;
case 3:
std::cout << "Not yet implemented\n";
//system("C:/Users/iceberg/Desktop/kocgun/march/iesnc/build-iesnc-Desktop_Qt_6_2_3_MinGW_64_bit-Release/release/iesnc.exe");
break;
case 4:
std::cout << "Not yet implemented\n";
//cout << "save dataBase\n";
//saveDatabase(saveLoadDatabase, client);
break;
case 0:
break;
default:
std::cout << "Invalid option\n";
break;
}
}
}
void outputLine(std::ostream& output, const Iesnc& record) {
output << std::left << std::setw(10) << record.getAccountNumber()
<< std::setw(16) << record.getLastName()
<< std::setw(11) << record.getFirstName()
<< std::setw(10) << std::setprecision(2) << std::right << std::fixed
<< std::showpoint << record.getBalance() << '\n';
}
void writeToDat(std::fstream& masterStream) {
if (!masterStream) {
std::cerr << "File not opened.\n";
return;
}
int accountNumber {};
std::string lastName;
std::string firstName;
double balance {};
do {
do {
std::cout << "Enter account number (1 to 100, 0 to end input): ";
std::cin >> accountNumber;
} while ((accountNumber < 0 || accountNumber > 100) && (std::cout << "Invalid account\n"));
if (accountNumber != 0) {
std::cout << "Enter last name, first name, balance: ";
std::cin >> lastName;
std::cin >> firstName;
std::cin >> balance;
const Iesnc client {accountNumber, lastName, firstName, balance};
masterStream.seekp((accountNumber - 1) * sizeof(Iesnc));
masterStream.write(reinterpret_cast<const char*>(&client), sizeof(Iesnc));
}
} while (accountNumber != 0);
}
void readFromDat(std::fstream& masterStream) {
if (!masterStream) {
std::cerr << "File not opened.\n";
return;
}
std::cout << std::left << std::setw(10) << "\nAccount" << std::setw(16)
<< "Last Name" << std::setw(11) << "First Name" << std::left
<< std::setw(11) << std::right << "Balance\n";
masterStream.seekg(0);
for (Iesnc client; masterStream.read(reinterpret_cast<char*>(&client), sizeof(Iesnc)); )
if (client.getAccountNumber() != 0)
outputLine(std::cout, client);
masterStream.clear();
}
/*
void saveDatabase(fstream& saveLoadDatabase, const Iesnc&) {
cout << "saveDatabase" << endl;
cout << left << setw(10) << "Account" << setw(16)
<< "Last Name" << setw(11) << "First Name" << left
<< setw(10) << right << "Balance" << endl;
Iesnc client;
saveLoadDatabase.read(reinterpret_cast<char*>(&client), sizeof(Iesnc));
while (saveLoadDatabase && !saveLoadDatabase.eof()) {
if (client.getAccountNumber() != 0)
outputLine(cout, client);
saveLoadDatabase.read(reinterpret_cast<char*>(&client),
sizeof(Iesnc));
}
}
*/
|