Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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());
// 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);
}
//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);
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 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;