Advertisement
hakonhagland

perl-menu-loop

Nov 9th, 2021
1,217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.71 KB | None | 0 0
  1. use v5.22.0;   # sub routine signatures requires perl >= 5.22
  2. use feature qw(say);
  3. use strict;
  4. use warnings;
  5. use experimental qw(signatures);
  6.  
  7. my @category_prices = (
  8.    "Office Visit" => 75.0,
  9.    "Vaccination" => 50.0,
  10.    "Hospitalization" => 300.5,
  11.    "Heartworm Treatment" => 150.5,
  12.    "Overnight Stay" => 450.0,
  13.    "Dentistry" => 200.5,
  14.    "X-Ray" => 275.0,
  15.    "Lab Work" => 45.0,
  16.    "Prescription" => 15.5,
  17. );
  18.  
  19. sub calculate_total($cost, $total) { $cost+$total }
  20.  
  21. sub amount_due($total) {
  22.     my $sales_tax = 0.07 * $total;
  23.     $total += $sales_tax;
  24.  
  25.     say "Amount Due (inc. of sales tax)  ", $total;
  26.     say "Thank you for your visit bark bark! (❍ᴥ❍ʋ)~ Clearing for next customer.";
  27. }
  28.  
  29. sub print_menu($total) {
  30.     say "Welcome to 'Lions Tigers and Bears' Pet Clinic! ฅ^•ﻌ•^ฅ  ~(=^‥^)";
  31.     say "Press the adjacent number to add the the service to your bill! meow~";
  32.     say "SERVICE              PRICE";
  33.  
  34.     my $num_categories = ($#category_prices+1)/2;
  35.     my @categories = map { $category_prices[$_*2] } 0..($num_categories-1);
  36.     my @prices = map { $category_prices[$_*2 +1] } 0..($num_categories-1);
  37.     my %prices = map { $categories[$_] => $prices[$_] } 0..($num_categories-1);
  38.     my %choices = map { ($_+ 1 ) => $categories[$_] } 0..($num_categories-1);
  39.     for my $i (0..$#categories) {
  40.         my $idx = $i+1;
  41.         my $price = format_price($prices[$i]);
  42.         say "$idx: $categories[$i]\t$price";
  43.     }
  44.     say <<'END';
  45. 0: Check Out        0
  46. Type 30 to Terminate  0
  47. END
  48.  
  49.     my $choice = -1;
  50.     my $cost = 0.0;
  51.     my @bill;
  52.     prompt();
  53.     while (my $choice = <>) {
  54.         next if !defined $choice;
  55.         if ( $choice !~ /^\d+$/) {
  56.             say "\tPlease type an integer.";
  57.             prompt();
  58.             next;
  59.         }
  60.         $choice = int $choice;
  61.         exit if $choice == 30;
  62.         last if $choice == 0;
  63.         if (!exists $choices{$choice}) {
  64.             say "\tIgnoring unrecognized command '$choice'";
  65.             prompt();
  66.             next;
  67.         }
  68.         my $category = $choices{$choice};
  69.         my $price = $prices{$category};
  70.         push @bill, [$category, $price];
  71.         $total = calculate_total($price, $total);
  72.         say "";
  73.         say "Items on your bill";
  74.         for my $item ( @bill ) {
  75.             my ( $category, $price ) = @$item;
  76.             $price = format_price($price);
  77.             say "$category \t\$$price";
  78.         }
  79.         say "";
  80.         say "Total       \$$total";
  81.         prompt();
  82.     }
  83.     amount_due($total);
  84.     return $total;
  85. }
  86.  
  87. sub format_price($price) { sprintf "%.1f", $price }
  88.  
  89. sub prompt { print "> " }
  90.  
  91. my $total = 0;
  92. while (1) {
  93.     $total = print_menu($total);
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement