buy sub routine fixes a bug in the original game which allowed the player to purchase a few million shares when the price reached zero.
sub buy {
my ($portfolio, $ticker, $cash) = @_;
my $stock_to_buy = query_user( q|What Stock would you like to buy? |);
foreach my $stock ( keys %$portfolio ) {
if ( $stock =~/$stock_to_buy/i ) {
if ( $$ticker{$stock}{price} == 0 ) {
notify_user(
qq|Sorry, trading has been suspended in $stock.| );
} else {
my $shares_to_buy = query_user(
qq|\n\nYou have $portfolio{$stock} shares in $stock and|,
qq|\$$cash\nBuy How many |,
qq|(max int( $cash / $$ticker{$stock}{price} ), q|) :| );
if ( $cash < $$ticker{$stock}{price} * $shares_to_buy ) {
notify_user( qq|Sorry, this isn't a margin account.\n| );
} else {
$$portfolio{$stock} += $shares_to_buy;
$cash -= $shares_to_buy * $$ticker{$stock}{price};
}
}
last;
}
}
return $cash;
}