Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Mohanad Ayman Aly
- 20180296
- */
- #include <iostream>
- #include <string.h>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- const int MAX_ADDRESS = 50, MIN_ADDRESS = 5;
- const int TRANSACTIONS = 10, CLIENTS = 20;
- const int MAX_NAME = 30, MIN_NAME = 3;
- const int MAX_TEMP = 20;
- const int BASEBALANCE = 5000;
- const char BREAK = '.';
- char cities[CLIENTS][MAX_ADDRESS];
- int citiesCounts[CLIENTS];
- int lastClient = 0;
- int userMaxTransactions, userMaxClients;
- /////////////////////////////////////////////////////////////
- // Start of Function Prototypes
- //Primary Functions
- void GetSizes();
- void PrintMain(int& i);
- void ClientInfo(int startID, int endID);
- void GetName(char name[], int& exitName);
- void GetAddress(char number[], char street[], char neighborhood[], char city[]);
- void ClientTransactions(int id, int startID, int endID);
- void EditInfo();
- void ClientsInCities();
- void PrintBalanceOf();
- void PrintAllBalances();
- void PrintGreatestBalance();
- // Supplementary Functions
- int CheckString(char variable[], int MIN, int MAX);
- int isNumeric(char variable[]);
- int FindClient(char name[]);
- int FindCity(char city[]);
- int CharToInt(char variable[]);
- void EditTransactions(int id);
- void SetFormat(char variable[]);
- void CalculateBalance(int id, char transaction, int amount);
- // End of Function Prototypes
- /////////////////////////////////////////////////////////////
- struct Clients
- {
- int tAmounts[TRANSACTIONS];
- int balance;
- int lastTransaction;
- char name[MAX_NAME];
- char number[MAX_ADDRESS / 4];
- char street[MAX_ADDRESS];
- char neighborhood[MAX_ADDRESS];
- char city[MAX_ADDRESS];
- char tTypes[TRANSACTIONS];
- };
- Clients client[CLIENTS];
- int main()
- {
- char preCHOICE[MAX_ADDRESS];
- int choice, i, exit = 0;
- GetSizes();
- ClientInfo(0, userMaxClients);
- PrintMain(i);
- while (true)
- {
- cin.getline(preCHOICE, MAX_ADDRESS);
- if (CharToInt(preCHOICE) != -1)
- {
- choice = CharToInt(preCHOICE);
- if (choice > 5 + i) cout<<"\nInvalid choice, please try again." << endl;
- else {
- switch (choice)
- {
- case 0: {exit = 1; break; }
- case 1: {PrintAllBalances(); break; }
- case 2: {PrintBalanceOf(); break; }
- case 3: {PrintGreatestBalance(); break; }
- case 4: {EditInfo(); break; }
- case 5: {ClientsInCities(); break; }
- case 6: {ClientInfo(lastClient, userMaxClients); break; }
- default: {break; }
- }
- if (exit == 1)
- {
- cout<<"\nThank you for using our system!" << endl;
- break;
- }
- PrintMain(i);
- }
- }
- else cout<<"\nPlease enter a positive integer number" << endl;
- }
- return 0;
- }
- /////////////////////////////////////////////////////////////
- // Start of Function Definitions
- void GetSizes()
- {
- char preCLIENTS[MAX_ADDRESS], preTRANSACTIONS[MAX_ADDRESS];
- while (true) {
- cout<<"How Many Clients Would You Like To Add?" << endl;
- cin.getline(preCLIENTS, MAX_ADDRESS);
- if (CharToInt(preCLIENTS) != -1)
- {
- userMaxClients = CharToInt(preCLIENTS);
- if (userMaxClients > CLIENTS) {
- cout<<"\nCan't add more than " << CLIENTS << " clients." << endl;
- continue;
- }
- break;
- }
- else cout<<"\nPlease enter a positive integer number" << endl;
- }
- while (true) {
- cout<<"\nHow Many Transactions Would You Like To Add?" << endl;
- cin.getline(preTRANSACTIONS, MAX_ADDRESS);
- if (CharToInt(preTRANSACTIONS) != -1)
- {
- userMaxTransactions = CharToInt(preTRANSACTIONS);
- if (userMaxTransactions > TRANSACTIONS) {
- cout<<"\nCan't add more than " << TRANSACTIONS << " transactions." << endl;
- continue;
- }
- break;
- }
- else cout<<"\nPlease enter a positive integer number" << endl;
- }
- }
- void PrintMain(int& i)
- {
- cout<<"\nWelcome To Bank Management System" << endl << endl;
- cout<<"(0) Exit" << endl;
- cout<<"(1) Print All Balances" << endl;
- cout<<"(2) Print Customer's Balance" << endl;
- cout<<"(3) Print Highest Balance" << endl;
- cout<<"(4) Change Customer's Info" << endl;
- cout<<"(5) Print Numbers of Clients in Cities" << endl;
- if (lastClient < userMaxClients) {
- i = 1;
- cout<<"(6) Add " << userMaxClients - lastClient << " new clients" << endl;
- }
- else i = 0;
- cout<<"\nEnter the number for your choice: ";
- }
- void ClientInfo(int startID, int endID)
- {
- int exitName = 0;
- for (int i = startID; i < endID; i++) {
- cout<<"\nPlease Enter Client " << i + 1 << "'s Name:" << endl;
- GetName(client[i].name, exitName);
- if (exitName == 1) break;
- cout<<"\nPlease Enter Their Address\n(Number, Street, Neighborhood, City)" << endl;
- GetAddress(client[i].number, client[i].street, client[i].neighborhood, client[i].city);
- cout<<"\nPlease Enter Their Transactions In The Following Format" << endl;
- cout<<"\n( W for Withdraw / D for Deposit ) (Numerical Amount)" << endl;
- cout<<"\n(Maximum " << userMaxTransactions << " Transactions)" << endl;
- cout<<"\n(Enter '" << BREAK << " 0' to stop)" << endl;
- client[i].balance = BASEBALANCE;
- ClientTransactions(i, 0, userMaxTransactions);
- cin.ignore();
- lastClient++;
- }
- }
- void GetName(char name[], int& exitName)
- {
- if (lastClient > 0) cout<<"\n(Enter '" << BREAK << "' To Stop)" << endl;
- exitName = 0;
- cin.getline(name, MAX_NAME);
- if (CheckString(name, MIN_NAME, MAX_NAME) == 0) {
- cout<<"\nInvalid or too short name, please try again." << endl;
- GetName(name, exitName);
- }
- else if (CheckString(name, MIN_NAME, MAX_NAME) == 2) {
- if (lastClient < 1) {
- cout<<"\nPlease enter info of at least 1 customer" << endl;
- GetName(name, exitName);
- }
- else exitName = 1;
- }
- else SetFormat(name);
- }
- void GetAddress(char number[], char street[], char neighborhood[], char city[])
- {
- cin.getline(number, MAX_ADDRESS / 4, ',');
- cin.getline(street, MAX_ADDRESS, ',');
- cin.getline(neighborhood, MAX_ADDRESS, ',');
- cin.getline(city, MAX_ADDRESS);
- if (isNumeric(number) == 0 ||
- CheckString(street, MIN_ADDRESS, MAX_ADDRESS) == 0 ||
- CheckString(neighborhood, MIN_ADDRESS, MAX_ADDRESS) == 0 ||
- CheckString(city, MIN_ADDRESS, MAX_ADDRESS) == 0)
- {
- cout<<"\nInvalid or too short address, please try again." << endl;
- GetAddress(number, street, neighborhood, city);
- }
- else {
- SetFormat(street);
- SetFormat(neighborhood);
- SetFormat(city);
- // Count numbers of clients in cities
- int matching = 1;
- for (int i = 0; i < userMaxClients; i++)
- {
- matching = 1; // The cities are matching by default
- for (int j = 0; city[j] != '\0'; j++)
- {
- if (city[j] != cities[i][j]) { matching = 0; break; } // A difference in one character makes them not-matching
- }
- if (matching == 1) { citiesCounts[FindCity(city)]++; break; } // If they end up matching, increase the count of city in cityCounts
- }
- static int id = 0;
- if (matching == 0) // If they are not matching, store the city and initiliaze its count with 1
- {
- strcpy(cities[id], city);
- citiesCounts[id] = 1;
- id++;
- }
- }
- }
- void ClientTransactions(int id, int startID, int endID)
- {
- client[id].lastTransaction = 0;
- for (int i = startID; i < endID; i++) {
- cin>>client[id].tTypes[i]>>client[id].tAmounts[i];
- if (client[id].tTypes[i] == BREAK && client[id].tAmounts[i] == 0) break;
- else if ((client[id].tTypes[i] != 'W' && client[id].tTypes[i] != 'w'
- && client[id].tTypes[i] != 'D' && client[id].tTypes[i] != 'd'
- ) || client[id].tAmounts[i] <= 0) {
- cout<<"\nInvalid transaction, please try again." << endl;
- i--;
- continue;
- }
- else {
- if (client[id].tTypes[i] == 'W' || client[id].tTypes[i] == 'w') {
- if (client[id].balance >= client[id].tAmounts[i]) CalculateBalance(id, client[id].tTypes[i], client[id].tAmounts[i]);
- else {
- cout<<"\nInsufficient Balance For Transaction, Please Try Again." << endl;
- i--;
- continue;
- }
- }
- else CalculateBalance(id, client[id].tTypes[i], client[id].tAmounts[i]);
- client[id].lastTransaction++;
- }
- }
- }
- void EditInfo()
- {
- char name[MAX_NAME], preCHOICE[MAX_TEMP];
- int exitName, choice, id;
- cout<<"\nWhose info would you like to edit?" << endl;
- cout<<"\nAvailable clients:"<<endl;
- for(int i = 0; i<lastClient; i++) cout<<'-'<<client[i].name<<endl;
- cout<<endl;
- while (true)
- {
- GetName(name, exitName);
- id = FindClient(name);
- if (id != -1 || exitName == 1) break;
- else cout<<"\nClient not found, please try again." << endl;
- }
- if (exitName != 1)
- {
- cout<<"\nChange " << name << "'s Info" << endl;
- while (true)
- {
- cout<<"\n(0) Exit" << endl;
- cout<<"(1) Name" << endl;
- cout<<"(2) Address" << endl;
- cout<<"(3) Transactions" << endl;
- cout<<"(4) Balance" << endl;
- cout<<"\nEnter the number of your choice:" << endl;
- cin.getline(preCHOICE, MAX_TEMP);
- if (CharToInt(preCHOICE) != -1) {
- choice = CharToInt(preCHOICE);
- if (choice < 0 || choice > 4) cout<<"\nInvalid choice, please try again." << endl;
- else if (choice >= 1 && choice <= 4)
- {
- switch (choice) {
- case 1: {
- cout<<"\nPlease enter client's new name:" << endl;
- GetName(client[id].name, exitName);
- if (exitName != 1) cout<<"\nClient's current name is " << client[id].name << endl;
- else { cout<<"\nClient's name not changed" << endl; strcpy(client[id].name, name); }
- break;
- }
- case 2: {
- cout<<"\nPlease enter " << client[id].name << "'s new address:" << endl;
- GetAddress(client[id].number, client[id].street, client[id].neighborhood, client[id].city);
- cout<<"\nClient's current address is:" << endl;
- cout<<endl << client[id].number << ", " << client[id].street << ", " << client[id].neighborhood << ", " << client[id].city << endl;
- break;
- }
- case 3: {
- EditTransactions(id);
- break;
- }
- case 4: {
- cout<<"\nPlease enter " << client[id].name << "'s new balance: " << endl;
- char preBALANCE[MAX_TEMP];
- while (true) {
- cin.getline(preBALANCE, MAX_TEMP);
- if(CharToInt(preBALANCE) != -1){
- client[id].balance = CharToInt(preBALANCE);
- cout<<endl << client[id].name << "'s current balance is " << client[id].balance << endl;
- client[id].lastTransaction = 0;
- cout<<"\nAll transactions were reset" << endl;
- break;
- }else cout<<"\nPlease enter a positive integer number" << endl;
- }
- break;
- }
- default: {break; }
- }
- }
- else break;
- }
- else cout<<"\nPlease enter a positive integer number" << endl;
- }
- }
- }
- void ClientsInCities()
- {
- char tempCHOICE[MAX_TEMP];
- while(true){
- cout<<"\n(0) Exit"<<endl;
- cout<<"(1) Numbers of clients in all cities"<<endl;
- cout<<"(2) Numbers of clients in a specific city"<<endl;
- cin.getline(tempCHOICE, MAX_TEMP, '\n');
- if(CharToInt(tempCHOICE) != -1){
- int choice = CharToInt(tempCHOICE);
- if(choice > 2) cout<<"\nInvalid choice, please try again"<<endl;
- else{
- if(choice != 0){
- switch(choice)
- {
- case 1:{
- for (int i = 0; citiesCounts[i] != '\0'; i++)
- cout<<"\nThe number of clients from " << cities[i] << " is " << citiesCounts[i] << " clients"<<endl<<endl;
- break;
- }
- case 2:{
- cout<<"\nAvailable cities:"<<endl;
- for(int i = 0; cities[i][0] != '\0'; i++) cout<<cities[i]<<' ';
- cout<<"\n\nEnter a city's name: "<<endl;
- char city[MAX_ADDRESS];
- int id = -1;
- while(true)
- {
- cin.getline(city, MAX_ADDRESS, '\n');
- if(city[0] != BREAK)
- {
- SetFormat(city);
- if(CheckString(city, MIN_ADDRESS, MAX_ADDRESS) == 0)
- {
- cout<<"\nInvalid city name, please try again.\n\n(Enter '"<<BREAK<<"' To Stop)"<<endl;
- }else{
- id = FindCity(city);
- if(id == -1) cout<<"City Not Found"<<endl;
- else cout<<"\nThe number of clients from " << cities[id] << " is " << citiesCounts[id] << " clients"<<endl;
- break;
- }
- }else break;
- }
- break;
- }
- }
- break;
- }else break;
- }
- }else cout<<"\nPlease enter a positive integer number"<<endl;
- cout<<endl;
- }
- }
- void PrintBalanceOf()
- {
- cout<<"\nWhose balance would you like to see?" << endl;
- cout<<"\nAvailable clients:"<<endl;
- for(int i = 0; i<lastClient; i++) cout<<'-'<<client[i].name<<endl;
- cout<<endl;
- cout<<"\n(Type '" << BREAK << "' To Stop)" << endl;
- char name[MAX_NAME];
- int id;
- while (true) {
- cin.getline(name, MAX_NAME, '\n');
- if (name[0] != '.') {
- SetFormat(name);
- id = FindClient(name);
- if (id == -1) {
- cout<<"\nClient Not Found. Please Try Again." << endl;
- cout<<"\n(Type '" << BREAK << "' To Stop)" << endl;
- }
- else {
- cout<<endl << client[id].name << "'s balance is " << client[id].balance << endl;
- break;
- }
- }
- else break;
- }
- }
- void PrintAllBalances()
- {
- cout<<endl << setw(MAX_NAME) << "Name" << setw(MAX_NAME) << "Balance" << endl;
- for (int i = 0; i < lastClient; i++)
- {
- cout<<endl << setw(MAX_NAME) << client[i].name << setw(MAX_NAME) << client[i].balance << endl;
- }
- }
- void PrintGreatestBalance()
- {
- int id = 0, maxBalance = client[id].balance;
- for (int i = 0; i < lastClient; i++)
- {
- if (client[i].balance > maxBalance) {
- maxBalance = client[i].balance;
- id = i;
- }
- }
- cout<<"\nThe greatest balance is " << client[id].name << "'s account with a balance of " << maxBalance << endl;
- }
- int CheckString(char variable[], int MIN, int MAX)
- {
- if (variable[0] == BREAK) return 2;
- else {
- int notFaulty = 1, correctSize = 1;
- for (int i = 0; variable[i] != '\0'; i++)
- {
- bool notUpperCase = (variable[i] < 65 || variable[i] > 90);
- bool notLowerCase = (variable[i] < 97 || variable[i] > 122);
- bool notSpace = (variable[i] != ' ');
- if (notLowerCase && notUpperCase && notSpace) {
- notFaulty = 0;
- break;
- }
- }
- if (strlen(variable) < MIN || strlen(variable) > MAX) correctSize = 0;
- return (notFaulty && correctSize);
- }
- }
- int isNumeric(char variable[])
- {
- int numeric = 1;
- for (int i = 0; variable[i] != '\0'; i++) {
- if (variable[i] < 48 || variable[i] > 57) numeric = 0;
- }
- return numeric;
- }
- int FindClient(char name[])
- {
- int i = 0, found = 1;
- while (i < lastClient)
- {
- found = 1;
- for (int j = 0; j < strlen(name); j++) if (name[j] != client[i].name[j]) {
- found = 0;
- break;
- }
- if (found == 1) return i;
- i++;
- }
- if (found == 0) return -1;
- }
- int FindCity(char city[])
- {
- int found = 1;
- for (int i = 0; i < lastClient; i++)
- {
- found = 1;
- for (int j = 0; j < strlen(city); j++) if (city[j] != cities[i][j]) {
- found = 0;
- break;
- }
- if (found == 1) return i;
- }
- if (found == 0) return -1;
- }
- int CharToInt(char variable[])
- {
- if (isNumeric(variable) == 0) return -1;
- else
- {
- long int num = 0, length = strlen(variable);
- for (int i = 0; i < length; i++)
- {
- num += (variable[i] - '0') * pow(10, (length - i - 1));
- }
- return num;
- }
- }
- void EditTransactions(int id)
- {
- char preCHOICE[MAX_TEMP];
- int choice, offset = 1, maximumChoice = client[id].lastTransaction + offset;
- cout<<endl << client[id].name << "'s Transactions" << endl;
- cout<<"\n(0) Exit" << endl;
- for (int i = 0; i < client[id].lastTransaction; i++)
- cout<<'(' << i + 1 << ") " << client[id].tTypes[i] << ' ' << client[id].tAmounts[i] << endl;
- cout<<'(' << client[id].lastTransaction + offset << ") Change All Transactions" << endl;
- if (client[id].lastTransaction < userMaxTransactions) {
- cout<<'(' << client[id].lastTransaction + (offset + 1) << ") Add New Transactions" << endl;
- maximumChoice = client[id].lastTransaction + (offset + 1);
- }
- cout<<"\nEnter the number of your choice:" << endl;
- while (true) {
- cin.getline(preCHOICE, MAX_TEMP);
- if (CharToInt(preCHOICE) != -1) {
- choice = CharToInt(preCHOICE);
- if (choice > maximumChoice) {
- cout<<"\nInvalid choice. Please try again." << endl;
- continue;
- }
- else {
- if (choice == 0) {
- cout<<"\nNo changes were made" << endl;
- }
- else if (choice == client[id].lastTransaction + offset) {
- cout<<"\nRe-Enter all transactions" << endl;
- cout<<"\n(Maximum " << userMaxTransactions << " Transactions)" << endl;
- ClientTransactions(id, 0, userMaxTransactions);
- }
- else if (choice == client[id].lastTransaction + (offset + 1)) {
- cout<<"\nEnter Transactions. Maximum " << userMaxTransactions - client[id].lastTransaction << " Transactions" << endl;
- ClientTransactions(id, client[id].lastTransaction, userMaxTransactions);
- }
- else {
- cout<<"\nEnter the new transaction:" << endl;
- if (client[id].tTypes[choice - 1] == 'W' || client[id].tTypes[choice - 1] == 'w') client[id].balance += client[id].tAmounts[choice - 1];
- else client[id].balance -= client[id].tAmounts[choice - 1];
- ClientTransactions(id, choice - 1, choice);
- }
- cout<<"\nClient's current balance is " << client[id].balance << endl;
- }
- break;
- }
- else cout<<"\nPlease enter a positive integer number" << endl;
- }
- }
- void SetFormat(char variable[])
- {
- if (variable[0] == ' ')
- {
- for (int i = 0; i < strlen(variable); i++)
- {
- variable[i] = variable[i + 1];
- }
- }
- int start = 0;
- for (int i = 0; variable[i] != '\0'; i++) {
- if (variable[start] >= 97 && variable[start] <= 122) variable[start] -= 32;
- if (variable[i] != ' ') {
- if (variable[i] >= 65 && variable[i] <= 90) variable[i] += 32;
- }
- else start = i + 1;
- }
- }
- void CalculateBalance(int id, char transaction, int amount)
- {
- if (transaction == 'W' || transaction == 'w') client[id].balance -= amount;
- else client[id].balance += amount;
- }
- // End of Function Definitions
- /////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement