Skip to content

Commit

Permalink
Updated last semester task
Browse files Browse the repository at this point in the history
  • Loading branch information
cylim committed Apr 2, 2015
1 parent 3fb55fb commit d3d2dc3
Show file tree
Hide file tree
Showing 82 changed files with 2,626 additions and 0 deletions.
Binary file added csci124/asgn3/CSCI124-Assignment3-July2014.docx
Binary file not shown.
Binary file added csci124/asgn3/asgn3_v01.zip
Binary file not shown.
201 changes: 201 additions & 0 deletions csci124/asgn3/cyber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
#include "fstream"
#include "cyber.h"
// This function Log in new user
void login(char ***lab, int total, int station[], User *&user, int &size){
User *newUser = new User; //create a struct to store the new user temporarily
bool valid = false;
//read data from user
while(valid == false){
valid = true;
cout << "Enter 5-digit ID: ";
cin >> newUser->id;
//if the id is existed in the user[]
for(int x=0;x<size;x++){
if(strcmp(newUser->id, user[x].id) == 0){
//inform client and reprompt
cout << "ID existed, please try again." << endl;
valid = false;
break;
}
}
}
valid = false;
char *empty;
empty = (char*)&"empty";
while(valid == false){
cout << "Enter Lab: ";
cin >> newUser->lab;
cout << "Enter Station: ";
cin >> newUser->station;
//check for the lab
for(int x=0; x<total; x++)
if(newUser->lab == x+1)
//check for the station
for(int y=0; y<station[x]; y++)
if(newUser->station == y+1)
//when the station is empty, then only assign it to user.
if(strcmp(lab[x][y], empty) == 0){
lab[x][y] = newUser->id;
valid = true;
} else {
cout << "The Lab Station Computer is occupied.\n";
}
}
valid = false;
//ask client to input the login time
while(valid == false){
cout << "Login Time(HH MM): ";
cin >> newUser->inHour >> newUser->inMin;
valid = true;
if(newUser->inHour > 23 || newUser->inMin > 59){
valid = false;
cout << "The time is in 24hours format. please try again." << endl;
}
}
//when user[] is null, perform if
//if there is info in id, perform else
if(strcmp(user[0].id, "") == 0){
user[0] = *newUser;
} else {
size+=1; //increase size
User *temp = new User[size]; //create temp User struct
for(int x=0; x<size;x++){
temp[x] = user[x]; //assign all old user[] data to temp[]
if(x == size-1){
temp[size-1] = *newUser; //last location for the new user
}
}
user = temp; //assign the data to user[]
}
}
// This function is used to Log out user that is leaving
void logout(char ***lab, User *&user, int &size, double &totalCharge){
int target;
char id[6];
bool valid = false;
//read id from user
while(valid == false){
cout << "Enter 5-digit ID: ";
cin >> id;
for(int x=0;x<size;x++){
if(strcmp(id, user[x].id) == 0){
target = x; //when the user is found, set x to target
valid = true;
break;
}
}
}
valid = false;
//prompt client to input the log out time
while(valid == false){
cout << "Logout Time(HH MM): ";
cin >> user[target].outHour >> user[target].outMin;
valid = true;
if(user[target].outHour > 23 || user[target].outMin > 59){
valid = false;
cout << "The time is in 24hours format. please try again." << endl;
}
}
//edit the lab table
strcpy(lab[user[target].lab-1][user[target].station-1], (char*)&"empty");
//calculate charges in minutes, each minutes = RM0.02
int login = user[target].inHour *60 + user[target].inMin;
int logout = user[target].outHour *60 + user[target].outMin;
// if the logout time is smaller than login time,
//logout time have to add 24hours as it had passed a day
if(logout < login){
logout = logout + (24 * 60);
}
user[target].price = (logout - login) * 0.02;
//print the payment details
cout << "Payment Amount: RM" << user[target].price << endl << endl;
//increase total charge
totalCharge += user[target].price;
//write the logout info to log file.
ofstream logfile("log.txt", ios::app);
logfile << "\n" << user[target].id << ":"
<< user[target].lab << ":"
<< user[target].station << ":"
<< user[target].inHour << "." << user[target].inMin << ":"
<< user[target].outHour << "." << user[target].outMin << ":"
<< user[target].price;
logfile.close();

//set the user[] smaller
size-=1;
// create a temp User struct to store the data without logout user
User *temp = new User[size];
for(int x=0; x<size;x++){
temp[x] = user[x];
if(x >= target){
temp[x] = user[x+1];
}
}
user = temp; // user User struct point to temp User struct.
}
// This function is used to Search user by the user id
void search(User user[], int &size){
char id[6];
bool valid = false;
//read data from user
cout << "Enter 5-digit ID: ";
cin >> id;
//search from the user[], if the id matched, print out the user current session info
for(int x=0;x<size;x++){
if(strcmp(id, user[x].id) == 0){
cout << "User ID: " << user[x].id << endl
<< "Lab:\t" << user[x].lab << endl
<< "Station: " << user[x].station << endl
<< "Login Time: " << user[x].inHour << ":" << user[x].inMin << endl << endl;
valid = true;
break;
}
}
//if the id is not found in the user[], tell client.
if(valid == false){
cout << "User ID: " << id << " not found!" << endl << endl;
}
}

//This function allow the user to choose particular lab for display purposes.
void list(char ***lab, int total, int station[]){
int num;
//prompt user to enter lab number
cout << "Enter Lab number: ";
cin >> num;
//if the lab number is not in range, reprompt.
while(num > total || num <= 0){
cout << "Lab number not found!" << endl;
cout << "There is " << total << " lab in this cyber cafe." << endl;
cout << "Enter Lab number: ";
cin >> num;
}
//formating - Lab: Stations
cout << "Lab Number\tComputer Stations" << endl
<< "----------\t--------------------------------------------" << endl;
cout << '\t' << num << '\t'; // display lab
for(int y=0; y<station[num-1]; y++){
cout << y+1 << ": " << lab[num-1][y] << '\t'; //display station
}
cout << endl << "----------\t--------------------------------------------" << endl;
}

//This function is used to display total charges of the cyber cafe at particular session
void displayTotal(double totalCharge){
cout << "Total charges of this session is RM" << totalCharge << endl << endl;
}

//This function is used to display the full details of the cyber cafe
void displayLab(char ***lab, int total, int station[]){
//formating - Lab: Stations
cout << "Lab Number\tComputer Stations" << endl
<< "----------\t--------------------------------------------" << endl;
for(int x=0; x<total; x++){
cout << '\t' << x+1 << '\t'; // display lab
for(int y=0; y<station[x]; y++){
cout << y+1 << ": " << lab[x][y] << '\t'; //display station
}
cout << endl;
}
cout << "----------\t--------------------------------------------" << endl;
}
16 changes: 16 additions & 0 deletions csci124/asgn3/cyber.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <cstring>
using namespace std;

struct User{
char id[6];
int lab, station, inHour, inMin, outHour, outMin;
float price;
};

void login(char***, int, int[], User*&, int&); // Log in
void logout(char***, User*&, int&, double&); // Log out
void search(User[], int&); // Search user
void list(char***, int, int[]); // List Lab
void displayTotal(double); // Display total charges
void displayLab(char***, int, int[]); // Display the computer lab info
1 change: 1 addition & 0 deletions csci124/asgn3/log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ID:lab:station:login:logout:price(RM)
Binary file added csci124/asgn3/main
Binary file not shown.
65 changes: 65 additions & 0 deletions csci124/asgn3/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//Author :Lim Chee Yeong
//Student ID: J14016414(INTI) or 4933643(UOW)
//Class :CSCI124
//Assignment:3
//Date :October 2nd, 2014
//This driver program is the main menu of a simulated cyber cafe system
#include "cyber.h"

int main(){
int total, station[total], size=1;
User *user = new User[size];
cout << "Enter Total Lab Number: ";
cin >> total;
char *** lab = new char **[total];
for(int x=0; x<total; x++){
cout << "Enter total computer in Lab " << x+1 << ": ";
cin >> station[x];
lab[x] = new char *[station[x]];
for(int y=0; y<station[x]; y++){
//strcpy(lab[x][y], "empty"); //segmentation error
lab[x][y] = (char*)&"empty";
}
}

//Display the computer lab info
displayLab(lab, total, station);

double totalCharge = 0;
int option;
while(true){
//Display the menu in proper formatting
cout << "\t\tCyber Cafe Menu" << endl
<< "1. Log in" << endl
<< "2. Log out" << endl
<< "3. Search user" << endl
<< "4. List lab" << endl
<< "5. Display total charges" << endl
<< "0. Exit" << endl;
cout << "Option: ";
cin >> option; // read choice from user
switch(option){
case 1: // Log in
login(lab, total, station, user, size);
displayLab(lab, total, station); //Display the computer lab info
break;
case 2: // Log out
logout(lab, user, size, totalCharge);
break;
case 3: // Search user
search(user, size);
break;
case 4: // List Lab
list(lab, total, station);
break;
case 5: // Display total charges
displayTotal(totalCharge);
break;
case 0: // close the program
cout << "Program terminated." << endl << endl;
return 0;
default: // re-prompt user the menu
cout << "Wrong input, please try again." << endl << endl;
}
}
}
11 changes: 11 additions & 0 deletions csci124/asgn3/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
main: main.o cyber.o
g++ -o main main.o cyber.o

main.o: main.cpp cyber.h
g++ -c main.cpp

cyber.o: cyber.cpp cyber.h
g++ -c cyber.cpp

clean:
rm *.o
Binary file added csci124/asgn4/CSCI124-Assignment4-July2014.docx
Binary file not shown.
63 changes: 63 additions & 0 deletions csci124/asgn4/Training.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <iostream>
#include <cstring>
#include <fstream>
#include "Training.h"
using namespace std;
//Class Training Implementation
//Accessor function implementation inside header.
//Mutator function implementation
void Training::setCourse(char id[],char description[],char date[],char trainingTime[],char charges[8]){
strcpy(this->id,id);
strcpy(this->description, description);
strcpy(this->date, date);
strcpy(this->trainingTime, trainingTime);
strcpy(this->charges, charges);
}
void Training::setTotal(int total){
this->total = total;
}
void Training::setParticipant(int num, char pid[], char firstName[], char lastName[]){
strcpy(this->pid[num], pid);
strcpy(this->firstName[num], firstName);
strcpy(this->lastName[num], lastName);
}
//delete participant from object
void Training::delParticipant(int del){
total-=1;
strcpy(pid[del], pid[total]);
strcpy(firstName[del], firstName[total]);
strcpy(lastName[del], lastName[total]);
}
//display every details
void Training::display(){
cout << "Course ID: " << id << endl
<< "Title: " << description << endl
<< "Date: " << date << endl
<< "Time: " << trainingTime << endl
<< "Charges: RM" << charges << endl
<< "Participant details," << endl
<< "Name\t\tParticipant ID" << endl
<< "------------\t--------------" << endl;
for(int x=0; x< total; x++){
cout << firstName[x] << " " << lastName[x] << "\t" << pid[x] << endl;
}
cout << endl;
}
//save objects into file
void Training::save(char file[]){
//Open file and write everything into it
ofstream write(file, ios::app);
write << '\n' << id;
write << '\n' << description;
write << '\n' << date;
write << '\n' << trainingTime;
write << '\n' << charges;
write << '\n' << total;
//check how many participant for the course is available, and write into file
for(int x=0; x<total; x++){
write << '\n' << pid[x];
write << '\n' << firstName[x];
write << '\n' << lastName[x];
}
write.close();
}
17 changes: 17 additions & 0 deletions csci124/asgn4/Training.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Training{
//traing details
char id[6], description[51], date[31], trainingTime[16], charges[8];
//participant details
int total;
char pid[30][15], firstName[30][10], lastName[30][10];
public:
void setCourse(char[],char[],char[],char[],char[]);
void setTotal(int);
void setParticipant(int, char[],char[],char[]);
char* getID(){ return id; }
int getTotal(){ return total; }
char* getPID(int num){ return pid[num]; }
void delParticipant(int); //delete participant from object
void display(); //display every details
void save(char[]); //save objects into file
};
Binary file added csci124/asgn4/asgn4_v01.zip
Binary file not shown.
Loading

0 comments on commit d3d2dc3

Please sign in to comment.