-
Notifications
You must be signed in to change notification settings - Fork 0
/
RailwayMember.h
100 lines (85 loc) · 3.36 KB
/
RailwayMember.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef RAILWAY_MEMBER_H
#define RAILWAY_MEMBER_H
#include <vector>
#include<fstream>
#include "Ticket.h"
class RailwayMember {
private:
int maxTickets;
public:
RailwayMember(int max) : maxTickets(max) {}
bool bookTicket(vector<Ticket>& tickets, const Ticket& newTicket) {
if (tickets.size() < maxTickets && newTicket.availableSeats > 0) {
tickets.push_back(newTicket);
cout << "Ticket booked successfully! Your PNR is: " << newTicket.PNR << "\n";
writePassengerDetails(newTicket); // Write passenger details to CSV file
updateAvailableSeats(tickets, newTicket); // Update available seats for the booked class and date
return true;
} else {
cout << "Sorry, maximum tickets reached or no available seats. Cannot book more tickets.\n";
return false;
}
}
void writePassengerDetails(const Ticket& ticket) {
ofstream fout("pdata.csv", ios::app); // Open file in append mode
fout << ticket.departureTime << ","
<< ticket.departs << ","
<< ticket.arrive << ","
<< ticket.arrivalTime << ","
<< ticket.duration << ","
<< ticket.passengerName << ","
<< ticket.ticketClass << ","
<< ticket.PNR << ","
<< ticket.travelDate << "\n";
fout.close();
}
void updateAvailableSeats(vector<Ticket>& tickets, const Ticket& newTicket) {
for (auto& ticket : tickets) {
if (ticket.travelDate == newTicket.travelDate && ticket.ticketClass == newTicket.ticketClass) {
ticket.availableSeats--;
}
}
}
void displayBookedTicket(const vector<Ticket>& tickets, const string& pnr) {
for (const auto& ticket : tickets) {
if (ticket.PNR == pnr) {
cout << "\nBooked Ticket Details:\n";
displayTicketDetails(ticket);
return;
}
}
cout << "Ticket with PNR " << pnr << " not found.\n";
}
void displayAllTickets(const vector<Ticket>& tickets) {
cout << "\nAll Booked Tickets:\n";
for (const auto& ticket : tickets) {
displayTicketDetails(ticket);
}
}
void readPassengerDetails() {
ifstream fin("pdata.csv");
if (!fin) {
cout << "Error opening pdata.csv file.\n";
return;
}
string line;
cout << "\nPassenger Details from CSV file:\n";
while (getline(fin, line)) {
cout << line << "\n";
}
fin.close();
}
void displayTicketDetails(const Ticket& ticket) {
cout << "Departure Time: " << ticket.departureTime << "\n"
<< "Departs: " << ticket.departs << "\n"
<< "Arrive: " << ticket.arrive << "\n"
<< "Arrival Time: " << ticket.arrivalTime << "\n"
<< "Duration: " << ticket.duration << "\n"
<< "Passenger Name: " << ticket.passengerName << "\n"
<< "Class: " << ticket.ticketClass << "\n"
<< "PNR: " << ticket.PNR << "\n"
<< "Travel Date: " << ticket.travelDate << "\n"
<< "Available Seats: " << ticket.availableSeats << "\n";
}
};
#endif // RAILWAY_MEMBER_H