This assignment requires that you modify the given program. The program uses a d
ID: 3926397 • Letter: T
Question
This assignment requires that you modify the given program. The program uses a do-while repetition statement and if-else if selection statement to accomplish its task. You are required to modify it by replacing the do-while with a for repetition statement, and also replace the if-else if with a switch selection statement. Your modified program must function in the same way as the original program.
Modifications should only be made to code that is in the Main method.
Editable Code:
using System;
public class Basket
{
private int breadCount;
private int milkCount;
private int cheeseCount;
public int getBreadCount()
{
return breadCount;
}
public void setBreadCount(int r)
{
breadCount = r;
}
public int getMilkCount()
{
return milkCount;
}
public void setMilkCount(int r)
{
milkCount = r;
}
public int getCheeseCount()
{
return cheeseCount;
}
public void setCheeseCount(int r)
{
cheeseCount = r;
}
public void displayBill()
{
double breadCost = getBreadCount() * 12.95;
double milkCost = getMilkCount() * 6.95;
double cheeseCost = getCheeseCount() * 24.95;
double subTotal = breadCost + milkCost + cheeseCost;
double tax = subTotal * 6 / 100;
Console.WriteLine(" ");
Console.WriteLine(" ACME SUPER STORE");
Console.WriteLine(" ==================== ");
Console.WriteLine(" Customer Receipt ");
Console.WriteLine(" Expensive Bread {0:C} ", breadCost);
Console.WriteLine(" Expensive Milk {0:C} ", milkCost);
Console.WriteLine(" Expensive Cheese {0:C} ", cheeseCost);
Console.WriteLine(" Subtotal {0:C} ", subTotal);
Console.WriteLine(" Sales Tax (6%) {0:C} ", tax);
Console.WriteLine(" Total {0:C} ", subTotal + tax);
}
public void displayWelcome()
{
Console.WriteLine(" ACME SUPER STORE");
Console.WriteLine(" ==================== ");
Console.WriteLine(" 10 Items Or Less Isle ");
Console.WriteLine(" We are strict about that...! ");
}
}
public class TestBasket
{
public static void Main(string[] args)
{
Basket myBasket = new Basket();
int input = 0;
int MAX_PRODUCTS = 10;
int productCount = 0;
myBasket.displayWelcome(); //display welcome message
do
{
Console.Write(" Product No. {0} ", productCount + 1);
Console.Write("Enter product code 1, 2, 3 or -1 to quit: ");
input = Convert.ToInt32(Console.ReadLine());
if (input == 1)
{
myBasket.setBreadCount(myBasket.getBreadCount() + 1); //update basket
productCount++; //increase product count
}
else if (input == 2)
{
myBasket.setMilkCount(myBasket.getMilkCount() + 1); //update basket
productCount++; //increase product count
}
else if (input == 3)
{
myBasket.setCheeseCount(myBasket.getCheeseCount() + 1); //update basket
productCount++; //increase product count
}
else if (input == -1) //user decides to quit
{
productCount = MAX_PRODUCTS + 1; //increase number of products so that loop terminates
Console.WriteLine("displaying total...");
}
else
Console.WriteLine("Invalid product number");
} while (productCount < MAX_PRODUCTS); //loop while number of products is less than max
myBasket.displayBill(); //display bill
Console.ReadKey();
}
}
Explanation / Answer
Here is the modified code for you:
using System;
public class Basket
{
private int breadCount;
private int milkCount;
private int cheeseCount;
public int getBreadCount()
{
return breadCount;
}
public void setBreadCount(int r)
{
breadCount = r;
}
public int getMilkCount()
{
return milkCount;
}
public void setMilkCount(int r)
{
milkCount = r;
}
public int getCheeseCount()
{
return cheeseCount;
}
public void setCheeseCount(int r)
{
cheeseCount = r;
}
public void displayBill()
{
double breadCost = getBreadCount() * 12.95;
double milkCost = getMilkCount() * 6.95;
double cheeseCost = getCheeseCount() * 24.95;
double subTotal = breadCost + milkCost + cheeseCost;
double tax = subTotal * 6 / 100;
Console.WriteLine(" ");
Console.WriteLine(" ACME SUPER STORE");
Console.WriteLine(" ==================== ");
Console.WriteLine(" Customer Receipt ");
Console.WriteLine(" Expensive Bread {0:C} ", breadCost);
Console.WriteLine(" Expensive Milk {0:C} ", milkCost);
Console.WriteLine(" Expensive Cheese {0:C} ", cheeseCost);
Console.WriteLine(" Subtotal {0:C} ", subTotal);
Console.WriteLine(" Sales Tax (6%) {0:C} ", tax);
Console.WriteLine(" Total {0:C} ", subTotal + tax);
}
public void displayWelcome()
{
Console.WriteLine(" ACME SUPER STORE");
Console.WriteLine(" ==================== ");
Console.WriteLine(" 10 Items Or Less Isle ");
Console.WriteLine(" We are strict about that...! ");
}
}
public class TestBasket
{
public static void Main(string[] args)
{
Basket myBasket = new Basket();
int input = 0;
int MAX_PRODUCTS = 10;
//int productCount = 0;
myBasket.displayWelcome(); //display welcome message
//do
for(int productCount = 0; productCount < MAX_PRODUCTS;)
{
Console.Write(" Product No. {0} ", productCount + 1);
Console.Write("Enter product code 1, 2, 3 or -1 to quit: ");
input = Convert.ToInt32(Console.ReadLine());
switch(input)
{
//if (input == 1)
case 1:
//{
myBasket.setBreadCount(myBasket.getBreadCount() + 1); //update basket
productCount++; //increase product count
//}
break;
//else if (input == 2)
case 2:
//{
myBasket.setMilkCount(myBasket.getMilkCount() + 1); //update basket
productCount++; //increase product count
//}
break;
//else if (input == 3)
case 3:
//{
myBasket.setCheeseCount(myBasket.getCheeseCount() + 1); //update basket
productCount++; //increase product count
//}
break;
//else if (input == -1) //user decides to quit
case -1:
//{
productCount = MAX_PRODUCTS + 1; //increase number of products so that loop terminates
Console.WriteLine("displaying total...");
//}
break;
//else
default:
Console.WriteLine("Invalid product number");
}
} //while (productCount < MAX_PRODUCTS); //loop while number of products is less than max
myBasket.displayBill(); //display bill
Console.ReadKey();
}
}
If you still need any refinements, just get back to me.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.