package Bank;

# Constructor
sub new {
  my $class = shift;
  my $name = shift;
  my $self = {
     name => $name,
     accountBase => {},
     nextAccountNumber => 1000,
  };
  bless($self, $class);
  return $self;
}

# Create new account
sub newAccount {
  my $self = shift;
  my $name = shift;
  my $curN = $self->{nextAccountNumber};
  # Add to hash
  $self->{accountBase}->{$curN} = BankAccount->new($name,$curN);

  $self->{nextAccountNumber}++;   # Increment num
  return $curN;                   # return num  
}

# Retrieve balance
sub getBalance {
  my $self = shift;
  my $num = shift;
  return $self->{accountBase}->{$num}->getBalance();
}

# Add to balance
sub deposit {
  my $self = shift;
  my ($num,$amnt) = @_;
  $self->{accountBase}->{$num}->{balance} += $amnt;
}

# Deduct from balance
sub withdraw {
  my $self = shift;
  my ($num,$amnt) = @_;
  $self->{accountBase}->{$num}->{balance} -= $amnt;
}

# Apply interest to all accounts 
sub applyInterests {
  my $self = shift;
  my $curN = $self->{nextAccountNumber}-1;
  while ($curN > 999) {
    my $amnt = $self->getBalance($curN);
    $self->{accountBase}->{$curN}->deposit($amnt*0.01);
    $curN--;
  }
}

# List all accounts
sub listAccounts {
  my $self = shift;
  my $curN = $self->{nextAccountNumber}-1;
  while ($curN > 999) {
    $self->{accountBase}->{$curN}->printAccountDetails();
    $curN--;
  }
}

1;

























