Louis Müller wrote:Here is the task I want to solve:
A mail-order house sells five products whose retail prices are as follows:
product 1, $2.98;
product 2, $4.50;
product 3, $9.98;
product 4, $4.49;
product 5, $6.87.
Write an application that reads a series of pairs of numbers as follows: <product number> <quantity sold>. Your program should use a switch statement to determine the retail price for each product. It should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to stop looping when reading 0 as product number and display the final results.
I haven't read through the entire
thread in detail but I can say this: A lot seems to have been said that didn't really apply to what is being asked for in original task description. Since the discussion afforded the OP a number of learning opportunities, I think it was still good in that way. However, this is still a prime example of over-engineering and gold-plating. We see this sort of thing happen all the time in the real world and it costs companies literally billions of dollars. Just keep that in mind as you go forward.
The task itself was very simple. Read in data until you find a sentinel value, then display totals. You are asked to use a switch statement to lookup the price of each product. To give you a point of comparison on what someone with a focus on getting things done would do, here's the code I would have written to just satisfy what is asked for in the problem. And even this solution goes a little bit above what is specifically asked.
Lines 4 to 27 make up a simple
test harness to exercise the solution which is the SalesReport class.
You can try this code out for yourself here:
https://repl.it/@jlacar/SalesReportWithTotals