diff --git a/serie2/ex2/.gitignore b/serie2/ex2/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..6b468b62a9884e67ca19b673f8e14e1931d65036 --- /dev/null +++ b/serie2/ex2/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/serie2/ex2/Account.java b/serie2/ex2/Account.java new file mode 100644 index 0000000000000000000000000000000000000000..f170ce100039fe345cf4b39953417c3467f2a946 --- /dev/null +++ b/serie2/ex2/Account.java @@ -0,0 +1,65 @@ +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()); + } +}