• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Create my own object that works like an array

 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic