need help in object orientated programming
Author |
Message |
Alck
|
Posted: Thu Aug 07, 2008 10:23 am Post subject: need help in object orientated programming |
|
|
Hi all,
my friend have a problem with his mini project. Please advise.
Question:
A community centre has decided to computerize the booking of two badminton courts.
The two courts are named as B1 and B2. Bookings are to be done on a two-hour block
from 10am to 10pm daily.
Each booking record should have the Facility Name, Day and Time, the player?s NRIC
number and the booking status of the court. Each player?s record should have name,
NRIC number and Telephone number.
Design the necessary classes and member functions to achieve the following tasks :
a. Allow user to book the court by entering the facility name, day and time. If it is
available, prompt the user to enter his name, NRIC number and telephone number.
Display a message ?Courts is booked? if the facility has already been booked by
other player.
b. Allow user to enquire the availability of court for the entire week.
c. Allow user to enquire his/her booking by entering NRIC number.
d. Store the booking record in a text file when the system terminates
Program
Quote:
--//Header--//
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Record
{
protected:
char F_name[2];
string Day;
int Time;
string NRIC;
int status;
string p_name;
string IC_no;
int Tele_no;
string temp;
};
class Book : protected Record
{
private:
ifstream fin;
ofstream fout;
protected:
string name;
string day;
int time;
public:
void book();
void availability();
void enquire();
};
//--functions--//
#include "class.h"
#include <string>
using namespace std;
void Book::book()
{
cin.get();
cout << "Pls enter the facility name : ";
getline(cin, name);
cout << "Pls enter the day u wan to book: ";
getline(cin, day);
cout << "Pls enter the time u wan to book : ";
cin >> time;
fin.open("BookRecord.txt");
do {
fin >> F_name;
getline(fin, temp, ' ');
getline(fin, p_name, ' ');
getline(fin, Day, ' ');
fin >> Time;
if ((F_name == name)&&(Day == day)&&(Time == time))
status = 1;
else status = 0;
getline(fin, temp);
} while ((status != 1)&&(!fin.eof()));
fin.close();
if (status == 1)
cout << "Court is booked.\n";
else {
fflush(stdin);
cout << "Pls enter ur name : ";
getline(cin, p_name);
cout << "Pls enter ur NRIC(num onli) : ";
cin >> IC_no;
cout << "Pls enter ur telephone no : ";
cin >> Tele_no;
fout.open("BookRecord.txt", ios::app);
fout << name << " " << p_name << " " << day << " " << time << " " << IC_no << " " << Tele_no << endl;
fout.close();
}
}
void Book::availability()
{
string s1,s2;
int t;
fin.open("BookRecord.txt");
fin >> name >> p_name >>Day >> Time >> s1 >> s2;
t = Time;
while (!fin.eof())
{
fin >> name >> p_name>> Day >> Time >>s1 >> s2;
for (int i = 0; i < 7; i += 2)
if (Time == 1000)
cout << "Day : " << Day << " The court " << name << " is available from " << t << ":00 to " << t+i << ":00.";
else cout << "\nDay: " << Day << " The court " << name << " is available from " << t << ":00 to " << t+2+i << ":00.";
}
}
void Book::enquire()
{
string s3,s4;
cout << "Pls enter ur NRIC : ";
cin>> NRIC;
fin.open ("BookRecord.txt");
{
fin >> F_name >> p_name >> Day >> Time >> s3, s4;
getline(fin, NRIC, ' ');
if (NRIC == IC_no)
{
while (fin >> IC_no)
{
fin >> F_name >> Day >> Time >> s3 >> s4;
cout << F_name << " " << Day << " " << Time << " " ;
}
}
fin.close();
return;
}
}
//main//
#include <iostream>
#include <fstream>
#include <string>
#include "class.h"
using namespace std;
void main()
{
int value;
cout<<"Please Select Your Choice: "<<endl;
cout<<"1)Court Booking"<<endl;
cout<<"2)Enquire Availability Of Court"<<endl;
cout<<"3)Enquire Your Booking"<<endl;
cout<<"Your Choice Is : ";
cin>>value;
Book B;
if(value == 1)
{
B.book();
}
else if(value == 2)
{
B.availability();
}
else
{
B.enquire();
}
}
He got problem displaying the entire week of availability of badminton court in the availability() function. Please assist as this coming tues is the dead line. Please re-write the code if possible. Thanks[/quote] |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zeroth
|
Posted: Thu Aug 07, 2008 11:21 am Post subject: Re: need help in object orientated programming |
|
|
Just posting to note there is a [code] tag. Wrap the code you're posting in that, and it'll be much more legible. Now, a question, is this for a paying job? We can only offer general advice(or at least, I'll offer general advice, or bugfixes), but not do his job for him. Even if its not a paying job, We(at the very least, I) won't do your work for you.
On that note:
I'm not sure, but I believe this is not correct:
[code]
for (int i = 0; i < 7; i += 2)
if (Time == 1000)
cout << "Day : " << Day << " The court " << name << " is available from " << t << ":00 to " << t+i << ":00.";
else cout << "\nDay: " << Day << " The court " << name << " is available from " << t << ":00 to " << t+2+i << ":00.";
[/code]
You may need to put braces around the block of the for statement. Its better to provide too much information to the compiler than not enough. Always be explicit, not implicit in code. I don't have a lot of experience with C++ however, and my co-worker says I'm wrong. |
|
|
|
|
|
Alck
|
Posted: Thu Aug 07, 2008 12:03 pm Post subject: Re: need help in object orientated programming |
|
|
hmm is our school mini project. yeah any advise on how do we need to rewrite this portion ? |
|
|
|
|
|
btiffin
|
Posted: Thu Aug 07, 2008 12:30 pm Post subject: RE:need help in object orientated programming |
|
|
Possible options; procedurally grunt across all current records, or if you are looking for better marks in an OO class; google "C++ container" or "C++ iterator". To limit the range to a certain week, perhaps look into std stable_sort after getting to grips with iterators.
That is not the only way to go about this but may help.
Cheers |
|
|
|
|
|
Rigby5
|
Posted: Thu Oct 09, 2008 11:11 pm Post subject: RE:need help in object orientated programming |
|
|
You need to indent and comment much more clearly.
But your main logic mistake is that inside the loop you are constantly setting status to 1 or 0 for each record you look up.
That is no good.
If you find a match and it is already booked, you cover it up with the next that you read that is not a match.
What you need to do instead, is to set status to 0, or not booked, once only, before the loop starts.
The if a match is found and the court is booked, only then is the value of status ever changed, and it is set to 1.
So then the value of status is not changed at all if there is never a match. |
|
|
|
|
|
|
|