steamengines

PA 7 - Phone Bill (updated)

Apr 21st, 2015
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.66 KB | None | 0 0
  1. // Christian Ballard
  2. // COSC 1336 - 001
  3. // 4/21/15
  4. // Should take customer information and display their bill.
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <ctime>
  9. #include <iomanip>
  10.  
  11. using namespace std;
  12.  
  13. // Forward declarations
  14. void getData(string&, int&, char&);
  15. int readInt(string = "", int = INT_MIN, int = INT_MAX);
  16. char getChoice(string, char char1, char char2);
  17. double calculate(char serviceType);
  18. void display(string name, int displayAccount, string displayServiceType, double displayServiceCharge);
  19.  
  20. int main()
  21. {
  22.         // Variable declarations
  23.         string name;            // Collects the customer name
  24.         int accountNum;         // Collects the customer account number
  25.         char serviceType;       // Collects customer service type
  26.         bool loop = true;       // Keeps the program indefinitely running until prompted to exit
  27.  
  28.         cout << "Welcome! please type the customer name to continue, " << endl << "or type \"exit\" to end the program." << endl << endl;
  29.  
  30.         // BEGIN LOOP
  31.         while (loop == true) {
  32.                 // Entering the program
  33.                 getData(name, accountNum, serviceType);
  34.         }
  35.         // END LOOP
  36.  
  37.         return 0;
  38. }
  39.  
  40. // Program body
  41. void getData(string& name, int& account, char& service)
  42. {
  43.         cout << "Customer name: ";
  44.         getline(cin, name);
  45.  
  46.         if (name == "Exit" || name == "exit" || name == "EXIT") {
  47.                 cout << "Program terminated by user, good-bye." << endl;
  48.                 system("PAUSE");
  49.                 exit(EXIT_SUCCESS);
  50.                 // Exits the program
  51.         }
  52.  
  53.         // Data processing
  54.         cout << "Enter account number: ";
  55.  
  56.         // Making sure the account number is between 0 and 9999
  57.         account = readInt("Account number", 0, 9999);
  58.  
  59.         // Making sure the service type is either an R or a P
  60.         service = getChoice("Service type", 'r', 'p');
  61.  
  62.         // Randomly generates minutes and calculates the total charge
  63.         double serviceCharge = calculate(service);
  64.  
  65.         // Being passed into display function for aesthetic appeal
  66.         string serviceName;
  67.  
  68.         if (service == 'p') {
  69.                 serviceName = "Premium";
  70.         } else {
  71.                 serviceName = "Regular";
  72.         }
  73.  
  74.         cout << endl;
  75.  
  76.         // Displays the final billing information for the customer
  77.         display(name, account, serviceName, serviceCharge);
  78. }
  79.  
  80. // Character validation
  81. char getChoice(string prompt, char char1, char char2)
  82. {
  83.         char someChar;
  84.         bool valid = false;
  85.  
  86.         do
  87.         {
  88.                 // Prompts the user to enter a valid service type
  89.                 cout << prompt << " (" << char1 << " or " << char2 << "): ";
  90.                 cin >> someChar;
  91.                 someChar = tolower(someChar);
  92.  
  93.                 // Determining valid service type
  94.                 valid = (someChar == char1 || someChar == char2);
  95.  
  96.                 // If the input is false, it clears the screen and prompts re-entry
  97.                 if (!valid || !isspace(cin.peek())) {
  98.                         cout << "Error!" << endl;
  99.                         cout << prompt << " must be " << char1 << " or " << char2 << endl;
  100.                         cout << "Try Again: ";
  101.                         cin.clear(); // Clearing the fail state
  102.                         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  103.  
  104.                         valid = false;
  105.                 }
  106.         } while (!valid);
  107.  
  108.         // Ignores following invalid data if it's correct
  109.         cin.ignore(100, '\n');
  110.  
  111.         return someChar;
  112. }
  113.  
  114. // Validates the account number
  115. int readInt(string errorMsg, int min, int max)
  116. {
  117.         int someInt;
  118.         bool valid = false;
  119.  
  120.         do
  121.         {
  122.                 // User entering account number
  123.                 cin >> someInt;
  124.  
  125.                 // Verifying the data is valid (0 to 9999)
  126.                 valid = (someInt >= min && someInt <= max);
  127.  
  128.                 // Clearing out invalid data and prompting for re-entry
  129.                 if (cin.fail() || !valid || !isspace(cin.peek()))
  130.                 {
  131.                         cout << "Error! Invalid input." << endl;
  132.                         cout << errorMsg << " must be whole number between " << min << " and " << max << endl;
  133.                         cout << "Try Again: ";
  134.                         cin.clear(); // Clearing the fail state
  135.                         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  136.  
  137.                         valid = false;
  138.                 }
  139.         } while (!valid);
  140.  
  141.         // Clears out following incorrect data once correct
  142.         cin.ignore(100, '\n');
  143.  
  144.         return someInt;
  145. }
  146.  
  147. // Generates minutes and calculates the final charges
  148. double calculate(char serviceType)
  149. {
  150.         // Set amounts to be paid
  151.         double payRegular = 10.0, payPremium = 25.0;
  152.         int dayMinutes,         // Holds random day minutes
  153.                 nightMinutes,   // Holds random night minutes
  154.                 finalPay;               // Used to return final pay post-processing
  155.  
  156.         // seeding the random number generator
  157.         srand(time(NULL));
  158.  
  159.         // Making sure decimals are displayed correctly
  160.         cout << fixed << setprecision(2);
  161.  
  162.         // randomly generated minute range between 0 and 300. completely arbitrary ceiling
  163.         dayMinutes = 0 + rand() % ((300 - 0) + 1);
  164.         nightMinutes = 0 + rand() % ((300 - 0) + 1);
  165.  
  166.         // Checking and calculating Regular service
  167.         if (serviceType == 'r') {
  168.  
  169.                 // Day minutes used as standard minutes on regular service
  170.                 cout << "Minutes Used: " << dayMinutes << endl;
  171.  
  172.                 if (dayMinutes > 50) {  // Checking if overcharge is necessary
  173.                         dayMinutes -= 50;       // Extracting amount to be charged
  174.  
  175.                         // Applying fee
  176.                         payRegular = (dayMinutes * .2) + payRegular;
  177.  
  178.                         // Finalizing payment due
  179.                         finalPay = payRegular;
  180.                 }
  181.         }
  182.         // Checking and calculating Premium service
  183.         else if (serviceType == 'p') {
  184.  
  185.                 cout << "Day Minutes: " << dayMinutes << endl;
  186.                 cout << "Night Minutes: " << nightMinutes << endl;
  187.  
  188.                 if (dayMinutes > 75)    // Checking to see if overcharge is necessary
  189.                         dayMinutes -= 75;       // Pulls minutes to be charged overtime
  190.                 else
  191.                         dayMinutes = 0;         // Applies standard charge if minutes not exceeded
  192.                 if (nightMinutes > 100) // Checking if overcharge is necessary
  193.                         nightMinutes-=100;      // Pulling minutes to be charged overtime
  194.  
  195.                 // Calculating the final pay based on pre-determined charges depending
  196.                 // on using more minutes than allowed.
  197.                 // Bases off day cycle as well
  198.                 finalPay = (dayMinutes * 0.1) + (nightMinutes * 0.05) + payPremium;
  199.         }
  200.  
  201.         return finalPay;
  202. }
  203.  
  204. void display(string displayName, int displayAccount, string displayServiceType, double displayServiceCharge) {
  205.         // Displaying final bill
  206.         cout << "Customer Name: " << displayName << endl;
  207.         cout << "Account Number: " << displayAccount << endl;
  208.         cout << "Service Type: " << displayServiceType << endl;
  209.         cout << "Service Charge: " << displayServiceCharge << endl << endl;
  210.         system("PAUSE");
  211.  
  212.         // Clear screen for aesthetic appeal
  213.         system("CLS");
  214.         cout << "(type EXIT to close program)" << endl;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment