-
Notifications
You must be signed in to change notification settings - Fork 0
/
Terminal.java
123 lines (100 loc) · 3.88 KB
/
Terminal.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
package datenightatthearcade;
import java.util.*;
import java.lang.Exception;
public class Terminal {
ArrayList<ArrayList<Prize>> prizes = new ArrayList<ArrayList<Prize>>();
public void addToBalance(Card c, int b) throws Exception {
if (b < 0) {
throw new Exception("Amount to add must be greater than 0");
} else if (c.balance > (Integer.MAX_VALUE - b) - b) {
throw new Exception("New balance must be <= " + Integer.MAX_VALUE);
}
c.balance += 2 * b;
}
public void printCardBalance(Card c) {
System.out.println("Card number: " + c.cardNumber + "\nCredits: " + c.balance);
System.out.println("Ticket Balance: " + c.ticketBalance);
System.out.println("");
}
public void transferCardCredits(Card source, Card target, int amountToTransfer) throws Exception {
if (amountToTransfer > source.balance) {
throw new Exception("Insufficient credit to transfer!");
}
if (target.balance > (Integer.MAX_VALUE - amountToTransfer)) {
throw new Exception("Amount to recieve will overflow recievers balance!");
}
source.balance -= amountToTransfer;
target.balance += amountToTransfer;
printCardBalance(source);
printCardBalance(target);
}
public void transferCardTickets(Card source, Card target, int amountToTransfer) throws Exception {
if (amountToTransfer > source.ticketBalance) {
throw new Exception("Insufficient tickets to transfer!");
}
if (target.ticketBalance > (Integer.MAX_VALUE - amountToTransfer)) {
throw new Exception("Amount to recieve will overflow recievers ticketBalance!");
}
source.ticketBalance -= amountToTransfer;
target.ticketBalance += amountToTransfer;
printCardBalance(source);
printCardBalance(target);
}
public void displayPrizes() {
for (int i = 0; i < prizes.size(); i++) {
if (prizes.get(i).size() > 0) {
System.out.println(prizes.get(i).get(0).ToString() + "\nNumber of prize(s) :" + prizes.get(i).size());
System.out.println("");
}
}
}
public Prize getPrize(String name, Card playerCard) throws Exception {
for (int i = 0; i < prizes.size(); i++) {
if (prizes.get(i).size() > 0) {
if (prizes.get(i).get(0).name == name) {
Prize result = prizes.get(i).get(0);
if (result.numberOfTicketsRequired > playerCard.ticketBalance) {
throw new Exception("Insufficient funds!");
}
else{
prizes.get(i).remove(0);
playerCard.ticketBalance -= result.numberOfTicketsRequired;
System.out.println("Please enjoy your " + result.name + "!");
System.out.println("");
return result;
}
}
}
}
throw new Exception("No prize found with that name!");
}
public Terminal() {
ArrayList<Prize> list = new ArrayList();
for (int i = 0; i < 5; i++) {
try {
list.add(new Prize("teddy", 100));
} catch (Exception e) {
return;
}
}
prizes.add(list);
list = new ArrayList();
for (int i = 0; i < 5; i++) {
try {
list.add(new Prize("baseball", 120));
} catch (Exception e) {
return;
}
}
prizes.add(list);
list = new ArrayList();
for (int i = 0; i < 5; i++) {
try {
list.add(new Prize("cap gun", 120));
} catch (Exception e) {
return;
}
}
prizes.add(list);
}
}