Skip to content
Snippets Groups Projects
Commit 9a689be0 authored by iliya's avatar iliya
Browse files

feat: ex2 serie2 finished

parent e0df49de
No related branches found
No related tags found
No related merge requests found
*.class
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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment