Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
5.1 kB
3
Indexable
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

const int MAX = 12; //Max number of products
string PROD_ID[MAX];
string PROD_NAME[MAX];
string PROD_PRICE[MAX];

string RECEIPTID[MAX];
string RECEIPTNAME[MAX];
string RECEIPTPRICE[MAX];
double balance;
 
//Todo: Sell/balance array (double)
//		change PROD_PRICE to double

//	ADD FUNCTION
void add() {
	char id[10];
	char name[50];
	char price[10];
	cin.ignore();
	cout<<"ID: ";
	cin.getline(id, 10);
	cout<<"Name: ";
	cin.getline(name, 50);
	cout<<"Price: ";
	cin.getline(price, 10);
	
	for (int x=0; x<MAX; x++) {
		if (PROD_ID[x] == "\0") {
			PROD_ID[x] = id;
			PROD_NAME[x] = name;
			PROD_PRICE[x] = price;
			break;
		}
	}
	cout<<endl;
}

//	LIST FUNCTION
void list() {
	int process = 0;
	for (int x=0; x<MAX; x++) {
		if (PROD_ID[x] != "\0") {
			process++;
			cout<<"\t"<<PROD_ID[x]<<"\t"<<PROD_NAME[x]<<"\t\t"<<PROD_PRICE[x]<<endl;
		}
	}
	cout<<endl;
	if (process == 0)
		cout<<"No records found.\n\n";
}

//	SEARCH AND UPDATE FUNCTION
void searchupdate() {
	int process = 0;
	string id;
	//SEARCH
	cin.ignore();
	cout<<"Input ID: ";
	getline(cin, id);
	for (int x=0; x<MAX; x++) {
		if (id == PROD_ID[x]) {
			process++;
			cout<<"\t"<<PROD_ID[x]<<"\t"<<PROD_NAME[x]<<"\t\t"<<PROD_PRICE[x]<<endl;
			
			//UPDATE			
			char nid[10];
			char nname[50];
			char nprice[10];
			cout<<"Change?\n"
				<<"1. ID\n"
				<<"2. Name\n"
				<<"3. Price\n"
				<<"4. Change all\n"
				<<"5. Delete record\n"
				<<"6. Go to main menu\n";
			char ch;
			cin>>ch;
			cin.ignore();
			switch (ch) {
				case '1':
					cout<<"Enter new ID: ";
					cin.getline(nid, 10);
					PROD_ID[x] = nid;
					break;
				case '2':
					cout<<"Enter new name: ";
					cin.getline(nname, 50);
					PROD_NAME[x] = nname;
					break;
				case '3':
					cout<<"Enter new price: ";
					cin.getline(nprice, 10);
					PROD_PRICE[x] = nprice;
					break;
				case '4':
					cout<<"Enter new ID: ";
					cin.getline(nid, 10);
					cout<<"Enter new name: ";
					cin.getline(nname, 50);
					cout<<"Enter new price: ";
					cin.getline(nprice, 10);
					
					PROD_ID[x] = nid;
					PROD_NAME[x] = nname;
					PROD_PRICE[x] = nprice;
					break;
				case '5':
					char choice;
					cout<<"Are you sure you want to delete? (Y/N) \n";
					cin>>choice;
					if (choice=='y' || 'Y') 
					{
						PROD_ID[x] ="";
						PROD_NAME[x] ="";
						PROD_PRICE[x] = "";
						
						cout<<"Successfully deleted\n\n";
					}
					
					else 
					{
					}
					break;	
				case '6':
				default:
					break;
			}	
		}
	}
	if (process == 0)
		cout<<"No record found.\n";
}

//	SAVE FUNCTION
void save() 
{
	ofstream out;
	out.open("records.txt");
	int process = 0;
	for (int x=0; x<MAX; x++) {
		if (PROD_ID[x] != "\0") {
			process++;
			out<<"\t"<<PROD_ID[x]<<"\t"<<PROD_NAME[x]<<"\t\t"<<PROD_PRICE[x]<<endl;
			cout<<"Your record(s) have been saved.\n";
		}
	}
	cout<<endl;
	if (process == 0)
		out<<"No records found.\n";
		
	out.close();
}
//multiple?
//	SELL FUNCTION
void sell()	
{
	int process = 0;
	string id;
	char choice;
	double result;

	cin.ignore();
	cout<<"Input ID: ";
	getline(cin, id);
	for (int x=0; x<MAX; x++) {
		if (id == PROD_ID[x]) {
			process++;
			cout<<"\t"<<PROD_ID[x]<<"\t"<<PROD_NAME[x]<<"\t\t"<<PROD_PRICE[x]<<endl;
			cout<<"Do you want to sell this product? (Y/N)\n";
			cin>>choice;
			if (choice=='y' || 'Y') 
			{
				for (int y=0; y<x+1; y++) {
					if (RECEIPTID[y] == "\0") {
						RECEIPTID[y] = PROD_ID[x];
						RECEIPTNAME[y] = PROD_NAME[x];
						RECEIPTPRICE[y] = PROD_PRICE[x];
					}
				}
				
				stringstream ss;
				ss << PROD_PRICE[x];
				ss >> result;
				
				PROD_ID[x] ="";
				PROD_NAME[x] ="";
				PROD_PRICE[x] = "";
			
				cout<<"Successfully sold!\n";
			}
		}
	}
	balance+=result;
	cout<<"Your balance is now: $"<<balance<<endl<<endl;
}

void getReceipt() {
	int process = 0;
	ofstream out;
	out.open("receipt.txt");
	for (int x=0; x<MAX; x++) {
		if (RECEIPTID[x] != "\0") {
			process++;
			out<<"\t"<<RECEIPTID[x]<<"\t"<<RECEIPTNAME[x]<<"\t\t"<<RECEIPTPRICE[x]<<endl;
		}
	}
	if (process != 0)
		cout<<"Your receipt has been generated!\n";

	if (process == 0)
		cout<<"No transactions found.\n";
	out.close();	
	cout<<endl;
}


//	TODO:
//	Console UI

int main() {
	char ch;
	bool exit = true;
	while (exit == true) {
		cout<<"Select function\n"
			<<"1. Add record\n"
			<<"2. List record\n"
			<<"3. Search and/or update record\n"
			<<"4. Sell product\n"
			<<"5. Save records\n"
			<<"6. Get receipt\n"
			<<"7. Exit program\n";
		cin>>ch;
		switch (ch) {
			case '1':
				add();
				break;
			case '2':
				list();
				break;
			case '3':
				searchupdate();
				break;
			case '4':
				sell();
				break;
			case '5':
				save();
				break;
			case '6':
				getReceipt();
				break;
			case '7':
				exit = false;
				break;
			}
		}
	}