Skip to content
Snippets Groups Projects
Select Git revision
  • 9a689be06eaf98a439de26018dab9b783cc3e91e
  • main default protected
2 results

Account.java

Blame
  • Account.java 1.64 KiB
    public class Account {
    
        private String owner;
        private int amount;
    
        public String getOwner() {
            return owner;
        }
    
        public int getAmount() {
            return amount;
        }
    
        public Account(String owner) {
            this.owner = owner;
        }
    
        public Account(String owner, int amount) {
            this(owner);
            amountAuthorizedCreation(amount);
            this.amount = amount;
        }
    
        public static void amountAuthorizedCreation(int amount) {
            if (amount < 0) {
                throw new RuntimeException("Amount has to be greater than or equal to 0.-");
            }
        }
    
        public static void amountAuthorizedOperations(int amount) {
            if (amount <= -2000) {
                throw new RuntimeException("Amount has to be greater than or equal to -2000.-");
            }
        }
    
        public void withdraw(int amountToRetrieve) {
            amountAuthorizedOperations(this.amount - amountToRetrieve);
            this.amount -= amountToRetrieve;
        }
    
        public void deposit(int addAmount) {
            amountAuthorizedOperations(this.amount + addAmount);
            this.amount += addAmount;
        }
    
        public void transfer(int amount, Account dst) {
            dst.deposit(amount);
            withdraw(amount);
        }
    
        @Override
        public boolean equals(Object obj) {
            return (obj instanceof Account) && (((Account) obj).getOwner() == this.owner)
                    && (((Account) obj).getAmount() == this.amount);
        }
    
        public static void main(String[] args) {
            Account p1 = new Account("Person 1", 100);
            Account p2 = new Account("Person 2", 100);
    
            p1.transfer(2101, p2);
    
            System.out.println(p1.getAmount());
        }
    }