Skip to content

Commit 897097a

Browse files
committed
Fix bug: Stock purchase showing incorrect values.
- When a user goes to check out the portions show the same as price without the currency sign. - Comments also added throughout purchase stock code block.
1 parent 3862c1f commit 897097a

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

game_menu.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -386,41 +386,44 @@ def purchase_stock_menu(stats):
386386
'''
387387
Purchase stock menu
388388
'''
389-
390-
# Main function constants.line stats here.
391389
while True:
392390
clear_terminal()
393-
391+
# Header
394392
print(f'{cyan("Purchase consumable stock")}')
395393
print(constants.LINE)
396394
print(f'Current balance {green(print_current_balance(stats))}')
397395
print('You have enough ingrediants to sell '
398396
+ f'{gold(get_portions_avaliable(stats))} hotdogs.\n')
399397

398+
# Show current ingredients in stock
400399
print(f'{cyan("Current stock:")}')
401400
print(constants.LINE)
402401
print(f'{stats["bun"]:<5} x {"Hotdog bun(s)":<20}'
403402
+ f'| {stats["sausage"]:<5} x Sausage(s)')
404403
print(f'{stats["onion"]:<5} x {"Onion(s)":<20}'
405404
+ f'| {stats["sauce"]:<5} x Special sauce(s)')
406405

406+
# Show price list for ingredients
407407
print(constants.PURCHASE_STOCK_OPTIONS)
408408

409+
# Tip to show.
409410
print(f"\n{pink('TIP: ')}This will purchase the amount of "
410411
+ "ingredients you \nneed for the amount of hotdogs "
411412
+ "you want to stock. All sales are final")
412413

413414
print_go_back()
414415

416+
# Get user input
415417
user_choice = input(orange('\nHow many hotdogs would you like to '
416418
+ 'have in stock? '))
417419

420+
# Validation checks
418421
if not validate_input(user_choice, 99999):
419422
continue
420-
421423
if int(user_choice) == 0:
422424
break
423425

426+
# Setting up variables
424427
cost = 0
425428
basket = { # Create empty basket
426429
"stock": [],
@@ -431,8 +434,9 @@ def purchase_stock_menu(stats):
431434
"total_qty_c": [] # Total cost for item
432435
}
433436

437+
# Calculate how many ingredients need to be purchased and thee
438+
# costs including total cost.
434439
for count, key in enumerate(constants.STOCK_OPTIONS):
435-
# stock = constants.STOCK_OPTIONS[count]
436440
basket["stock"].append(stats[key])
437441
basket["recipe"].append(stats['recipe'][key])
438442
basket["portions"].append(constants.STOCK_COSTS[key][1])
@@ -461,43 +465,50 @@ def purchase_stock_menu(stats):
461465

462466
cost += basket["total_qty_c"][count]
463467

468+
# If cost is 0 then no ingredients required to be purchased
469+
# Show error to user then go back to beginning of loop.
464470
if cost == 0:
465471
print_error_message("\nYou already have this many hotdogs in "
466472
+ "stock.")
467473
continue
468474

475+
# Displaying checkout to the user
469476
while True:
470477
clear_terminal()
478+
# Header
471479
print(f'\n{cyan("Checkout:")}')
472480
print(constants.LINE)
473481
print(f'{"Item:":<23}{"Qty:":<10}{"Portions:":<12}'
474482
+ f'{"Sub total:":<10}')
475483
print(constants.LINE)
476484

485+
# Basket
477486
for count, key in enumerate(constants.STOCK_COSTS):
478487
text = (basket["portions"][count]
479488
* basket["total_qty_r"][count])
480-
481-
text = basket["total_qty_c"][count]
489+
text2 = basket["total_qty_c"][count]
482490
print(f'{constants.STOCK_COSTS[key][0]:<23}'
483491
+ f'{basket["total_qty_r"][count]:<12}'
484492
+ f'{text:<10}'
485-
+ f'£{"{:.2f}".format(text):<10}')
493+
+ f'£{"{:.2f}".format(text2):<10}')
486494

487495
print(constants.LINE)
496+
497+
# Basket total
488498
print('TOTAL COST: ' + green(f"£{'{:.2f}'.format(cost)}"))
489499

500+
# User input required
490501
user_choice = input(f'\n{orange("Would you like to make this")}'
491502
+ f'{orange(" purchase? (yes / no) ")}')
492503

504+
# Validate user input
493505
if not validate_yes_no(user_choice):
494506
continue
495-
496507
if user_choice.lower() in ['y', 'yes']:
508+
497509
# Check if remaining cash will above 0 after purchase,
498510
# if so continue, else loop
499511
remaining_cash = stats["cash"] - cost
500-
501512
if remaining_cash < 0:
502513
print_error_message("Not enough funds")
503514
break
@@ -507,13 +518,15 @@ def purchase_stock_menu(stats):
507518
stats[i] += (basket["portions"][count]
508519
* basket["total_qty_r"][count])
509520

521+
# Purchase successful, update user cash balance.
510522
print(green('Purchase Successful'))
511523
stats["cash"] = remaining_cash
512524
print(cyan('Remaining balance '
513525
+ f'{print_current_balance(stats)}'))
514526
print_press_enter_to("Press Enter to continue...")
515527
break
516528

529+
# Purchaser unsuccssgful
517530
print(red('Purchase Aborted'))
518531
print_press_enter_to("Press Enter to continue...")
519532
break

0 commit comments

Comments
 (0)