Auto Loan



Auto Loan

This is an example of a C# program using inheritance to project an auto loan.

Here's the code!

BankLoan.cs

                            namespace AutoLoan
                            {
                               class BankLoan
                                {
                                    public double LoanAmount { get; set; }
                                    private const double INTERESTRATE = .05;

                                    public virtual void makePayment()
                                    {
                                        LoanAmount -= 200;
                                    }

                                    public void addInterest()
                                    {
                                        LoanAmount = (LoanAmount * INTERESTRATE) + LoanAmount;
                                    }
                                }

                                class CarLoan : BankLoan
                                {
                                    public string DealerName { get; set; }

                                    public override void makePayment()
                                    {
                                        LoanAmount -= 450;
                                    }
                                }
                            }
                        

Program.cs

                            using System;
                            namespace AutoLoan
                            {
                                class Program
                                {
                                    static void Main(string[] args)
                                    {
                                        BankLoan myLoan = new BankLoan();
                                        CarLoan myCarLoan = new CarLoan();

                                        double amount;
            
                                        do
                                        {
                                            Console.Write("Please enter a positive number for the loan amount: ");
                                            amount = Convert.ToDouble(Console.ReadLine());
                                        } while (amount < 0);

                                        myLoan.LoanAmount = amount;

                                        myLoan.makePayment();

                                        Console.WriteLine("After 1 Payment the bank loan is down to {0}", myLoan.LoanAmount.ToString("C"));

                                        myLoan.addInterest();

                                        Console.WriteLine("After 1 interest fee accumulation the loan is now {0}", myLoan.LoanAmount.ToString("C"));


                                        Console.Write("What is the name of the dealer for your car loan: ");
                                        myCarLoan.DealerName = Console.ReadLine();


                                        do
                                        {
                                            Console.Write("Please enter a positive number for the car loan amount: ");
                                            amount = Convert.ToDouble(Console.ReadLine());
                                        } while (amount < 0);

                                        myCarLoan.LoanAmount = amount;

                                        myCarLoan.makePayment();

                                        Console.WriteLine("After 1 Payment the car loan from {0} is down to {1}", myCarLoan.DealerName, myCarLoan.LoanAmount.ToString("C"));

                                        myCarLoan.addInterest();

                                        Console.WriteLine("After 1 interest fee accumulation the car loan from {0} is now {1}", myCarLoan.DealerName, myCarLoan.LoanAmount.ToString("C"));

                                        Console.ReadLine();
                                    }
                                }
                            }