chiark / gitweb /
Improvements to financial table
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Mon, 18 May 2009 01:59:53 +0000 (02:59 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Mon, 18 May 2009 01:59:53 +0000 (02:59 +0100)
ypp-restock-rum

index ce76aa794d8eeb11189b0c5d4a8a8e05df829e6f..96e6867f7ad0564cdc518a64dec2e8c5b3894eba 100755 (executable)
@@ -17,12 +17,10 @@ Each of which may appear only once, except \`have' which may appear
 more than once which case we calculate the differences and the profit
 for each one.
 
 more than once which case we calculate the differences and the profit
 for each one.
 
-If /<shot> is not specified at all, relevant information about shot is
-not reported.  For have and want, specifying an empty string means
-zero.  Entirely Missing <swill>/<grog>/ is taken as if both were
-specified and zero.
+If <shot> is not specified at all, relevant information about shot is
+not reported.  For have and want, missing entries count as zero.
 
 
-In price, missing entries mean the commodity is unavailable.
+In price, \`x' means the commodity is unavailable.
 END
 ;
 
 END
 ;
 
@@ -35,7 +33,7 @@ sub parse_info ($$$\@$) {
     badusage("$what: specified more than once")
        if defined $storeary->[2];
     my (@v) = ();
     badusage("$what: specified more than once")
        if defined $storeary->[2];
     my (@v) = ();
-    while (@ARGV and $ARGV[0] =~ m/^\d/) {
+    while (@ARGV and $ARGV[0] =~ m/^\d|^x$/) {
        $_ = shift @ARGV;
        push @v, $_;
     }
        $_ = shift @ARGV;
        push @v, $_;
     }
@@ -53,13 +51,17 @@ sub parse_info ($$$\@$) {
        push @v, $default;
     }
     if (@v != 4) {
        push @v, $default;
     }
     if (@v != 4) {
-       badusage("$what: invalid syntax (wrong number of /s)");
+       badusage("$what: invalid syntax (wrong number of parameters)");
     }
     my $i=0;
     foreach $_ (@v) {
     }
     my $i=0;
     foreach $_ (@v) {
-       $_ = $default if !length;
-       m/^0*(\d+)$/ or badusage("$what: $kinds[$i] \`$_': bad syntax");
-       $_= $1;
+       if ($default>0 and m/^x$/) {
+           $_ = $default;
+       } elsif (m/^0*(\d+)$/) {
+           $_= $1;
+       } else {
+           badusage("$what: $kinds[$i] \`$_': bad syntax");
+       }
        $i++;
     }
     @$storeary = @v;
        $i++;
     }
     @$storeary = @v;
@@ -193,10 +195,14 @@ sub pr1s ($) {
 
 sub compute_stock_values() {
     return unless @have;
 
 sub compute_stock_values() {
     return unless @have;
-    print <<END
+    print @have>1 ? <<END
                  Rum      Rum     Shot    Shot     total      Profit    Profit
                 equiv.   value   stocks   value    value      this leg   total
 END
                  Rum      Rum     Shot    Shot     total      Profit    Profit
                 equiv.   value   stocks   value    value      this leg   total
 END
+: <<END
+                             Rum      Rum             Shot    Shot     total
+                            equiv.   value           stocks   value    value
+END
 ;
 
     my $initial_value;
 ;
 
     my $initial_value;
@@ -208,9 +214,11 @@ END
        my $total_value= $rum_value + $shot_value;
 
        printf "%-10s ", 'stocks'.fmt_stock_index(++$si).':';
        my $total_value= $rum_value + $shot_value;
 
        printf "%-10s ", 'stocks'.fmt_stock_index(++$si).':';
+       print '            ' if @have==1;
        pr1s($stock_rum);
        pr1s($rum_value);
        pr1s($stock_rum);
        pr1s($rum_value);
-       printf "%6d", $stocks->[3];
+       print '        ' if @have==1;
+       printf "%6s", $stocks->[3] ? $stocks->[3] : '';
        pr1s($shot_value);
        pr1s($total_value);
        
        pr1s($shot_value);
        pr1s($total_value);
        
@@ -223,21 +231,19 @@ END
        $last_value= $total_value;
        print "\n";
     }
        $last_value= $total_value;
        print "\n";
     }
-    print <<END
+    print @have>1 ? <<END
                  fine      poe    units    poe       poe     delta-poe     poe
                  fine      poe    units    poe       poe     delta-poe     poe
-
+END
+: <<END
+                             fine      poe            units    poe       poe
 END
 ;
 END
 ;
+    print "\n";
 }
 
 sub pr2 ($$$) {
     my ($k,$v1,$v2) = @_;
 }
 
 sub pr2 ($$$) {
     my ($k,$v1,$v2) = @_;
-    printf "%-20s %-23s %s\n", "$k:", $v1, $v2;
-}
-
-sub pr3 ($$$$) {
-    my ($k,$v1,$v2,$v3) = @_;
-    printf "%-20s %-23s %-20s %s\n", "$k:", $v1, $v2, $v3;
+    printf "%-25s %-23s %s\n", "$k:", $v1, $v2;
 }
 
 sub pr2rs ($$$) {
 }
 
 sub pr2rs ($$$) {
@@ -269,21 +275,34 @@ sub compute_restock_requirements () {
     if (@price) {
        my ($rum_buy,$shot_buy) = ('','');
        my ($rum_bill,$shot_bill) = qw(0 0);
     if (@price) {
        my ($rum_buy,$shot_buy) = ('','');
        my ($rum_bill,$shot_bill) = qw(0 0);
-       if (defined $best and $rum_need > 0) {
-           my $rum_qty= $rum_need * 100 / $proofs[$best];
-           $rum_qty= ceil($rum_qty);
-           $rum_buy= sprintf('%12s %-11s  ',"** $rum_qty","$kinds[$best] **");
-           $rum_bill= $rum_qty * $price[$best];
+       my $ok=1;
+       if ($rum_need > 0) {
+           if (defined $best) {
+               my $rum_qty= $rum_need * 100 / $proofs[$best];
+               $rum_qty= ceil($rum_qty);
+               $rum_buy= sprintf('%12s %-11s  ',
+                                 "** $rum_qty","$kinds[$best] **");
+               $rum_bill= $rum_qty * $price[$best];
+           } else {
+               $rum_buy= '   (rum unavailable)';
+               $ok= 0;
+           }
        }
        }
-       if ($shot_need > 0 and valid($price[3])) {
-           $shot_buy= sprintf('%7s shot **', "** $shot_need");
-           $shot_bill= $shot_need * $price[3];
+       if ($shot_need > 0) {
+           if (valid($price[3])) {
+               $shot_buy= sprintf('%7s shot **', "** $shot_need");
+               $shot_bill= $shot_need * $price[3];
+           } else {
+               $shot_buy= '  (shot unavailable)';
+               $ok= 0;
+           }
        }
        if (length($rum_buy) or length($shot_buy)) {
        }
        if (length($rum_buy) or length($shot_buy)) {
-           pr3('for',
+           pr2($ok
+               ? sprintf('for a total of %d poe', $rum_bill + $shot_bill)
+               : 'for',
                $rum_bill ? sprintf("%12d poe       ", $rum_bill) : '',
                $rum_bill ? sprintf("%12d poe       ", $rum_bill) : '',
-               $shot_bill ? sprintf("%10d poe", $shot_bill) : '',
-               sprintf("%7d total", $rum_bill + $shot_bill));
+               $shot_bill ? sprintf("%10d poe", $shot_bill) : '');
            pr2('BUY', $rum_buy, $shot_buy);
        } else {
            print "stocks are sufficient.\n";
            pr2('BUY', $rum_buy, $shot_buy);
        } else {
            print "stocks are sufficient.\n";
@@ -298,38 +317,5 @@ compute_cheapest_rum();
 compute_stock_values();
 compute_restock_requirements();
 
 compute_stock_values();
 compute_restock_requirements();
 
-__DATA__
-
-#    if (defined $price{Swill}) {
-#      map { $price{$_}= undef if $price{$_} eq 'x' } @rums;
-#    }
-#    if (defined $have{Swill}) {
-#      $have_proof= 0;
-#      map { $have_proof += $have{$_} * $proof{$_} } @rums;
-#    }
-
-our ($best);
-
-our $have_proof;
-
-our ($need_proof, %need, %buy);
-           
-
-sub compute_restock_cheapest_rum() {
-    my %bill;
-    map {
-       $bill{$_} = $buy{$_} * $price{$_} if defined $price{$_};
-    } @rums;
-    prf('nominal bill', %bill, 'poe');
-    print "\n";
-    if ($need_proof < 0) {
-       printf "stocks are sufficient";
-    } else {
-       my $buy= ceil($buy{$best});
-       printf "buy %d %s at %d poe each for %d poe",
-           $buy, $best, $price{$best}, $buy * $price{$best};
-    }
-    print "\n\n";
-}
-
-main();
+STDOUT->error and die $!;
+close STDOUT or die $!;