Advertisement
Stann

BeerTime

Mar 24th, 2014
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. class BeerTime
  4. {
  5.     static void Main()
  6.     {
  7.         //A beer time is after 1:00 PM and before 3:00 AM. Write a program that enters a time in format “hh:mm tt” (an hour in range [01...12], a minute in range [00…59] and AM / PM designator) and prints “beer time” or “non-beer time” according to the definition above or “invalid time” if the time cannot be parsed. Note that you may need to learn how to parse dates and times.
  8.  
  9.         string startBeerTime = "1:00 PM";
  10.         string endBeerTime = "3:00 AM";
  11.         string preMidnight = "23:59 PM";
  12.         string midnight = "12:00 AM";
  13.         DateTime start = Convert.ToDateTime(startBeerTime);
  14.         DateTime end = Convert.ToDateTime(endBeerTime);
  15.         DateTime preMid = Convert.ToDateTime(preMidnight);
  16.         DateTime mid = Convert.ToDateTime(midnight);
  17.         DateTime time;
  18.         string input = Console.ReadLine();
  19.         if (DateTime.TryParseExact(input, "h:mm tt", CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.None, out time))
  20.         {
  21.             if (time >= start && time <= preMid)
  22.             {
  23.                 Console.WriteLine("Beer time!");
  24.             }
  25.             else if (time >= mid && time < end)
  26.             {
  27.                 Console.WriteLine("Beer time!");
  28.             }
  29.             else
  30.             {
  31.                 Console.WriteLine("Non-beer time!");
  32.             }
  33.         }
  34.         else
  35.         {
  36.             Console.WriteLine("Invalid time!");
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement