JAVA TP Banque
/***********************************************************************
* Module: Compte.java * Author: Propriétaire
* Purpose: Defines the Class Compte
***********************************************************************/
import java.util.*;
public class Compte{
private int numero;
private char typeCompte;
private double solde;
private int codeSecret;
public Compte(){
this.numero = 999999;
this.typeCompte = ' ';
this.solde = 0.00;
this.codeSecret = 0;
}
/** @param unNumero * @param unType * @param unSolde */
public Compte(int unNumero, char unType, double unSolde){
this.numero = unNumero;
if ((unType == 'E') || (unType == 'D')){
this.typeCompte = unType;
} else{
this.typeCompte = ' ';
this.solde = unSolde; } this.attribueCode();
}
/** @param unType */
public void setType(char unType){
if (unType == 'E' || unType == 'D'){
this.typeCompte = unType;
} else{
this.typeCompte = ' ';
} }
/** @param unNumero */
public void setNumero(int unNumero){
this.numero = unNumero;
}
public int getNumero(){
return this.numero;
}
public int getCodeSecret(){
return this.codeSecret;
}
public void attribueCode(){
this.codeSecret = (int)(Math.random()*9900+100);
}
public String toString(){
String retour = "Compte numero "+this.numero+ " de type ";
if (this.typeCompte == 'E') retour = retour + "Epargne";
else if (this.typeCompte == 'D') retour = retour + "Depot";
else retour = retour + "indetermine";
retour = retour + "\nLe solde est de " + this.solde + " euros";
retour = retour + "\nLe code secret est " + this.codeSecret+ "\n";
return retour;
}}
Page 1
public class TestCompte {
public static void main(String args[]){
Compte c1 = new Compte();
System.out.println(c1.toString());
Compte c2 = new Compte(123456 , 'E' , 3215.24);
System.out.println(c2.toString());
Compte c3 = new Compte(129856 , 'T' , 215.24);
System.out.println(c3.toString());
c1.setType('E');
System.out.println(c1.toString());
c1.setNumero(598000);
System.out.println(c1.toString());
System.out.println(c2.getNumero());
System.out.println(c2.getCodeSecret());
Compte c4 = new Compte();
c4.attribueCode();
System.out.println(c4.toString());
}}
Exercice 2 : Un tableau en java
import java.util.*;
public class TableauMontants {
public static void main(String args[]){
double[] tab;
tab = new double[] {10 , 687.35 , -54.00 , 0 , 54.85};
for (int i = 0 ; i < tab.length ; i++){
System.out.println(tab[i]);
} Arrays.sort(tab);
System.out.println(Arrays.toString(tab));
}}
Page 2
Exercice 3 : Un tableau d’opérations
/***********************************************************************
* Module: Operation.java * Author: Propriétaire
* Purpose: Defines the Class Operation
***********************************************************************/
import java.util.*;
public class Operation {
private String date;
private double montant;
public Operation(String uneDate, double unMontant) {
this.date = uneDate;
this.montant = unMontant;
}
public Operation() { this.montant = 0;
this.date = new Date().toString();
}
public String toString() {
return this.date + " : " + this.montant;
} }
import java.util.*;
public class UtiliseOperation {
public static void main(String args[]){
Operation[] lesOperations = new Operation[50];
int rep = 9;
double leMontant = 0;
String laDate;
do{
System.out.println("\n\n\n0. Quitter");
System.out.println("1. Voir les operations");
System.out.println("2. Ajouter une operation");
System.out.print("\n\tVotre choix:");
rep = Lire.lireInt();
if (rep == 1){
int i = 0;
while (i < lesOperations.length && lesOperations[i] != null){
System.out.println(lesOperations[i].toString());
i++;
} }
if (rep == 2){
int i = 0;
while (i < lesOperations.length && lesOperations[i] != null){
i++;
}
System.out.println("Saisie d une operation:");
System.out.println("\tSaisissez la date:");
laDate = Lire.lireStr();
System.out.println("\tSaisissez le montant:");
leMontant = Lire.lireDouble();
Operation o = new Operation(laDate , leMontant);
lesOperations[i] = o;
}}
while (rep != 0);
} }
Page 3