'Project Rental System Management - problem with rent method
I'm creating a simple project which is used to manage library. I've a problem with a method rent to user. When user rents a book/magazine, the current quantity should reduce, but it's doesn't work. Where I did a mistake? Below is my code:
public class Library {
private final ArrayList<User> users = new ArrayList<>();
private final HashMap<Item, ItemDetails> items = new HashMap<>();
private boolean canBeRented(Item item) {
return items.get(item).getCurrentQuantity() > 0;
}
private boolean canRent(Item item, User user) {
return canBeRented(item) && user.canRent(item);
}
public boolean rentItemToUser(Item item, User user) {
if (canRent(item, user)) {
user.rent(item);
items.get(item).decreaseCurrentQuantity();
return true;
}
return false;
}
ItemDetails class:
public class ItemDetails {
private int totalQuantity;
private int currentQuantity;
public ItemDetails() {
this(1, 1);
}
private ItemDetails(int totalQuantity, int currentQuantity) {
this.totalQuantity = totalQuantity;
this.currentQuantity = currentQuantity;
}
public int getTotalQuantity() {
return totalQuantity;
}
public int getCurrentQuantity() {
return currentQuantity;
}
public void decreaseCurrentQuantity() {
this.currentQuantity = --currentQuantity;
}
void increaseQuantity() {
this.currentQuantity++;
this.totalQuantity++;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
