/* המחלקה הראשית */ using System; public class ParkingManager { public static void Main() { //הגדרת משתנים והקצאתם ParkingLot parkingLot = new ParkingLot(); // עצם מסוג חניון int action; // הפעולה המבוקשת int hour; // שעת כניסה או יציאה int numOfSpace; // מספר מקום החניה int sumToPay; // סכום לתשלום //קליטת הפעולה המבוקשת Console.Write("Enter 1 for entry, 2 for exit, -1 to end: "); action = int.Parse(Console.ReadLine()); while (action != -1) { if (action == 1) // כניסה לחניון { Console.Write("Enter the time – a number between 6-22: "); hour = int.Parse(Console.ReadLine()); numOfSpace = parkingLot.CarIn(hour); if (numOfSpace == 0) Console.WriteLine("No free spaces"); else Console.WriteLine("Space number {0}", numOfSpace); } if (action == 2) // יציאה מהחניון { Console.Write("Enter the time – a number between 7-23: "); hour = int.Parse(Console.ReadLine()); Console.Write("Enter your space number: "); numOfSpace = int.Parse(Console.ReadLine()); sumToPay = parkingLot.CarOut(numOfSpace, hour); Console.WriteLine("You need to pay {0}", sumToPay); } Console.Write("Enter 1 for entry, 2 for exit, -1 to end: "); action = int.Parse(Console.ReadLine()); } // while // סגירת החניון Console.WriteLine("Total sum at the end of the day: {0}", parkingLot.EndOfDay()); } // Main } // class ParkingManager /* מחלקת חניון */ public class ParkingLot { private int[] spaces; private int cash; private const int HOURLY_RATE = 14; private const int NUM_OF_SPACES = 318; //פעולה בונה, מאפסת את הקופה ואת המערך public ParkingLot() { cash = 0; spaces = new int[NUM_OF_SPACES]; for (int i = 0; i < spaces.Length; i++) spaces[i] = 0; } // פעולה המקבלת שעת כניסה ומחזירה את מספרו של המקום הפנוי הראשון, // אם אין מקום פנוי יוחזר 0 public int CarIn(int hour) { int spaceNumber = 0; int i = 0; while (i < spaces.Length && spaceNumber == 0) { if (spaces[i] == 0) { spaceNumber = i + 1; spaces[i] = hour; } i++; } return spaceNumber; } // פעולה המקבלת שעת כניסה ומקום בחניון, ומחזירה את הסכום שיש לשלם public int CarOut(int spaceNumber, int hour) { int sumToPay; sumToPay = (hour - spaces[spaceNumber - 1]) * HOURLY_RATE; spaces[spaceNumber - 1] = 0; cash = cash + sumToPay; return sumToPay; } // פעולה המחזירה את הפדיון היומי public int EndOfDay() { return cash; } } // class ParkingLot