-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBill.java
149 lines (134 loc) · 4.55 KB
/
Bill.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Student name: Ross Petridis
* Student ID: 1080249
* LMS username: rpetridis
*/
/**
* Bill class for facilitated actions and method surrounding a 'bill'
* in the competition. Keeps track of bill uses and member associated with
* bill.
* In addition to being their own entity holding important data about themselves
* and their ability to affect entries in competions and thus competitions,
* bills also acts as a link to members via the memberID and game data.
*
* @author Ross Petridis
*/
public class Bill {
private String billId;
private String memberId;
private float totalBillAmount;
private boolean used;
private static final int BILL_ID_LENGTH = 6;
/**
* Standard constructor used for reading bill entry from file
*
* @param billID This bills ID
* @param memberId The member ID used/owner of bill.
* @param totalBillAmount money spent on this bill
* @param used true iff this bill has been used in a competition
*/
public Bill(String billID, String memberId, float totalBillAmount, boolean used) {
this.billId = billID;
this.memberId = memberId;
this.totalBillAmount = totalBillAmount;
this.used = used;
}
/**
* Safe copy constuctor
*
* @param bill The bill to safely duplicate
*/
public Bill(Bill bill) {
this.billId = bill.billId;
this.memberId = bill.memberId;
this.totalBillAmount = bill.totalBillAmount;
this.used = bill.used;
}
/**
* Calculates the number of competition entrys this bill is
* eligible for given the total cost and price per entry.
*
* @return the number of entries this bill is eligible for
*/
public int getNumEntries() {
int numEntries = 0;
float billAmount = totalBillAmount;
while (billAmount >= Competition.COST_PER_ENTRY) {
numEntries += 1;
billAmount -= Competition.COST_PER_ENTRY;
}
return numEntries;
}
/**
* For printing bill data to output more appropriately.
*/
@Override
public String toString() {
return ("BillID: " + billId + ", memberID: " + memberId + ", totalBillAmount: " + totalBillAmount +
", used: " + used);
}
/**
* This method determines whether or not a string is of valid bill ID form.
* The string must match a 6 digit number format.
*
* This method is static as it is called in higher level logic of the
* SimpleCompetitions program when a bill is in the process of being
* instatiated. Ultimately, using public static enables simpler class structure and
* information flow only up and down the class hierarchy and not across the
* class hiearchy, this is explained further below.
*
* There are many checks for when a bill is being entered which are dependant on
* other objects, such as the data class. I did not want the data class and bill
* class to be directly interacting, but rather, interacting through a
* hiearchical structure through a competition which has both a bill and data
* object.
* Hence, this method had to be public to be availabe, and static as it is not
* for a particular bill and infact is used before a bill object has been
* instatiated.
*
* @param billId
* @return true iff a valid bill ID is recieved.
*/
public static boolean validBillID(String billId) {
if (billId.length() != BILL_ID_LENGTH || !billId.matches(SimpleCompetitions.DIGITS_ONLY_REGEX)) {
System.out.println("Invalid bill id! It must be a "+BILL_ID_LENGTH+"-digit number. Please try again.");
return false;
}
return true;
}
/**
*
* @return iff this bill has a member associated with it and can
* therefor be entered into a competition
*/
public boolean hasMember() {
return (!memberId.equals(""));
}
/**
*
* @param Id the id to compare with this.id
* @return true iff this bill has id equal to inputted id.
*/
public boolean hasId(String Id) {
return (billId.equals(Id));
}
/**
*
* @return iff bill has been used in a competition.
*/
public boolean hasBeenUsed() {
return used;
}
public String getMemberId() {
return memberId;
}
public void useBill() {
used = true;
}
public float getTotalAmount() {
return totalBillAmount;
}
public String getId() {
return billId;
}
}