CarDemo Console App


Program.cs

[code language=”csharp”] using System;
using System.Collections.Generic;
using static System.Console;

/// <summary>
/// # # # # # # # #
/// # Mark Hesser #
/// # Feb 14, 18 #
/// # Car Demo #
/// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
/// # Instructions: #
/// # Create an application named CarDemo that declares at least two Car objects and demonstrates how #
/// # they can be incremented using an overloaded ++ operator. Create a Car class that contains a model #
/// # and a value for miles per gallon. Include two overloaded constructors. One accepts parameters for #
/// # the model and miles per gallon; the other accepts a model and sets the miles per gallon to 20. #
/// # Overload a ++ operator that increases the miles per gallon value by 1. The CarDemo application #
/// # creates at least one Car using each constructor and displays the Car values both before and #
/// # after incrementation. #
/// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
/// </summary>
namespace CarDemo
{
class Program
{
static void Main(string[] args)
{
Title = "Car Demo";
Clear();

#region Declarations
//Create oldCars and newCars Lists
var oldCars = new List<object>();
var newCars = new List<object>();

//Create old Cars with Car Class.
var oc1 = new Car("Ford Focus", 32); oldCars.Add(oc1);
var oc2 = new Car("Ford Escape"); oldCars.Add(oc2);

//Create new cars from old cars.
var nc1 = new Car(oc1.Model, oc1.MPG);
var nc2 = new Car(oc2.Model, oc2.MPG);

//Use Overloaded Operator to increament MPG by 1 and Add to newCars.
nc1++; newCars.Add(nc1);
nc2++; newCars.Add(nc2);
#endregion

WriteLine(" Old Cars");
Display(oldCars);

WriteLine("\n New Cars");
Display(newCars);

Quit();
}

/// <summary>
/// Displays each object in the list
/// </summary>
/// <param name="cars"></param>
static void Display(List<object> cars)
{
foreach (var item in cars)
{
WriteLine(item);
}
}

/// <summary>
/// Prompts the User to Press any key to Quit
/// </summary>
static void Quit()
{
Write("\n Press any Key to Quit. "); //Put message to the user here.
ReadKey(); //Listens for Key
}
}
}
[/code]

Car.cs

[code language=”csharp”] using System;

///<summary>
/// # # # # # # # #
/// # Mark Hesser #
/// # Feb 14, 18 #
/// # Car Demo #
/// # # # # # # # #
/// </summary>
namespace CarDemo
{
class Car
{
/// <summary>
/// No Argument Constructor Method.
/// </summary>
public Car()
{

}

/// <summary>
/// Car Entry Constructor Method. Default MPG = 20.
/// </summary>
/// <param name="model"></param>
/// <param name="mpg"></param>
public Car(string model, double mpg = 20)
{
Model = model;
MPG = mpg;
}

/// <summary>
/// ++ operator that adds 1 to MPG.
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public static Car operator ++(Car a)
{
a.MPG++;
return a;
}

public string Model;
public double MPG;

public override string ToString()
{
return String.Format(" {0}, {1} MPG", Model, MPG);
}
}
}
[/code]