Last active
April 16, 2021 05:02
-
-
Save AlBannaTechno/5419eadd48915f1644158eadc579efff to your computer and use it in GitHub Desktop.
Solve [Marwa] Accounts Information System - v1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include <sstream> | |
#include<iomanip> | |
using namespace std; | |
string int_to_str(int i) // convert int to string | |
{ | |
stringstream s; | |
s << i; | |
return s.str(); | |
} | |
struct account_query { | |
int account_number; | |
char firstName[10]; | |
char lastName[10]; | |
float total_Balance; | |
}; | |
struct link { | |
account_query data{}; | |
link *previous{nullptr}; | |
link *next{nullptr}; | |
}; | |
class Records { | |
private: | |
link *latest{nullptr}; | |
static void print_account(link link){ | |
cout << "Account number: " << link.data.account_number | |
<< ", FN: " << link.data.firstName | |
<< ", LN: " << link.data.lastName | |
<< ", Balance = " << link.data.total_Balance | |
<< endl; | |
} | |
static int read_account_number(){ | |
int n; | |
cout << "your number "; | |
cin >> n; | |
return n; | |
} | |
link* create_record(link* old_version = nullptr){ | |
int a; | |
char f[10], l[10]; | |
float t; | |
link *ctr = new link; | |
if (old_version != nullptr){ | |
cout << "enter dash character - to ignore character based fields update" << endl; | |
cout << "enter number -1 to ignore numeric based fields update" << endl; | |
} | |
// user should not be able to update account id. | |
if (old_version == nullptr){ | |
while (true){ | |
cout << endl << "number is [write -1, to delegate the decision to the system]"; | |
cin >> a; | |
if (a == -1){ | |
if (latest == nullptr){ | |
ctr->data.account_number = 1; | |
} else{ | |
int i; | |
// to prevent duplication if user used mixed automatic and manual account number insertion | |
for (i = latest->data.account_number + 1; i < INT_MAX ; ++i) { | |
if (find_record(i) == nullptr){ | |
break; | |
} | |
} | |
ctr->data.account_number = i; | |
} | |
} else{ | |
auto acc = find_record(a, true); | |
if (acc == nullptr){ | |
ctr->data.account_number = a; | |
break; | |
}else{ | |
cout << endl << "This number already exist" << endl; | |
} | |
} | |
} | |
} | |
cout <<"first name is : " << ((old_version != nullptr) ? old_version->data.firstName : "") << " "; | |
cin >> f; | |
strncpy_s(ctr->data.firstName, f, 10); | |
cout <<"last name is " << ((old_version != nullptr) ? old_version->data.lastName : "") << " "; | |
cin >> l; | |
strncpy_s(ctr->data.lastName, l, 10); | |
cout <<"balance is " << ((old_version != nullptr) ? int_to_str(old_version->data.total_Balance) : "") << " "; | |
cin >> t; | |
ctr->data.total_Balance = t; | |
return ctr; | |
} | |
link* find_record(int id, bool silent = false){ | |
link *read; | |
read = latest; | |
if (read == nullptr){ | |
if(!silent){ | |
cout << "not inserted" << endl; | |
} | |
} | |
while (read != nullptr) { | |
if (read->data.account_number == id){ | |
return read; | |
} | |
read = read->previous; | |
} | |
return nullptr; | |
} | |
public: | |
Records() { latest = nullptr; } | |
void write_rec() { | |
auto account = create_record(); | |
account->previous = latest; | |
if (latest != nullptr){ | |
latest->next = account; | |
} | |
latest = account; | |
} | |
void read_rec() { | |
int n = read_account_number(); | |
auto account = find_record(n); | |
if (account == nullptr){ | |
cout << "Not Found"; | |
} else{ | |
print_account(*account); | |
} | |
cout << endl; | |
} | |
void update_rec() { | |
int n = read_account_number(); | |
auto account = find_record(n); | |
if (account == nullptr){ | |
cout << "Not Found"; | |
} else{ | |
auto new_account = create_record(account); | |
if (!strcmp("-", new_account->data.firstName)){ | |
strncpy_s(account->data.firstName, new_account->data.firstName, 10); | |
} | |
if (!strcmp("-", new_account->data.lastName)){ | |
strncpy_s(account->data.lastName, new_account->data.lastName, 10); | |
} | |
if (new_account->data.account_number != -1){ | |
account->data.total_Balance = new_account->data.total_Balance; | |
} | |
} | |
cout << endl; | |
} | |
void delete_rec() { | |
int n = read_account_number(); | |
auto account = find_record(n); | |
if (account == nullptr){ | |
cout << "Not Found"; | |
} else{ | |
if (account->previous != nullptr){ | |
account->previous->next = account->next; | |
} | |
if (account->next != nullptr){ | |
account->next->previous = account->previous; | |
} | |
delete account; | |
} | |
cout << endl; | |
} | |
void show_all(){ | |
auto ctr = latest; | |
while (ctr != nullptr){ | |
print_account(*ctr); | |
ctr = ctr->previous; | |
} | |
} | |
}; | |
int main() { | |
Records s1; | |
int n; | |
while (true) { | |
cout << "***Accounts Information System***" << endl; | |
cout << "Select one option from" << endl; | |
cout << setw(6) << "1-->Add record to the system" << endl; | |
cout << setw(6) << "2-->Show record from the system" << endl; | |
cout << setw(6) << "3-->Update record from the system" << endl; | |
cout << setw(6) << "4-->Delete record from the system" << endl; | |
cout << setw(6) << "5-->Show All records from the system" << endl; | |
cout << setw(6) << "6-->Quit the system" << endl; | |
cout << "enter your choice:" << endl; | |
cin >> n; | |
switch (n) { | |
case 1: | |
s1.write_rec(); | |
break; | |
case 2: | |
s1.read_rec(); | |
break; | |
case 3: | |
s1.update_rec(); | |
break; | |
case 4: | |
s1.delete_rec(); | |
break; | |
case 5: | |
s1.show_all(); | |
break;; | |
case 6: | |
return 0; | |
default: | |
cout << "wrong number." << endl; | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment