Usually in these situations you have to think like in real life. Imagine your problem as a real life situation and you will understand what to implement and what you need. For e.g.
You need to be able to work with Products. Which means it's alright to have a class Product. But will you ever be able to work directly with a product? Or will you be only working with derivatives of a product (Oranges, Apples, Bananas) ? This means using "abstract class".
Now that you use abstract class, you can never touch an actual product because you can not have an instance of it, but only an instance of a type-of-product. Now you ask yourself: Will I be needing a type of fruit which derives (extends) class Product, and each of them has it's own special characteristic? Or do I not care and consider that Apples and Oranges all are fruit, and differ only by name.
If you want to have an orange and have the ability to call orange.peel() , and have an apple and have the ability to call apple.rott(), then you should create diffrent classes for Apples, Oranges, etc. , each extending product, each having their own methods inside.
If not, you can just create a class Fruit, extending product, and each fruit differing from fruit.setName(String name)
Now, you want to be able to Order something. The Order class you created is ok, but you will never know how to differ from one fruit to another. So, instead of creating separate methods "orderOranges(number), orderBananas(number)", you create a generic method that accepts any Product
order.orderProducts (List<Product> products)
Where the method should look like this
public int orderProducts (List<Product> products) {
int price = 0;
for (Product prod : products) {
price += prod.getPrice();
}
return price;
}
This method is similar to taking your groceries to the check-out section.
Now, you can add anything you want. The quantity format, or whatever you want.
If you want to be able to access your products from anywhere in the program, you should (as in reality) create a static class Basket, which contains a list of products, where you can .add(Product), throwOut(Product), and you can send this basket to the check-out
order.checkOut(Basket basket);
Everything is similar to how reality works. I hope this is the guidance you wished for.
order
" This is wrong. You don't have a method calledorder
, you have a constructor calledOrder
. Please mind the difference of method and constructor and note the difference betweenorder
andOrder
(the uppercase "O" counts).