//  Author: Brandon Mohawk
//  Teacher: Dr. Wang
//
//  Goal:  The program will creat an array of customers, and let the
//  user modify the entire array, modify a single customer in the array,
//  print the information of one customer, and search the array for a
//  certain name.
//
//  File: customer.cpp
//  Compile: g++ customer.cpp -o customer.out
//  Run: ./run.out

#include <iostream>

using namespace std;

struct Date{
        int month;
        int day;
        int year;
        string monthName;};

struct Customer{
        string name;
        float balance;
        Date lastPay;};

const int M = 20;

void menu();
void input(Customer[]);
void edit(Customer[]);
void print(Customer[]);
void search(Customer[]);

int main()
{
	Customer account[M];
	char ch;
	char ch2;
	
	bool run;

	while(run)
	{
		system("clear");
		menu();
		cin >> ch;
	
                if(tolower(ch) == 'q')
                        run = false;
		else
		{
			if(tolower(ch) == 'e')
				input(account);
			if(tolower(ch) == 'c')
				edit(account);
			if(tolower(ch) == 'p')
				print(account);
			if(tolower(ch) == 's')
				search(account);
	
			cout << "\n\n\t\tRun the program again? Y or N: ";
				cin >> ch2;

			if(tolower(ch2) == 'n')
				run = false;
		}
	}
		return 0;
}

void menu()
{	
	cout << "\tMENU\n";
	cout << "\t=========\n";
	cout << "\tE - Enter customer data\n";
	cout << "\tC - Change ..\n";
	cout << "\tP - Print .. \n";
	cout << "\tS - Search .. \n";
	cout << "\tQ - Quit\n\n";
	cout << "\tYour input: ";
}

void input(Customer list[])
{
	bool run;	

	char trash;
	for(int i=0; i<M; i++)
	{
		cout << "Customer number " << i+1 <<endl;
		while ( run )
		{	
			cout << "Name: ";
			cin.get(trash);
			getline(cin, list[i].name);

			if(!list[i].name.empty())
				run = false;
       		}

		run = true;

		while ( run )
        	{
                	cout << "Balance: ";
                	cin >> list[i].balance;

                	if(list[i].balance >= 0)
                       		run = false;
       		}

		run = true;

		while ( run )
        	{
                	cout << "Month of last pay: ";
                	cin >> list[i].lastPay.month;

                	if(list[i].lastPay.month <= 12 && list[i].lastPay.month > 0)
                        	run = false;
        	}

		run = true;

		while ( run )
                {
                        cout << "Day of last pay: ";
                        cin >> list[i].lastPay.day;

                        if(list[i].lastPay.day <= 32 && list[i].lastPay.day > 0)
                                run = false;
                }

		run = true;

		while ( run )
                {
                        cout << "Year of last pay: ";
                        cin >> list[i].lastPay.year;

                        if(list[i].lastPay.year < 2011 && list[i].lastPay.year > 1900)
                                run = false;
                }


		system("clear");
	}
}

void edit(Customer person[])
{
	system("clear");
	char trash;
	bool run;
	int numb;

	cout << "Which customer are you editing? : ";
	cin >> numb;

	int a = numb-1;

	system("clear");
	
	cout << "Customer number " << numb;

	cin.get(trash);

        while ( run )
        {
        	cout << "\nName: ";
                getline(cin, person[a].name);

                if(!person[a].name.empty())
                	run = false;
        }

	run = true;

        while ( run )
        {
        	cout << "Balance: ";
                cin >> person[a].balance;

                if(person[a].balance >= 0)
                	run = false;
        }

	run = true;

	while ( run )
        {
                cout << "Month of last payment: ";
                cin >> person[a].lastPay.month;

                if(person[a].lastPay.month <= 12 && person[a].lastPay.month > 0)
                        run = false;
        }

	run = true;

	while ( run )
        {
                cout << "Day of last pay: ";
                cin >> person[a].lastPay.day;

                if(person[a].lastPay.day <= 32 && person[a].lastPay.day > 0)
                        run = false;
        }

        run = true;

	while ( run )
        {
                cout << "Year of last pay: ";
                cin >> person[a].lastPay.year;

                if(person[a].lastPay.year < 2011 && person[a].lastPay.year  > 1900)
                        run = false;
        }


	
	system("clear");
}

void print(Customer list[])
{       
	system("clear");
        char trash;
        int numb;

        cout << "Which customer are you printing? : ";
        cin >> numb;

        int a = numb-1;

	system("clear");

	cout << "\tCustomer number: " << numb;
	cout << "\n\tCustomer's name: " << list[a].name;
	cout << "\n\tCustomer's balance: " << list[a].balance;
	cout << "\n\tDate of last payment: " << list[a].lastPay.month << "/";
	cout << list[a].lastPay.day << "/" << list[a].lastPay.year;
}

void search(Customer list[])
{
	system("clear");

	int n=0;
	char trash;
	string str;

	cout << "Search the database.  Enter the customer's name you are looking for: ";
	cin.get(trash);
	getline(cin, str);

	for(int i=0; i<12; i++)
	{
		if(list[i].name.find(str,0) != -1)
		{
			cout << "Match found.\n";
			cout << "\tCustomer number " << i+1;
			cout << "\n\t" << list[i].name << "\n";
			n++;
		}
	}
	
	if(n==0)
		cout << "No matches found.";
}

