Assignment 2203057 CSE1204
Assignment 2203057 CSE1204
Assignment 2203057 CSE1204
Student Id:2203057
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;
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();
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();
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;
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();
}
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;
}
vehiclesFileIn.close();
pauseForInput();
}
void tollSettings() {
string adminUserName, adminUserPassword;
cout << "Enter Admin Username: ";
cin >> adminUserName;
cout << "Enter Admin Password: ";
cin >> adminUserPassword;
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;
}
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;
}
}
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;
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;
}