Advertisement
bazmikel

hotfixed v1.0.1

Jun 23rd, 2020
1,391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5.  
  6. namespace EEngine_Task_2
  7. {
  8.  
  9.  
  10.     // enum colors mysi byc poza klasą program (w przestrzeni nazw), ponieważ nie jest on tam widoczny
  11.     // o ile Random jest objektem (tzn ze dziedziczy po klasie Object), musi on byc zainicjowany do dalszego użycia
  12.     // slowo kluczowe "new" służy do tworzenia nowego objektu, dla tego nie może być na początku deklaracji
  13.     // [private set] - pozwala zmieniac wartosc/inicjować wewnątrz klasy, natomiast [set] - nadaje dostęp na zewnątrz
  14.  
  15.     enum colors
  16.     {
  17.         Red,
  18.         Blue,
  19.         Pink,
  20.         Yellow,
  21.         Black
  22.     }
  23.     class Program
  24.     {
  25.        
  26.         public class Car
  27.         {
  28.             Random rnd = new Random();
  29.             public Guid Id { get; set; }  
  30.             public string Name { get; set; }
  31.             public colors Color { get; set; }
  32.             public int Speed { get; set; } = 0;
  33.  
  34.             public void SpeedUp()
  35.             {
  36.                
  37.                 this.Speed +=rnd.Next(5, 20);
  38.             }
  39.  
  40.  
  41.  
  42.             public void SlowDown()
  43.             {
  44.                 this.Speed -= rnd.Next(5, 20);
  45.                 if (this.Speed < 0)
  46.                 {
  47.                     Console.WriteLine("${this.Name} has stopped.");
  48.                 }
  49.             }
  50.         }
  51.         static void Main(string[] args)
  52.         {
  53.            
  54.             List<Car> cars = new List<Car>();
  55.  
  56.             cars.Add(new Car()
  57.             {
  58.                 Id = Guid.NewGuid(),
  59.                 Name = "Crashy",
  60.                 Color = colors.Red
  61.             });
  62.  
  63.             cars.Add(new Car()
  64.             {
  65.                 Id = Guid.NewGuid(),
  66.                 Name = "Trashy",
  67.                 Color = colors.Blue
  68.             });
  69.  
  70.             cars.Add(new Car()
  71.             {
  72.                 Id = Guid.NewGuid(),
  73.                 Name = "Rushy",
  74.                 Color = colors.Black
  75.             });
  76.  
  77.             while (!cars.Any(c => c.Speed >= 200))
  78.             {
  79.                 foreach (Car car in cars)
  80.                 {
  81.                     car.SpeedUp();
  82.                     Console.WriteLine($"{car.Name} is speeding up! current speed {car.Speed}!");
  83.                 }
  84.  
  85.                 Thread.Sleep(100);
  86.             }
  87.             while (!cars.Any(c => c.Speed <= 0))
  88.             {
  89.                 foreach (Car car in cars)
  90.                 {
  91.                     car.SlowDown();
  92.                     Console.WriteLine($"{car.Name} is slowing down! current speed {car.Speed}!");
  93.                 }
  94.                 Thread.Sleep(100);
  95.             }
  96.             Console.WriteLine("{0} {1} has stopped first.", cars.Single(c => c.Speed <= 0).Color, cars.Single(c => c.Speed <= 0).Name);
  97.             Console.ReadKey();
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement