[JAVA] méthode de transfert

Fermé
John5 - 12 nov. 2007 à 00:13
kij_82 Messages postés 4089 Date d'inscription jeudi 7 avril 2005 Statut Contributeur Dernière intervention 30 septembre 2013 - 12 nov. 2007 à 14:05
Bonjour,

J'ai besoin d'aide pour ceproblme:

Écrire une classe Compte Bancaire. Les informations sur un compte sont : nom du
détenteur, numéro de compte, type du compte (chèque ou épargne), et solde. La classe
doit contenir :
- un constructeur ;
- une méthode pour faire un transfert entre deux comptes ;
-une méthode pour faire un dépôt ;
- une méthode pour faire un retrait ;
- une méthode pour connaître le solde ;
-une redéfinition des méthodes equals et compareTo. La comparaison est basée
sur le solde ;
- une méthode toString.
Le programme de test doit contenir un menu permettant de tester toutes les méthodes
de la classe.

aidez moi svp , je vois pas comment m'y prendre...
je me suis basé sur le programme suivant afin de le modifier:
(c'est pour les variables privées et les méthodes que je cherche)

public class Trans {
private double n1;
private int n2;
private string nom1, nom2

/**
* @param args
*/
public Trans(double n1, double n2, double n3) {
this.n1 = n1;
this.n2 = n2;
this.n3 = n3;
}

public double setdepot() {
return n1 + n2 + n3;
}

public double setretrait() {
return somme() / 3;

}

public double getsolde() {
return somme() / Math.sqrt(n1 * n2 * n3);
}

public double max() {
return Math.max(n1, Math.max(n2, n3));
}

public double min() {
return Math.min(n1, Math.min(n2, n3));
}

public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("Nombre réels entrés: ["+n1+", "+n2+", "+n3+"]").append("\n");
buffer.append("Somme = " + somme()).append("\n");
buffer.append("Moyenne = " + moyenne()).append("\n");
buffer.append("Ecart-type = " + ecartType()).append("\n");
buffer.append("Max = " + max()).append("\n");
buffer.append("Min = " + min()).append("\n");
return buffer.toString();
}

public static void main(String[] args) {
// TODO Auto-generated method stub

Trans stat = new Trans(1, 2, 3);
System.out.println("Somme = " + stat.somme());
System.out.println("Moyenne =" + stat.moyenne());
System.out.println("Ecart-type = " + stat.ecartType());
System.out.println("Max = " + stat.max());
System.out.println("Min = " + stat.min());
System.out.println("toString():");
System.out.println(stat.toString());
}
}
A voir également:

1 réponse

kij_82 Messages postés 4089 Date d'inscription jeudi 7 avril 2005 Statut Contributeur Dernière intervention 30 septembre 2013 857
12 nov. 2007 à 14:05
Salut,

Voilà pour ta classe :
public class Compte {

	/**************************/
	/** PROPERTIES           **/
	/**************************/
	
	/** Numéro du compte **/
	private int id			= 0;
	/** Nom du détenteur **/
	private String name		= null;
	/** Type du compte (i.e. chèque ou épargne) **/
	private String type		= null;
	/** Solde **/
	private double solde	= 0;
	

	/**************************/
	/** CONSTRUCTORS         **/
	/**************************/
	
	/**
	 * Compte
	 * Defaul class constructor
	 * @param theId int
	 * @param theName String
	 * @param theType String
	 */
	public Compte ( int theId, String theName, String theType ){
		super();
		setId(theId);
		setName(theName);
		setType(theType);
		// --- Set default value for property 'solde'
		setSolde(0);
	}
	
	/**************************/
	/** METHODS              **/
	/**************************/
	
	/**
	 * depot
	 * Increase current balance with new amount
	 * @param theAmount double
	 * @return boolean
	 */
	public boolean depot ( double theAmount ){
		
		// --- Test if the amount is a real value and has not a negative value
		if ( ! Double.isNaN(theAmount) && theAmount > 0 ){
			// --- Increase current balance with the amount
			setSolde( getSolde() + theAmount );
			// --- Return TRUE => operation successfull
			return true;
		}
		
		// --- Return FALSE => operation unsuccessfull
		return false;
	}
	
	/**
	 * retrait
	 * Withdraw money from current account
	 * @param theAmount double
	 * @return boolean
	 */
	public boolean retrait ( double theAmount ){
		
		// --- Test if the amount is a real value and has not a negative value
		// --- Test if (current balance - amount) is still positive !!
		if ( ! Double.isNaN(theAmount) && theAmount > 0 && (getSolde()-theAmount) > 0 ){
			// --- Do the treatment
			setSolde( getSolde() - theAmount );
			// --- Return TRUE => operation successfull
		}
		
		// --- Return FALSE => operation unsuccessfull
		return false;
	}
	
	/**
	 * toString
	 * Get a string description of the account
	 * @return String
	 */
	public String toString ( ){
		return String.valueOf(getId()).concat(" - ").concat(getType()).concat(" - ").concat(getName()).concat(" - Montant : ").concat(String.valueOf(getSolde()));
	}
	
	/**
	 * transfert
	 * Transfert the amount from theAccount to current account
	 * @param TheAccountFrom Compte
	 * @param theAmount double
	 * @return boolean
	 */
	public boolean transfert ( Compte TheAccountFrom, double theAmount ){
		
		// --- Do the transfert only if the account from exists (not null) and if it contain the amount to decrease and add to current account
		if ( TheAccountFrom != null && TheAccountFrom.retrait(theAmount) ){
			// --- Add the amount to current account
			depot(theAmount);
			// --- Return TRUE => operation successfull
			return true;
		}
		
		// --- Return FALSE => operation unsuccessfull
		return false;
	}
	
	/**
	 * equals
	 * Check if current balance is equals to the amount
	 * @param theAmount double
	 * @return boolean
	 */
	public boolean equals ( double theAmount ){
		return theAmount == getSolde();
	}
	
	/**
	 * compareTo
	 * Compare current balance with the amount
	 * @param theAmount double
	 * @return int
	 */
	public int compareTo ( double theAmount ){
		if ( getSolde() > theAmount )
			return 1;
		else if ( getSolde() == theAmount )
			return 0;
		else 
			return -1;
		
	}
	
	/**************************/
	/** GETTERS & SETTERS    **/
	/**************************/
	
	/**
	 * setId
	 * Set id value
	 * @param theId int
	 */
	public void setId ( int theId ){
		this.id = theId;
	}
	
	/**
	 * getId
	 * Get id value
	 * @return int
	 */
	public int getId ( ){
		return this.id;
	}
	
	/**
	 * setName
	 * Set name value
	 * @param theName String
	 */
	public void setName ( String theName ){
		this.name = theName;
	}
	
	/**
	 * getName
	 * Get name value
	 * @return String
	 */
	public String getName ( ){
		return this.name;
	}
	
	/**
	 * setSolde
	 * Set solde value
	 * @param theSolde String
	 */
	private void setSolde ( double theSolde ){
		this.solde = theSolde;
	}
	
	/**
	 * getSolde
	 * Get solde value
	 * @return double
	 */
	public double getSolde ( ){
		return this.solde;
	}
	
	/**
	 * setType
	 * Set type value
	 * @param theType String
	 */
	public void setType ( String theType ){
		this.type = theType;
	}
	
	/**
	 * getType
	 * Get type value
	 * @return String
	 */
	public String getType ( ){
		return this.type;
	}

}


- une méthode pour faire un transfert entre deux comptes => transfert()
-une méthode pour faire un dépôt => depot()
- une méthode pour faire un retrait => retrait()
- une méthode pour connaître le solde => getSolde()
-une redéfinition des méthodes equals et compareTo. => equals() et compareTo()
- une méthode toString => toString()

Il ne te reste plus qu'à faire une méthode main permettant de présenter le fameux menu pour tester les différentes opérations.
0