using System; using System.Collections.Generic; using System.Text; namespace _303_fall_07_hw4 { class Program { // ------------------------------------------------------------------------------------------- static void Main(string[] args) { // declare and initialize variables needed double total_receipts = 0.0; double hours_parked; double fee; // keep track of how many customers (not required, but nice) int customer_count = 1; do { // prompt the user Console.Write("How long did patron {0} park (enter 0 to quit) : ", customer_count); // read in hours parked hours_parked = Convert.ToDouble(Console.ReadLine()); // invoke function to determine how much to charge fee = CalculateCharges(hours_parked); // if valid charge (and not quitting the program) if (fee != 0) { // add charge to running total total_receipts += fee; // display results to user Console.WriteLine("Cost for patron {0} is = {1:c}", customer_count, fee); Console.WriteLine("Total receipts taken in is = {0:c}", total_receipts); // increase count of customers customer_count++; } } while (hours_parked != 0); // keep repeating until user selects quit } // ------------------------------------------------------------------------------------------- private static double CalculateCharges(double hours_parked) { // round up for fractional hours (ensures 4.1 hours becomes 5, for example) hours_parked = Math.Ceiling(hours_parked); double cost; // verify the input is valid if ((hours_parked < 0) || (hours_parked > 24)) { Console.WriteLine("Incorrect number; hours parked must be >= 0 and <= 24"); cost = 0.0; } else if (hours_parked == 0) cost = 0.0; else if (hours_parked <= 3) // flat-rate for up to 3 hours cost = 2.0; else cost = (hours_parked - 3) * 0.5 + 2.0; // $2 + $0.50/hour after 3 hours // ensure that no charge exceeds 10 // b/c in the case where hours_parked > 19, cost would be too high unless this check is in place if (cost > 10.0) cost = 10; return cost; } // ------------------------------------------------------------------------------------------- } }