This week's book giveaway is in the Go forum.
We're giving away four copies of Head First Go and have Jay McGavren on-line!
See this thread for details.
Win a copy of Head First Go this week in the Go forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Create my own object that works like an array  RSS feed

 
Sheriff
Posts: 13183
219
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!