unsplash-image-TrvLsDu4sHk.jpg

C# - Vehicle Depreciation

Vehicle Depreciation Calculator - C#

This program is for calculating the depreciation of a motor vehicle based on the users input.

This is accomplished with three classes. The class that houses the main method prompts the user for input and calls the methods from the other classes. The “Menu” class loops through the menu to be displayed and takes the users input. The “DepreciationTables” class contains the different types of depreciation and the equations for the proper calculations.

A video demonstrating the output and the code is shown below:

Source Code

Main Method
Menu
DepreciationTables
Exported from Notepad++
1 // Name: Mostapha Abdelaziz 2 // File: Program.cs 3 // Purpose: Calculate the depreciation of vehicles with user input 4 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 11 namespace CPSC1012_CorePortfolio2_mabdelaziz 12 { 13 class Program 14 { 15 static void Main(string[] args) 16 { 17 // declare variables 18 char menuSelection; 19 bool quitLoop = false; 20 double depreciationAmount = 0; 21 int depreciationYears = 0; 22 23 // display program purpose 24 Console.WriteLine("This program computes depreciation tables using various methods of depreciation."); 25 26 // start loop 27 while (quitLoop == false) 28 { 29 // display menu 30 Menu.DisplayMenu(); 31 32 // ask for and capture input 33 menuSelection = Menu.GetMenuChoice(); 34 35 // enact the corresponding decision 36 switch (menuSelection) 37 { 38 case 'A': 39 Console.WriteLine("How much money is to be depreciated?"); 40 depreciationAmount = double.Parse(Console.ReadLine()); 41 Console.WriteLine("Over how many years?"); 42 depreciationYears = int.Parse(Console.ReadLine()); 43 break; 44 case 'B': 45 DepreciationTables.StraightLineDepreciation(depreciationAmount, depreciationYears); 46 break; 47 case 'C': 48 DepreciationTables.SumOfYearsDigitsDepreciation(depreciationAmount, depreciationYears); 49 break; 50 case 'D': 51 DepreciationTables.DoubleDecliningBalanceDepreciation(depreciationAmount, depreciationYears); 52 break; 53 default: 54 quitLoop = true; 55 break; 56 } 57 58 } 59 60 // good bye message 61 Console.WriteLine("Good-bye."); 62 63 // keep the console window open 64 Console.ReadLine(); 65 } 66 } 67 } 68
Exported from Notepad++
1 // Name: Mostapha Abdelaziz 2 // File: Menu.cs 3 // Purpose: Display a menu and capture user input in relation to the menu 4 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 11 namespace CPSC1012_CorePortfolio2_mabdelaziz 12 { 13 class Menu 14 { 15 public static char GetMenuChoice() 16 { 17 // declare variables 18 char charValue; 19 bool inputIsValid = false; 20 string inputQuestion = "Option? "; 21 string errorMessage = "*** Invalid menu choice {0}."; 22 23 // loop to determine whether char is valid menu choice 24 do 25 { 26 // ask for and capture an char value 27 Console.Write(inputQuestion); 28 29 // loop to determine whether the input is a character 30 while (!char.TryParse(Console.ReadLine().Trim().ToUpper(), out charValue)) 31 { 32 Console.WriteLine(errorMessage, charValue); 33 Console.Write(inputQuestion); 34 } 35 if (!charValue.Equals('A') && !charValue.Equals('B') && !charValue.Equals('C') && !charValue.Equals('D') && !charValue.Equals('Q')) 36 { 37 Console.WriteLine(errorMessage, charValue); 38 } 39 else 40 { 41 inputIsValid = true; 42 } 43 } while (inputIsValid == false); 44 45 return charValue; 46 } 47 48 public static void DisplayMenu() 49 { 50 // declare variables 51 // set menu choices here to easily change if need be 52 string menuHeader = "Please enter:"; 53 string menuOptionOne = "A - to enter a new Amount and/or Number of Years,"; 54 string menuOptionTwo = "B - to use the straight-line method,"; 55 string menuOptionThree = "C - to use the sum-of-years-digits method,"; 56 string menuOptionFour = "D - to use the double-declining balance method,"; 57 string menuOptionFive = "Q - to quit."; 58 59 // place an empty line before 60 Console.WriteLine(""); 61 // display menu 62 Console.WriteLine("{0}", menuHeader); 63 Console.WriteLine(" {0}", menuOptionOne); 64 Console.WriteLine(" {0}", menuOptionTwo); 65 Console.WriteLine(" {0}", menuOptionThree); 66 Console.WriteLine(" {0}", menuOptionFour); 67 Console.WriteLine(" {0}", menuOptionFive); 68 // place an empty line afterwards 69 Console.WriteLine(""); 70 } 71 } 72 } 73
Exported from Notepad++
1 // Name: Mostapha Abdelaziz 2 // File: DepreciationTables.cs 3 // Purpose: Calculate and display the depreciation tables 4 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 11 namespace CPSC1012_CorePortfolio2_mabdelaziz 12 { 13 class DepreciationTables 14 { 15 public static void StraightLineDepreciation(double amount, int years) 16 { 17 // declare variables 18 int loopCounter = 1; 19 double depreciation; 20 21 // display table header 22 Console.WriteLine("{0,-4}{1,17}", "Year", "Depreciation"); 23 Console.WriteLine("{0,-4}{1,17}", "----", "----------------"); 24 25 // code the loop to fill and display the table 26 for (loopCounter = 1; loopCounter <= years; loopCounter++) 27 { 28 depreciation = amount / years; 29 Console.WriteLine("{0,4}{1,17:C2}", loopCounter, depreciation); 30 } 31 32 // place an empty line afterwards 33 Console.WriteLine(""); 34 } 35 36 public static void SumOfYearsDigitsDepreciation(double amount, int years) 37 { 38 // declare variables 39 int loopCounter = 1; 40 int sumOfYears; 41 double depreciation; 42 43 // calculate sum 44 sumOfYears = (years * (years + 1)) / 2; 45 46 // display table header 47 Console.WriteLine("{0,-4}{1,17}", "Year", "Depreciation"); 48 Console.WriteLine("{0,-4}{1,17}", "----", "----------------"); 49 50 // code the loop to fill and display the table 51 for (loopCounter = 1; loopCounter <= years; loopCounter++) 52 { 53 depreciation = (years - loopCounter + 1) * amount / sumOfYears; 54 Console.WriteLine("{0,4}{1,17:C2}", loopCounter, depreciation); 55 } 56 57 // place an empty line afterwards 58 Console.WriteLine(""); 59 } 60 61 public static void DoubleDecliningBalanceDepreciation(double amount, int years) 62 { 63 // declare variables 64 int loopCounter = 1; 65 double depreciation; 66 double revisedAmount = amount; 67 68 // display table header 69 Console.WriteLine("{0,-4}{1,17}", "Year", "Depreciation"); 70 Console.WriteLine("{0,-4}{1,17}", "----", "----------------"); 71 72 // code the loop to fill and display the table 73 for (loopCounter = 1; loopCounter <= years; loopCounter++) 74 { 75 depreciation = (2.0 / years) * revisedAmount; 76 revisedAmount = revisedAmount - depreciation; 77 Console.WriteLine("{0,4}{1,17:C2}", loopCounter, depreciation); 78 } 79 80 // place an empty line afterwards 81 Console.WriteLine(""); 82 } 83 } 84 } 85