-
Notifications
You must be signed in to change notification settings - Fork 0
/
Card.java
46 lines (37 loc) · 1.26 KB
/
Card.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
package datenightatthearcade;
import java.util.Random;
import java.util.Set;
import java.util.HashSet;
public class Card {
protected int balance;
int cardNumber;
int ticketBalance;
static Random rnd = new Random();
static Set<Integer> existingCardsNums = new HashSet<>();
static boolean cardExists(int cardNumber) {
return existingCardsNums.contains(cardNumber);
}
private void InitializeCard(int cardNumber){
balance = 0;
this.cardNumber = cardNumber;
ticketBalance = 0;
existingCardsNums.add(cardNumber);
}
public Card()throws Exception{
if(existingCardsNums.size() == 1 + (long)Integer.MAX_VALUE)
{
throw new Exception("All card numbers have been used!");
}
//this isn't the best way to this as if all card numbers but a few are in use then its going to loop for a long time!
cardNumber = rnd.nextInt();
while(cardExists(cardNumber)){
cardNumber = rnd.nextInt();
}
InitializeCard(cardNumber);
}
public Card(int u) throws Exception{
if(cardExists(u))
throw new Exception("card number already in use");
InitializeCard(cardNumber);
}
}