using System;
namespace MyProgram
{
class MainClass
{
static void Main()
{
string Version = "1.0";
Console.WriteLine("==================");
Console.WriteLine("Car Driving - v{0}", Version);
Console.WriteLine("==================\n");
Car car1 = new Car("Fiat Uno", 45);
Car car2 = new Car(20);
car1.ShowStats();
car2.ShowStats();
car1.Accelerate(15);
car1.ShowStats();
car2.Decelerate(10);
car2.ShowStats();
car2.Decelerate(20);
car2.ShowStats();
}
}
}
using System;
namespace MyProgram
{
public class Car
{
// Member Variables
// ================
public string Name = "Unnamed Car";
public int Speed;
// Default Constructor
// ===================
public Car() { }
// Custom Constructors
// ===================
public Car(string Name)
{
this.Name = Name;
}
public Car(int Speed)
{
this.Speed = Speed;
}
public Car(string Name, int Speed)
{
this.Name = Name;
this.Speed = Speed;
}
// Member Methods
// ==============
public void ShowStats()
{
Console.WriteLine("Car name: {0} --- Current Speed: {1}", this.Name,
this.Speed);
}
public void Accelerate(int Delta)
{
this.Speed += Delta;
Console.WriteLine("{0} now goes {1} Km/h!", this.Name, this.Speed);
}
public void Decelerate(int Delta)
{
if(this.Speed - Delta <= 0) {
this.Speed = 0;
}
else {
this.Speed -= Delta;
}
Console.WriteLine("{0} now goes {1} Km/h!", this.Name, this.Speed);
}
}
}