-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnotherClassHavingFile.cpp
More file actions
109 lines (103 loc) · 3.46 KB
/
Copy pathAnotherClassHavingFile.cpp
File metadata and controls
109 lines (103 loc) · 3.46 KB
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
101
102
103
104
105
106
107
108
109
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class Reservation{
string name;
int age,trainNo,seatNo;
public:
void inputDetails(){
cout<<"\n----------Book Ticket----------"<<endl;
cout<<"Enter Passenger Name :";
// cin.ignore();
// getline(cin,name);
cin>>name;
cout<<"Enter age :";
cin>>age;
cout<<"Enter Train Number :";
cin>>trainNo;
cout<<"Enter Seat Number :";
cin>>seatNo;
}
bool isSeatAvailable(){
ifstream inFile("reservation.txt");
if(!inFile.is_open()){
return true;
}
string rName;
int rAge,rTrain,rSeat;
while(inFile >> rName >> rAge >> rTrain >> rSeat){
if(rTrain == trainNo && rSeat == seatNo){
inFile.close();
return false;
}
}
inFile.close();
return true;
}
void saveToFile(){
if(!isSeatAvailable()){
cout<<"That Seat is already Booked sry for that :)\nTry another."<<endl;
return;
}
ofstream outFile("reservation.txt",ios::app);
if(!outFile.is_open()){
cout<<"Error! Opening file for writing!"<<endl;
return;
}
outFile << name << " " << age << " " << trainNo << " " << seatNo<<endl;
outFile.close();
cout<<"Ticket Booked Successfully :)"<<endl;
}
void viewAll(){
ifstream inFile("reservation.txt");
if(!inFile.is_open()){
cout<<"\n No Reservations found."<<endl;
return;
}
cout<<"\n---------------------------------All Reservations---------------------------------"<<endl;
cout<<"Train\tSeat\tName\tAge"<<endl;
cout<<"-------------------------------"<<endl;
string rName;
int rAge, rTrain,rSeat;
while (inFile >> rName >> rAge >> rTrain >> rSeat){
cout<<rTrain<<"\t"<<rSeat<<"\t"<<rName<<"\t"<<rAge<<endl;
}
inFile.close();
}
void cancelTicket(){
cout<<"\n----------Cancel Ticket----------"<<endl;
cout<<"Enter Train Number =";
cin>> trainNo;
cout<<"Enter Seat Number to Cancel:";
cin>>seatNo;
ifstream inFile("reservation.txt");
if(!inFile.is_open()){
cout<<"No reservations to cancel."<<endl;
return;
}
ofstream temp("temp.txt");
bool found=false;
string rName;
int rAge, rTrain, rSeat;
while(inFile >>rName >>rAge>> rTrain>> rSeat){
if(rTrain == trainNo && rSeat == seatNo){
found =true;
}
else{
temp <<rName<<" "<<rAge<<" "<<rTrain<<" "<<rSeat<<endl;
}
}
inFile.close();
temp.close();
if(found){
remove("reservations.txt");
rename("temp.txt","reservation.txt");
cout<<"Ticket Cancelled Succcessfully."<<endl;
}
else{
remove("temp.txt");
cout<<"No Matching reservation found."<<endl;
}
}
};