0% found this document useful (0 votes)
9 views

Manag CPP

Uploaded by

reyar94831
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Manag CPP

Uploaded by

reyar94831
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

#include <fstream>

using namespace std;

class Tour {
private:
char destination[100];
char startDate[20];
char endDate[20];
float cost;

public:
Tour(const char* dest, const char* start, const char* end, float cst)
: cost(cst) {
copyString(destination, dest, sizeof(destination));
copyString(startDate, start, sizeof(startDate));
copyString(endDate, end, sizeof(endDate));
}

const char* getDestination() const { return destination; }


const char* getStartDate() const { return startDate; }
const char* getEndDate() const { return endDate; }
float getCost() const { return cost; }

private:
void copyString(char* dest, const char* src, size_t maxSize) {
size_t i = 0;
while (i < maxSize - 1 && src[i] != '\0') {
dest[i] = src[i];
++i;
}
dest[i] = '\0';
}
};

void addTour() {
ofstream outFile("tours.txt", ios::app);
if (!outFile) {
cerr << "Error opening file!" << endl;
return;
}

char dest[100];
char start[20];
char end[20];
float cost;

cout << "Enter destination: ";


cin >> dest;

cout << "Enter start date: ";


cin >> start;

cout << "Enter end date: ";


cin >> end;

cout << "Enter cost: ";


cin >> cost;
Tour newTour(dest, start, end, cost);

outFile << newTour.getDestination() << "\n"


<< newTour.getStartDate() << "\n"
<< newTour.getEndDate() << "\n"
<< newTour.getCost() << "\n";

outFile.close();
cout << "Tour added successfully!" << endl;
}

void searchByDestination(const char* searchDest) {


ifstream inFile("tours.txt");
if (!inFile) {
cerr << "Error opening file!" << endl;
return;
}

char line[100];
bool found = false;

while (inFile.getline(line, sizeof(line))) {


if (compareStrings(line, searchDest) == 0) {
cout << "Tour found:" << endl;
cout << "Destination: " << line << endl;

inFile.getline(line, sizeof(line));
cout << "Start Date: " << line << endl;

inFile.getline(line, sizeof(line));
cout << "End Date: " << line << endl;

inFile.getline(line, sizeof(line));
cout << "Cost: " << line << endl;

found = true;
break;
} else {
// Skip the rest of the tour details
for (int i = 0; i < 3; ++i) {
inFile.getline(line, sizeof(line));
}
}
}

if (!found) {
cout << "Tour not found." << endl;
}

inFile.close();
}

void updateByDestination(const char* searchDest) {


fstream file("tours.txt", ios::in | ios::out);
if (!file) {
cerr << "Error opening file!" << endl;
return;
}
char line[100];
bool found = false;

while (file.getline(line, sizeof(line))) {


if (compareStrings(line, searchDest) == 0) {
found = true;

cout << "Tour found. Enter new details:" << endl;

char newDest[100];
char newStart[20];
char newEnd[20];
float newCost;

cout << "Enter new destination: ";


cin >> newDest;

cout << "Enter new start date: ";


cin >> newStart;

cout << "Enter new end date: ";


cin >> newEnd;

cout << "Enter new cost: ";


cin >> newCost;

file.seekp(file.tellg() - strlen(line) - 1);


file << newDest << "\n"
<< newStart << "\n"
<< newEnd << "\n"
<< newCost << "\n";
break;
} else {
// Skip the rest of the tour details
for (int i = 0; i < 3; ++i) {
file.getline(line, sizeof(line));
}
}
}

if (!found) {
cout << "Tour not found." << endl;
}

file.close();
}

int compareStrings(const char* str1, const char* str2) {


while (*str1 && *str2 && *str1 == *str2) {
++str1;
++str2;
}
return (*str1 - *str2);
}

int main() {
int userType;
cout << "Select User Type" << endl;
cout << "1. Admin" << endl;
cout << "2. Customer" << endl;
cout << "Enter your choice: ";
cin >> userType;

if (userType == 1) {
char username[50];
char password[50];

cout << "Admin Login" << endl;


cout << "Enter username: ";
cin >> username;

cout << "Enter password: ";


cin >> password;

if (authenticateAdmin(username, password)) {
cout << "Login successful. Welcome, admin!" << endl;

int adminChoice;
do {
cout << "\nAdmin Menu" << endl;
cout << "1. Add Tour" << endl;
cout << "2. Display Tours" << endl;
cout << "3. Search Tour" << endl;
cout << "4. Update Tour" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> adminChoice;

switch (adminChoice) {
case 1:
addTour();
break;
// Add cases for 2, 3, 4...
default:
cout << "Invalid choice. Please try again." << endl;
}

} while (adminChoice != 5);

} else {
cout << "Invalid username or password. Access denied." << endl;
}
} else if (userType == 2) {
// Customer menu
int customerChoice;

do {
cout << "\nCustomer Menu" << endl;
cout << "1. Show Tours" << endl;
cout << "2. Purchase Tickets" << endl;
cout << "3. Enter Phone Number" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> customerChoice;

switch (customerChoice) {
case 1:
// Function definition for displaying tours
cout << "Displaying Tours..." << endl;
break;
case 2:
// Function definition for purchasing tickets
cout << "Purchasing Tickets..." << endl;
break;
case 3: {
// Function definition for inputting phone number
char input[15];
int valid = 0;
char customerPhone[15] = {0}; // Initialize the array to zero

while (!valid) {
cout << "Enter your phone number (only digits): ";
cin >> input;

valid = 1; // Initially assume valid


for (int i = 0; input[i] != '\0'; ++i) {
if (input[i] < '0' || input[i] > '9') {
valid = 0; // Not valid if any character is not a
digit
break;
}
}

if (valid) {
int i = 0;
while (input[i] != '\0') {
customerPhone[i] = input[i];
++i;
}
customerPhone[i] = '\0';
cout << "Phone number entered: " << customerPhone <<
endl;
} else {
cout << "Invalid phone number! Please enter only
digits." << endl;
}
}
break;
}
case 4:
cout << "Exiting customer menu." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}

} while (customerChoice != 4);


} else {
cout << "Invalid user type. Exiting program." << endl;
}
return 0;
}

You might also like