-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlaskanCruise.cpp
127 lines (114 loc) · 3.98 KB
/
AlaskanCruise.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//A. Noel Cothren
//Program 5 - Alaskan Cruise
//AlaskanCruise.cpp
#include "AlaskanCruise.h"
#include "Date.h"
int AlaskanCruise::resNumber{ 0 };
void AlaskanCruise::CalculateFare()
{
//for numPassengers, add fares FARE_SEVEN or FARE_NINE
fare = FARE_SEVEN;
if (numberDaysCruise == 7) fare *= numberOfPass;
else fare = FARE_NINE * numberOfPass;
//figure out the number of days from booking date to departure date using the date class
numDaysToDepart = dep.GetDayOfYear() - book.GetDayOfYear();
if (book.GetYear() != dep.GetYear()) numDaysToDepart += 365; //NOT in same year
//add 366 for leap year?? ^^
//depending on how many days that is, may give a discount
if (numDaysToDepart < DAYS_TO_DEPART_REQD) fare *= DISCOUNT_PERCENT; //discount = 60% on FARE only
// then add excursions
double excursionsCost{ 0.0 };
if (numberOfExcursions) excursionsCost *= numberOfExcursions;
fare += excursionsCost;
}
void AlaskanCruise::ValidateInput()
{
// ValidateInput will check if the Departure Date object is valid
if (!dep.ValidateThisDate()) //use Date class validate date to make sure it's a real date
{
bValidInput = false;
dateString = "\n Error: not a valid departure date.";
}
else if (dep.GetFormattedDate() == book.GetFormattedDate())
{
bValidInput = false;
dateString = "\n Error: departure date cannot be today's date.";
}
else if ((dep.GetYear() != book.GetYear()) || (dep.GetYear() != (book.GetYear() + 1))) //check that dep is this yr or next yr
//expand on this and make sub-checks for remaining items??
{
bValidInput = false;
dateString = "\n Error: departure date must be this year or next year.";
}
else if (dep.GetYear() == book.GetYear())
{
if (book.GetDayOfYear() > dep.GetDayOfYear()) //check that the Date of Departure is after the Date of Booking
{
bValidInput = false;
dateString = "\n Error: departure date must be later than booking date.";
}
}
else if (reservation == "") //check that reservation name isn't empty
{
bValidInput = false;
dateString = "\n Error: please enter a reservation name.";
}
else //if it passed all the above tests, it should be ok
{
bValidInput = true;
dateString = "\n Input valid.";
}
}
void AlaskanCruise::WriteReservationConfirmation()
{
//create the filename out of reservation name, resNum and “.txt”.
filename = reservation + "_" + to_string(AlaskanCruise::resNumber+1) + ".txt";
ofstream output;
output.open(filename);
if (!output) {
//If not open, set bool, report that no file was written.
bWrite = false;
dateString += "\n Error. No file was written.";
}
else {
//If successful...
//append that to the ReservationDescription and display the filename.
dateString += "\n The file was written successfully to " + filename;
output << dateString;
//increment resNumber
AlaskanCruise::resNumber += 1;
//set the bool
bWrite = true;
}
output.close();
}
//Overloaded Constructor
AlaskanCruise::AlaskanCruise(string resName, int numPass, int numDays, int numEx, Date booking, Date departure) :
reservation(resName), numberOfPass(numPass), numberDaysCruise(numDays), numberOfExcursions(numEx), book(booking), dep(departure)
{
ValidateInput();
if (bValidInput) CalculateFare();
}
void AlaskanCruise::SetDates(Date& bk, Date& dpt)
{
book = bk;
dep = dpt;
ValidateInput();
if (bValidInput) CalculateFare();
}
string AlaskanCruise::ReservationDescription()
{
stringstream ss;
//If there is no error, then call
if (bValidInput) {
WriteReservationConfirmation();
//that contains the reservation name, number of passengers, the booking date, travel date and total fare.
ss << "\r\n " << dateString << "\r\n Your reservation name: " << reservation << "\r\n Number of Passengers: " << numberOfPass << "\r\n " << book.GetFormattedDate() << "\r\n " << dep.GetFormattedDate() << "\r\n Total cost: $" << to_string(fare);
}
else {
//If there is an error in the reservation, it will display the specific error message from the method
ss << "\r\n " << dateString;
}
return ss.str();
}