/
borrow_library_panel2.k
474 lines (423 loc) · 13.9 KB
/
borrow_library_panel2.k
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
class MyPanel extends Panel {
public MyPanel() {
Panel();
frame_ = new Frame("Book Checkout");
frame_.add("Center", this);
frame_.resize(600, 400);
repaint();
setColor(Color.blue);
}
/*
public void handleEvent(Event event) {
int eventId = event.id.clone;
if (eventId == Event.MOUSE_DOWN) {
System.out.print("MOUSE_DOWN event: ")
} else if (eventId == Event.MOUSE_UP) {
System.out.print("MOUSE_UP event: ")
} else if (eventId == Event.MOUSE_DRAG) {
System.out.print("MOUSE_DRAG event: ")
} else if (eventId == Event.KEY_PRESS) {
System.out.print("KEY_PRESS event: \"");
String key = event.keyString.clone;
System.out.print(key).print("\" ")
} else {
System.out.print(eventId).print(" ")
}
System.out.print("handled event at (")
.print(event.x).print(", ")
.print(event.y).println(")")
}
*/
public Frame frame() {
return frame_
}
private Frame frame_
}
interface View {
public void notifyChanged();
}
class TextListView implements View {
int TextIndent = 5;
int TopMargin = 15;
int FrameWidth = 400;
int FrameHeight = 75;
int LineHeight = 14;
int TitleHeight = 20;
public TextListView(String title, MyPanel panel, int xOffset, int yOffset) {
items_ = new List<String>();
panel_ = panel;
xOffset_ = xOffset.clone;
yOffset_ = yOffset.clone;
frame_ = panel.frame();
title_ = title.clone;
reframe()
}
public void notifyChanged() {
int x = xOffset_ + TextIndent;
int y = yOffset_ + TopMargin + TitleHeight;
reframe();
int listCounter = 1;
for (String item : items_) {
String toPrint = listCounter.toString() + ". " + item;
panel_.setColor(Color.black);
panel_.drawString(x, y, toPrint);
y = y + LineHeight;
listCounter++
}
panel_.setColor(Color.blue);
}
public void removeAll() { items_ = new List<String>() }
public void addItem(String item) { items_.add(item) }
private void reframe() {
Color saveColor = panel_.getColor();
panel_.setColor(Color.white)
panel_.fillRect(xOffset_, yOffset_ + TitleHeight,
FrameWidth, FrameHeight)
panel_.setColor(saveColor)
// Text outline
panel_.drawString(xOffset_, yOffset_ + LineHeight, title_);
panel_.drawRect(xOffset_, yOffset_ + TitleHeight,
FrameWidth, FrameHeight);
}
private List<String> items_;
private MyPanel panel_;
private int xOffset_, yOffset_;
private Frame frame_;
private String title_
}
class TextLogView implements View {
int TextIndent = 5;
int TopMargin = 15;
int CharacterPixelWidth = 6;
int FrameWidth = 550, FrameHeight = 150;
int numberOfLinesInFrame = 9;
int LineHeight = 14;
public TextLogView(MyPanel panel, int xOffset, int yOffset) {
text_ = new List<ALine>();
panel_ = panel;
frame_ = panel.frame();
xOffset_ = xOffset.clone;
yOffset_ = yOffset.clone;
reframe()
}
private void reframe() {
Color saveColor = panel_.getColor();
panel_.setColor(Color.white)
panel_.fillRect(xOffset_, yOffset_, FrameWidth, FrameHeight)
panel_.setColor(saveColor)
// Text outline
panel_.drawRect(xOffset_, yOffset_, FrameWidth, FrameHeight)
}
class ALine {
public ALine(String text, Color color) {
text_ = text.clone; color_ = color.clone
}
public String text() { return text_ }
public Color color() { return color_ }
String text_; Color color_;
}
public void print(String text) {
text_.add(new ALine(text.clone, panel_.getColor()));
this.notifyChanged()
}
public void println(String text) {
this.print(text);
text_.add(new ALine("\n", Color.blue))
}
private int widthOf(String s) {
return (s.length() * CharacterPixelWidth)
}
public void notifyChanged() {
int y = yOffset_ + TopMargin, x = xOffset_ + TextIndent;
int currentLine = 1;
reframe();
int numberOfLinesInText = numberOfLinesIn(text_);
for (ALine aLine : text_) {
if (numberOfLinesInText - currentLine < numberOfLinesInFrame) {
if (aLine.text() == "\n") {
x = xOffset_ + TextIndent;
y = y + LineHeight
} else {
panel_.setColor(aLine.color());
panel_.drawString(x, y, aLine.text());
x = x + widthOf(aLine.text())
}
} else if (aLine.text() == "\n") {
currentLine++;
}
}
panel_.setColor(Color.blue);
panel_.repaint();
frame_.setVisible(true)
}
private int numberOfLinesIn(List<ALine> lines) {
int retval = 0;
for (ALine aLine : lines)
if (aLine.text() == "\n") retval++;
return retval
}
public void flush() {
this.notifyChanged()
}
private List<ALine> text_;
private MyPanel panel_
private Frame frame_;
private int xOffset_, yOffset_
}
interface ItemRecord {
public String name();
}
class Book implements ItemRecord {
public Book(String name) {
name_ = name.clone;
id_ = Math.random();
}
public double id() const { return id_ }
public String name() const { return name_ }
private String name_;
private double id_;
}
class MockBookScanner {
public MockBookScanner() {
scannedItems_ = new List<ItemRecord>();
// A list of books for testing purposes
books_ = new List<ItemRecord>();
books_.add(new Book("War and Peace"));
books_.add(new Book("Anna Karenina"));
books_.add(new Book("The Kingdom of God is Within You"));
books_.add(new Book("Master and Man"));
}
public ItemRecord scanItem() {
ItemRecord item;
if(Math.random() > 0.75) item = books_.get(0)
else if(Math.random() > 0.75) item = books_.get(1)
else if(Math.random() > 0.75) item = books_.get(2)
else item = books_.get(3);
System.out.println("*** Scanning '" + item.name() + "' ***");
return item;
}
public List<ItemRecord> scannedItems() { return scannedItems_ }
List<ItemRecord> scannedItems_;
List<ItemRecord> books_;
}
class MockCardReader {
public String scanCardPin() const { return "1234" }
}
class ScreenPrinter {
void println(String text) const {
System.out.println("| " + text);
}
void cutPaper() const {
System.out.println("--------------------------------------------------------");
}
}
/////////////////////////////////////////////////////////
context BorrowLibraryItems {
stageprop CardReader {
public void validateCardPin() {
int attempts = 0;
while (++attempts <= 3) {
Transcript.displayEnterPin();
if (Keypad.readPin() == scanCardPin()) break
else Transcript.displayInvalidPin();
}
if (attempts > 3)
Transcript.displayInvalidPinAttempts()
else
BookScanner.borrowCurrentLoanItem();
}
} requires {
String scanCardPin() const;
}
role BookScanner {
public void borrowCurrentLoanItem() {
ItemRecord item = scanItem();
if(scannedItems().contains(item))
Transcript.displayItemAlreadyScanned(item)
else {
scannedItems().add(item);
Accountant.displayScannedItems();
}
}
public List<ItemRecord> currentScannedItems() {
return scannedItems()
}
} requires {
ItemRecord scanItem();
List<ItemRecord> scannedItems();
}
role ColorHandler {
public void setForegroundColor(Color c) { setColor(c) }
public Color inputColor() { return new Color(50, 200, 5) }
} requires {
void setColor(Color c)
}
role TranscriptView {
public void addText(String text) { print(text) }
public void addTextColored(String text, Color c) {
ColorHandler.setForegroundColor(c);
print(text);
ColorHandler.setForegroundColor(Color.blue)
}
public void addTextLine(String text) { println(text) }
public void addTextLineColored(String text, Color c) {
ColorHandler.setForegroundColor(c);
println(text);
ColorHandler.setForegroundColor(Color.blue)
}
public void echoInvisibleChar() {
ColorHandler.setForegroundColor(ColorHandler.inputColor());
print("*")
ColorHandler.setForegroundColor(Color.blue);
}
public void closeLine() { println("") }
public void changed() { notifyChanged();/* CheckoutList.changed() */}
} requires {
void print(String text);
void println(String text);
void notifyChanged()
}
role Transcript {
public void displayEnterPin() {
TranscriptView.addText("Enter PIN: ");
TranscriptView.changed();
}
public void displayWelcomeMessage() {
TranscriptView.addTextLine(
"Welcome to the automatic library borrowing service!");
CardReader.validateCardPin();
}
public void addText(String s) {
TranscriptView.addText(s)
}
public void addTextLine(String s) {
TranscriptView.addTextLine(s)
}
public void addTextLineColored(String s, Color c) {
TranscriptView.addTextLineColored(s, c)
}
public void displayItemAlreadyScanned(ItemRecord item) {
TranscriptView.addTextLineColored(
"Item '"+item.name()+"' has already been scanned.", Color.red);
Menu.selectOptions();
}
public void displayInvalidPinAttempts() {
TranscriptView.addTextLineColored(
"Three invalid attempts, please remove your card.",
Color.red);
}
public void displayInvalidPin() {
TranscriptView.addTextLineColored("Invalid PIN code.", Color.red)
}
public void displayRemoveLibraryCard() {
TranscriptView.addTextLine("Don't forget your library card!");
displayThankyouMessage();
}
public void displayThankyouMessage() {
TranscriptView.addTextLine(
"Thank you for using our automatic borrowing service!");
}
}
role Accountant {
public void displayScannedItems() {
Transcript.addTextLine("Borrowed items:");
CheckoutList.reset();
for (ItemRecord item : BookScanner.currentScannedItems()) {
Transcript.addTextLineColored(item.name(), Color.magenta);
CheckoutList.nowIncludes(item.name())
}
CheckoutList.changed();
Menu.selectOptions();
}
}
stageprop Printer {
public void printReceiptOfScannedItems() {
cutPaper();
println("Borrowed items:");
for(ItemRecord item : BookScanner.currentScannedItems()) {
println(item.name());
}
cutPaper();
Transcript.displayRemoveLibraryCard();
}
} requires {
void println(String text) const;
void cutPaper() const;
}
role Keypad {
public String readChar() {
int a = read();
assert (a isnot null);
TranscriptView.changed();
CheckoutList.changed();
return a.to1CharString()
}
public String readPin() {
String retval = "";
for (int i = 0; i < 4; i++) {
String theChar = readChar();
retval = retval + theChar;
TranscriptView.echoInvisibleChar();
TranscriptView.changed();
}
TranscriptView.closeLine();
return retval
}
} requires {
int read();
}
role Menu {
// Not part of the use case, but required because of
// limitations in the enviroment.
public void selectOptions() {
Transcript.addText("");
Transcript.addTextLine("1. Borrow another item");
Transcript.addTextLine("2. Finish with a receipt");
Transcript.addTextLine("3. Finish without a receipt");
Transcript.addText("Select option: ");
String choice = Keypad.readChar();
Transcript.addTextLineColored(choice, ColorHandler.inputColor());
switch(choice) {
case "2":
Printer.printReceiptOfScannedItems();
break;
case "3":
Transcript.displayRemoveLibraryCard();
break;
case "1":
BookScanner.borrowCurrentLoanItem();
break;
default:
Transcript.addTextLine("Invalid option.");
Menu.selectOptions();
break;
}
CheckoutList.changed()
}
}
role CheckoutList {
public void nowIncludes(String book) { addItem(book) }
public void changed() { notifyChanged() }
public void reset() { removeAll() }
} requires {
void addItem(String x);
void notifyChanged();
void removeAll()
}
///////////////////////////////////////////////////////////
public BorrowLibraryItems() {
MyPanel panel = new MyPanel();
ColorHandler = panel;
CardReader = new MockCardReader();
BookScanner = new MockBookScanner();
Transcript = null;
TranscriptView = new TextLogView(panel, 20, 85);
CheckoutList = new TextListView("Checkout List", panel, 20, 245)
Printer = new ScreenPrinter();
Keypad = new InputStream(panel);
}
public void start() {
Transcript.displayWelcomeMessage();
}
}
new BorrowLibraryItems().start()