CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
string answer;
float totalPrice;
Console.WriteLine("Would you like to calculate a tip?");
answer = Console.ReadLine();
if (answer == "yes")
{
Console.WriteLine("Please enter the total price of your meal");
totalPrice = float.Parse(Console.ReadLine());
if (totalPrice >= 20.00)
Console.WriteLine(TipCalculator.over20(totalPrice));
else if (totalPrice < 20)
Console.WriteLine(TipCalculator.under20(totalPrice));
else Console.WriteLine("Error. Please type in the value of the bill again");
}
else if (answer == "no")
Console.WriteLine("Please run this again when you wish to calculate a tip.");
else
Console.WriteLine("Error. Please type in \"yes\" or \"no\"");
Console.ReadLine();
}
}
public class TipCalculator
{
public static string over20(float bill)
{
float tip, totalBill;
tip = (bill * (float)1.2);
totalBill = (tip + bill);
return "Very good. The total tip to be paid is " + tip + " and this will cost " + totalBill + "in total.";
}
public static string under20(float bill)
{
float tip, totalBill;
tip = (bill * (float)1.1);
totalBill = (tip + bill);
return "Very good. The total tip to be paid is " + tip + " and this will cost " + totalBill + "in total.";
}
}