#!/usr/bin/perl use warnings; use strict; package Customer { sub new { my $classname = shift; my $self = {}; $self->{name} = shift; return bless($self, $classname); } } package Product { sub new { my $classname = shift; my $self = {}; $self->{price} = shift; return bless($self, $classname); } } package Order { sub new { my $classname = shift; my $self = {}; $self->{id} = shift; $self->{quantity} = shift; $self->{customer} = shift; $self->{product} = shift; $self->{addtlinfo} = shift; 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("Mueller")); push(@customers, Customer->new("Meier")); push(@customers, Customer->new("Schulze")); push(@customers, Customer->new("Schmidt")); push(@customers, Customer->new("Schulz")); push(@customers, Customer->new("Scholz")); my @products = (); push(@products, Product->new(5)); push(@products, Product->new(250.75)); push(@products, Product->new(3)); push(@products, Product->new(125)); push(@products, Product->new(25)); push(@products, Product->new(0.25)); my @orders = (); push(@orders, Order->new(12345, 10, $customers[2], $products[0], "Angebot")); push(@orders, Order->new(12346, 25, $customers[1], $products[4], "Aktion")); push(@orders, Order->new(12347, 75, $customers[0], $products[5], "Reduziert")); push(@orders, Order->new(12348, 100, $customers[3], $products[1], "Sonderangebot")); push(@orders, Order->new(12349, 2, $customers[4], $products[2], "Billig")); push(@orders, Order->new(12350, 14, $customers[5], $products[3], "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";