This assignment requires that you modify the given program T

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(\"\\t\\t\\tACME SUPER STORE\");
        Console.WriteLine(\"\\t\\t      ====================\ \");
        Console.WriteLine(\"\\t\\t\\tCustomer Receipt\ \ \");
        Console.WriteLine(\"\\t\\tExpensive Bread\\t\\t{0:C}\ \ \", breadCost);
        Console.WriteLine(\"\\t\\tExpensive Milk\\t\\t{0:C}\ \ \", milkCost);
        Console.WriteLine(\"\\t\\tExpensive Cheese\\t{0:C}\ \ \", cheeseCost);
        Console.WriteLine(\"\\t\\tSubtotal\\t\\t{0:C}\ \", subTotal);
        Console.WriteLine(\"\\t\\tSales Tax (6%)\\t\\t{0:C}\ \", tax);
        Console.WriteLine(\"\\t\\tTotal\\t\\t\\t{0:C}\ \ \", subTotal + tax);
    }

    public void displayWelcome()
    {
        Console.WriteLine(\"\\t\\t\\tACME SUPER STORE\");
        Console.WriteLine(\"\\t\\t      ====================\ \");
        Console.WriteLine(\"\\t\\t     10 Items Or Less Isle\ \");
        Console.WriteLine(\"\\t\\t   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();
    }
}

Solution

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(\"\\t\\t\\tACME SUPER STORE\");
Console.WriteLine(\"\\t\\t ====================\ \");
Console.WriteLine(\"\\t\\t\\tCustomer Receipt\ \ \");
Console.WriteLine(\"\\t\\tExpensive Bread\\t\\t{0:C}\ \ \", breadCost);
Console.WriteLine(\"\\t\\tExpensive Milk\\t\\t{0:C}\ \ \", milkCost);
Console.WriteLine(\"\\t\\tExpensive Cheese\\t{0:C}\ \ \", cheeseCost);
Console.WriteLine(\"\\t\\tSubtotal\\t\\t{0:C}\ \", subTotal);
Console.WriteLine(\"\\t\\tSales Tax (6%)\\t\\t{0:C}\ \", tax);
Console.WriteLine(\"\\t\\tTotal\\t\\t\\t{0:C}\ \ \", subTotal + tax);
}

public void displayWelcome()
{
Console.WriteLine(\"\\t\\t\\tACME SUPER STORE\");
Console.WriteLine(\"\\t\\t ====================\ \");
Console.WriteLine(\"\\t\\t 10 Items Or Less Isle\ \");
Console.WriteLine(\"\\t\\t 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.

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 it
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 it
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 it
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 it
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 it

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site