In C# window Form Add a class library project (VehiclesLib) To the Library proje
ID: 3890178 • Letter: I
Question
In C# window Form Add a class library project (VehiclesLib) To the Library project
Add class: Vehicle (_make, _model, _year, _price, _vin), Add constructor and get properties, Method: UpdatePrice(decimal newPrice).
Then Add a class: Person (_firstname, _lastname, List vehicleList), Add constructor, get properties, Method: Add(Vehicle v), Method: Remove(string vin) (remove by vin number)
In Form1: create a list of person, preload the list with at least 7 people to each add at least 2 vehicles, provide gui to display all the people and their vehicles, provide gui to add a vehicle to a person, provide gui to update the price of a vehicle of a person, provide gui to remove (sell) a car of a person.
Explanation / Answer
namespace DataContracts
{
//Vehicle Class
public class Vehicle
{
private static int count = 0;
#region private properties
private string _make;
private string _model;
private int _year;
private decimal _price;
private string _vin;
private int _id;
#endregion
#region Properties
public string Make { get { return _make; } }
public string Mode { get { return _model; } }
public int Year { get { return _year; } }
public decimal Price { get { return _price; } }
public string Vid { get { return _vin; } }
#endregion
#region constructor
public Vehicle(string make = "", string model = "", int year = 1980, decimal price = 0, string vin = "")
{
_id = ++count;
_make = make;
_model = model;
_year = year;
_price = price;
_vin = vin;
}
#endregion
#region public methods
// To update the price of the vehicle.
/// <param name="newPrice">new value.</param>
/// <returns>true if update is successfull.</returns>
public bool UpdatePrice(decimal newPrice)
{
if (newPrice == 0)
return false;
_price = newPrice;
return true;
}
#endregion
}
}
namespace DataContracts
{
using System.Collections.Generic;
using System.Linq;
public class Person
{
private static int personCount = 0;
#region private properties
private string _firstname;
private string _lastname;
private IList<Vehicle> _vehicleList;
private int _pid;
#endregion
#region properties
public string Firstname { get { return _firstname; } }
public string Lastname { get { return _lastname; } }
public IList<Vehicle> VehicleList { get { return _vehicleList; } }
#endregion
#region constructor
public Person(string firstname = "", string lastname = "", IList<Vehicle> vehicleList = null)
{
_firstname = firstname;
_lastname = lastname;
_vehicleList = vehicleList;
}
#endregion
#region public methods
/// <summary>
/// to add vehicle.
/// </summary>
/// <param name="v">vehicle.</param>
/// <returns>true if succesfull.</returns>
public bool Add(Vehicle v)
{
if (v == null)
return false;
_vehicleList.Add(v);
return true;
}
/// <summary>
/// remove vehicle.
/// </summary>
/// <param name="vin">vehicle number.</param>
/// <returns>true if removed.</returns>
public bool Remove(string vin)
{
if (string.IsNullOrEmpty(vin))
return false;
((List<Vehicle>)_vehicleList).RemoveAll(v => v.Vid == vin);
return true;
}
#endregion
}
}