Assignment 2203057 CSE1204

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

CSE 1203

Name: Fabliha Naowar Niazam Deya

Student Id:2203057

Course Title: Object Oriented Programming

Submission Date: 23/10/2024

Assignment Name :Toll Plaza

Problem Statement:” Write a C++ menu program to automate the toll


management system of a newly established bridge in Bangladesh. Assume for the
simplicity consider three types of vehicles namely as Bus, Truck and Car.”
Solution:
C++ Code:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

struct User {
string name;
string password;
};

vector<User> userDatabase;
bool isAuthenticated = false;
string currentUserName;

class Transport {
public:
string regNumber;
string entryDate;
string entryTime;
string operatorName;

virtual void inputDetails() = 0;


virtual double calculateToll() = 0;
virtual void logDetails(ofstream& file) = 0;
};

class Bus : public Transport {


public:
int seatCount;
static double tollAmount;

void inputDetails() override {


cout << "Input Bus Registration No: ";
cin >> regNumber;
cout << "Input Date (dd/mm/yyyy): ";
cin >> entryDate;
cout << "Input Time (hh:mm): ";
cin >> entryTime;
cout << "Input Seat Count: ";
cin >> seatCount;
operatorName = currentUserName;
}
double calculateToll() override {
return tollAmount;
}

void logDetails(ofstream& file) override {


file << "Bus\t" << regNumber << "\t" << entryDate << "\t" << entryTime <<
"\t" << seatCount << "\t" << calculateToll() << "\t" << operatorName << "\n";
}
};

double Bus::tollAmount = 500.0;

class Truck : public Transport {


public:
double loadWeight;
static double tollAmount;

void inputDetails() override {


cout << "Input Truck Registration No: ";
cin >> regNumber;
cout << "Input Date (dd/mm/yyyy): ";
cin >> entryDate;
cout << "Input Time (hh:mm): ";
cin >> entryTime;
cout << "Input Load Weight: ";
cin >> loadWeight;
operatorName = currentUserName;
}

double calculateToll() override {


return tollAmount;
}

void logDetails(ofstream& file) override {


file << "Truck\t" << regNumber << "\t" << entryDate << "\t" << entryTime <<
"\t" << loadWeight << "\t" << calculateToll() << "\t" << operatorName << "\n";
}
};

double Truck::tollAmount = 400.0;

class Car : public Transport {


public:
string ownerName;
static double tollAmount;

void inputDetails() override {


cout << "Input Car Registration No: ";
cin >> regNumber;
cout << "Input Date (dd/mm/yyyy): ";
cin >> entryDate;
cout << "Input Time (hh:mm): ";
cin >> entryTime;
cout << "Input Owner's Name: ";
cin >> ownerName;
operatorName = currentUserName;
}

double calculateToll() override {


return tollAmount;
}

void logDetails(ofstream& file) override {


file << "Car\t" << regNumber << "\t" << entryDate << "\t" << entryTime <<
"\t" << ownerName << "\t" << calculateToll() << "\t" << operatorName << "\n";
}
};

double Car::tollAmount = 150.0;

vector<Transport*> vehicleList;

void pauseForInput() {
cout << "Press any key to continue to the main menu.....\n";
cin.get();
cin.ignore();
}

void addUser() {
string userName, userPassword, confirmPassword;
cout << "Enter New Username: ";
cin >> userName;

ifstream usersFileIn("users.txt");
string line;
while (getline(usersFileIn, line)) {
istringstream iss(line);
string existingUser;
if (iss >> existingUser) {
if (existingUser == userName) {
cout << "Username already exists!\n";
usersFileIn.close();
pauseForInput();
return;
}
}
}
usersFileIn.close();
cout << "Enter Password: ";
cin >> userPassword;
cout << "Re-enter Password: ";
cin >> confirmPassword;

if (userPassword == confirmPassword) {
ofstream usersFileOut("users.txt", ios::app);
usersFileOut << userName << " " << userPassword << "\n";
usersFileOut.close();

cout << "Registration Successful\n";


pauseForInput();
} else {
cout << "Passwords do not match!\n";
pauseForInput();
}
}

bool authenticateUser() {
string userName, userPassword;
cout << "Enter Username: ";
cin >> userName;
cout << "Enter Password: ";
cin >> userPassword;
ifstream usersFile("users.txt");
string line;
while (getline(usersFile, line)) {
istringstream iss(line);
string storedUser, storedPassword;
if (iss >> storedUser >> storedPassword) {
if (storedUser == userName && storedPassword == userPassword) {
cout << "Login Successful\n";
currentUserName = userName;
isAuthenticated = true;
usersFile.close();
pauseForInput();
return true;
}
}
}
usersFile.close();

cout << "Invalid Username or Password!\n";


pauseForInput();
return false;
}
bool isDateInRange(const string& date, const string& startDate, const string&
endDate) {
int d, m, y;
sscanf(date.c_str(), "%d/%d/%d", &d, &m, &y);
int currentDate = y * 10000 + m * 100 + d;

sscanf(startDate.c_str(), "%d/%d/%d", &d, &m, &y);


int start = y * 10000 + m * 100 + d;

sscanf(endDate.c_str(), "%d/%d/%d", &d, &m, &y);


int end = y * 10000 + m * 100 + d;

return currentDate >= start && currentDate <= end;


}

void searchByDateToDate() {
string startDate, endDate;
cout << "Enter Start Date (dd/mm/yyyy): ";
cin >> startDate;
cout << "Enter End Date (dd/mm/yyyy): ";
cin >> endDate;

ifstream vehiclesFile("vehicles_data.txt");
if (!vehiclesFile) {
cout << "Error: Could not open vehicles data file.\n";
pauseForInput();
return;
}

string line;
cout << "Date\tTime\tVehicle\tAmount\tOperator\n";
while (getline(vehiclesFile, line)) {
istringstream iss(line);
string vehicleType, regNumber, date, time, ownerOrWeight, amountStr,
operatorName;
iss >> vehicleType >> regNumber >> date >> time >> ownerOrWeight >>
amountStr >> operatorName;

if (isDateInRange(date, startDate, endDate)) {


cout << date << "\t" << time << "\t" << vehicleType << "\t" << amountStr <<
"\t" << operatorName << endl;
}
}

vehiclesFile.close();
pauseForInput();
}

void searchByOperator() {
string operatorName;
cout << "Enter Operator Name: ";
cin >> operatorName;

ifstream vehiclesFile("vehicles_data.txt");
if (!vehiclesFile) {
cout << "Error: Could not open vehicles data file.\n";
pauseForInput();
return;
}

string line;
cout << "Date\tTime\tVehicle\tAmount\tOperator\n";
while (getline(vehiclesFile, line)) {
istringstream iss(line);
string vehicleType, regNumber, date, time, ownerOrWeight, amountStr,
opName;
iss >> vehicleType >> regNumber >> date >> time >> ownerOrWeight >>
amountStr >> opName;

if (opName == operatorName) {
cout << date << "\t" << time << "\t" << vehicleType << "\t" << amountStr <<
"\t" << opName << endl;
}
}
vehiclesFile.close();
pauseForInput();
}

void processToll(Transport *vehicle) {


double amountPaid, change;
vehicle->inputDetails();
cout << "Enter Payment Amount (Tk. " << vehicle->calculateToll() << "): ";
cin >> amountPaid;

ofstream vehiclesFileOut("vehicles_data.txt", ios::app);

if (amountPaid >= vehicle->calculateToll()) {


change = amountPaid - vehicle->calculateToll();
cout << "Change: " << change << " Tk.\n";
cout << "Toll Collection Successful\n";
vehicle->logDetails(vehiclesFileOut);
} else {
cout << "Insufficient Payment. Toll Collection Failed.\n";
}

vehicleList.push_back(vehicle);
vehiclesFileOut.close();
pauseForInput();
}

void displayVehicleData() {
ifstream vehiclesFileIn("vehicles_data.txt");
if (!vehiclesFileIn) {
cout << "Error: Could not open vehicles data file.\n";
pauseForInput();
return;
}

cout << "Vehicle\tReg


No\tDate\t\tTime\tSeats/Weight/Owner\tAmount\tOperator\n";
string line;
while (getline(vehiclesFileIn, line)) {
cout << line << endl;
}

vehiclesFileIn.close();
pauseForInput();
}

void tollSettings() {
string adminUserName, adminUserPassword;
cout << "Enter Admin Username: ";
cin >> adminUserName;
cout << "Enter Admin Password: ";
cin >> adminUserPassword;

if (adminUserName == "admin" && adminUserPassword == "admin") {


cout << "Set Toll for Bus: ";
cin >> Bus::tollAmount;
cout << "Set Toll for Truck: ";
cin >> Truck::tollAmount;
cout << "Set Toll for Car: ";
cin >> Car::tollAmount;
cout << "Toll Rates Updated Successfully\n";
} else {
cout << "Invalid Admin Credentials\n";
}
pauseForInput();
}

void viewStatistics() {
string startDate, endDate;
cout << "Enter Start Date (dd/mm/yyyy): ";
cin >> startDate;
cout << "Enter End Date (dd/mm/yyyy): ";
cin >> endDate;
ifstream vehiclesFile("vehicles_data.txt");
if (!vehiclesFile) {
cout << "Error: Could not open vehicles data file.\n";
pauseForInput();
return;
}

int busCount = 0, truckCount = 0, carCount = 0;


double busTotal = 0, truckTotal = 0, carTotal = 0;

string line;
while (getline(vehiclesFile, line)) {
if (line.empty()) {
continue;
}

istringstream iss(line);
string vehicleType, regNumber, date, time, ownerOrWeight, amountStr,
operatorName;

if (!(iss >> vehicleType >> regNumber >> date >> time >> ownerOrWeight >>
amountStr >> operatorName)) {
cout << "Error parsing line: " << line << endl;
continue;
}
if (!isDateInRange(date, startDate, endDate)) {
continue;
}

try {
double amount = stod(amountStr);
if (vehicleType == "Bus") {
busCount++;
busTotal += amount;
} else if (vehicleType == "Truck") {
truckCount++;
truckTotal += amount;
} else if (vehicleType == "Car") {
carCount++;
carTotal += amount;
}
} catch (const invalid_argument&) {
cout << "Invalid amount value in line: " << line << endl;
continue;
} catch (const out_of_range&) {
cout << "Amount value out of range in line: " << line << endl;
continue;
}
}

cout << "Vehicle Number\tAmount\n";


cout << "Bus\t" << busCount << "\t" << busTotal << " Tk\n";
cout << "Truck\t" << truckCount << "\t" << truckTotal << " Tk\n";
cout << "Car\t" << carCount << "\t" << carTotal << " Tk\n";

double totalAmount = busTotal + truckTotal + carTotal;


cout << "Total Amount: " << totalAmount << " Tk\n";

vehiclesFile.close();
pauseForInput();
}

void searchMenu() {
int option;
do {
cout << "******* Search Menu *******\n";
cout << "1. Vehicle\n";
cout << "2. Date to Date\n";
cout << "3. Operator\n";
cout << "4. Back to Main Menu\n";
cout << "Enter Your Option (1-4): ";
cin >> option;
switch (option) {
case 1:
displayVehicleData();
break;
case 2:
searchByDateToDate();
break;
case 3:
searchByOperator();
break;
case 4:
return;
default:
cout << "Invalid Option\n";
}
} while (option != 4);
}

void saveAndExit() {
ofstream outFile("vehicles_data.txt", ios::app);
for (auto &vehicle : vehicleList) {
vehicle->logDetails(outFile);
}
outFile.close();
cout << "Data Saved Successfully. Exiting...\n";
exit(0);
}

void mainMenu() {
int choice;
do {
cout << "******* Toll Plaza Main Menu ******\n";
cout << "1. Bus\n";
cout << "2. Truck\n";
cout << "3. Car\n";
cout << "4. Search Menu\n";
cout << "5. Toll Settings\n";
cout << "6. Statistics\n";
cout << "7. Save and Exit\n";
cout << "Enter Your Option (1-7): ";
cin >> choice;

Transport* vehicle = nullptr;

switch (choice) {
case 1:
vehicle = new Bus();
processToll(vehicle);
break;
case 2:
vehicle = new Truck();
processToll(vehicle);
break;
case 3:
vehicle = new Car();
processToll(vehicle);
break;
case 4:
searchMenu();
break;
case 5:
tollSettings();
break;
case 6:
viewStatistics();
break;
case 7:
saveAndExit();
break;
default:
cout << "Invalid Option\n";
}
} while (choice != 7);
}

int main() {
while (!isAuthenticated) {
cout << "******* Toll Plaza Login ******\n";
cout << "1. Register\n";
cout << "2. Login\n";
cout << "3. Exit\n";
int option;
cout << "Select Option (1-3): ";
cin >> option;

switch (option) {
case 1:
addUser();
break;
case 2:
if (authenticateUser()) {
mainMenu();
}
break;
case 3:
cout << "Exiting...\n";
return 0;
default:
cout << "Invalid Option\n";
}
}

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy