forked from Apress/functional-interfaces-in-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicketingSystem.java
More file actions
187 lines (160 loc) · 5.21 KB
/
TicketingSystem.java
File metadata and controls
187 lines (160 loc) · 5.21 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package chapter7;
import java.util.ArrayList;
import java.util.Scanner;
import java.time.LocalDate;
import java.util.function.Supplier;
import java.util.function.BooleanSupplier;
abstract class Ticket implements Comparable<Ticket>
{
String customerName;
int id;
String description;
LocalDate dueDate;
LocalDate servicedDate;
public Ticket(String cn, int i, String d, int due)
{
customerName = cn;
id = i;
description = d;
dueDate = LocalDate.now().plusDays(due);
servicedDate = LocalDate.now().minusDays(1);
}
@Override
public String toString()
{
return "NAME: " + customerName
+ "\nID: " + id
+ "\nDESCRIPTION: " + description
+ "\nDUE DATE: " + dueDate
+ "\nSERVICED DATE: " + servicedDate;
}
@Override
public int compareTo(Ticket t)
{
return dueDate.compareTo(t.dueDate);
}
}
class HardwareTicket extends Ticket
{
String device;
String model;
String serialNumber;
public HardwareTicket(String cn, int i, String d, int due,
String dev, String mod, String sn)
{
super(cn, i, d, due);
device = dev;
model = mod;
serialNumber = sn;
}
@Override
public String toString()
{
return super.toString()
+ "\nDEVICE: " + device
+ "\nMODEL: " + model
+ "\nSERIAL NUMBER: " + serialNumber;
}
}
enum Domain {WEB_HOSTED, PHONE_HOSTED}
class SoftwareTicket extends Ticket
{
String application;
String version;
Domain domain;
public SoftwareTicket(String cn, int i, String d, int due,
String app, String ver, Domain dom)
{
super(cn, i, d, due);
application = app;
version = ver;
domain = dom;
}
@Override
public String toString()
{
return super.toString()
+ "\nAPPLICATION: " + application
+ "\nVERSION: " + version
+ "\nDOMAIN: " + domain;
}
}
public class TicketingSystem
{
private static ArrayList<Ticket> tickets = new ArrayList<>();
private static LocalDate today = LocalDate.now();
private static void populateDatabase()
{
tickets.add(new HardwareTicket("Kalpana Patel",54641,
"Cell phone won't power on",
5, "Smartie", "SM250",
"SN546497-S23"));
tickets.add(new SoftwareTicket("Chester Rodriguez",89034,
"MapApp can't find grandma's house",
2, "MapApp", "1.01",
Domain.PHONE_HOSTED));
tickets.add(new SoftwareTicket("Britney Delmonica",91472,
"Can't change banking info on website",
1, "awesomecheapcellphones.com",
"2.65", Domain.WEB_HOSTED));
tickets.add(new HardwareTicket("Kalpana Patel",54641,
"Cell phone's screen goes black",
7, "Rover", "RV100",
"SN456742-R31"));
}
public static void main(String[] args)
{
Supplier<? extends Ticket> nextTicket = () ->
{
Ticket next = null;
for (int i=0; i < tickets.size(); ++i)
{
Ticket t = tickets.get(i);
if (t.servicedDate.compareTo(today) < 0)
{
if (next == null || t.compareTo(next) < 0)
next = t;
}
}
if (next != null)
next.servicedDate = today;
return next;
};
BooleanSupplier canClose = () -> {
boolean result = false;
Scanner scan = new Scanner(System.in);
System.out.print("\nCan you close the ticket (Y or N)?");
if (scan.nextLine().charAt(0) == 'Y')
result = true;
return result;
};
BooleanSupplier isQuittingTime = () -> {
boolean result = false;
Scanner scan = new Scanner(System.in);
System.out.print("Is it quitting time (Y or N)?");
if (scan.nextLine().charAt(0) == 'Y')
result = true;
return result;
};
populateDatabase();
Ticket next;
boolean done = false;
do
{
next = nextTicket.get();
if (next != null)
{
System.out.println("\n" + next);
if (canClose.getAsBoolean())
tickets.remove(next);
if (isQuittingTime.getAsBoolean())
done = true;
}
} while(next != null && !done);
if (next == null)
System.out.println(
"\nCongrats, you get a $50 bonus today!");
else
System.out.println("\nSee you tomorrow");
}
}