Reputation: 63
In my problem, I am trying to output a Sales Summary for an employees commission on sales. One of the columns needed is "Sold" which requires you to get the amount of products sold for that specific product. The problem I am having is that numberSold variable is initialized in the While loop and it looks like I am not able to use it because of that. What is the best option for being able to be able to use it. Any other tips on how to make my CommEarnings cleaner would also be appreciated as I feel like there is a better way to do it compared to how I am getting it.
decimal grossSales = 0; // total gross sales
decimal earnings; // earnings made from sales
int product = 0; // the product number
int numberSold; // number sold of a given product
decimal commission1 = .09M;
decimal commission2 = .0925M;
decimal commission3 = .0701M;
decimal commission4 = .095M;
decimal commission5 = .10M;
decimal commission6 = .083M;
decimal basePay = 250.00M;
decimal product1CommEarnings = 0.00M;
decimal product2CommEarnings = 0.00M;
decimal product3CommEarnings = 0.00M;
decimal product4CommEarnings = 0.00M;
decimal product5CommEarnings = 0.00M;
decimal product6CommEarnings = 0.00M;
while ( product < 6 )
{
++product;
// prompt for and read number of the product sold from user
Console.Write( "Enter number sold of product # {0} -> ",
product );
numberSold = Convert.ToInt32( Console.ReadLine() );
// determine gross of each individual product and add to total
if (product == 1)
{
grossSales += numberSold * 239.99M;
product1CommEarnings = Math.Round(commission1 * numberSold * 239.99M, 2);
}
else if (product == 2)
{
grossSales += numberSold * 129.75M;
product2CommEarnings = Math.Round(commission2 * numberSold * 129.75M, 2);
}
else if (product == 3)
{
grossSales += numberSold * 99.95M;
product3CommEarnings = Math.Round(commission3 * numberSold * 99.95M, 2);
}
else if (product == 4)
{
grossSales += numberSold * 350.89M;
product4CommEarnings = Math.Round(commission4 * numberSold * 350.89M, 2);
}
else if (product == 5)
{
grossSales += numberSold * 100.00M;
product5CommEarnings = Math.Round(commission5 * numberSold * 100.00M, 2);
}
else if (product == 6)
{
grossSales += numberSold * 1000.00M;
product6CommEarnings = Math.Round(commission6 * numberSold * 1000.00M, 2);
}
} // end while loop
Console.WriteLine("\n");
Console.WriteLine(" Commission Price Comm");
Console.WriteLine(" Sales Summary Earnings Sold Each Extentsion Rate");
Console.WriteLine(" ------------- ---------- ---- ----- --------- ----");
Console.WriteLine($" Base Pay {basePay:C} ");
Console.WriteLine($" Product 1 {product1CommEarnings} ");
Console.WriteLine($" Product 2 {product2CommEarnings} ");
Console.WriteLine($" Product 3 {product3CommEarnings} ");
Console.WriteLine($" Product 4 {product4CommEarnings} ");
Console.WriteLine($" Product 5 {product5CommEarnings} ");
Console.WriteLine($" Product 6 {product6CommEarnings} ");
Upvotes: 0
Views: 90
Reputation: 652
You should initialize the numberSold.
int numberSold = 0; //initial value will be discarded by the loop.
But you are still not going to get the numberSold for each item as you are rewriting it in the loop and the value being printed will be the value from the last iteration of the loop.
Use a record class or a tuple to save the values from each iteration.
Upvotes: 2