#!/usr/bin/perl use warnings; use strict; package Customer { sub new { my $classname = shift; my $args = {@_}; my $self = { id => $args->{id}, name => $args->{name} }; $self->{orders} = []; return bless($self, $classname); } sub updateOrders { my $self = shift; my @orders = @_; my $order; foreach $order (@orders) { if ($self->{id} eq $order->{customer}->{id}) { push($self->{orders}, $order); } } } sub getOrders { my $self = shift; return @{ $self->{orders} }; } } package Product { sub new { my $classname = shift; my $args = {@_}; my $self = { price => $args->{price} }; return bless($self, $classname); } } package Order { sub new { my $classname = shift; my $args = {@_}; my $self = { id => $args->{id}, quantity => $args->{quantity}, customer => $args->{customer}, product => $args->{product}, addtlinfo => $args->{addtlinfo} }; return bless($self, $classname); } } sub toEUR { my $n = shift; $n =~ s/\,/./; if ($n =~ /\,/) { print "\nError: Too many commas in '$n'.\n\n"; exit 1; } $n = sprintf("%.02f", $n); $n =~ s/\./,/; if ($n =~ /\./) { print "\nError: Too many points in '$n'.\n\n"; exit 2; } return $n; } sub print_formatted { # @fields: $quantity, $id, $name, $price, $addtlinfo: my @fields = @_; $fields[3] = toEUR($fields[3]); my @formats = ([5, "r"], [7, "r"], [40, "l"], [6, "r"], [20, "r"]); my $separator = " "; my $s = ""; my $i; my @v; my $spaces; for $i (0 .. $#formats) { @v = @{$formats[$i]}; if (length($fields[$i]) > $v[0]) { print "\nError: Length of '$fields[$i]' shouldn't be bigger than $v[0].\n\n"; exit 3; } $spaces = " " x ($v[0] - length($fields[$i])); if ($v[1] eq "r") { $s .= $spaces; $s .= $fields[$i]; } else { $s .= $fields[$i]; $s .= $spaces; } if ($i == 3) { $s .= " Euro "; } else { $s .= $separator; } } print "$s\n"; } my @customers = (); push(@customers, Customer->new(id => 1, name => "Mueller")); push(@customers, Customer->new(id => 2, name => "Meier")); push(@customers, Customer->new(id => 3, name => "Schulze")); push(@customers, Customer->new(id => 4, name => "Schmidt")); push(@customers, Customer->new(id => 5, name => "Schulz")); push(@customers, Customer->new(id => 6, name => "Scholz")); my @products = (); push(@products, Product->new(price => 5)); push(@products, Product->new(price => 250.75)); push(@products, Product->new(price => 3)); push(@products, Product->new(price => 125)); push(@products, Product->new(price => 25)); push(@products, Product->new(price => 0.25)); my @orders = (); push(@orders, Order->new(id => 12345, quantity => 10, customer => $customers[2], product => $products[0], addtlinfo => "Angebot")); push(@orders, Order->new(id => 12346, quantity => 25, customer => $customers[1], product => $products[4], addtlinfo => "Aktion")); push(@orders, Order->new(id => 12347, quantity => 75, customer => $customers[0], product => $products[5], addtlinfo => "Reduziert")); push(@orders, Order->new(id => 12348, quantity => 100, customer => $customers[3], product => $products[1], addtlinfo => "Sonderangebot")); push(@orders, Order->new(id => 12349, quantity => 2, customer => $customers[4], product => $products[2], addtlinfo => "Billig")); push(@orders, Order->new(id => 12350, quantity => 14, customer => $customers[5], product => $products[3], addtlinfo => "Lieferung")); print "\nMenge Produkt Name" . " " x 36 . " Preis(Euro) " . " " x 16 . "Info\n"; print "========================================================================================\n"; my $order; for $order (@orders) { print_formatted($order->{quantity}, $order->{id}, $order->{customer}->{name}, $order->{product}->{price}, $order->{addtlinfo} ); } print "\n"; my $customer; foreach $customer (@customers) { $customer->updateOrders(@orders); } my @o; my $i; foreach $customer (@customers) { print "\nCustomer '$customer->{name}' has the following orders:\n\n"; @o = $customer->getOrders(); foreach $i (@o) { print_formatted($i->{quantity}, $i->{id}, $i->{customer}->{name}, $i->{product}->{price}, $i->{addtlinfo}); } }