OOP Assignment 3
24k-0655
CODE:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
class Driver;
class Bus;
class Journey;
class TransportCompany;
class Journey {
private:
static const float LONG_DISTANCE = 50.0f;
char start[20], end[20];
float distance;
bool isLong;
public:
Journey() : distance(0), isLong(false) {}
Journey(const char* from, const char* to, float dist) {
strcpy(start, from);
strcpy(end, to);
distance = dist;
isLong = (distance > LONG_DISTANCE);
}
void inputJourney() {
cout << "From (location): ";
cin >> start;
cout << "To (destination): ";
cin >> end;
cout << "Distance in KM: ";
cin >> distance;
isLong = (distance > LONG_DISTANCE);
void save(ostream& out) {
out.write(start, sizeof(start));
out.write(end, sizeof(end));
out.write((char*)&distance, sizeof(distance));
out.write((char*)&isLong, sizeof(isLong));
void load(istream& in) {
in.read(start, sizeof(start));
in.read(end, sizeof(end));
in.read((char*)&distance, sizeof(distance));
in.read((char*)&isLong, sizeof(isLong));
char* getStart() { return start; }
char* getEnd() { return end; }
float getDistance() { return distance; }
bool longTrip() { return isLong; }
void operator=(const Journey& j) {
strcpy(start, j.start);
strcpy(end, j.end);
distance = j.distance;
isLong = j.isLong;
void display() {
cout << "Route: " << start << " to " << end
<< " | " << distance << " km"
<< (isLong ? " (Long Route)\n" : "\n");
};
class Bus {
private:
char type[15];
Journey route;
bool hasAC;
int studentCap, facultyCap;
int studentCount, facultyCount;
public:
Bus() : route(), studentCap(0), facultyCap(0),
studentCount(0), facultyCount(0), hasAC(false) {}
Bus(const char* name, bool ac, const Journey& j)
: route(j), studentCount(0), facultyCount(0), hasAC(ac) {
strcpy(type, name);
if (strcmp(type, "Coaster") == 0) {
studentCap = 25;
facultyCap = 7;
} else if (strcmp(type, "Bus") == 0) {
studentCap = 40;
facultyCap = 12;
} else {
studentCap = facultyCap = 0;
void save(ostream& out) {
out.write(type, sizeof(type));
out.write((char*)&hasAC, sizeof(hasAC));
out.write((char*)&studentCap, sizeof(studentCap));
out.write((char*)&facultyCap, sizeof(facultyCap));
out.write((char*)&studentCount, sizeof(studentCount));
out.write((char*)&facultyCount, sizeof(facultyCount));
route.save(out);
void load(istream& in) {
in.read(type, sizeof(type));
in.read((char*)&hasAC, sizeof(hasAC));
in.read((char*)&studentCap, sizeof(studentCap));
in.read((char*)&facultyCap, sizeof(facultyCap));
in.read((char*)&studentCount, sizeof(studentCount));
in.read((char*)&facultyCount, sizeof(facultyCount));
route.load(in);
void reserveStudent() {
if (studentCount < studentCap)
++studentCount;
else
throw "No more student seats!";
void reserveFaculty() {
if (facultyCount < facultyCap)
++facultyCount;
else
throw "No more faculty seats!";
int availableSeats(const char* role) const {
return strcmp(role, "student") == 0
? studentCap - studentCount
: facultyCap - facultyCount;
bool isAC() const { return hasAC; }
const char* getType() const { return type; }
char* getOrigin() { return route.getStart(); }
void operator=(const Bus& b) {
strcpy(type, b.type);
hasAC = b.hasAC;
studentCap = b.studentCap;
facultyCap = b.facultyCap;
studentCount = b.studentCount;
facultyCount = b.facultyCount;
route = b.route;
void display() {
cout << "Bus Type: " << type
<< " | AC: " << (hasAC ? "Available" : "Not Available") << "\n"
<< "Available Seats -> Students: " << (studentCap - studentCount)
<< ", Faculty: " << (facultyCap - facultyCount) << "\n";
route.display();
};
class Driver {
char fullName[20];
bool validLicense;
public:
Bus assignedBus;
Driver(): assignedBus() {}
Driver(const char* name, bool license, const Bus& b)
: assignedBus(b) {
strcpy(fullName, name);
validLicense = license;
void save(ostream& out) {
out.write(fullName, sizeof(fullName));
out.write((char*)&validLicense, sizeof(validLicense));
assignedBus.save(out);
void load(istream& in) {
in.read(fullName, sizeof(fullName));
in.read((char*)&validLicense, sizeof(validLicense));
assignedBus.load(in);
char* getName() { return fullName; }
bool isLicensed() { return validLicense; }
char* getLocation() { return assignedBus.getOrigin(); }
void operator=(const Driver& d) {
strcpy(fullName, d.fullName);
validLicense = d.validLicense;
assignedBus = d.assignedBus;
void display() {
cout << "Driver: " << fullName
<< (validLicense ? " [Licensed]" : " [Not Licensed]") << "\n";
assignedBus.display();
};
class TransportCompany {
int count;
public:
Driver* fleet;
TransportCompany(int c) {
count = c;
fleet = new Driver[count];
~TransportCompany() {
delete[] fleet;
void storeData() {
ofstream file("Transport.dat", ios::binary | ios::app);
for (int i = 0; i < count; ++i)
fleet[i].save(file);
file.close();
};
class User {
protected:
char userName[25];
char userType[10];
int userId;
char address[20];
float feeAmount;
bool feePaid;
public:
Driver bookedDriver;
bool isBooked;
char* getName() { return userName; }
char* getType() { return userType; }
char* getAddress() { return address; }
int getId() { return userId; }
float getFee() { return feeAmount; }
bool hasPaidFee() { return feePaid; }
void updateFee(float f) { feeAmount += f; }
void markFeePaid() { feePaid = true; }
void saveData() {
ofstream file("Clients.dat", ios::binary | ios::app);
file.write((char*)this, sizeof(*this));
file.close();
void displayData() {
cout << userName << " | ID: " << userId
<< " | Address: " << address
<< " | Type: " << userType
<< " | Fee: " << feeAmount
<< " | Fee Status: " << (feePaid ? "Paid" : "Pending") << "\n";
};
class Student : public User {
public:
Student() {}
Student(const char* name, int id, const char* loc) {
strcpy(userName, name);
userId = id;
strcpy(address, loc);
feeAmount = 0;
feePaid = false;
strcpy(userType, "student");
isBooked = false;
void find(int id) {
ifstream file("Clients.dat", ios::binary);
Student temp;
bool found = false;
while (!found && file.read((char*)&temp, sizeof(temp))) {
if (temp.userId == id) {
temp.displayData();
found = true;
file.close();
cout << (found ? "? Student found\n" : "? Student not found\n");
}
void remove(int id) {
ifstream in("Clients.dat", ios::binary);
ofstream out("temp.dat", ios::binary);
Student temp;
bool found = false;
while (in.read((char*)&temp, sizeof(temp))) {
if (temp.userId != id)
out.write((char*)&temp, sizeof(temp));
else {
temp.displayData();
found = true;
in.close(); out.close();
std::remove("Clients.dat");
std::rename("temp.dat", "Clients.dat");
cout << (found ? "? Student removed\n" : "? Student not found\n");
};
class Faculty : public User {
public:
Faculty() {}
Faculty(const char* name, int id, const char* loc) {
strcpy(userName, name);
userId = id;
strcpy(address, loc);
feeAmount = 0;
feePaid = false;
strcpy(userType, "faculty");
isBooked = false;
}
void find(int id) {
ifstream file("Clients.dat", ios::binary);
Faculty temp;
bool found = false;
while (!found && file.read((char*)&temp, sizeof(temp))) {
if (temp.userId == id) {
temp.displayData();
found = true;
file.close();
cout << (found ? "? Faculty found\n" : "? Faculty not found\n");
void remove(int id) {
ifstream in("Clients.dat", ios::binary);
ofstream out("temp.dat", ios::binary);
Faculty temp;
bool found = false;
while (in.read((char*)&temp, sizeof(temp))) {
if (temp.userId != id)
out.write((char*)&temp, sizeof(temp));
else {
temp.displayData();
found = true;
in.close(); out.close();
std::remove("Clients.dat");
std::rename("temp.dat", "Clients.dat");
cout << (found ? "? Faculty removed\n" : "? Faculty not found\n");
}
};
class Reservation {
public:
void checkAvailability(Student& s) {
ifstream file("Transport.dat", ios::binary);
Driver temp;
bool matched = false;
while (file.peek() != EOF) {
temp.load(file);
Bus& b = temp.assignedBus;
if (!matched &&
strcmp(s.getAddress(), temp.getLocation()) == 0 &&
temp.isLicensed() &&
b.availableSeats("student") > 0)
matched = true;
s.bookedDriver = temp;
float fare = 30000.0f + (b.isAC() ? 2000.0f : 0.0f);
s.updateFee(fare);
cout << "\n"; temp.display();
cout << "Total Fee: Rs. " << s.getFee() << "\n\n";
file.close();
if (!matched) cout << "? No student seat available\n\n";
void checkAvailability(Faculty& f) {
ifstream file("Transport.dat", ios::binary);
Driver temp;
bool matched = false;
while (file.peek() != EOF) {
temp.load(file);
Bus& b = temp.assignedBus;
if (!matched &&
strcmp(f.getAddress(), temp.getLocation()) == 0 &&
temp.isLicensed() &&
b.availableSeats("faculty") > 0)
matched = true;
f.bookedDriver = temp;
float fare = 22000.0f + (b.isAC() ? 2000.0f : 0.0f);
f.updateFee(fare);
cout << "\n"; temp.display();
cout << "Total Fee: Rs. " << f.getFee() << "\n\n";
file.close();
if (!matched) cout << "? No faculty seat available\n\n";
void payFee(User& u) {
cout << "Available seats before payment: "
<< u.bookedDriver.assignedBus.availableSeats(u.getType()) << "\n";
if (u.bookedDriver.assignedBus.availableSeats(u.getType()) > 0) {
if (!u.hasPaidFee()) {
cout << "? Fee paid successfully: Rs. " << u.getFee() << "\n\n";
u.markFeePaid();
} else throw "? Fee already paid";
} else throw "? No seats left";
void finalizeBooking(User& u) {
if (!u.hasPaidFee()) throw "? Fee not paid yet!";
if (strcmp(u.getType(), "student") == 0)
u.bookedDriver.assignedBus.reserveStudent();
else
u.bookedDriver.assignedBus.reserveFaculty();
fstream tf("Transport.dat", ios::binary | ios::in | ios::out);
Driver temp;
streampos pos;
while (tf.peek() != EOF) {
pos = tf.tellg();
temp.load(tf);
if (strcmp(temp.getName(), u.bookedDriver.getName()) == 0) {
tf.seekp(pos);
u.bookedDriver.save(tf);
break;
tf.close();
fstream cf("Clients.dat", ios::binary | ios::in | ios::out);
User tempUser;
while (cf.read((char*)&tempUser, sizeof(tempUser))) {
if (tempUser.getId() == u.getId()) {
cf.seekp(-static_cast<int>(sizeof(tempUser)), ios::cur);
cf.write((char*)&u, sizeof(u));
break;
cf.close();
cout << "? Booking complete\n\n";
};
int main() {
cout << "Ashhad Iqbal\n24k-0655\n";
ofstream("Clients.dat", ios::binary | ios::trunc).close();
ofstream("Transport.dat", ios::binary | ios::trunc).close();
TransportCompany t(2);
Journey j("johar", "fast", 10.0f);
Bus studentBus("Coaster", true, j);
Bus facultyBus("Bus", false, j);
t.fleet[0] = Driver("Mr. Kamal", true, studentBus);
t.fleet[1] = Driver("Mr. Imran", true, facultyBus);
t.storeData();
Student stu1("Adeel", 101, "johar");
Student stu2("Bilal", 102, "johar");
Faculty fac1("Dr. Sana", 201, "johar");
Faculty fac2("Prof. Khalid", 202, "johar");
stu1.saveData(); stu2.saveData();
fac1.saveData(); fac2.saveData();
Reservation res;
cout << "=== Booking for " << stu1.getName() << " ===\n";
res.checkAvailability(stu1);
try { res.payFee(stu1); } catch(const char* e) { cout << e << "\n\n"; }
try { res.finalizeBooking(stu1); } catch(const char* e) { cout << e << "\n\n"; }
cout << "=== Booking for " << stu2.getName() << " ===\n";
res.checkAvailability(stu2);
try { res.payFee(stu2); } catch(const char* e) { cout << e << "\n\n"; }
try { res.finalizeBooking(stu2); } catch(const char* e) { cout << e << "\n\n"; }
cout << "=== Booking for " << fac1.getName() << " ===\n";
res.checkAvailability(fac1);
try { res.payFee(fac1); } catch(const char* e) { cout << e << "\n\n"; }
try { res.finalizeBooking(fac1); } catch(const char* e) { cout << e << "\n\n"; }
cout << "=== Booking for " << fac2.getName() << " ===\n";
res.checkAvailability(fac2);
try { res.payFee(fac2); } catch(const char* e) { cout << e << "\n\n"; }
try { res.finalizeBooking(fac2); } catch(const char* e) { cout << e << "\n\n"; }
cout << "=== Removing Student Records ===\n";
stu1.find(stu1.getId());
stu2.find(stu2.getId());
stu1.remove(stu1.getId());
stu2.remove(stu2.getId());
cout << "=== Removing Faculty Records ===\n";
fac1.find(fac1.getId());
fac2.find(fac2.getId());
fac1.remove(fac1.getId());
fac2.remove(fac2.getId());
return 0;
}
OUTPUT: