Question: 10.5B. Solution must be in C#. Please refer the code below! Using the Automobile class as a base, derive a FinancedAutomobile class that contains all
10.5B. Solution must be in C#. Please refer the code below!
Using the Automobile class 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 classs 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:
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
CODE
using System; using static System.Console; class AutomobileDemo2 { static void Main() { Automobile[] automobiles = new Automobile[8]; int x, y; double grandTotal = 0; bool goodNum; for(x = 0 ; x < automobiles.Length; ++x) { automobiles[x] = new Automobile(); Write("Enter automobile ID number "); automobiles[x].IdNumber = Convert.ToInt32(ReadLine()); goodNum = true; for(y = 0; y < x; ++y) { if(automobiles[x].Equals(automobiles[y])) goodNum = false; } while(!goodNum) { Write("Sorry, the ID number " + automobiles[x].IdNumber + " is a duplicate. " + " Please reenter "); automobiles[x].IdNumber = Convert.ToInt32(ReadLine()); goodNum = true; for(y = 0; y < x; ++y) { if(automobiles[x].Equals(automobiles[y])) goodNum = false; } } Write("Enter make "); automobiles[x].Make = ReadLine(); Write("Enter year "); automobiles[x].Year = Convert.ToInt32(ReadLine()); Write("Enter price "); automobiles[x].Price = Convert.ToDouble(ReadLine()); } Array.Sort(automobiles); WriteLine(" Summary: "); for(x = 0; x < automobiles.Length; ++x) { WriteLine(automobiles[x].ToString()); grandTotal += automobiles[x].Price; } WriteLine(" Total for all Automobiles is " + grandTotal.ToString("C")); } } class Automobile : IComparable { public Automobile(int num, string make, int year, double price) { IdNumber = num; Make = make; Year = year; Price = price; } public Automobile() : this(999, "ZZZ", 0, 0) { } public int IdNumber {get; set;} public string Make {get; set;} public int Year {get; set;} public double Price {get; set;} public override string ToString() { return(GetType() + " " + IdNumber + " " + Year + " " + Make + " Price is " + Price.ToString("C")); } public override bool Equals(Object e) { bool isEqual; Automobile temp = (Automobile)e; if(IdNumber == temp.IdNumber) isEqual = true; else isEqual = false; return isEqual; } public override int GetHashCode() { return IdNumber; } int IComparable.CompareTo(Object o) { int returnVal; Automobile temp = (Automobile)o; if(this.IdNumber > temp.IdNumber) returnVal = 1; else if(this.IdNumber < temp.IdNumber) returnVal = -1; else returnVal = 0; return returnVal; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
