Universität Ulm, Fakultät für Mathematik und Wirtschaftswissenschaften, SAI

# Lösung zu Blatt 7 --- OO-Datenbank Anwendungen (WS 2002)

## Viel Glück an der Börse

```#!/usr/local/bin/perl -w

use strict;
use IO::File;
use LWP::Simple;

sub getStocks {
my (\$url) = \$_[0] || 'http://de.finance.yahoo.com/q?s=@^GDAXI&o=S';
my \$content = LWP::Simple::get(\$url);
return undef unless defined \$content;
my \$stocks;
foreach (split(/\n/, \$content)) {
my (\$wkn, \$name, \$kurs) =
(/^<td .*d=t\">(\d{6})\.DE<.*>([A-Z][A-Z\. ]*)\s+<.*border=0>(\d+\,\d+)<\/b>/);
next unless defined \$kurs;
\$name =~ s/\s*\$//; \$kurs =~ s/\,/./;
#print "\$wkn: \$name: \$kurs\n";
\$stocks->{\$wkn} = [\$name, \$kurs];
}
\$stocks->{cash} = ["Euro", 0];
return \$stocks;
}

my \$filename = \$_[0] || "depot.txt";
my \$fh = new IO::File \$filename;
return () unless defined \$fh;
my %depot = ();
while (<\$fh>) {
chomp;
next if /^\s*(#|\$)/;
my (\$wkn, \$anz) = split /:/;
next unless defined \$anz;
\$depot{\$wkn} = \$anz;
}
\$fh->close;
return %depot;
}

sub saveDepot {
my \$depot = \$_[0];
my \$filename = \$_[1] || "depot.txt";
my \$fh = new IO::File ">\$filename";
return undef unless defined \$fh;
foreach (keys %{\$depot}) {
print \$fh "\$_:", \$depot->{\$_}, "\n";
}
\$fh->close;
}

my \$stocks = getStocks;
\$depot{cash} = 100000 unless defined \$depot{cash};
while (<>) {
chomp;
if (/^h/) { print "Short help\n"; }
elsif (/^b/) {
my (\$wkn, \$anz) = (/^b\w*\s+(\d{6})\s+(\d+)\s*\$/);
unless (defined \$anz and \$anz > 0 and defined \$stocks->{\$wkn}) {
} else {
\$stocks = getStocks;
if (\$anz * \$stocks->{\$wkn}->[1] > \$depot{cash}) {
print "Not enough money\n";
} else {
\$depot{cash} -= \$anz * \$stocks->{\$wkn}->[1];
\$depot{\$wkn} += \$anz;
print "Bought \$anz for \$stocks->{\$wkn}->[1] each\n";
}
}
} elsif (/^s/) {
my (\$wkn, \$anz) = (/^s\w*\s+(\d{6})\s+(\d+)\s*\$/);
unless (defined \$anz and \$anz > 0 and defined \$depot{\$wkn} and
\$depot{\$wkn} >= \$anz) {
print "Illegal sell request\n";
} else {
\$stocks = getStocks;
\$depot{cash} += \$anz * \$stocks->{\$wkn}->[1];
\$depot{\$wkn} -= \$anz;
print "Sold \$anz for \$stocks->{\$wkn}->[1] each\n";
}
} elsif (/^p/) {