//Diego Martinez CSC5 Chapter 4, P.226,#25
/*******************************************************************************
* VALIDATING INTERNET SERVICE HOURS
* ______________________________________________________________________________
* This program enhances input validation by asking the user to enter the month
* and ensures that the number of hours entered does not exceed the maximum
* possible hours for that specific month.
*
*
* Computation is based on the Formula:
* maxHours : 744 : if month has 31 days
* : 720 : if months has 30 days
* : 672 : if month is February (28 days)
*______________________________________________________________________________
* INPUT
* Package Type
* Month name
* Hours used
*
* OUTPUT
* Final bill amount
*******************************************************************************/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
// Function to convert string to lowercase
string toLower(string str) {
transform(str.begin(), str.end(), str.begin(), ::tolower);
return str;
}
// Function to get max hours based on month
int getMaxHours(string month) {
month = toLower(month);
if (month == "january" || month == "march" || month == "may" ||
month == "july" || month == "august" || month == "october" ||
month == "december")
return 744;
else if (month == "april" || month == "june" ||
month == "september" || month == "november")
return 720;
else if (month == "february")
return 672;
else
return -1; // invalid month
}
int main() {
char package;
int hours;
string month;
double bill = 0.0;
// Input package
cout << "Enter your package (A, B, or C): ";
cin >> package;
package = toupper(package);
if (package != 'A' && package != 'B' && package != 'C') {
cout << "Invalid package selection.\n";
return 1;
}
// Input month
cout << "Enter the month name: ";
cin >> month;
int maxHours = getMaxHours(month);
if (maxHours == -1) {
cout << "Invalid month entered.\n";
return 1;
}
//Input hours used
cout << "Enter number of hours used: ";
cin >> hours;
if (hours < 0 || hours > maxHours) {
cout << "Invalid number of hours for " << month
<< ". Maximum allowed is " << maxHours << " hours.\n";
return 1;
}
// Calculate Bill
switch (package) {
case 'A':
bill = 9.95;
if (hours > 10)
bill += (hours -10) * 2.0;
break;
case 'B':
bill = 14.95;
if (hours > 20)
bill += (hours - 20) * 1.0;
break;
case 'C':
bill = 19.95;
break;
}
// Output result
cout << "Your total bill for " << month << " is: $" << bill << endl;
return 0;
}