-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCost2.java
More file actions
53 lines (44 loc) · 1.03 KB
/
Cost2.java
File metadata and controls
53 lines (44 loc) · 1.03 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
package code;
public class Cost2 {
private String costName;
private int costID;
private static int numCost = 0;
private double cost;
public Cost2(String name,double cost) {
this.costName = name;
costID = ++numCost;
setCost(cost);
}
//setters
public void setCost(double cost) {
if(cost < 0) {
throw new IllegalArgumentException("Invalid cost.");
}
this.cost = cost;
}
public void setCostName(String name) {
this.costName = name;
}
//getters
public double getCost() {
return cost;
}
public int getCostID() {
return costID;
}
public String getCostName() {
return costName;
}
//static method to reset the costID so will reset when all cost are deleted
public static void resetCostCounter() {
numCost = 0;
}
/** @return Overriden toString() method */
@Override
public String toString() {
StringBuilder str = new StringBuilder("Cost ID: " + costID);
str.append(", Cost name: " + costName);
str.append(String.format(", Cost value: $%.2f",cost));
return str.toString();
}
}