Here is the code with comments
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TipCalculator { class Program { /* Get the amount of the bill Get the tip percent Get the tax calculate the tax calculte the tip Get the total Display the amount, the tax and the total */ //since every method uses this I set it at Class Scope double amount=0; static void Main(string[] args) { //initialize the class Program p = new Program(); //call get amount p.GetAmount(); //call calculate total which calls all the //other methods p.CalculateTotal(); p.PauseIt(); } void GetAmount() { //this method gets the amount Console.WriteLine("Enter the Amount"); amount = double.Parse(Console.ReadLine()); } double GetTipPercent() { //in this method we create a simple menu //to give the user a choice of tip options double tipPercent = 0; int choice = 0; Console.WriteLine("Choose your tip Percent"); Console.WriteLine("1 for 10 Percent"); Console.WriteLine("2 for 15 Percent"); Console.WriteLine("3 for 20 Percent"); Console.WriteLine("4 for other"); choice = int.Parse(Console.ReadLine()); //the switch analyses their choice and assigns //the appropriate tip switch (choice) { case 1: tipPercent = .1; break; case 2: tipPercent = .15; break; case 3: tipPercent = .2; break; case 4: //when choosing other we give them the //opportunity to enter their own tip amount Console.WriteLine("Enter your alternate tip amount"); tipPercent = double.Parse(Console.ReadLine()); //make sure it is in decimal form if (tipPercent > 1) { tipPercent =tipPercent/ 100; } break; default: Console.WriteLine("Not a valid choice."); break; } return tipPercent; } double GetTaxPercent() { //let the user enter the tax percent Console.WriteLine("Enter The tax Percent"); double taxPercent = double.Parse(Console.ReadLine()); if (taxPercent > 1) { taxPercent /= 100; } return taxPercent; } double CalculateTax() { //calculate the tax --calls //get Tax percent double tax=amount * GetTaxPercent(); return tax; } double CalculateTip() { //calculate tip calls GetTipPercent return amount * GetTipPercent(); } void CalculateTotal() { //CalculateTip calls GetTipPercent //CalculateTax call GetTaxPercent. //This method also calls Display, //so this is the only method besides //GetAmount that we need to call //From Main double tip = CalculateTip(); double tax = CalculateTax(); double total = amount + tax + tip; //call display and pass the values Display(tip, tax, total); } void Display(double tip, double tax, double total) { //display the results Console.WriteLine("Your amount is {0}", amount); Console.WriteLine("the tip is {0}", tip); Console.WriteLine("Your tax is {0}", tax); Console.WriteLine("the Total is {0}", total); } void PauseIt() { //pause the console Console.WriteLine("Press any key to exit"); Console.ReadKey(); } } }
Here is the diagram we drew of how the methods execute
No comments:
Post a Comment