I have been trying to complete the following assignment for my college. So far, I have received help on this assignment a couple of times (which I really appreciate).
Since this is an assignment for college, I would appreciate non-direct answers that explain the concepts with examples that don't directly fix my assignment.
The assignment requires me to check for some things depending on user input.
If the user inputs an order, then inputs the same order with the same code again, it replaces the previous information with the new one. EG, if the user inputs G22 as code and 5 as quantity, then again enters G22 and then 4 as quantity, it forgets about the 5 and replaces that with 4. How can I make it so it remembers the previous one and simply adds the new order and makes it Quantity = 9 (I am using arrays for user input).
If the user enters G22 and quantity 3, but then doesn't enter anything and just hits enter, the counter adds one and upon pressing X (to quit) the shipping charges show up as $2.00 which is for 2 items. How can I avoid the counter from adding 1 in case nothing sensible is entered, i.e. anything else apart from the 4 options available is entered.
I understand that using doubles is not advisable for anything related to money and BigDecimal is recommended (alongwith NumberFormat, maybe). Can I replace my current doubles with BigDecimal with minimal problems, or do I have to replace the whole code? I also do not understand how to implement BigDecimal that easily so I would appreciate layman examples on that (especially on whether I can add/subtract/multiply/divide BigDecimal with ints etc).
I would post the code here but I don't want it copied by anyone else doing the same course and then submit it before I do (not that my code is the most awesome thing in the world, I just put in a lot of effort since its my first ever program).
1) I would use a Map<String, Integer>
to store all the quantities for a product code.
2) I would get the total of items ordered by summing the quantities in the map. How the user enters the data shouldn't matter.
3) This advice is project dependant. In your case the option of the marker is what matters. Working in investment banks and trading firms for many years, and I haven't seen anyone use BigDecimal for money. Its not a major rewrite, and at a minimum you should know how to use both double
and BigDecimal
handling rounding correctly.