forked from unibo-pps/pps-21-22-lab01
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
300 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
50 changes: 25 additions & 25 deletions
50
pps-lab01-intellij-basic-example/src/lab01/example/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
package lab01.example; | ||
|
||
import lab01.example.model.AccountHolder; | ||
import lab01.example.model.BankAccount; | ||
import lab01.example.model.SimpleBankAccount; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
final AccountHolder accountHolder = new AccountHolder("Mario", "Rossi", 1); | ||
final BankAccount bankAccount = new SimpleBankAccount(accountHolder, 0); | ||
|
||
bankAccount.deposit(accountHolder.getId(), 100); | ||
|
||
System.out.println("Current balance is " + bankAccount.getBalance()); | ||
|
||
bankAccount.withdraw(accountHolder.getId(), 30); | ||
|
||
System.out.println("Current balance is " + bankAccount.getBalance()); | ||
|
||
bankAccount.withdraw(accountHolder.getId(), 80); | ||
|
||
System.out.println("Current balance is " + bankAccount.getBalance()); | ||
} | ||
} | ||
package lab01.example; | ||
|
||
import lab01.example.model.AccountHolder; | ||
import lab01.example.model.BankAccount; | ||
import lab01.example.model.SimpleBankAccount; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
final AccountHolder accountHolder = new AccountHolder("Mario", "Rossi", 1); | ||
final BankAccount bankAccount = new SimpleBankAccount(accountHolder, 0); | ||
|
||
bankAccount.deposit(accountHolder.getId(), 100); | ||
|
||
System.out.println("Current balance is " + bankAccount.getBalance()); | ||
|
||
bankAccount.withdraw(accountHolder.getId(), 30); | ||
|
||
System.out.println("Current balance is " + bankAccount.getBalance()); | ||
|
||
bankAccount.withdraw(accountHolder.getId(), 80); | ||
|
||
System.out.println("Current balance is " + bankAccount.getBalance()); | ||
} | ||
} |
102 changes: 51 additions & 51 deletions
102
pps-lab01-intellij-basic-example/src/lab01/example/model/AccountHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,51 @@ | ||
package lab01.example.model; | ||
|
||
/** | ||
* This class represents the account holder concept. | ||
* That is: a person that can subscribe a bank account. | ||
* | ||
* Each account holder has a name, a surname and an ID (unique in the bank system) | ||
*/ | ||
public class AccountHolder { | ||
private final int id; | ||
private final String name; | ||
private final String surname; | ||
|
||
public AccountHolder(final String name, final String surname, final int id) { | ||
this.name = name; | ||
this.surname = surname; | ||
this.id = id; | ||
} | ||
|
||
/** | ||
* Retrieve the name of the the person registered as possible account holder | ||
* @return the name of the holder | ||
*/ | ||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
/** | ||
* Retrieve the surname of the person registered as possible account holder | ||
* @return the surname of the holder | ||
*/ | ||
public String getSurname() { | ||
return this.surname; | ||
} | ||
|
||
/** | ||
* Retrieve the ID of the person registered as possible account holder | ||
* @return the id of the holder | ||
*/ | ||
public int getId() { | ||
return this.id; | ||
} | ||
|
||
/** | ||
* Provides a string representation | ||
* @return the string representation for an AccountHolder instance | ||
*/ | ||
public String toString() { | ||
return String.format("[ID = %d] %s %s", this.id, this.name, this.surname); | ||
} | ||
} | ||
package lab01.example.model; | ||
|
||
/** | ||
* This class represents the account holder concept. | ||
* That is: a person that can subscribe a bank account. | ||
* | ||
* Each account holder has a name, a surname and an ID (unique in the bank system) | ||
*/ | ||
public class AccountHolder { | ||
private final int id; | ||
private final String name; | ||
private final String surname; | ||
|
||
public AccountHolder(final String name, final String surname, final int id) { | ||
this.name = name; | ||
this.surname = surname; | ||
this.id = id; | ||
} | ||
|
||
/** | ||
* Retrieve the name of the the person registered as possible account holder | ||
* @return the name of the holder | ||
*/ | ||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
/** | ||
* Retrieve the surname of the person registered as possible account holder | ||
* @return the surname of the holder | ||
*/ | ||
public String getSurname() { | ||
return this.surname; | ||
} | ||
|
||
/** | ||
* Retrieve the ID of the person registered as possible account holder | ||
* @return the id of the holder | ||
*/ | ||
public int getId() { | ||
return this.id; | ||
} | ||
|
||
/** | ||
* Provides a string representation | ||
* @return the string representation for an AccountHolder instance | ||
*/ | ||
public String toString() { | ||
return String.format("[ID = %d] %s %s", this.id, this.name, this.surname); | ||
} | ||
} |
74 changes: 37 additions & 37 deletions
74
pps-lab01-intellij-basic-example/src/lab01/example/model/BankAccount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,37 @@ | ||
package lab01.example.model; | ||
|
||
/** | ||
* This interface defines the concept of a very basic bank account. | ||
*/ | ||
public interface BankAccount { | ||
|
||
/** | ||
* Allows to know who is the holder of this bank account | ||
* @return the AccountHolder instance related to this bank account. | ||
*/ | ||
AccountHolder getHolder(); | ||
|
||
/** | ||
* Returns the current balance of the bank account | ||
* @return the current balance | ||
*/ | ||
double getBalance(); | ||
|
||
/** | ||
* Allows the deposit of an amount on the account, if the given usrID corresponds to the register holder ID | ||
* of the bank account. This ID acts like an "identification token" . | ||
* | ||
* @param usrID the id of the user that wants do the deposit | ||
* @param amount the amount of the deposit | ||
*/ | ||
void deposit(int usrID, double amount); | ||
|
||
/** | ||
* Allows the withdraw of an amount from the account, if the given usrID corresponds to the register holder ID | ||
* of the bank account. This ID acts like an "identification token" . | ||
* | ||
* @param usrID the id of the user that wants do the withdraw | ||
* @param amount the amount of the withdraw | ||
*/ | ||
void withdraw(int usrID, double amount); | ||
} | ||
package lab01.example.model; | ||
|
||
/** | ||
* This interface defines the concept of a very basic bank account. | ||
*/ | ||
public interface BankAccount { | ||
|
||
/** | ||
* Allows to know who is the holder of this bank account | ||
* @return the AccountHolder instance related to this bank account. | ||
*/ | ||
AccountHolder getHolder(); | ||
|
||
/** | ||
* Returns the current balance of the bank account | ||
* @return the current balance | ||
*/ | ||
double getBalance(); | ||
|
||
/** | ||
* Allows the deposit of an amount on the account, if the given usrID corresponds to the register holder ID | ||
* of the bank account. This ID acts like an "identification token" . | ||
* | ||
* @param usrID the id of the user that wants do the deposit | ||
* @param amount the amount of the deposit | ||
*/ | ||
void deposit(int usrID, double amount); | ||
|
||
/** | ||
* Allows the withdraw of an amount from the account, if the given usrID corresponds to the register holder ID | ||
* of the bank account. This ID acts like an "identification token" . | ||
* | ||
* @param usrID the id of the user that wants do the withdraw | ||
* @param amount the amount of the withdraw | ||
*/ | ||
void withdraw(int usrID, double amount); | ||
} |
96 changes: 48 additions & 48 deletions
96
pps-lab01-intellij-basic-example/src/lab01/example/model/SimpleBankAccount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
package lab01.example.model; | ||
|
||
/** | ||
* This class represent a particular instance of a BankAccount. | ||
* In particular, a Simple Bank Account allows always the deposit | ||
* while the withdraw is allowed only if the balance greater or equal the withdrawal amount | ||
*/ | ||
public class SimpleBankAccount implements BankAccount { | ||
|
||
private double balance; | ||
private final AccountHolder holder; | ||
|
||
public SimpleBankAccount(final AccountHolder holder, final double balance) { | ||
this.holder = holder; | ||
this.balance = balance; | ||
} | ||
@Override | ||
public AccountHolder getHolder(){ | ||
return this.holder; | ||
} | ||
|
||
@Override | ||
public double getBalance() { | ||
return this.balance; | ||
} | ||
|
||
@Override | ||
public void deposit(final int usrID, final double amount) { | ||
if (checkUser(usrID)) { | ||
this.balance += amount; | ||
} | ||
} | ||
|
||
@Override | ||
public void withdraw(final int usrID, final double amount) { | ||
if (checkUser(usrID) && isWithdrawAllowed(amount)) { | ||
this.balance -= amount; | ||
} | ||
} | ||
|
||
private boolean isWithdrawAllowed(final double amount){ | ||
return this.balance >= amount; | ||
} | ||
|
||
private boolean checkUser(final int id) { | ||
return this.holder.getId() == id; | ||
} | ||
} | ||
package lab01.example.model; | ||
|
||
/** | ||
* This class represent a particular instance of a BankAccount. | ||
* In particular, a Simple Bank Account allows always the deposit | ||
* while the withdraw is allowed only if the balance greater or equal the withdrawal amount | ||
*/ | ||
public class SimpleBankAccount implements BankAccount { | ||
|
||
private double balance; | ||
private final AccountHolder holder; | ||
|
||
public SimpleBankAccount(final AccountHolder holder, final double balance) { | ||
this.holder = holder; | ||
this.balance = balance; | ||
} | ||
@Override | ||
public AccountHolder getHolder(){ | ||
return this.holder; | ||
} | ||
|
||
@Override | ||
public double getBalance() { | ||
return this.balance; | ||
} | ||
|
||
@Override | ||
public void deposit(final int usrID, final double amount) { | ||
if (checkUser(usrID)) { | ||
this.balance += amount; | ||
} | ||
} | ||
|
||
@Override | ||
public void withdraw(final int usrID, final double amount) { | ||
if (checkUser(usrID) && isWithdrawAllowed(amount)) { | ||
this.balance -= amount; | ||
} | ||
} | ||
|
||
private boolean isWithdrawAllowed(final double amount){ | ||
return this.balance >= amount; | ||
} | ||
|
||
private boolean checkUser(final int id) { | ||
return this.holder.getId() == id; | ||
} | ||
} |
Oops, something went wrong.