Visual C# 6.17 (Calculating Sales)

This Solution is for the “Calculating Sales” of chapter 6 in the textbook; Visual c# How to program 6th edition by Deitel.

//Date: 5/11/2017

/*Description:
Reads user input then uses Switch statement to determine retail price of each product,
calculates and displays total retail value of all products sold.
*/
using System;

class Program
{
static void Main()
{
//initialises variables
int itemtotal = 0;//tracks item total
int itemcounter = 0;// counts amount of items
int i1Count = 0;//count of item 1
int i2Count = 0;// count of item 2
int i3Count = 0;// count of item 3
decimal item1 = 0;
decimal item2 = 0;
decimal item3 = 0;
string exit;

//prints ID list
Console.WriteLine(“Product IDs; \n Orange Juice: 1\n Cat Food: 2\n Bagels: 3”);

do // loops until user enters the end of file indicator <Ctrl> z
{

Console.WriteLine(“Enter the product ID for the product you wish to purchase.”);
string input = Console.ReadLine(); // read user input for each item

Console.WriteLine(“Enter the product quantity for the product ID entered:”);
int quant = int.Parse(Console.ReadLine());//reads quantity of each item

Console.WriteLine(“Press <enter> to continue or input <Ctrl> z to calculate total.”);
//reads input; if <enter> switches item ID for respective case then loops user prompts //again
//if<ctrl> z exits loop
exit = Console.ReadLine();

//reads input from memory changes input from string to int
//sets input to item
int item = int.Parse(input);
//tracks item total
itemtotal += item;
//increments itemcounter
++itemcounter;
switch (item)//switch statement for item ids
{
case 1://item ID is 1
item1 = 2.98M;//sets price for item
i1Count = quant + i1Count;

break;//exits switch

case 2:
item2 = 4.50M;
i2Count = quant + i2Count;

break;

case 3:
item3 = 9.98M;
i3Count = quant + i3Count;

break;

default://anything other than 1, 2, or 3 throws up error message
Console.WriteLine(“Invalid product ID, please enter product ID again.”);
break;

}

} while (exit != null);//exits loop

Console.WriteLine(“\nReciept:”);
if (itemcounter != 0)//if at least one item entered calculates totals
{

decimal purchasetotal = (i1Count * item1) + (i2Count * item2) + (i3Count * item3);//calculates purchase total
decimal itemtotal1 = i1Count * item1;//calculates item totals
decimal itemtotal2 = i2Count * item2;
decimal itemtotal3 = i3Count * item3;

//prints amount input of each item
Console.WriteLine($” {i1Count,3} of item 1: {itemtotal1,8:C}”);
Console.WriteLine($” {i2Count,3} of item 2: {itemtotal2,8:C}”);
Console.WriteLine($” {i3Count,3} of item 3: {itemtotal3,8:C}”);// and the total for that item

Console.WriteLine($”Total retail price for the {itemcounter} item(s) entered is {purchasetotal:C}.”);//prints purchase total
Console.WriteLine();
}
else// else prints error message
{
Console.WriteLine(“No product IDs were entered…”);
}
}
}

ProgramOutput
Product IDs;
Orange Juice: 1
Cat Food: 2
Bagels: 3
Enter the product ID for the product you wish to purchase.
2
Enter the product quantity for the product ID entered:
3
Press <enter> to continue or input <Ctrl> z to calculate total.

Enter the product ID for the product you wish to purchase.
1
Enter the product quantity for the product ID entered:
13
Press <enter> to continue or input <Ctrl> z to calculate total.

Enter the product ID for the product you wish to purchase.
3
Enter the product quantity for the product ID entered:
5
Press <enter> to continue or input <Ctrl> z to calculate total.

Enter the product ID for the product you wish to purchase.
23
Enter the product quantity for the product ID entered:
1
Press <enter> to continue or input <Ctrl> z to calculate total.

Invalid product ID, please enter product ID again.
Enter the product ID for the product you wish to purchase.
3
Enter the product quantity for the product ID entered:
2
Press <enter> to continue or input <Ctrl> z to calculate total.
^Z

Reciept:
13 of item 1: $38.74
3 of item 2: $13.50
7 of item 3: $69.86
Total retail price for the 5 item(s) entered is $122.10.

Press any key to continue . . .

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s