De l'aide pour Vector

tocotodo Messages postés 1 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 15 mars 2004 - 15 mars 2004 à 20:19
dmaillet Messages postés 500 Date d'inscription mercredi 20 août 2003 Statut Membre Dernière intervention 11 juillet 2007 - 16 mars 2004 à 08:54
Bonjour
je une class Radio.java et autre TestRadio.
pour mon vector , toutes champs d'information donc brandName, model , option et prix, je les optien d'un fichier externe.
tout va bien jusque la mais le probleme c,est qu'il faut que j'ai cree une fonction qui puise modifier les object du vector en question.

VOICI MON CODE POUR RADIO.JAVA:

import java.io.*;
import java.text.DecimalFormat; // format des réels
import java.util.Vector;
import java.lang.Double;
public class Radio
{
private String brandName;
private double model, prix;
private String option; private boolean bonBrandName false, bonModel false;

public Radio(String brandName, double model, String option, double prix) {
this.brandName = brandName;
this.model = model;
this.option = option;
this.prix = prix;
}

// constructeur spécial pour la recherche (selon nom de la marque et le model ):
public Radio(String brandName, double model) {
this.brandName = brandName;
this.model = model;
}

public Radio(String brandName) {
this.brandName = brandName;
}

// quelques méthodes d'accès
public String getBrandName() {
return brandName;
}

public double getModel() {
return model;
}

public String getOption() {
return option;
}

public double getPrix() {
return prix;
}

// quelques modificateur
public Object changeBrandName(String unBrandName) { for (int i 0, nbRadio TestRadio.radio.size(); i < nbRadio - 1; i++) {
if (TestRadio.radio.elementAt(i).equals(unBrandName))
brandName = unBrandName;

}
return brandName;
}

public void changeModel(Object unModel) { for (int i 0, nbRadio TestRadio.radio.size(); i < nbRadio - 1; i++) {
if (TestRadio.radio.elementAt(i).equals(unModel))
model = unModel;
}

}

public void changeOption(String unOption) { for (int i 0, nbRadio TestRadio.radio.size(); i < nbRadio - 1; i++) {
if (TestRadio.radio.elementAt(i).equals(unOption))
option = unOption;
}

}
/**** *********fonction avec un bog ***************/
public void changePrix(Object unPrix) { for (int i 0, nbRadio TestRadio.radio.size(); i < nbRadio - 1; i++) {
if (TestRadio.radio.elementAt(i).equals(unPrix))
TestRadio.radio.Object(prix) = unPrix;
}

}

public void afficher(String message) {
//System.out.println();
//System.out.print(message);
System.out.print(brandName);
DecimalFormat fmt = new DecimalFormat(" 0000 ");
System.out.print(fmt.format(model));
System.out.print(option);
fmt = new DecimalFormat(" 0.00 ");
System.out.println(fmt.format(prix));

}

/* redéfinition de la méthode equals */

public boolean equals(Object autre) {
boolean r;
if ( (autre instanceof String))
return ( (String) autre).compareTo(brandName.trim()) == 0;

if (this == autre) // réflexitivité (comparer à soi-même)
if (! (autre instanceof Radio))
return false;
if (this == autre) // réflexitivité (comparer à soi-même)
return true;
Radio aut = (Radio) autre;

/* ici on compare un marque et un model à un autre*/

bonBrandName = brandName.trim().equals(aut.brandName.trim());

if (model == aut.model)
bonModel = true;

if (bonBrandName == bonModel)
r = true;
else
r = false;

return r;

}
}

ET MON CODE POUR TESTRADIO.JAVA:


public class TestRadio
{
static Vector lireCreer(String nomFichier) throws IOException {
Vector v = new Vector();
boolean existeFichier = true; // à ajuster après
FileReader fr = null; // initialiser pour Java
// essayer de LOCALISER le fichier à partir de son nom
try {
fr = new FileReader(nomFichier);
}
// intercepter l'erreur si le fichier n'existe pas
catch (java.io.FileNotFoundException erreur) {
System.out.println("Probleme d'ouvrir le fichier " +
nomFichier);
existeFichier = false; // ajuster
}

if (existeFichier) {
// construire l'objet d'entrée qui va permettre
// d'appliquer la lecture d'une ligne de texte
BufferedReader entree = new BufferedReader(fr);
boolean finFichier = false;
final int COL1 = 9, // longueur de la marque
COL2 = 14, // modele
COL3 = 18, // options
COL4 = 24; // prix
while (!finFichier) {
// lire une ligne
String uneLigne = entree.readLine();
if (uneLigne == null)
finFichier = true;
else {
// extraire + convertir pour des champs d'information
String unMarque = uneLigne.substring(0, COL1);
int unModel = (new Integer(uneLigne.substring(COL1,
COL2).trim())).intValue();

String unOption = uneLigne.substring(COL2, COL3).trim();

double unPrix = (new Double(uneLigne.substring(COL3,
COL4).trim())).doubleValue();

// construire un élément du tableau (une radio)
v.addElement(new Radio(unMarque, unModel, unOption, unPrix));
}
}
entree.close();
}

return v;
}

static void permuter(Vector v, int i, int indMin) {
Object tempo = v.elementAt(i);
v.setElementAt(v.elementAt(indMin), i);
v.setElementAt(tempo, indMin);
}

//le trie se fait en ordre croissante du prix
static void trier(Vector radio) {
for (int i 0, nbRadio radio.size(); i < nbRadio - 1; i++) {
int indMin = i;
for (int j = i + 1; j < nbRadio; j++)
if ( ( (Radio) radio.elementAt(j)).getPrix() <=
( ( (Radio) radio.elementAt(indMin)).getPrix()))
indMin = j;
if (indMin != i)
permuter(radio, i, indMin);

}
}

public static Vector radio = new Vector();

//modificateur de prix
public void changer(String brandName, double pourcent) {
double newPrix = 0.0;
for (int i = 0; i < radio.size(); i++) {
if (radio.elementAt(i).equals(brandName))
newPrix = ( ( (Radio) radio.elementAt(i)).getPrix());

newPrix = (newPrix + pourcent);

}

}

public static void main(String[] args) throws IOException {
radio = lireCreer("C:\\radio.txt");

trier(radio);

for (int i = 0; i < radio.size(); i++)
( (Radio) radio.elementAt(i)).afficher("");

System.out.println("\n Fin du tri ");

System.out.println("Voici toutes les marques de radios Sony et koss :\n");
for (int i = 0; i < radio.size(); i++) {

if ( ( (Radio) radio.elementAt(i)).equals("Sony") == true)
( (Radio) radio.elementAt(i)).afficher("");

if ( ( (Radio) radio.elementAt(i)).equals("Koss") == true)
( (Radio) radio.elementAt(i)).afficher("");

}
/******* FONCTION A FINIR *************/
/* System.out.println("Modification du prix: Koss: + 15% Sony - 7% \n");
for (int i = 0; i < radio.size(); i++) {
if ( ( (Radio) radio.elementAt(i)).equals("Sony") == true)
if ( ( (Radio) radio.elementAt(i)).equals("Koss") == true)
( (Radio) radio.elementAt(i)).afficher("");*/

}
System.out.println("Fin\n");

}
}

1 réponse

dmaillet Messages postés 500 Date d'inscription mercredi 20 août 2003 Statut Membre Dernière intervention 11 juillet 2007
16 mars 2004 à 08:54
Attends, tu veux juste modifier les objets en question?
Alors tu fais un:

NomClasse objettruc = ((NomClasse)tonVector.get(l'indice de ton objet dans le vector))
et ensuiite tu modifies objettruc, c'est tout...
Tu a s le pointeur sur l'objet.....
0
Rejoignez-nous