Ryley C

Ryley C

  • NA
  • 4
  • 618

How to repeat code after invalid data is entered

Aug 29 2018 12:20 AM
I have an assignment to calculate the *parkingFee*, I'm having a hard time figuring out how to get this code to repeat after a invalid number (greater than 24 or less than 1) has been entered. 
 
The parking fee in a parking station is calculated on whole number of hours multiplied by the hourly rate of $2.50. A program is required to input and validate the hours to be between 1-24 inclusive. Calculate the parking fee given there is a maximum parking fee of $20.00. Output the hours parked and the parking fee (formatted to 2 decimal places)."
Below you'll find the code that I'm having trouble with. Any help would be greatly appreciated. 
  1. namespace _3PRB_Assignment_Part_1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.            /* Ryley Copeman 
  8.             * 29/08/2018 
  9.             * Assignment parkingFee2*/  
  10.   
  11.             /* PSEUDOCODE */  
  12.             /* HOURLY_RATE=2.5 
  13.              * INPUT parkTime 
  14.              * parkFee = HOURLY_RATE * hours 
  15.              * OUTPUT parkFee */  
  16.   
  17.   
  18.                 decimal parkTime;  // input - time in hour eg 1.5 for 1 and a half hours   
  19.                 const decimal HOURLY_RATE = 2.50m; // HOURLY_RATE * INPUT parkTime = parkFee  
  20.                 const decimal MAX_FEE = 20.00m; // MAX_FEE is set as a payment cap and ignores any extra charges incurred over 8 hours  
  21.                 decimal parkFee;  
  22.                 Console.WriteLine("ParkingFee1 program developed by: Ryley Copeman");  
  23.                 Console.WriteLine("Please enter your total time parked in hours: Eg 1.5 or 3.0");  
  24.                 parkTime = decimal.Parse(Console.ReadLine());  
  25.   
  26.                 if (parkTime > 8)  
  27.                 {  
  28.                     Console.Write("Total fee is $" + MAX_FEE);  
  29.                 }  
  30.                 else  
  31.                 {  
  32.                     parkFee = Math.Ceiling(parkTime) * HOURLY_RATE;  
  33.                     Console.Write("Parking Fee = $" + parkFee);  
  34.                 }  
  35.                 while(parkTime < 0 || parkTime > 24)  // validate...   
  36.                 //while (parkTime <= 0) )  
  37.                 {  
  38.                 Console.WriteLine("Error – Park Time out of range");  
  39.                 Console.WriteLine("Enter - Park Time between 0 and 24 (HOURS):");  
  40.                 parkTime = int.Parse(Console.ReadLine());  
  41.                   
  42.             }  
  43.                                                       
  44.             }  
  45.         }  
  46.     }  

Answers (2)