User can make a order of a certain quantity of bottles of the same wine. Class Shop
holds a list of different wines that it has. How can I make an order method that, instead of remove the object by the wines list in Shop class, remove the "quantity"? I was thinking about creating a new class (Inventory
for example) that has Wine
and quantity and managing the decrement from there. Hope you can help me! Thanks
public class Shop {
private ArrayList<Wine> wines;
private ArrayList<User> users;
private ArrayList<Employee> employees;
private ArrayList<Order> orders;
public Shop(ArrayList<Wine> wines, ArrayList<User> users, ArrayList<Employee> employees, ArrayList<Order> orders) {
this.wines = wines;
this.users = users;
this.employees = employees;
this.orders = orders;
}
}
public class Wine {
private String name;
private String productor;
private Integer year;
private String notes;
private String vine;
public Wine(String name, String productor, Integer year, String notes, String vine) {
this.name = name;
this.productor = productor;
this.year = year;
this.notes = notes;
this.vine = vine;
}
}
public class Order {
private User user;
private Wine wine;
private int quantity;
public Order(User user, Wine wine, int quantity) {
this.user = user;
this.wine = wine;
this.quantity = quantity;
}
}
I tried to use Hashmap<Wine, Integer>
instead of ArrayList<Wine>
in Shop
class and I've created this orderWine
method.
public void orderWine(Shop s, Wine w, Integer q){
if(this.login(s)){ //authenticated access
HashMap<Wine, Integer> wines = new HashMap<>(s.getWines());
ArrayList<Order> orders = new ArrayList<>(s.getOrders());
orders.add(new Order(this, w, q));
s.setOrders(orders);
if(s.getWines().containsKey(w)){
if(s.getWines().get(w) > q){
int upQuant = wines.get(w) - q;
wines.put(w,upQuant);
}
}
}
}