Advertisement
Samorokimetal

HW 29/10/20 - Person

Oct 29th, 2020
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Person {
  4.     private String name;
  5.     private int age;
  6.     private ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
  7.  
  8.     public Person(String name, int age) {
  9.         this.setName(name);
  10.         this.setAge(age);
  11.         this.setAccounts(accounts);
  12.     }
  13.  
  14.     public String getName() {
  15.         return name;
  16.     }
  17.  
  18.     public void setName(String name) {
  19.         if(name.length() > 2){
  20.             this.name = name;
  21.         }
  22.     }
  23.  
  24.     public int getAge() {
  25.         return age;
  26.     }
  27.  
  28.     public void setAge(int age) {
  29.         if(age > 0){
  30.             this.age = age;
  31.         }
  32.     }
  33.  
  34.  
  35.     public void AddBankAccount(BankAccount BankAccount){
  36.         this.accounts.add(BankAccount);
  37.     }
  38.  
  39.  
  40.     public ArrayList<BankAccount> getAccounts() {
  41.         return accounts;
  42.     }
  43.  
  44.     public void setAccounts(ArrayList<BankAccount> accounts) {
  45.         this.accounts = accounts;
  46.     }
  47.  
  48.     public double getBalance(){
  49.         double sum = 0;
  50.         for (int i = 0; i < this.accounts.size(); i++) {
  51.             sum += this.accounts.get(i).getBalance();
  52.         }
  53.         return sum;
  54.     }
  55.  
  56.    public BankAccount getAccountWithMostMoney(Person p1){
  57.         BankAccount accWithMostMoney = new BankAccount(p1.getAccounts().get(0).getID(), p1.getAccounts().get(0).getBalance());
  58.         for(int i = 1; i < p1.accounts.size(); i++) {
  59.             if(p1.getAccounts().get(i).getBalance() > accWithMostMoney.getBalance()) {
  60.                 accWithMostMoney = p1.getAccounts().get(i);
  61.             }
  62.         }
  63.         return accWithMostMoney;
  64.    }
  65.  
  66.     @Override
  67.     public String toString() {
  68.         return "[Name: " + this.name + " age: " + this.age + "]";
  69.     }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement