problem with my programme

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manjava
    New Member
    • Sep 2009
    • 132

    problem with my programme

    Hello ,

    please can any one help me for my this i don't know why didn't gave me my return true he juste return this :les magasins situés dans la ville projetd314.List eMagasin@a401c2
    why see my code
    Code:
    public interface IMagasin {
    public double getLatitude();
    public double getLongitude();
    public String getCode();
    public String getVille();
    public void setLatitude(double l);
    public void setLongitude(double lon);
    public void setCode(String string);
    public void setVille(String string);
    }
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package projetd314;
    import java.sql.*;
    import java.sql.PreparedStatement;
    import java.lang.Math.*;
    import java.util.Vector;
    
    
    
    
    
    
    public class Magasin {
        static java.sql.Connection c=null;
    double latitude=0;
    double longitude=0;
    String codeville="";
    String ville="";
    public double getLatitude(){
        return latitude;
    }
    public double getLongitude(){
            return longitude;
            }
    public String getCode(){
        return codeville;
    }
    public String getVille(){
        return ville;
    }
    public void setLatitude(double l){
        latitude=l;
    }
    public void setLongitude(double lon){
        longitude=lon;
    }
    public void setCode(String string){
        codeville=string;
    
    }
    public void setVille(String string){
        ville=string;
    }
    
    
        }
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package projetd314;
    import projetd314.IMagasin;
    import projetd314.ListeMagasin;
    import projetd314.ListeMagasinAdapter;
    import projetd314.Magasin;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Vector;
    
    
    
    
    public class MagasinServices {
        public  ListeMagasin listeMagasin=new ListeMagasin();
        static java.sql.Connection c=null;
    	public static void etablirConnexion(){
    		String pilote="com.mysql.jdbc.Driver";
    		try{Class.forName(pilote).newInstance();}
    		catch(Exception e){System.out.println("echec pilote :"+e);
    		}
    		String url="jdbc:mysql://localhost:3306/boutiquegeographique";String user="root";String password="";
    		try{c=DriverManager.getConnection(url,user,password);
    
            }
    		catch(SQLException e){System.out.println("echec connection a la bdd :"+e);}
    
    	}
    
            public  ListeMagasin listMagasinsVille(String nomville)
            {
          	           double latitude;
                       double l;
                    if(Magasin.c==null) etablirConnexion();
          try{
                            PreparedStatement st=c.prepareStatement("select latitude,longitude,nomville from liste_magasins where nomville=?");
                            st.setString(1, nomville);
                            ResultSet rs=st.executeQuery();
    
                    while(rs.next())
                    {
                            Magasin m=new Magasin();
                            m.setLatitude(rs.getDouble("latitude"));
                            m.setLongitude(rs.getDouble("longitude"));
                            m.setVille(rs.getString("nomville"));
                            listeMagasin.addMagasin(m);
                   }
              }catch(Exception e){System.out.println("erreur selection"+e);}
               return  listeMagasin;
        }
    
    
    
    }
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package projetd314;
    
    import java.util.Vector;
    
    /**
     *
     * @author kamar
     */
    public class ListeMagasinAdapter {
    public IMagasin[] liste;
    public ListeMagasinAdapter(ListeMagasin obj){
       Vector<IMagasin>  l=obj.getListe();
       liste =l.toArray(new IMagasin[l.size()]);
    }
    public IMagasin[] getListe(){
        return liste;
    }
    public void setListe(IMagasin[] liste){
        this.liste=liste;
    }
    }
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package projetd314;
    
    import java.util.Iterator;
    import java.util.Vector;
    
    
    public class ListeMagasin {
        private Vector liste=new Vector();
        public Magasin getMagasin(int i){
            return (Magasin)liste.get(i);
        }
    
        public Vector getListe(){
            return liste;
        }
        public void setListe(Vector liste){
            this.liste=liste;
    
        }
        public void addMagasin(Magasin m){
            int i;
            for(i=0;i<liste.size();i++)
            {
                liste.add(m);
                    return;
                
            }
            liste.add(m);
        }
        public String listMagasinsVille(String nomville){
    
        Iterator  iterateur=liste.iterator();
        String str="";
        while(iterateur.hasNext())
        {
            Magasin ma=(Magasin)iterateur.next();
            String v=ma.getVille();
            if(nomville==v){
                double lat=ma.getLatitude();
                double lon=ma.getLongitude();
                str+="Latitude:"+lat+"Longitude:"+lon+"\n";}
        }
        return str;
        }
    
    }
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package projetd314;
    
    
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            MagasinServices c=new MagasinServices();
           System.out.println("les magasins situés dans la ville "+c.listMagasinsVille("Amiens"));
        }
    
    }
    thanks
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    That's a lot of code for a small question!

    Anyway, if you want a ListeMagasin instance to print nicely you need to give the class a toString() method. println() will use this to create the string to be printed.

    Code:
    public class ListeMagasin {
    
        // other stuff
    
        public String toString() {
            // use a StringBuilder and loop over the Vector contents?
            // or use the vector toString() method
            // or etc
            return "some string that describes the magasin list";
        }
    }
    Why are using Vector rather than one of the other list implementations?

    Comment

    • manjava
      New Member
      • Sep 2009
      • 132

      #3
      hello,

      i want to call my method listMagasinsVil le(String nomville) but the return this les magasins situés dans la ville projetd314.List eMagasin@c51355

      why please help me

      thanks

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Please re-read the suggestion that pbrockway2 posted.

        You need to write a ToString() method for your ListeMagasin class. If you do not write a ToString() method for the class then the default ToString method is used. The Default ToString method returns you "projetd314.Lis teMagasin@c5135 5" which doesn't make much sense.

        Code:
        public class ListeMagasin {
        //....Other Stuff .....//
        public String ToString(){
           Iterator  iterateur=liste.iterator();
           String str="";
           while(iterateur.hasNext())
           {
                 Magasin ma=(Magasin)iterateur.next();
                 String v=ma.getVille();
                 if(nomville==v){
                     double lat=ma.getLatitude();
                     double lon=ma.getLongitude();
                     str+="Latitude:"+lat.ToString()+" Longitude:"+lon.ToString()+"\n";}
           }
           return str;
        }
        Then call the ToString Method:
        Code:
         
        public class Main {
         
            /**
             * @param args the command line arguments
             */
            public static void main(String[] args) {
                MagasinServices c=new MagasinServices();
               System.out.println("les magasins situés dans la ville "+c.listMagasinsVille("Amiens").ToString());
            }
         
        }

        Comment

        • manjava
          New Member
          • Sep 2009
          • 132

          #5
          i put my method listMagasinsVil le in class and ADD the method ToString but there somme body
          Code:
          { Iterator  iterateur=liste.iterator();
              String str="";
              while(iterateur.hasNext())
              {
                  Magasin ma=(Magasin)iterateur.next();
                  String v=ma.getVille();
                  if(nomville==v){
                      double lat=ma.getLatitude();
                      double lon=ma.getLongitude();
                      str+="Latitude:"+lat+"Longitude:"+lon+"\n";}
              }
              return str;
              }
          CAN YOU TELL ME WHAT I PUT IN MY CLASS listmagasin

          thank you for help

          Comment

          • pbrockway2
            Recognized Expert New Member
            • Nov 2007
            • 151

            #6
            But note that toString() should start with a lower case "t". And the methods cannot be applied to primitive values. So one possible way of building up the string could be:

            Code:
            //str+="Latitude:"+lat.ToString()+" Longitude:"+lon.ToString()+"\n";
            str += "Latitude: "  + lat + " Longitude: " + lon + "\n";
            (as I think you had)

            Also note that you can't really compare strings with ==. So "v.equals(nomvi lle)" not "nomville== v".

            Comment

            • manjava
              New Member
              • Sep 2009
              • 132

              #7
              thank you for your help

              Comment

              Working...