Advertisement
bartigames

C###

Mar 3rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace csharpGWSH2
  6. {
  7.     class Parking
  8.     {
  9.         private int liczba_miejsc;
  10.         private bool[] P;
  11.         private int wolne;
  12.         char k;        
  13.         public Parking(int ile)
  14.         {
  15.             liczba_miejsc = ile;
  16.             wolne = ile;
  17.             P = new bool[ile];
  18.         }
  19.         public int wjazd()
  20.         {
  21.             int wolnem = -1;
  22.             //sprawdź czy są wolne miejsca
  23.             for (int i = 0; i < liczba_miejsc; i++)
  24.             {
  25.                 if (P[i] == false)
  26.                 {
  27.                    wolnem = i;
  28.                    P[i] = true;
  29.                    break;
  30.                 }
  31.             }
  32.  
  33.             if(wolnem == -1){
  34.              
  35.                 Console.Beep(5000, 100);
  36.                 return wolnem;
  37.  
  38.             }
  39.             else
  40.             {
  41.              
  42.                 return wolnem;
  43.                
  44.             }
  45.  
  46.          
  47.            
  48.         }
  49.         public void wyjazd(int nr)
  50.         {
  51.  
  52.             if (P[nr] == false) { Console.WriteLine("To miejsce jest już wolne!"); }
  53.             else
  54.             {
  55.                
  56.                 P[nr] = false;
  57.                 wolne++;
  58.             }      
  59.         }
  60.         public void drukuj_info()
  61.         {
  62.             for (int i = 0; i < liczba_miejsc; i++)
  63.             {
  64.                 if (P[i] == true)
  65.                 {
  66.                     kolor('r');
  67.                     Console.WriteLine("nr {0} - zajety", i);
  68.                     kolor('x');
  69.  
  70.                 }
  71.                 else { kolor('b'); Console.WriteLine("nr {0} - wolne", i); kolor('x'); }
  72.             }
  73.         }
  74.  
  75.        
  76.         public void kolor(char k)
  77.         {
  78.             if(k == 'r'){          
  79.                 Console.BackgroundColor = ConsoleColor.Red;
  80.             }
  81.             else  if (k == 'b')
  82.             {
  83.                 Console.BackgroundColor = ConsoleColor.Blue;
  84.             }
  85.  
  86.             else{ Console.BackgroundColor = ConsoleColor.Black; }
  87.                
  88.         }
  89.  
  90.             }
  91.              
  92.        
  93.    
  94.    
  95.  
  96.     class Program
  97.     {
  98.         static void Main(string[] args)
  99.         {
  100.             int miejsce;
  101.             char opcja = '1';
  102.             Parking Par = new Parking(10);
  103.             while (opcja != '4')
  104.             {
  105.                
  106.                 Console.WriteLine("*************************");
  107.                 Console.WriteLine("1. Wjazd samochodu");
  108.                 Console.WriteLine("2. Wyjazd samochodu");
  109.                 Console.WriteLine("3. Drukuj stan parkingu");
  110.                 Console.WriteLine("4. Koniec");
  111.                 Console.WriteLine("*************************");
  112.                 opcja = (char)Console.Read();
  113.                 Console.ReadLine();
  114.                 Console.Clear();
  115.                 switch (opcja)
  116.                 {
  117.                     case '1': miejsce = Par.wjazd();
  118.                         if (miejsce == -1){
  119.                             Par.kolor('r');
  120.                             Console.WriteLine("BRAK MIEJSC");
  121.                             Par.kolor('x');
  122.                          }
  123.                         else
  124.                             Console.WriteLine("numer miejsca {0}", miejsce);
  125.                         break;
  126.                     case '2':
  127.                         Par.drukuj_info();
  128.                         Console.Write("Które miejsce zwalniasz? ");
  129.                         miejsce = Int32.Parse(Console.ReadLine());
  130.                         Par.wyjazd(miejsce);
  131.                         break;
  132.                     case '3': Par.drukuj_info();
  133.                         break;
  134.                     case '4': break;
  135.                 }
  136.             }
  137.             Console.ReadLine();
  138.  
  139.         }
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement