Sei sulla pagina 1di 6

using System;

using System.Collections.Generic;
public class Program
{
public const double dblSaleTax = 0.1;
public const double dblImportTax = 0.05;
public static void Main()
{
double dblTotalTax = 0;
double dblTotalPrice = 0;
// Create a list of items.
List<Item> Order = new List<Item>();
//begin order
Console.WriteLine("Please enter the letter for the item you woul
d like to buy");
Console.WriteLine("(b-Book, c-Choocolate, m-Music CD, p-Perfume,
h-Headache Pills)");
Console.WriteLine("or press e to exit:");
//Validate the item Name input
// Assign the full name
string strName = validateName(Console.ReadLine().ToLower());

while( strName != "exit"){


//Get the item price.
//Validate the item price Input
//Convert Price input to double
Console.WriteLine("Please enter the prics: (##.##)");
double dblPrice = validatePrice(Console.ReadLine().ToLow
er());
//Get the item amount
//Validate the amount input
//convert the amount input to integer
Console.WriteLine("Please enter the amount: (#)");
int intAmount = validateAmount(Console.ReadLine().ToLower())
;

//Get if imported item


//Validate the imported input
Console.WriteLine("Would you like an imported product? (y/n)"
);
bool bolImported = validateImported(Console.ReadLine().ToLowe
r());

// Tax Calculator
double dblTax = CalcTax(strName,dblPrice,bolImported);
// create an Item obj and add to list
Item ItemOrdered = new Item();
ItemOrdered.Name = strName;
ItemOrdered.Price = (intAmount * dblPrice);
ItemOrdered.Amount = intAmount;
ItemOrdered.Imported = bolImported;
ItemOrdered.SaleTax = (intAmount * dblTax);
Order.Add(ItemOrdered);

//-------------- clean and begin new item ------------strName = "";


dblPrice = 0;
intAmount = 0;
bolImported = false;
Console.WriteLine("To add an item to the order please enter th
e letter for the item");
Console.WriteLine("(b-Book, c-Choocolate, m-Music CD, p-Perfum
e , h-Headache Pills)");
Console.WriteLine("or press e to exit order:");
//Validate the Name
strName = validateName(Console.ReadLine().ToLower());

}
//output the order items lines
foreach (Item itemordered in Order){
outputOrderLine(itemordered.Name,itemordered.Price,itemo
rdered.Amount,itemordered.Imported, itemordered.SaleTax);
dblTotalTax += itemordered.SaleTax;
dblTotalPrice += (itemordered.Price + itemordered.SaleTa
x);
}
//output the order totals
outputSaleTaxLine (dblTotalTax, dblTotalPrice);

// Function to validate the name input. If valid (a or c or m or p or e)


static string validateName(string name){
while (name != "b" && name != "c" && name != "m" && name != "
p" && name != "e" && name != "h"){
Console.WriteLine("Invalid Input");
Console.WriteLine("Please enter the letter of the item");
Console.WriteLine("(b-Book, c-Choocolate, m-Music CD, p-Perfum
e, h-Headache Pills)");
Console.WriteLine("or press e to exit:");
name = Console.ReadLine().ToLower();
}
if (name == "e"){
name = "exit";
}

return name;
}

// Function to validate the price input. If price valid (number and grea
ter than 0 ) convert to double.
static double validatePrice (string price){
double dbprice;
while (double.TryParse(price, out dbprice) != true || dbprice <=
0){
Console.WriteLine("Invalid Input");
Console.WriteLine("Please enter the prics: (##.##)");
price = Console.ReadLine().ToLower();
}
return Math.Round(dbprice,2);
}

// Function to validate the amount input. If valid (number and greater t


han 0 ) convert to integer.
static int validateAmount (string amount){
int intamount;
while (int.TryParse(amount, out intamount) != true || intamount
<= 0){
Console.WriteLine("Invalid Input");
Console.WriteLine("Please enter the amount of you would
like to order: (#)");
amount = Console.ReadLine().ToLower();
}
return intamount;

}
//Function to validate the import input. if valid ( y or n) convert to b
oolean.
static bool validateImported (string imported){
while (imported != "y" && imported != "n"){
Console.WriteLine("Invalid Input");
Console.WriteLine("Would you like an imported product? (y/n)
" );
imported = Console.ReadLine().ToLower();
}
if (imported == "y"){
return true;
}
else{
return false;
}
}
//Function to calculate the item tax
static double CalcTax(string name, double price, bool imported){
double dblTax;
if (name == "m" || name == "p" ){
dblTax = Math.Round(price * dblSaleTax, 2);
}else {
dblTax = 0;
}
if (imported == true){
dblTax += Math.Round(price * dblImportTax, 2);
}else {
dblTax += 0;
}
return dblTax;
}
//Function to build output the item ordered line
static void outputOrderLine(string name,double price ,int amount, bool i
mported, double tax){

double dblSumPrice;
string strOutputName;
if (!imported){
if (name == "b"){
strOutputName = "book";
}else if (name == "c"){
strOutputName = "chocolate bar";
}else if (name == "m"){
strOutputName = "music CD";
}else if (name == "p"){
strOutputName = "bottle of perfume";
}else if (name == "h"){
strOutputName = "packet of headache pills";
}else {
strOutputName = "";
}
}else {

}else
}else
}else
}else

if (name == "b"){
strOutputName = "imported
if (name == "c"){
strOutputName = "imported
if (name == "m"){
strOutputName = "imported
if (name == "p"){
strOutputName = "imported
if (name == "h"){
strOutputName = "imported
}else {
strOutputName = "";
}

book";
box of chocolates";
music CD";
bottle of perfume";
packet of headache pills";

}
dblSumPrice = price + tax;

Console.WriteLine("{0} {1}: {2}",amount , strOutputName , dblSum


Price);
}
//Function to build the output for the total (tax and price) li
nes
static void outputSaleTaxLine(double TotalTax, double TotalPrice){
Console.WriteLine("Sales Taxes: {0}", TotalTax);
Console.WriteLine("Total: {0}", TotalPrice);

public class Item


{
public string Name {get;set;}
public double Price {get;set;}
public int Amount{get;set;}
public bool Imported {get;set;}
public double SaleTax {get;set;}
}

Potrebbero piacerti anche