-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStart-Lemonade.ps1
580 lines (505 loc) · 18.3 KB
/
Start-Lemonade.ps1
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
<#
.SYNOPSIS
Implements a lemonade stand simulation.
.DESCRIPTION
You've decided to run a lemonade stand on each Saturday of the Summer.
Purchase your inventory of cups, lemons and sugar from the grocery store.
Price your lemonade based on the cost of ingredients, weather forecast, etc.
Lowering your prices will sell more lemonade but return less on each sale.
You might be able to sell more lemonade on hot days at higher prices.
Try to maximize your profits before the Summer ends.
Currency units are selected based on the current computer locale.
Please consider giving to cancer research.
.PARAMETER title
Specifies an alternate title for the lemonade stand.
.PARAMETER celsius
When present, displays the temperature as Celsius.
.PARAMETER noglyphs
When present, the weather glyphs will not be displayed.
.PARAMETER nowait
When present, skips the "now serving" wait loop.
.INPUTS
None.
.OUTPUTS
A whole lot of fun.
.EXAMPLE
.\Start-Lemonade.ps1
Starts a new lemonade stand.
.EXAMPLE
.\Start-Lemonade.ps1 -title "Penny's Lemonade"
Starts a new lemonade stand, with an alternate title value.
The title value can be up to 30 characters in length.
.EXAMPLE
.\Start-Lemonade.ps1 -celsius
Starts a new lemonade stand, using Celsius as the temperature scale.
.EXAMPLE
.\Start-Lemonade.ps1 -noglyphs
Starts a new lemonade stand, without displaying the weather glyphs.
Older console windows may not fully support these UTF8 encoded characters.
.EXAMPLE
.\Start-Lemonade.ps1 -nowait
Starts a new lemonade stand, skipping the "now serving" wait loop.
.NOTES
MIT License
Copyright (c) 2023 TigerPointe Software, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Most console windows support UTF8 encoding. If you do not see the weather
glyphs (such as the clouds, rain or sun), your console window most likely does
not have UTF8 encoding enabled. Be sure to start the game with the "noglyphs"
option enabled to avoid displaying the invalid character symbols.
Dedicated to fond memories of "Lemonade Stand" on the Commodore 64.
If you enjoy this software, please do something kind for free.
History:
01.00 2023-Jan-07 Scott S. Initial release.
01.01 2023-Jan-18 Scott S. Removed extra parenthesis from calculations.
02.00 2023-Jan-22 Scott S. Revised sales calculation, added best price.
.LINK
https://en.wikipedia.org/wiki/Lemonade_Stand
.LINK
https://en.wikipedia.org/wiki/ASCII_art
.LINK
https://braintumor.org/
.LINK
https://www.cancer.org/
#>
# Accept the command line parameters
param
(
# Specifies the title of the lemonade stand
[string]$title = "Lemonade Stand"
# Use Celsius as the temperature scale
, [switch]$celsius
# Do not display the weather glyphs (limited UTF8 console support)
, [switch]$noglyphs
# Skip the "now serving" wait loop
, [switch]$nowait
)
# Define the ASCII art (original image by Scott S.)
$ascii = @"
.===================.
| FRESH LEMONADE |
'==================='
!! !!
!! !!
!! !!
!! !!
!! !!
!! !!
!! ______ !!
!! \..../ !!
!! (::::)O !!
=======================
\___________________/
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
v| | | | | | | | | |v
'"'"'"'"'"'"'"'"'"'"'"'
"@;
$ascii = $ascii.Replace("`r",""); # Standardize the CrLf to use the Lf only
$lines = $ascii.Split("`n"); # Split the ASCII image on the Lf
$width = $lines[$lines.Count - 1].Length; # Longest line of the ASCII image
# Define the temperature unit symbols
$FahrenheitUnit = "ºF";
$CelsiusUnit = "ºC";
# Inventory data (contains the item levels)
$inventory = @{};
$inventory.cups = 0;
$inventory.lemons = 0;
$inventory.sugar = 0;
$inventory.cash = 30.00;
$inventory.start = $inventory.cash;
# Cups data (includes a calculated cost per unit)
$cups = @{};
$cups.cost = 2.50; # current price
$cups.count = 25; # servings per box
$cups.min = 0.99; # minimum price
$cups.unit = [Math]::Round($cups.cost / $cups.count, 2);
# Lemons data (includes a calculated cost per unit)
$lemons = @{};
$lemons.cost = 4.00; # current price
$lemons.count = 8; # servings per bag
$lemons.min = 2.00; # minimum price
$lemons.unit = [Math]::Round($lemons.cost / $lemons.count, 2);
# Sugar data (includes a calculated cost per unit)
$sugar = @{};
$sugar.cost = 3.00; # current price
$sugar.count = 15; # servings per bag
$sugar.min = 1.50; # minimum price
$sugar.unit = [Math]::Round($sugar.cost / $sugar.count, 2);
# Weeks data (measures the session duration)
$weeks = @{};
$weeks.current = 1; # start with the 1st week
$weeks.total = 12; # span the 12 weeks of Summer
$weeks.sales = 99; # 99 maximum sales per week
$weeks.summary = @(); # empty array
# Forecast data (includes percentage values, UTF8 glyphs and display names)
$forecast = [ordered]@{};
$forecast.sunny = @(1.00, 0x2600, "Sunny");
$forecast.partly = @(0.90, 0x26C5, "Partly Sunny");
$forecast.cloudy = @(0.70, 0x2601, "Mostly Cloudy");
$forecast.rainy = @(0.40, 0x2602, "Rainy");
$forecast.stormy = @(0.10, 0x26C8, "Stormy");
# Temperature data (uses Fahrenheit as the percentage values)
$temperature = @{};
$temperature.min = 69;
$temperature.max = 100;
$temperature.units = $FahrenheitUnit;
$temperature.forecast = $null;
$temperature.value = $null;
# Score data (based on actual vs. maximum net sales)
$score = @{};
$score.value = 0.00;
$score.total = 0.00;
# Gets the sales amount
# Multiply the potential sales by a ratio of unit cost to actual price; the
# exponent results in the values falling along a curve, rather than along a
# straight line, resulting in more realistic sales values at each price
function Get-SalesAmount
{
param(
[int]$potential # potential sales
, [decimal]$unit # unit cost
, [decimal]$price # actual price
)
return [Math]::Floor($potential * ($unit / [Math]::Pow($price, 1.5)));
}
# Sanity check the title
if ([String]::IsNullOrWhiteSpace($title))
{
$title = "";
}
elseif ($title.Trim().Length -gt 30)
{
$title = "$($title.Trim().Substring(0, 30))... ";
}
else
{
$title = "$($title.Trim()) ";
}
# Check for Celsius
if ($celsius.IsPresent)
{
$temperature.units = $CelsiusUnit;
}
# Start the main loop
while ($weeks.current -le $weeks.total)
{
# Create a new display buffer for the text messages
Clear-Host;
$buffer = [string[]]::new($lines.Count);
# Display the current week number
$buffer[0] = "";
$buffer[1] = "$($title)Week #$($weeks.current)";
# Generate a random weather forecast and temperature
$temperature.forecast = `
(Get-Random -Minimum 0 -Maximum $forecast.Keys.Count);
$temperature.value = `
(Get-Random -Minimum $temperature.min -Maximum $temperature.max);
$formatted = $temperature.value.ToString("N0");
if ($temperature.units -eq $CelsiusUnit)
{
$formatted = (($temperature.value - 32) * (5/9)).ToString("N0");
}
$glyph = "";
if (-not $noglyphs.IsPresent)
{
$glyph = [char]$forecast[$temperature.forecast][1];
}
$buffer[2] = "";
$buffer[3] = "Weather Forecast: " + `
"$formatted$($temperature.units) " + `
"$($forecast[$temperature.forecast][2]) $glyph";
# Calculate the potential sales as a percentage of the maximum value
# (lower temperature = fewer sales, severe weather = fewer sales)
$potential = [Math]::Floor($weeks.sales * `
($temperature.value / 100) * `
($forecast[$temperature.forecast][0]));
$buffer[4] = "Estimated Sales: $potential cups";
# Update the cups cost
$cups.cost = $cups.cost + `
[Math]::Round((Get-Random -Minimum -1.50 -Maximum 1.50), 2);
if ($cups.cost -lt $cups.min) { $cups.cost = $cups.min; }
$cups.unit = [Math]::Round($cups.cost / $cups.count, 2);
# Update the lemons cost
$lemons.cost = $lemons.cost + `
[Math]::Round((Get-Random -Minimum -1.50 -Maximum 1.50), 2);
if ($lemons.cost -lt $lemons.min) { $lemons.cost = $lemons.min; }
$lemons.unit = [Math]::Round($lemons.cost / $lemons.count, 2);
# Update the sugar cost
$sugar.cost = $sugar.cost + `
[Math]::Round((Get-Random -Minimum -1.50 -Maximum 1.50), 2);
if ($sugar.cost -lt $sugar.min) { $sugar.cost = $sugar.min; }
$sugar.unit = [Math]::Round($sugar.cost / $sugar.count, 2);
# Display the updated item prices
$buffer[5] = "";
$buffer[6] = "Grocery Store Prices";
$buffer[7] = `
" Cups: $($cups.cost.ToString("C2")) box of $($cups.count)";
$buffer[8] = `
" Lemons: $($lemons.cost.ToString("C2")) bag of $($lemons.count)";
$buffer[9] = `
" Sugar: $($sugar.cost.ToString("C2")) bag for $($sugar.count) cups";
# Calculate the unit cost
$unit = $cups.unit + $lemons.unit + $sugar.unit;
$buffer[10] = " $($unit.ToString("C2")) cost per serving";
# Display the current inventory
$buffer[11] = "";
$buffer[12] = "My Current Inventory";
$buffer[13] = " Cups: $($inventory.cups)";
$buffer[14] = " Lemons: $($inventory.lemons)";
$buffer[15] = " Sugar: $($inventory.sugar)";
# Display the current cash
$gainloss = $inventory.cash - $inventory.start;
$buffer[16] = "";
$buffer[17] = "My Cash: $($inventory.cash.ToString("C2"))";
$buffer[18] = "Gain/Loss: $($gainloss.ToString("C2"))";
$buffer[19] = "";
# Output the display buffer (combines the ASCII art with the text messages)
for ($i = 0; $i -lt $buffer.Count; $i++)
{
"$($lines[$i].PadRight($width)) $($buffer[$i])";
}
# Read the number of cup boxes to purchase
[int]$newcups = -1;
while ($newcups -lt 0)
{
try
{
[int]$newcups = Read-Host -Prompt "How many boxes of cups to buy?";
if ($newcups -gt 0)
{
$cost = $newcups * $cups.cost;
if ($cost -gt $inventory.cash)
{
throw "You do not have enough cash.";
}
$inventory.cups = $inventory.cups + ($newcups * $cups.count);
$inventory.cash = $inventory.cash - $cost;
" Purchased $newcups box(es) of cups for $($cost.ToString("C2"))";
" $($inventory.cups) cup inventory, " + `
"$($inventory.cash.ToString("C2")) cash remaining";
}
else
{
" No additional cups were purchased";
}
}
catch
{
Write-Host -ForegroundColor Yellow -Object " $_";
$newcups = -1;
}
}
# Read the number of lemon bags to purchase
[int]$newlemons = -1;
while ($newlemons -lt 0)
{
try
{
[int]$newlemons = Read-Host -Prompt "How many bags of lemons to buy?";
if ($newlemons -gt 0)
{
$cost = $newlemons * $lemons.cost;
if ($cost -gt $inventory.cash)
{
throw "You do not have enough cash.";
}
$inventory.lemons = $inventory.lemons + ($newlemons * $lemons.count);
$inventory.cash = $inventory.cash - $cost;
" Purchased $newlemons bag(s) of lemons for $($cost.ToString("C2"))";
" $($inventory.lemons) lemon inventory, " + `
"$($inventory.cash.ToString("C2")) cash remaining";
}
else
{
" No additional lemons were purchased";
}
}
catch
{
Write-Host -ForegroundColor Yellow -Object " $_";
$newlemons = -1;
}
}
# Read the number of sugar bags to purchase
[int]$newsugar = -1;
while ($newsugar -lt 0)
{
try
{
[int]$newsugar = Read-Host -Prompt "How many bags of sugar to buy?";
if ($newsugar -gt 0)
{
$cost = $newsugar * $sugar.cost;
if ($cost -gt $inventory.cash)
{
throw "You do not have enough cash.";
}
$inventory.sugar = $inventory.sugar + ($newsugar * $sugar.count);
$inventory.cash = $inventory.cash - $cost;
" Purchased $newsugar bag(s) of sugar for $($cost.ToString("C2"))";
" $($inventory.sugar) sugar inventory, " + `
"$($inventory.cash.ToString("C2")) cash remaining";
}
else
{
" No additional sugar was purchased";
}
}
catch
{
Write-Host -ForegroundColor Yellow -Object " $_";
$newsugar = -1;
}
}
# Read the actual price
[decimal]$price = 0.00;
while ($price -le 0.00)
{
try
{
$raw = Read-Host -Prompt "How much should the lemonade cost?";
[decimal]$price = ($raw -replace "[^0-9.-]", ""); # strip non-numbers
if ($price -le 0.00)
{
throw "The price must be greater than zero.";
}
}
catch
{
Write-Host -ForegroundColor Yellow -Object " $_";
$price = 0.00;
}
}
" Setting the price at $($price.ToString("C2"))";
# Calculate the weekly sales based on price and lowest inventory level
# (higher markup price = fewer sales, limited by the inventory on-hand)
$sales = (Get-SalesAmount -potential $potential `
-unit $unit `
-price $price);
$set = @($potential, $sales, `
$inventory.cups, $inventory.lemons, $inventory.sugar);
$sales = ($set | Sort-Object -Descending | Select -Last 1); # lowest value
$margin = $price - $unit;
$gross = $sales * $price;
$net = $sales * $margin;
# Add a new row to the summary
$weeks.summary += @{ sales = $sales; price = $price; };
# Simulate a sense of time passing (each dot represents a sale)
if (-not $nowait.IsPresent)
{
Write-Host -Object "`nNow Serving:";
for ($i = 0; $i -lt $sales; $i++)
{
Write-Host -NoNewline -Object ". ";
Start-Sleep -Milliseconds (Get-Random -Minimum 250 -Maximum 1500);
}
Write-Host;
}
# Update the inventory levels to reflect consumption
$inventory.cups = $inventory.cups - $sales;
$inventory.lemons = $inventory.lemons - $sales;
$inventory.sugar = $inventory.sugar - $sales;
$inventory.cash = $inventory.cash + $gross;
$gainloss = $inventory.cash - $inventory.start;
# Display the calculated sales information
Clear-Host;
"`nSales Results Week #$($weeks.current) of $($weeks.total)";
" Unit Cost (per serving): $($unit.ToString("C2"))";
" Actual Price: $($price.ToString("C2"))";
" Profit Margin: $($margin.ToString("C2"))";
" Actual Sales: $sales x $($price.ToString("C2"))";
" Gross Profit: $($gross.ToString("C2"))";
" Net Profit: $($net.ToString("C2"))";
" Current Cash: $($inventory.cash.ToString("C2"))";
" Total Gain/Loss: $($gainloss.ToString("C2"))";
# Display the updated inventory levels
"`nRemaining Inventory";
" Cups: $($inventory.cups)";
" Lemons: $($inventory.lemons)";
" Sugar: $($inventory.sugar)";
# Display the weekly sales summary
$padWeek = $weeks.total.ToString().Length;
$padSale = $weeks.sales.ToString().Length;
$total = 0;
"`nWeekly Sales Summary";
for ($i = 0; $i -lt $weeks.summary.Count; $i++)
{
" Week $(($i + 1).ToString().PadLeft($padWeek)): " + `
"$($weeks.summary[$i]["sales"].ToString().PadLeft($padSale)) sold x " + `
"$(($weeks.summary[$i]["price"]).ToString("C2")) ea.";
$total = $total + $weeks.summary[$i]["sales"];
}
# Loop through a range of prices to find the highest net profit
$maxsales = 0;
$maxprice = 0.00;
$maxgross = 0.00;
$maxnet = 0.00;
$minnet = $net;
for ($price = 0.25; $price -le 25.00; $price += 0.25)
{
$sales = (Get-SalesAmount -potential $potential `
-unit $unit `
-price $price);
$margin = $price - $unit;
$gross = $sales * $price;
$net = $sales * $margin;
if (($sales -gt 0) -and `
($sales -le $potential) -and `
($unit -le $price))
{
if ($net -gt $maxnet)
{
$maxsales = $sales;
$maxprice = $price;
$maxgross = $gross;
$maxnet = $net;
}
}
}
if ($maxnet -gt $minnet)
{
"`nYour sales could have been:";
" $maxsales sold x " + `
"$($maxprice.ToString("C2")) ea. = " + `
"$($maxgross.ToString("C2")) for a net profit of " + `
"$($maxnet.ToString("C2"))";
if ($inventory.cups -le 0) { " You ran out of cups."; }
if ($inventory.lemons -le 0) { " You ran out of lemons."; }
if ($inventory.sugar -le 0) { " You ran out of sugar."; }
}
else
{
"`nCongratulations -- your sales were perfect!";
}
# Increment the score counters
$score.value = $score.value + $minnet;
$score.total = $score.total + $maxnet;
# Increment the week number
if ($weeks.current -eq $weeks.total)
{
$success = [Math]::round(($score.value / $score.total) * 100);
"`nYou've made $($score.value.ToString("C2")) " + `
"out of a possible $($score.total.ToString("C2")) " + `
"for a score of $success%";
"You've sold $total total cups -- see you again next time!";
}
$weeks.current++;
Read-Host -Prompt "`nPress ENTER to Continue";
}