1 using System; 2 3 namespace parking 4 { 5 //************************************** 6 // Program Name: parkingdriver.cs * 7 // Author: Pat Moss * 8 // Date: Mar 12, 2006 Rev. 01 * 9 // * 10 // Driver for the calcparking.cs class * 11 //************************************** 12 13 // Create a calcparking object. 14 // Park vehicles while there are more to park. 15 // For each vehicle: 16 // 1. Execute the calcparking object to 17 // determine and display the parking fee. 18 // 2. Accumulate the parking fee total 19 // amount collected. 20 // When all vehicles have been parked, 21 // format and display the parking fee total 22 // amount collected. 23 24 public class parkingdriver 25 { 26 public static void Main () 27 { 28 bool moretopark = true; 29 double totalamtCollected = 0.0; 30 string inputctrl = ""; 31 calcparking calcparking1 = new calcparking(); 32 33 Console.WriteLine ("parkingdriver.cs rev. 01 by pat moss"); 34 Console.WriteLine (""); 35 36 Console.Write ("Do you have more cars to park (Y or N)?"); 37 inputctrl = Console.ReadLine().ToUpper(); 38 39 if (inputctrl.Length < 1) 40 moretopark = false; 41 else if (inputctrl[0] == 'Y') 42 moretopark = true; 43 else moretopark = false; 44 45 while (moretopark == true) // Use sentinel = false to terminate the loop 46 { 47 // calculate and display the parking fee information for this vehicle 48 // and accumulate the total amount collected 49 totalamtCollected += calcparking1.getInputData(); 50 51 Console.Write ("Do you have more cars to park (Y or N)?"); 52 inputctrl = Console.ReadLine().ToUpper(); 53 54 if (inputctrl.Length < 1) 55 moretopark = false; 56 else if (inputctrl[0] == 'Y') 57 moretopark = true; 58 else moretopark = false; 59 } 60 61 // round the output value to two decimal places 62 63 // format and display the total amount collected 64 Console.WriteLine ("\nTotal amount collected: {0:f2}", totalamtCollected); 65 66 Console.WriteLine ("All Done..."); 67 } 68 } 69 70 //**************************************** 71 // Program Name: calcparking.cs * 72 // Author: Pat Moss * 73 // Date: Mar 12, 2006 Rev. 01 * 74 // * 75 // Demonstrate class "calcparking", etc. * 76 //**************************************** 77 78 // calculate and display the parking fee 79 // for the current vehicle, as follows: 80 81 // Code: Desc.: Parking Fees: 82 // C Cars: First 2 hours = Free 83 // Next 3 hours = 0.50/hour 84 // Next 11 hours = 0.25/hour 85 86 // T Trucks: First 1 hours = Free 87 // Next 2 hours = 1.00/hour 88 // Next 13 hours = 0.75/hour 89 90 // S Senior Citizens: No charge 91 92 // H Display User Help screen 93 94 public class calcparking 95 { 96 string inputstr; 97 char glinputCode; 98 string glinputCodestr; 99 100 bool inputcodeok, bgntimeok, endtimeok, totalhrsok; 101 102 double bgnhrs, endhrs, totalhrs; 103 104 double glcheckHrs, gltotalHrs, glparkingFee; 105 106 // constructor 107 public calcparking() 108 { 109 initall(); 110 } 111 112 // initialize all variables 113 private void initall() 114 { 115 inputstr = ""; 116 glinputCode = ' '; 117 glinputCodestr = ""; 118 inputcodeok = false; 119 bgntimeok = false; 120 endtimeok = false; 121 totalhrsok = false; 122 bgnhrs = 0.0; 123 endhrs = 0.0; 124 totalhrs = 0.0; 125 glcheckHrs = 0.0; 126 gltotalHrs = 0.0; 127 glparkingFee = 0.0; 128 } 129 130 // process the current vehicle information 131 public double getInputData() 132 { 133 initall(); 134 135 do 136 { 137 Console.Write ("Car(C), Truck(T), Senior(S), or Help(H)?"); 138 inputstr = Console.ReadLine().ToUpper(); 139 inputcodeok = checkinputcode(inputstr); 140 } while (!inputcodeok); 141 142 // Console.WriteLine("** glinputCode: " + glinputCode); 143 144 do 145 { 146 // "S" = Senior citizen = "No charge" 147 if (glinputCode == 'S') 148 { 149 totalhrs = 0.0; 150 gltotalHrs = 0.0; 151 break; 152 } 153 154 do 155 { 156 Console.Write ("Begin time (HH:MM AM/PM)?"); 157 inputstr = Console.ReadLine().ToUpper(); 158 bgntimeok = checkinputtime(inputstr); 159 } while (!bgntimeok); 160 161 bgnhrs = glcheckHrs; 162 163 do 164 { 165 Console.Write ("Exit time (HH:MM AM/PM)?"); 166 inputstr = Console.ReadLine().ToUpper(); 167 endtimeok = checkinputtime(inputstr); 168 } while (!endtimeok); 169 170 endhrs = glcheckHrs; 171 172 totalhrs = endhrs - bgnhrs; 173 gltotalHrs = Math.Ceiling (totalhrs); 174 175 // Console.Write ("**bgn/end/totalhrs/gltotalHrs: "); 176 // Console.Write (bgnhrs + " " + endhrs + " " + totalhrs); 177 // Console.Write (" " + gltotalHrs); 178 179 if((totalhrs < 0.0) || (totalhrs > 16.0)) 180 { // error 181 Console.WriteLine ("*Invalid hours: {0:f2", totalhrs); 182 } 183 else 184 { 185 totalhrsok = true; 186 } 187 } while (!totalhrsok); 188 189 // calculate the Parking Fee 190 glparkingFee = getParkingFee(glinputCode, gltotalHrs); 191 192 // display the parking Code/Total hours/Parking Fee 193 printReport (glinputCodestr, gltotalHrs, glparkingFee); 194 195 // return the parking fee to the main driver program 196 return glparkingFee; 197 } 198 199 // check for a valid input code 200 // must be one of "C", "T", "S", or "H" 201 private bool checkinputcode (string inpstr) 202 { 203 glinputCode = ' '; 204 glinputCodestr = ""; 205 206 if (inpstr.Length != 1) 207 return false; 208 209 char inpcode = inpstr[0]; 210 211 switch (inpcode) 212 { 213 case 'C': 214 glinputCode = inpcode; 215 glinputCodestr = "Car"; 216 break; 217 case 'T': 218 glinputCode = inpcode; 219 glinputCodestr = "Truck"; 220 break; 221 case 'S': 222 glinputCode = inpcode; 223 break; 224 case 'H': 225 // "H" = Help = Display a Help screen 226 Console.WriteLine ("User Help:"); 227 Console.WriteLine (""); 228 Console.WriteLine ("C Cars: First 2 hours = Free"); 229 Console.WriteLine (" Next 3 hours = 0.50/hour"); 230 Console.WriteLine (" Next 11 hours = 0.25/hour"); 231 Console.WriteLine (""); 232 Console.WriteLine ("T Trucks: First 1 hours = Free"); 233 Console.WriteLine (" Next 2 hours = 1.00/hour"); 234 Console.WriteLine (" Next 13 hours = 0.75/hour"); 235 Console.WriteLine (""); 236 Console.WriteLine ("Hours are adjusted upward to next whole hour."); 237 Console.WriteLine (""); 238 Console.WriteLine ("S Senior Citizens: No charge."); 239 Console.WriteLine (""); 240 Console.WriteLine ("H This User Help screen."); 241 Console.WriteLine (""); 242 return false; 243 default: 244 return false; 245 } 246 return true; 247 } 248 249 // check for a valid input time string value 250 // valid input format is HH:MM XM (where X = "A" or "P") 251 // and convert a valid input time to decimal hours since midnight 252 // for example, 2:30 AM is 2.50 hours, and 2:30 PM is 14.50 hours 253 private bool checkinputtime (string inpstr) 254 { 255 glcheckHrs = 0.0; 256 257 if (inpstr.Length != 8) 258 { 259 Console.WriteLine ("*Invalid length: {0}", inpstr.Length); 260 return false; 261 } 262 263 if (!checkformat (inpstr, "99:99 XM")) 264 { 265 Console.WriteLine ("*Invalid format: {0}", inpstr); 266 return false; 267 } 268 269 // fetch hours, minutes, and AM/PM from the input string 270 string strHH = inpstr.Substring (0, 2); 271 string strMM = inpstr.Substring (3, 2); 272 string strXM = inpstr.Substring (6, 2); 273 274 // Console.WriteLine("inpstr: " + inpstr); 275 // Console.WriteLine("HH/MM/XM: " + strHH + " " + strMM + " " + strXM); 276 // Console.Write ("Lens: " + strHH.Length + " ") 277 // Console.WriteLine(strMM.Length + " " + strXM.Length); 278 279 // convert hours and minutes from string to integer format 280 int intHH = int.Parse (strHH); 281 int intMM = int.Parse (strMM); 282 283 // compute total hours in decimal format 284 double numhrs = (double) intHH + intMM / 60.0; 285 286 // fix per Dave Hammer: 287 // a. if hours >= 12, then subtract 12 from hours 288 // this applies for 12:xx AM and for 12:xx PM 289 // b. if suffix = "PM", then add 12 back to hours 290 // then we have decimal "military time, 291 // 00.00 hours to approx 23.99 hours 292 293 if (numhrs >= 12.0) 294 numhrs -= 12.0; 295 296 if (strXM.Equals("PM")) 297 numhrs += 12.0; 298 299 glcheckHrs = numhrs; 300 return true; 301 } 302 303 // check for a valid string format 304 305 // valid format characters include: 306 // 9 = must be 0 to 9 307 // X = must be A or P (for AM or PM) 308 // Literal characters: space or : or M 309 // 310 // Example format string for an input time: 311 // 99:99 XM 312 // 313 private bool checkformat (string inpstr, string inpfmt) 314 { 315 int inplen = inpstr.Length; 316 317 for (int i=0; i '9')) 347 return false; 348 break; 349 default: 350 return false; 351 } 352 } 353 return true; 354 } 355 356 // calculate the parking fee 357 // based on: 358 // 1. The input code ("C", "T", "S", or "H") 359 // 2. The total hours (0.0 to 16.0) 360 361 // for the current vehicle, as follows: 362 363 // Code: Desc.: Parking Fees: 364 // C Cars: First 2 hours = Free 365 // Next 3 hours = 0.50/hour 366 // Next 11 hours = 0.25/hour 367 368 // T Trucks: First 1 hours = Free 369 // Next 2 hours = 1.00/hour 370 // Next 13 hours = 0.75/hour 371 372 // S Senior Citizens: No charge 373 374 // H Help: No charge 375 376 private double getParkingFee (char inpcode, double tothrs) 377 { 378 // adjust total hours upward to the next whole hour value 379 double dblhrs = Math.Ceiling (tothrs); 380 double totfee = 0.0; 381 382 // Console.WriteLine ("tothrs,dblhrs: " + tothrs + " " + dblhrs); 383 384 switch (inpcode) 385 { 386 case 'C': 387 if (dblhrs <= 2.0) 388 { 389 totfee = 0.0; 390 } 391 else if ((dblhrs > 2.0) && (dblhrs <= 5.0)) 392 { 393 totfee = (dblhrs - 2.0) * 0.50; 394 } 395 else 396 { 397 totfee = (3) * 0.50; 398 totfee += (dblhrs - 5.0) * 0.25; 399 } 400 break; 401 case 'T': 402 if (dblhrs <= 1.0) 403 { 404 totfee = 0.0; 405 } 406 else if ((dblhrs > 1.0) && (dblhrs <= 3.0)) 407 { 408 totfee = (dblhrs - 1.0) * 1.00; 409 } 410 else 411 { 412 totfee = (2.0) * 1.00; 413 totfee += (dblhrs - 3.0) * 0.75; 414 } 415 break; 416 case 'S': 417 totfee = 0.0; 418 break; 419 case 'H': 420 totfee = 0.0; 421 break; 422 default: 423 totfee = 0.0; 424 break; 425 } 426 return totfee; 427 } 428 429 // display the parking code, parking hours, and parking fee 430 private void printReport (string inpcodestr, double dblhrs, double parkfee) 431 { 432 // adjust total hours upward to the next whole hour value 433 int inthrs = (int) Math.Ceiling (dblhrs); 434 435 // Round the output to two decimal places 436 437 Console.WriteLine (""); 438 Console.Write ("The " + inpcodestr); 439 Console.Write (" parking fee due for "); 440 Console.Write (inthrs + " hours is "); 441 Console.WriteLine ("{0:f2} ", parkfee); 442 } 443 } 444 }