Commit 6c88515b authored by Killian LUCAS's avatar Killian LUCAS
Browse files

dates

parent 98cb4c82
package fr.unantes.software.construction;
import java.time.*
import fr.unantes.software.construction.associations.associations_bidirectionnelles.MultipleBidirectionnalReference;
import fr.unantes.software.construction.associations.associations_bidirectionnelles.SingleBidirectionnalReference;
......@@ -8,6 +10,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Vector;
import java.util.Iterator;
import static org.junit.jupiter.api.Assertions.*;
public class Account {
......@@ -19,18 +22,18 @@ public class Account {
private float overdraft;//decouvert autorise
/**
* Initializes the owner, amount, overdraft and the account number with parameters
* The method also initializes the history with a new empty Vector
* @param p: Client possedant le compte
* @param amount: Montant du compte
*
* @param p: Client possedant le compte
* @param amount: Montant du compte
* @param overdraft: Decouvert maximal autorise
* @param n: numero du compte
* @param type: type du compte(private ou company)
* @param n: numero du compte
* @param type: type du compte(private ou company)
*/
public Account(Client p, float amount, float overdraft, int n, String type) throws InvalidClassException {
assertTrue(amount >= 0); //amount ne peut pas etre negatif
assertTrue(amount >= 0); //amount ne peut pas etre negatif
assertTrue(overdraft >= 0); //overdraft ne peut pas etre negatif
this.balance = amount;
this.overdraft = overdraft;
......@@ -45,73 +48,71 @@ public class Account {
}
/**
*Change le type du compte
* Change le type du compte
*
* @param t: nouveau type du compte (private ou company)
*/
public void setType(String t ){
public void setType(String t) {
type = t;
}
/**
*
* @return le type du compte (private ou company)
*/
public String getType(){
public String getType() {
return type;
}
/**
*
* @return une reference vers l historiques des operations
*/
public MultipleBidirectionnalReferenceOperation getOperation(){
public MultipleBidirectionnalReferenceOperation getOperation() {
return historique_operations;
}
/**
* Adds the amount to the current balance
* Adds this operation in the history
* Adds the amount to the current balance
* Adds this operation in the history
*
* @param amount: montant a deposer
*/
public void deposit(float amount) {
assert amount > 0;
balance = balance + amount;
Operation o = new DepositOperation(amount, (int)System.currentTimeMillis());
Operation o = new DepositOperation(amount, LocalTime.now());
historique_operations.add(o);
}
/**
* If the amount is greater or equal to the balance, withdraws the amount
* If the amount is lower than the balance and greater than the overdraft,
* If the amount is lower than the balance and greater than the overdraft,
* withdraws the amount and throws an exception to warn that the balance is negative
* If the amount is lower than the overdraft,
*the amount is not withdrawn and exception is thrown to indicate that there is not enough credit.
* If the amount is lower than the overdraft,
* the amount is not withdrawn and exception is thrown to indicate that there is not enough credit.
*
* @param amount: montant a retirer
* @throws Exception: leve une exception si la balance est negative ou si l operation est impossible
*/
public void withdraw(float amount) throws Exception {
assert amount > 0;
float newBalance = balance - amount;
if (newBalance > 0){
if (newBalance > 0) {
balance = newBalance;
Operation o = new WithdrawOperation(amount, (int)System.currentTimeMillis());
Operation o = new WithdrawOperation(amount, LocalTime.now());
historique_operations.add(o);
}
else{
if ((Math.abs(newBalance)) <= overdraft){
} else {
if ((Math.abs(newBalance)) <= overdraft) {
balance = newBalance;
Operation o = new WithdrawOperation(amount,(int)System.currentTimeMillis());
Operation o = new WithdrawOperation(amount, LocalTime.now());
historique_operations.add(o);
throw new Exception("Warning: the balance is negative: "+balance);
}
else{
throw new Exception("Warning: the balance is negative: " + balance);
} else {
throw new Exception("Could not proceed withdrawal. Not enough credit.");
}
}
}
/**
*
* @return la valeur du decouvert
*/
public float getOverdraft() {
......@@ -120,6 +121,7 @@ public class Account {
/**
* Remplace la valeur du decouvert par la valeur du parametre
*
* @param overdraft: nouveau decouvert autorise
*/
public void setOverdraft(float overdraft) {
......@@ -127,7 +129,6 @@ public class Account {
}
/**
*
* @return la balance actuelle
*/
public float getBalance() {
......@@ -136,14 +137,14 @@ public class Account {
/**
* Remplace la valeur de la balance par la valeur du parametre
* @param balance: nouvelle balance
*
* @param balance: nouvelle balance
*/
void setBalance(float balance) {
this.balance = balance;
}
/**
*
* @return le numero du compte
*/
public int getNumber() {
......@@ -152,6 +153,7 @@ public class Account {
/**
* Remplace le numero du compte
*
* @param number: nouveau numero du compte
*/
public void setNumber(int number) {
......@@ -159,7 +161,6 @@ public class Account {
}
/**
*
* @return la reference vers le proprietaire du compte
*/
public SingleBidirectionnalReferenceClient getOwner() {
......@@ -169,34 +170,40 @@ public class Account {
/**
* Implemente l interface SingleBidirectionnalReference vers Client
*/
public class SingleBidirectionnalReferenceClient implements SingleBidirectionnalReference<Client>{
public class SingleBidirectionnalReferenceClient implements SingleBidirectionnalReference<Client> {
Client c;
public void set(Client newValue){
if(c != null){
public void set(Client newValue) {
if (c != null) {
c.getAccounts().basicRemove(Account.this);
}
newValue.getAccounts().basicAdd(Account.this);
c = newValue;
}
public void basicSet(Client newValue){
if(c != null){
public void basicSet(Client newValue) {
if (c != null) {
c.getAccounts().basicRemove(Account.this);
}
c = newValue;
}
public void unset(){
if(c != null){
public void unset() {
if (c != null) {
c.getAccounts().basicRemove(Account.this);
c = null;
}
}
public void basicUnset(){
public void basicUnset() {
c = null;
}
public boolean isSet(){
public boolean isSet() {
return c != null;
}
public Client get(){
public Client get() {
return c;
}
}
......@@ -204,43 +211,51 @@ public class Account {
/**
* Implemente l interface MultipleBidirectionnalReference vers Operation
*/
public class MultipleBidirectionnalReferenceOperation implements MultipleBidirectionnalReference<Operation>{
public class MultipleBidirectionnalReferenceOperation implements MultipleBidirectionnalReference<Operation> {
private ArrayList<Operation> operations = new ArrayList<Operation>();
public void add(Operation newValue){
public void add(Operation newValue) {
operations.add(newValue);
newValue.getAccount().basicSet(Account.this);
}
public void basicAdd(Operation newValue){
public void basicAdd(Operation newValue) {
operations.add(newValue);
}
public boolean remove(Operation val){
public boolean remove(Operation val) {
val.getAccount().basicUnset();
return operations.remove(val);
}
public boolean basicRemove(Operation val){
public boolean basicRemove(Operation val) {
return operations.remove(val);
}
public void clear(){
for(Operation o : operations){
public void clear() {
for (Operation o : operations) {
o.getAccount().basicUnset();
}
operations.clear();
}
public boolean contains(Operation val){
public boolean contains(Operation val) {
return operations.contains(val);
}
public int size(){
public int size() {
return operations.size();
}
public boolean isEmpty(){
public boolean isEmpty() {
return operations.isEmpty();
}
public Operation get(int i){
public Operation get(int i) {
return operations.get(i);
}
public ArrayList<Operation> get(){
public ArrayList<Operation> get() {
return operations;
}
}
......
......@@ -14,7 +14,7 @@ public class Bank {
private int accountNumbers;//nombre de comptes
/**
* this constructor initializes the attributes : accountNumbers is initially 0,
* this constructor initializes the attributes : accountNumbers is initially 0,
* accounts and clients and initialized with new empty Vectors
*/
public Bank() {
......@@ -26,7 +26,6 @@ public class Bank {
/**
*
* @return le nombre de compte
*/
public int getAccountNumbers() {
......@@ -35,6 +34,7 @@ public class Bank {
/**
* Modifie le nombre de compte
*
* @param accountNumbers: nouveau nombre de compte
*/
public void setAccountNumbers(int accountNumbers) {
......@@ -42,8 +42,6 @@ public class Bank {
}
/**
*
*
* @return la liste des clients
*/
public LinkedList getClients() {
......@@ -52,6 +50,7 @@ public class Bank {
/**
* Modifie la liste de clients
*
* @param value: nouvelle liste de clients
*/
public void setClients(LinkedList value) {
......@@ -59,7 +58,6 @@ public class Bank {
}
/**
*
* @return un iterateur sur la liste de clients
*/
public Iterator clientsIterator() {
......@@ -68,6 +66,7 @@ public class Bank {
/**
* Ajoute le client a la liste de clients
*
* @param element: nouveau client
* @return vrai si le client a ete ajoute, faux sinon
*/
......@@ -77,6 +76,7 @@ public class Bank {
/**
* Supprime le client de la liste
*
* @param element: client a supprime
* @return vrai si le client a ete supprime, faux sinon
*/
......@@ -85,7 +85,6 @@ public class Bank {
}
/**
*
* @return vrai si la liste de client est vide, faux sinon
*/
public boolean isClientsEmpty() {
......@@ -101,6 +100,7 @@ public class Bank {
/**
* Verifie la presence d un client
*
* @param element: client
* @return vrai si client est present dans la liste, faux sinon
*/
......@@ -110,6 +110,7 @@ public class Bank {
/**
* Verifie si la liste de clients est incluse dans la liste de clients de la banque
*
* @param elements: liste a tester
* @return vrai si la liste de clients est incluse dans la liste de clients de la banque, faux sinon
*/
......@@ -118,7 +119,6 @@ public class Bank {
}
/**
*
* @return le nombre de clients
*/
public int clientsSize() {
......@@ -127,6 +127,7 @@ public class Bank {
/**
* TODO: Methode est elle utile ?
*
* @return un tableau de la liste de clients
*/
public Client[] clientsToArray() {
......@@ -134,19 +135,20 @@ public class Bank {
}
/**
* Creates an account for the person named name.
* If no client has this name, a new client object is created and is added to the list of clients, then the account is created
* If the client exists the account is created, added to the bank's and the client's list of accounts
* @param name: nom du client
* @param amount: montant du compte
* Creates an account for the person named name.
* If no client has this name, a new client object is created and is added to the list of clients, then the account is created
* If the client exists the account is created, added to the bank's and the client's list of accounts
*
* @param name: nom du client
* @param amount: montant du compte
* @param overdraft: decouvert maximum
* @param type: type du compte (private ou company)
* @param type: type du compte (private ou company)
* @return le numero du compte
*/
public int addAccount(String name, float amount, float overdraft, String type){
Client p = getClient(name);
public int addAccount(String name, float amount, float overdraft, String type) {
Client p = getClient(name);
//if a client named name already exists in the bank's set of clients
if (p!=null){
if (p != null) {
Account a = null;
try {
a = new Account(p, amount, overdraft, accountNumbers, type);
......@@ -156,13 +158,11 @@ public class Bank {
p.getAccounts().add(a);
this.addAccounts(a);
}
else{
} else {
Client c = null;
if (type.equals("private")){
if (type.equals("private")) {
c = new ClientPrivate(name);
}
else if (type.equals("company")){
} else if (type.equals("company")) {
c = new ClientCompany(name);
}
assert c != null;
......@@ -181,119 +181,120 @@ public class Bank {
}
/**
* Closes the account number accountNumber.
* Closes the account number accountNumber.
* If the account exists, it is removed form the bank's list of accounts and from the owner's list of accounts.
* If the account does not exist, an Exception is thrown.
*
* @param accountNumber: numero du compte a supprimer
* @throws Exception: Impossible de supprimer un compte inexistant
*/
public void closeAccount(int accountNumber) throws Exception {
Account a = getAccount(accountNumber);
if (a!=null){
if (a != null) {
a.getOwner().unset();
this.removeAccounts(a);
}
else{
Exception error = new Exception("The account number "+accountNumber+" does not exist. Impossible to close this account");
} else {
Exception error = new Exception("The account number " + accountNumber + " does not exist. Impossible to close this account");
throw error;
}
}
/**
* Looks for a person named name in the set of clients.
* Looks for a person named name in the set of clients.
* Returns the Client object corresponding to the client if it exists
* Returns null if there is no client named name
*
* @param name: nom du client a trouver
* @return le client correspondant a name
*/
public Client getClient(String name) {
Iterator it = this.clientsIterator();
while (it.hasNext()){
Client p = (Client)it.next();
if(p.getName()==name){
while (it.hasNext()) {
Client p = (Client) it.next();
if (p.getName() == name) {
return p;
}
}
return null;
}
/**
* Looks for an account with the number accountNumber in the set of accounts
* Looks for an account with the number accountNumber in the set of accounts
* Returns the account if it exists
* Returns null if no account has the number accountNumber
* Returns null if no account has the number accountNumber
* The assumption is that there cannot be several accounts with the same number
*
* @param accountNumber: numero du compte a recuperer
* @return le compte correspondant a accoutNumber
*/
public Account getAccount(int accountNumber) {
Iterator it = this.accountsIterator();
while (it.hasNext()){
Account a = (Account)it.next();
if(a.getNumber()==accountNumber){
while (it.hasNext()) {
Account a = (Account) it.next();
if (a.getNumber() == accountNumber) {
return a;
}
}
return null;
}
/**
* Deposits the amount on the account number accountNumber
* Deposits the amount on the account number accountNumber
* Throws an exception if there is no account number accountNumber
*
* @param accountNumber: numero du compte ou deposer la somme
* @param amount: montant a deposer
* @param amount: montant a deposer
* @throws Exception: Si le compte est inexistant on ne peut pas y deposer de l argent
*/
public void deposit(int accountNumber, float amount) throws Exception {
assert amount > 0;
Account a = getAccount(accountNumber);
if (a!=null){
if (a != null) {
a.deposit(amount);
}
else{
Exception error = new Exception("The account number "+accountNumber+" does not exist. Impossible to deposit");
} else {
Exception error = new Exception("The account number " + accountNumber + " does not exist. Impossible to deposit");
throw error;
}
}
/**
* Withdraws the amount from the account number accountNumber
* Withdraws the amount from the account number accountNumber
* Throws an exception if there is no account number accountNumber
*
* @param accountNumber: numero du compte a debiter
* @param amount: montant a debiter
* @param amount: montant a debiter
* @throws Exception: Si le compte est inexistant on ne peut pas y retirer de l argent
*/
public void withdraw(int accountNumber, float amount) throws Exception {
assert amount > 0;
Account a = getAccount(accountNumber);
if (a!=null){
if (a != null) {
a.withdraw(amount);
}
else{
Exception error = new Exception("The account number "+accountNumber+" does not exist. Impossible to withdraw");
} else {
Exception error = new Exception("The account number " + accountNumber + " does not exist. Impossible to withdraw");
throw error;
}
}
/**
* Returns the collection of accounts of the client named name
* Returns the collection of accounts of the client named name
* If there is no client named name, the method returns null
*
* @param name: nom du client possedant les comptes a envoyer
* @return la liste des comptes du client
*/
public ArrayList<Account> getAccountsOfClient(String name) {
Client client = getClient(name);
if (client!=null){
if (client != null) {
return client.getAccounts().getAccounts();
}
else{
} else {
return null;
}
}
/**
*
* @return la liste des comptes
*/
public LinkedList getAccounts() {
......@@ -302,14 +303,14 @@ public class Bank {
/**
* Modifie la liste des comptes
* @param value: nouvelle liste de comptes
*
* @param value: nouvelle liste de comptes
*/
public void setAccounts(LinkedList value) {
accounts = value;
}
/**
*
* @return un iterateur sur la liste de comptes
*/
public Iterator accountsIterator() {
......@@ -318,6 +319,7 @@ public class Bank {
/**