-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerySimpleShop.pas
502 lines (444 loc) · 13.5 KB
/
VerySimpleShop.pas
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
program VerySimpleShop;
// Importing the necessary libraries.
uses
Crt;
type
Button = (UP, DOWN, LEFT, RIGHT, ESC, ENTER, UNKNOWN);
ProgramPhase = (WAITING, SHOPPING, PAYING, ENDING, EXITING);
// Product Object.
TSimpleProduct = class
private
fName: string;
fPrice: integer;
fCount: integer;
public
// TSimpleProduct constructor.
constructor Create(Name: string; Price: integer);
begin
fName := if Name = nil then 'NULL' else Name;
fPrice := if Price < 1 then 1 else Price;
fCount := 0;
end;
// If we want to change fields without methods (just small note).
property Price: integer read fPrice;
// Methods.
function ToString: string; override;
procedure WriteToFile(var chequeFile: TextFile);
procedure ResetCount;
function AddCount: Boolean;
function RemCount: Boolean;
end;
TTransaction = record
cardNumber: Integer;
isSuccess: Boolean;
end;
const
TITLE: String = 'VerySimpleShop | ';
INPUT_CHAR: Char = '_';
HIDDEN_INPUT_CHAR: Char = '*';
OFFSET: Integer = 7;
CURSOR_CHAR: Char = '>';
var
currentPhase: ProgramPhase;
products: array of TSimpleProduct;
productsCount: Integer;
currentPosition: integer := 0;
totalPrice: integer := 0;
// A method for displaying product information in console.
function TSimpleProduct.ToString: string;
begin
Result := (if fCount > 0 then '[x]' else '[ ]') + ($' "{fName}" - {fPrice}$ x {fCount} pcs. = {fPrice * fCount}$') + StringOfChar(' ', 10);
end;
// Write product to cheque.txt.
procedure TSimpleProduct.WriteToFile(var chequeFile: TextFile);
begin
if fCount > 0 then
begin
WriteLn(chequeFile, $' "{fName}" - {fPrice}$ x {fCount} pcs. = {fPrice * fCount}$');
end;
end;
// Reset the amount of product.
procedure TSimpleProduct.ResetCount();
begin
fCount := 0;
end;
// Increase the amount of product.
function TSimpleProduct.AddCount(): Boolean;
begin
Result := false;
if fCount < 16 then
begin
fCount += 1;
Result := true;
end;
end;
// Reduce the amount of product.
function TSimpleProduct.RemCount(): Boolean;
begin
Result := false;
if fCount > 0 then
begin
fCount -= 1;
Result := true;
end;
end;
//
// UTILS
//
// Get pressed button
function ReadInput(): Button;
var
ch: Char;
begin
ch := ReadKey();
case ch of
#0 : begin
ch := ReadKey();
case ch of
#37 : Result := Button.LEFT;
#38 : Result := Button.UP;
#39 : Result := Button.RIGHT;
#40 : Result := Button.DOWN;
end;
end;
#13 : Result := Button.ENTER;
#27 : Result := Button.ESC;
else Result := Button.UNKNOWN;
end;
end;
// Add char to input and print it.
procedure AddToInput(inputChar:Char; var currentInput:String; var currentLength:Integer; hideInput:Boolean);
begin
Write(if hideInput then HIDDEN_INPUT_CHAR else inputChar);
currentLength += 1;
currentInput := currentInput + inputChar;
end;
// Get input integer.
function ReadIntInput(inputLength:Integer; inputX:Integer; inputY:Integer; hideInput:Boolean): Integer;
var
ch: Char;
currentLength: Integer := 0;
currentInput: String := '';
begin
Crt.GotoXY(inputX, inputY);
Write(StringOfChar(INPUT_CHAR, inputLength));
Crt.GotoXY(inputX, inputY);
while currentLength <> inputLength do
begin
ch := ReadKey();
case ch of
#48 : AddToInput('0', currentInput, currentLength, hideInput);
#49 : AddToInput('1', currentInput, currentLength, hideInput);
#50 : AddToInput('2', currentInput, currentLength, hideInput);
#51 : AddToInput('3', currentInput, currentLength, hideInput);
#52 : AddToInput('4', currentInput, currentLength, hideInput);
#53 : AddToInput('5', currentInput, currentLength, hideInput);
#54 : AddToInput('6', currentInput, currentLength, hideInput);
#55 : AddToInput('7', currentInput, currentLength, hideInput);
#56 : AddToInput('8', currentInput, currentLength, hideInput);
#57 : AddToInput('9', currentInput, currentLength, hideInput);
#8 : begin
if currentLength > 0 then
begin
currentInput := Copy(currentInput, 1, currentLength - 1);
inputX := Crt.WhereX() - 1;
Crt.GotoXY(inputX, inputY);
Write(INPUT_CHAR);
Crt.GotoXY(inputX, inputY);
currentLength -= 1;
end;
end;
end;
end;
try
Result := StrToInt(currentInput);
except
// Just in case.
ErrorScreen('You input an illegal symbol. How?');
Result := 0;
end;
end;
// Clear the selection of products.
procedure ResetProductsCount();
begin
currentPosition := 0;
totalPrice := 0;
for i:Integer := 0 to High(products) do
begin
Products[i].ResetCount();
end;
end;
// Create cheque.txt.
procedure CreateCheque(transaction: TTransaction);
var
chequeFile: TextFile;
begin
AssignFile(chequeFile, 'cheque.txt');
Rewrite(chequeFile);
WriteLn(chequeFile, '----------------------------------------');
WriteLn(chequeFile, ' VerySimpleShop');
WriteLn(chequeFile, '');
WriteLn(chequeFile, 'Date and time: ');
WriteLn(chequeFile, DateTime.Now.ToString('yyyy-MM-dd HH:mm:ss'));
WriteLn(chequeFile, '');
WriteLn(chequeFile, 'Items:');
for i:Integer := 0 to productsCount do
begin
Products[i].WriteToFile(chequeFile);
end;
WriteLn(chequeFile, '');
WriteLn(chequeFile, 'Total: ' + totalPrice.ToString() + '$');
WriteLn(chequeFile, '----------------------------------------');
CloseFile(chequeFile);
end;
//
// SCREENS
//
// Show welcome screen.
procedure WelcomeScreen;
begin
Crt.ClrScr(); // Clear Screen.
currentPhase := ProgramPhase.WAITING;
Crt.SetWindowTitle(TITLE + 'Welcome');
Crt.TextColor(Crt.Yellow); // Set terminal text color.
WriteLn(' __ __ ___ _ _ ___ _ ');
WriteLn(' \ \ / /__ _ _ _ _/ __(_)_ __ _ __| |___/ __| |_ ___ _ __ ');
WriteLn(' \ V / -_) ''_| || \__ \ | '' \| ''_ \ / -_)__ \ '' \/ _ \ ''_ \');
WriteLn(' \_/\___|_| \_, |___/_|_|_|_| .__/_\___|___/_||_\___/ .__/');
WriteLn(' |__/ |_| |_| ');
Crt.TextColor(Crt.White);
WriteLn();
WriteLn(' We are glad to welcome you to our store!');
WriteLn();
Crt.TextColor(Crt.LightGray);
WriteLn(' This is a small educational project distributed under MIT License.');
WriteLn(' You can find the source code and more information');
WriteLn(' on GitHub repository: https://github.com/ANameSpace/VerySimpleShop');
WriteLn();
Crt.TextColor(Crt.White);
WriteLn('Press any key to start or ESC to quit.');
if ReadInput() = Button.ESC then
begin
Crt.ClrScr();
currentPhase := ProgramPhase.EXITING;
Halt(0); // Program termination with code 0.
end;
end;
// Show error screen.
procedure ErrorScreen(description: String);
var
btn: Button;
begin
Crt.ClrScr();
currentPhase := ProgramPhase.EXITING;
Crt.SetWindowTitle(TITLE + 'Error');
Crt.TextColor(Crt.LightRed);
WriteLn(' ___ ___ ___ ___ ___ _ ');
WriteLn(' | __| _ \ _ \/ _ \| _ \ |');
WriteLn(' | _|| / / (_) | /_|');
WriteLn(' |___|_|_\_|_\\___/|_|_(_)');
WriteLn(' ');
Crt.TextColor(Crt.LightGray);
WriteLn('A critical error occurred while the program was running.');
Write('Info: ');
Crt.TextColor(Crt.White);
WriteLn(description);
WriteLn();
WriteLn('Press ESC to quit.');
repeat
btn := ReadInput();
until btn = Button.ESC;
Crt.ClrScr();
Halt(0);
end;
// Shop screen move cursor.
procedure ShopScreenMoveCursor(cursorMove: Integer);
begin
if currentPhase <> ProgramPhase.SHOPPING then // <> <---> !=
begin
raise new System.ArgumentException('The operation cannot be performed in the current phase of the program.');
end;
// Clear.
Crt.GotoXY(1, OFFSET + currentPosition);
Write(' ');
currentPosition += cursorMove;
// Print.
Crt.GotoXY(1, OFFSET + currentPosition);
Crt.TextColor(Crt.Yellow);
Write(CURSOR_CHAR);
Crt.TextColor(Crt.White);
end;
// Shop screen update product info.
procedure ShopScreenUpdateProduct();
begin
if currentPhase <> ProgramPhase.SHOPPING then
begin
raise new System.ArgumentException('The operation cannot be performed in the current phase of the program.');
end;
Crt.GotoXY(2, OFFSET + currentPosition);
Write(products[currentPosition]);
Crt.GotoXY(1, OFFSET + productsCount + 2);
Crt.ClearLine();
Write('Total: ', totalPrice, '$');
end;
// Show shop screen.
procedure ShopScreen();
var
btn: Button;
begin
Crt.ClrScr();
currentPhase := ProgramPhase.SHOPPING;
Crt.SetWindowTitle(TITLE + 'Shop');
Crt.TextColor(Crt.Yellow);
WriteLn('SELECT THE PRODUCT:');
WriteLn();
Crt.TextColor(Crt.LightGray);
WriteLn('| UP/DOWN - Move the cursor.');
WriteLn('| LEFT/RIGHT - Change the number of items in the shopping cart.');
WriteLn('| ENTER - Proceed to payment or exit.');
WriteLn();
Crt.TextColor(Crt.White);
for i:Integer := 0 to productsCount do
begin
Write(' '); // Place for the cursor.
WriteLn(Products[i]);
end;
ShopScreenMoveCursor(0);
ShopScreenUpdateProduct();
// Main loop.
repeat
btn := ReadInput();
case btn of
Button.UP : begin
if currentPosition <> 0 then
begin
ShopScreenMoveCursor(-1);
end;
end;
Button.DOWN : begin
if currentPosition <> productsCount then
begin
ShopScreenMoveCursor(1);
end;
end;
Button.LEFT : begin
var product: TSimpleProduct := products[currentPosition];
if product.RemCount() then
begin
totalPrice -= product.Price;
ShopScreenUpdateProduct();
end;
end;
Button.RIGHT : begin
var product: TSimpleProduct := products[currentPosition];
if product.AddCount() then
begin
totalPrice += product.Price;
ShopScreenUpdateProduct();
end;
end;
Button.ESC : begin
ResetProductsCount();
exit;
end;
end;
until btn = Button.ENTER;
if totalPrice > 0 then
begin
currentPhase := ProgramPhase.PAYING;
end;
end;
// Show pay screen and get result.
function PayScreen(): TTransaction;
var
cardNumber: Integer;
cardCVV: Integer;
begin
Crt.ClrScr();
Crt.SetWindowTitle(TITLE + 'Pay');
Crt.TextColor(Crt.Yellow);
WriteLn('ENTER YOUR CARD DETAILS:');
Crt.TextColor(Crt.LightGray);
WriteLn('(You can enter any card number value. CVV - 123)');
WriteLn();
Crt.TextColor(Crt.White);
WriteLn('____________________________________');
WriteLn('| Card number: |');
WriteLn('| |');
WriteLn('| |');
WriteLn('| CVV: |');
WriteLn('| |');
WriteLn('| |');
WriteLn('|__________________________________|');
Crt.TextColor(Crt.Yellow);
cardNumber := ReadIntInput(16, 4, 6, false);
cardCVV := ReadIntInput(3, 31, 9, true);
Crt.TextColor(Crt.White);
var transaction: TTransaction := new TTransaction();
transaction.cardNumber := cardNumber;
transaction.isSuccess := (cardCVV = 123);
Result := transaction;
end;
// Show end screen.
procedure EndScreen(transaction: TTransaction);
begin
Crt.ClrScr();
currentPhase := ProgramPhase.ENDING;
Crt.SetWindowTitle(TITLE + 'Goodbye');
Crt.TextColor(Crt.Yellow);
WriteLn(' ___ _ _ _ ');
WriteLn(' / __|___ ___ __| | |__ _ _ ___| |');
WriteLn(' | (_ / _ \/ _ \/ _` | ''_ \ || / -_)_|');
WriteLn(' \___\___/\___/\__,_|_.__/\_, \___(_)');
WriteLn(' |__/ ');
Crt.TextColor(Crt.White);
WriteLn();
if transaction.isSuccess then
begin
WriteLn(' Thank you for your purchase!');
WriteLn(' Your receipt is in the file cheque.txt.');
CreateCheque(transaction);
end
else
begin
WriteLn('There are not enough money on the card!');
WriteLn('You were unable to make the purchase.');
end;
Crt.TextColor(Crt.LightGray);
ResetProductsCount();
for i: Integer := 5 downto 1 do
begin
Crt.GotoXY(1, 10);
WriteLn('Transitioning to the main screen in ', i, ' seconds...');
Crt.Delay(1000);
end;
Crt.TextColor(Crt.White);
// Reading all buttons pressed during this time (Without this, the program will immediately transition to the shop screen).
while Crt.KeyPressed() do
begin
Crt.ReadKey();
end;
end;
//
// Main block of the program.
//
begin
SetLength(products, 5);
Products[0] := TSimpleProduct.Create('Phone 15 Pro', 500);
Products[1] := TSimpleProduct.Create('Phone 15', 450);
Products[2] := TSimpleProduct.Create('Phone 14 Pro', 300);
Products[3] := TSimpleProduct.Create('Phone 14', 250);
Products[4] := TSimpleProduct.Create('Phone 1 Pro', 9999);
productsCount := High(products);
Crt.HideCursor();
// Main loop.
while currentPhase <> ProgramPhase.EXITING do
begin
WelcomeScreen();
ShopScreen();
if currentPhase = ProgramPhase.PAYING then
begin
EndScreen(PayScreen());
end;
end;
end.