// local variable declarations for class System
int x = 0; // for counter in Users array
const int size = 50; // max size for User account
int position, y;
char pinPrompt[7];
char pinAdmin[7] = "admin\0";
// create instances from classes
Activity Researcher[7]; // for Vote Researcher
Activity aTrans; // for activity option
void System::ReadFile()
{
// open a file - file processing
ifstream input;
input.open("research.txt");
if (input.fail())
{
cout << "Unable to open a file." << endl;
getch();
exit(1); // exit a program
}
// reading from a file
while (input>>dat.password>>dat.name>>dat.votenum>>dat.allocation>>dat.balance)
{
// writing to Users array
Researcher[x].SetPassword(dat.password);
Researcher[x].SetName(dat.name);
Researcher[x].SetVoteNo(dat.votenum);
Researcher[x].SetAllocation(dat.allocation);
Researcher[x].SetBalance(dat.balance);
x++;
}
input.close(); // close file - end of file processing
}
// checking pinnumber
for (y=0; y<x; y++)
{
// compare the pin enter with the pin in the file
if (strcmp(pinPrompt, Researcher[y].GetPassword()) == 0)
{
// password matched
position = y; // position of the researcher in the array
break;
}
if (y == (x-1))
{
// end of array has been reached
// password does not matched
cout << endl << "Incorrect password, program terminates" << endl;
getch();
exit(0); // exit a program
}
}
}
void System::Menu()
{
// if successful proceed with menu of activity
char option = 'Y';
int choice;
int commitAmount;
Order aorder; // for order file
while (option == 'y' || option == 'Y')
{
if (option == 'y' || option == 'Y')
choice = aTrans.GetActivityType();
switch (choice)
{
case 1:
Researcher[position].Commit(commitAmount);
option = aTrans.GetAnotherAct();
cout << endl << "Enter the amount to commit, RM";
cin >> commitAmount;
break;
case 2:
Researcher[position].CheckBalance();
option = aTrans.GetAnotherAct();
break;
case 3:
Researcher[position].DisplayDetails();
option = aTrans.GetAnotherAct();
break;
case 4:
if (strcmp(pinPrompt, pinAdmin) == 0)
{
cout << endl << "The order from the researchers are as follows: " << endl;
aorder.GetData();
}
else
{
cout << endl;
cout << "Sorry, you are not allowed to view the order file." << endl;
cout << "Only administrator is allowed to view the order file. "<< endl;
}
break;
case 5:
option = 'N';
break;
default:
option = 'N';
break;
}
}
cout << endl << "Thank you for using the UKM Research Management System " << endl;
getch();
//exit(0);
}
//----------------------------------
// Activity.h
#include <iostream>
#include "Research.h"
#include "Order.h"
using namespace std;
class Activity : public Research
{
public:
Activity();
void Commit(int Amt);
void CheckBalance();
void DisplayDetails();
int GetActivityType();
char GetAnotherAct();
};
Activity::Activity()
{
Research();
}
void Activity::Commit(int Amt)
{
Order aorder;
if (Balance - Amt >= 0)
{
aorder.SetData();
Balance = Balance - Amt;
cout << endl << "Your order is successful. The amount of order is RM" << Amt;
}
else
{
cout << endl;
cout << "Cannot complete your order" << endl;
cout << "Your order is greater than your balance" << endl;
cout << "Your current balance is RM" << GetBalance() << endl;
cout << "Please try again" << endl;
}
}
class Order {
private:
struct Data {
char Name[25];
char VoteNo[8];
char OrderDetail[25];
int Amount;
} stdata;
public:
void SetData();
void GetData();
};
void Order::SetData()
{
// open file "order.txt" as input file
fstream input("order.txt", ios::in|ios::out);
// ask input stdata from keyboard
cout << "Enter your name: ";
cin >> stdata.Name;
cout << "Enter your vote number: ";
cin >> stdata.VoteNo;
cout << "Enter your order detail: ";
cin >> stdata.OrderDetail;
cout << "Enter order amount to commit: RM";
cin >> stdata.Amount;
// move the cursor to the end of the file
input.seekg(0, ios::end);
// write the stdata into the file
input.write((char*) & stdata, sizeof(stdata));
// close the file once we have finished using it
input.close();
}
void Order::GetData()
{
// open file "order.txt" as output file
fstream output("order.txt", ios::in|ios::out);
// move the cursor to the beginning of the file
output.seekg(0, ios::beg);
// as long as the file is not end of file
while (!output.eof())
{
// read the stdata from the file
output.read((char*) & stdata, sizeof(stdata));
if (output.eof())
break; // exit if end of file is encountered
// display the stdata into screen
cout << endl;
cout << "Researcher Name: " << stdata.Name << endl;
cout << "Vote Number: " << stdata.VoteNo << endl;
cout << "Order Detail: " << stdata.OrderDetail << endl;
cout << "Order Amount: RM" << stdata.Amount << endl;
}
// close the file once we have finished using it
output.close();
cout << endl << "Press enter to continue." << endl;
getch();
}
//-----------------------------------------
// Research.h
#include <iostream>
#include <string>
using namespace std;
class Research {
private:
char Password[7];
char Name[20];
char VoteNo[8];
int Allocation;