Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- namespace EEngine_Task_2
- {
- // enum colors mysi byc poza klasą program (w przestrzeni nazw), ponieważ nie jest on tam widoczny
- // o ile Random jest objektem (tzn ze dziedziczy po klasie Object), musi on byc zainicjowany do dalszego użycia
- // slowo kluczowe "new" służy do tworzenia nowego objektu, dla tego nie może być na początku deklaracji
- // [private set] - pozwala zmieniac wartosc/inicjować wewnątrz klasy, natomiast [set] - nadaje dostęp na zewnątrz
- enum colors
- {
- Red,
- Blue,
- Pink,
- Yellow,
- Black
- }
- class Program
- {
- public class Car
- {
- Random rnd = new Random();
- public Guid Id { get; set; }
- public string Name { get; set; }
- public colors Color { get; set; }
- public int Speed { get; set; } = 0;
- public void SpeedUp()
- {
- this.Speed +=rnd.Next(5, 20);
- }
- public void SlowDown()
- {
- this.Speed -= rnd.Next(5, 20);
- if (this.Speed < 0)
- {
- Console.WriteLine("${this.Name} has stopped.");
- }
- }
- }
- static void Main(string[] args)
- {
- List<Car> cars = new List<Car>();
- cars.Add(new Car()
- {
- Id = Guid.NewGuid(),
- Name = "Crashy",
- Color = colors.Red
- });
- cars.Add(new Car()
- {
- Id = Guid.NewGuid(),
- Name = "Trashy",
- Color = colors.Blue
- });
- cars.Add(new Car()
- {
- Id = Guid.NewGuid(),
- Name = "Rushy",
- Color = colors.Black
- });
- while (!cars.Any(c => c.Speed >= 200))
- {
- foreach (Car car in cars)
- {
- car.SpeedUp();
- Console.WriteLine($"{car.Name} is speeding up! current speed {car.Speed}!");
- }
- Thread.Sleep(100);
- }
- while (!cars.Any(c => c.Speed <= 0))
- {
- foreach (Car car in cars)
- {
- car.SlowDown();
- Console.WriteLine($"{car.Name} is slowing down! current speed {car.Speed}!");
- }
- Thread.Sleep(100);
- }
- Console.WriteLine("{0} {1} has stopped first.", cars.Single(c => c.Speed <= 0).Color, cars.Single(c => c.Speed <= 0).Name);
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement