-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProduct.java
More file actions
54 lines (47 loc) · 1.53 KB
/
Product.java
File metadata and controls
54 lines (47 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Base class for all products the store will sell
public abstract class Product implements Comparable<Product> {
private double price;
private int stockQuantity;
private int soldQuantity;
public Product(double initPrice, int initQuantity) {
price = initPrice;
stockQuantity = initQuantity;
}
public int getStockQuantity() {
return stockQuantity;
}
public int getSoldQuantity() {
return soldQuantity;
}
public void setSoldQuantity(int sold) {
this.soldQuantity = sold;
}
public void setStockQuantity (int stock){
this.stockQuantity = stock;
}
public double getPrice() {
return price;
}
//Returns the total revenue (price * amount) if there are at least amount items in stock
//Return 0 otherwise (i.e., there is no sale completed)
public double sellUnits(int amount) {
if (amount > 0 && stockQuantity >= amount) {
stockQuantity -= amount;
soldQuantity += amount;
return price * amount;
}
return 0.0;
}
@Override
public int compareTo(Product p) {
if (p == null) {
throw new NullPointerException("null is not comparable");
}
else if (this != null & p != null && this.soldQuantity == p.soldQuantity) {
return 0;
} else if (this != null & p != null && this.soldQuantity < p.getSoldQuantity()) {
return -1;
}
return 1;
}
}