-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagazine.java
More file actions
80 lines (67 loc) · 1.83 KB
/
Magazine.java
File metadata and controls
80 lines (67 loc) · 1.83 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
public class Magazine {
// instance variables
private String title;
private double cost;
private int numInStock;
// static varible
private static int numOfMagazine;
// Constructors
// makes the code flexible
Magazine() {
this.title = "";
this.cost = 0;
this.numInStock = 0;
numOfMagazine++;
}
Magazine(String title, double cost) {
this.title = title;
this.cost = cost;
numOfMagazine++;
}
Magazine(String title, double cost, int numInStock) {
this.title = title;
this.cost = cost;
this.numInStock = numInStock;
}
// Accessors
public String getTitle() {
return this.title;
}
public double getCost() {
return this.cost;
}
public int getNumInStco() {
return this.numInStock;
}
public static int getNumOfMagazine(){
return numOfMagazine;
}
// Mutator
public void setTitle(String title) {
if (!title.equals(""))
this.title = title;
else
throw new IllegalArgumentException("the magazine title can not be empty");
}
public void setCost(double cost) {
if (cost > 0)
this.cost = cost;
else
throw new IllegalArgumentException("the price can't be negative");
}
public void setNumInStock(int numInStock) {
if (numInStock > 0)
this.numInStock = numInStock;
else
throw new IllegalArgumentException("The Stock can't be negative");
}
public static void updateNumOfMagazine(){
numOfMagazine--;
}
// toString Method
public String toString() {
return "Title " + this.getTitle() +
"\n" + "Cost " + this.getCost() +
"\n" + "Stock " + this.getNumInStco();
}
}