C#. Using the Automobile class you created Chapter 10, Programming Exercise 5A as a base, derive a FinancedAutomobile class that contains all the data of an Automobile, plus the following field:
- AmtFinanced - The amount financed (a double)
Override the parent class ToString() method to include the child class’s additional data.
Create a program named AutomobileDemo2 that contains an array of four FinancedAutomobile objects. Prompt the user for all the necessary data, and do not allow duplicate ID numbers and do not allow the amount financed to be greater than the price of the automobile.
Sort all the records in ID number order and display them with a total price for all FinancedAutomobiles and a total amount financed. For example, the output should be in the following format:
Summary: FinancedAutomobile 1 2017 Honda Price is $26,000.00 Amount financed $12,000.00 FinancedAutomobile 4 2019 Honda Price is $36,000.00 Amount financed $0.00 FinancedAutomobile 5 2016 Toyota Price is $30,000.00 Amount financed $3,000.00 FinancedAutomobile 8 2018 Honda Price is $16,000.00 Amount financed $6,000.00 Total for all Automobiles is $108,000.00 Total financed all Automobiles is $21,000.00
using System;
using System.Globalization;
namespace AutomobileDemo
{
class Program
{
static void Main(string[] args)
{
bool check = false;
//Create an array of automobiles for test
Automobile[] ad = new FinancedAutomobile[8];
double totalPrice = 0, totalFinancedPrice = 0; ;
//Inititalize array
for (int i = 0; i < 3; i++){
ad[i] = new FinancedAutomobile();
}
//Read values
for (int i = 0; i < 3; i++){
Console.WriteLine("ID number: ");
ad[i].ID = Convert.ToInt32(Console.ReadLine());
//ID check
for (int j = 0; j < 3; j++){
if (ad[i].ID == ad[j].ID && i != j){
check = true;
break;
}
else{
check = false;
}
}
while (check){
Console.WriteLine("Enter different id number, last one was already present in an array: ");
ad[i].ID = Convert.ToInt32(Console.ReadLine());
for (int j = 0; j < 3; j++){
if (ad[i].ID == ad[j].ID && i != j){
check = true;
break;
}
else{
check = false;
}
}
}
Console.WriteLine("Make: ");
ad[i].Make = Console.ReadLine();
Console.WriteLine("Year: ");
ad[i].Year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Price: ");
ad[i].Price = Convert.ToDouble(Console.ReadLine());
totalPrice += ad[i].Price;
Console.WriteLine("Financed Price: ");
ad[i].AmountFinanced= Convert.ToDouble(Console.ReadLine());
totalFinancedPrice += ad[i].AmountFinanced;
}
//Sort array using ID
Automobile temp = new Automobile();
for (int i = 0; i < 3; i++){
for (int j = i + 1; j < 3; j++){
if (ad[i].ID > ad[j].ID) {
temp = ad[i];
ad[i] = ad[j];
ad[j] = temp;
}
}
}
//Display details
Console.WriteLine("\nSummary:");
for (int i = 0; i < 3; i++){
Console.WriteLine(ad[i].ToString());
}
Console.WriteLine("\nTotal for all Automobile is " + totalPrice.ToString("C", CultureInfo.GetCultureInfo("en_US")));
Console.WriteLine("\nTotal financed all Automobile is " +totalFinancedPrice.ToString("C", CultureInfo.GetCultureInfo("en_US")));
Console.ReadLine();
}
}
//Create a class Automobile
class Automobile
{
//Members
private int idNumber, year;
private String make;
private double price;
//Constructors
public Automobile()
{
idNumber = -1;
year = 0;
price = 0;
make = "";
}
public Automobile(int id, string m, int yr, double pr)
{
idNumber = id;
make = m;
year = yr;
price = pr;
}
//Getter setter
public int ID
{
set
{
idNumber = value;
}
get { return idNumber; }
}
public string Make
{
get { return make; }
set { make = value; }
}
public int Year
{
set
{
year = value;
}
get { return year; }
}
public double Price
{
set { price = value; }
get { return price; }
}
public virtual double AmountFinanced { get; set; }
//Tostring
public virtual string ToString()
{
return "Automobile " + idNumber + " " + year + " " + make + " " + price.ToString("C", CultureInfo.GetCultureInfo("en_US"));
}
}
//Create a child class FinancedAutomobile
class FinancedAutomobile : Automobile
{
//Member variable
private double amtFinanced;
//Constructors
public FinancedAutomobile():base()
{
amtFinanced = 0;
}
public FinancedAutomobile(int id, string m, int yr, double pr, double amtF):base(id,m,yr,pr)
{
amtFinanced = amtF;
}
//Getter setter
public override double AmountFinanced
{
set { amtFinanced = value; }
get { return amtFinanced; }
}
public override string ToString()
{
return "Financed" + base.ToString() + " Amount Finances is " + amtFinanced.ToString("C", CultureInfo.GetCultureInfo("en_US"));
}
}
}