I have a constructor based on the above variables and getters and setters and a toString method.
In my App class in main I have instantiated an ArrayList<Car> and added 3 cars to it
My question is and it might be so simple and stupid but it is currently beyond my comprehension.
But I want to just sum the totalPrice of my 3 cars using a method in my Car class and to call it in my App class within main.
That is it.
I do not think there is any need for me to send code as it should be an understandable scenario to our Java gurus out there but if need be I will provide the code.
Please show us what you have got already, and tell us who you think you would add all the prices. If that doesn't give you the answer, try thisJava™ Tutorials section. Look at any pats with “List” in their names, and the section about aggregate operations.
We believe people remember things much better if they work them out for themselves than if somebody gives them a solution on a plate.
I have only had a 2‑second look at your code, but it looks all right so far. Did you find anything useful from those Java™ Tutorials? If you had to calculate that total without a computer, how would you do it? Your program will probably use the same technique.
I have noticed something: make all the fields private.
Many thanks for pointing me in the right direction.
I moved my method into the App class and then did some further reading on Streams and Lambda expressions and came up with a solution that was further refined by suggestions from IntelliJ my IDE.
Stephen Potter wrote:// variables
What is the difference between the name and make in this context?
Please avoid writing such comments, which tell nothing extra for the reader of your code, but just clutter the code.
Why did you define those setName(), setMake() methods? Is car's make meant to change after the car is built (via constructor)? Remove all those and you'll see how your class will get more concise an simpler to cope with in your head, simply because it will have only what it has to have and nothing else.
In addition to Campbell's advice to make fields/variables private, make them final as well, that way you'll ensure they won't change their values over the time. That not only decreases errors, but also increases readability of the code, because knowing variable can't change its value, is much easier to read the code and concentrate on the things which may change their value.
We experienced people know that doubles are not suitable for exact calculations, and “exact calculations” includes anything to do with money. We also know that int arithmetic is suitable for exact calculations, but has a limited range because of the risk of overflow errors. We also know that the best datatype for calculating money is BigDecimal. The challenge is to change the price field to type BigDecimal and show us how you would do the arithmetic. It is easy enough to add ints with a Stream:- You can/should change the int to var. ...but you can't use sum() on a Stream<BigDecimal>. It is however possible to write that Stream code to add BigDecimals. Please show us how you totalled the prices, and see if you can work out how to total BigDecimals.
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad: