CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2011
    Posts
    13

    [RESOLVED] collections, maps, lists problem

    Hello All,


    I have spent qite a bit of time trying to solve my problem, but it looks like I'm not getting even close.


    Code:
    public class MusicLibrary
    {
       //instance variable declaration
       private Map<String, List<CD>> cdCatalogue;
       
       
       
    
       // constructor
       public MusicLibrary()
       {
          super();
          this.cdCatalogue = new TreeMap<String, List<CD>>();
       }
       
       //  instance methods
      public void displayCDs()
       {
          for (String eachKey : cdCatalogue.keySet())
          {
             
            cdCatalogue.get(eachKey);
            System.out.println(cdCatalogue.get(eachKey));//This doesn't work as it should
            
            
    
             
           
          }
       }
             
    
       public void populate()
       {
          List<CD> temp;
          temp = new ArrayList<CD>();
          temp.add(new CD("Andy Sheppard","Soft on the Inside", "Jazz",1990));
          this.cdCatalogue.put("Andy Sheppard", temp);
          temp = new ArrayList<CD>();
          temp.add(new CD("Barbara Thompson","Songs from the Centre of the Earth","jazz",1991));
          this.cdCatalogue.put("Barbara Thompson", temp);
    }
    }
    The other class is:

    Code:
    public class CD
    {
       /* instance variables */
       private String artist;  // name of artist or composer
       private String title;   // title of album
       private String genre;   // genre of album
       private int year;       // year of album release or composition
      
    
       /**
        * Constructor for objects of class CD
        */
       public CD(String anArtist, String aTitle, String aGenre, int aYear)
       {
          super();
          this.artist = anArtist;
          this.title = aTitle;
          this.genre = aGenre;
          this.year = aYear;
       }
       
       /* instance methods */
    
       /**
        * Returns the receiver's artist
        */
       public String getArtist()
       {
          return this.artist;
          
       }
      
       
       /**
        * Returns the receiver's title
        */   
       public String getTitle()
       {
          return this.title;
       }
       
       
       /**
        * Returns the receiver's genre
        */   
       public String getGenre()
       {
          return this.genre;
       }
       
       
       /**
        * Returns the receiver's year
        */
       public int getYear()
       {
          return this.year;
       }
       
       
       
      public void printString()
       {
         System.out.println(getArtist() + ", " + getTitle() + ", " + getGenre() + ", " + getYear());
       }
       
     }

    My problem is with method dispayCDs() I just can't get it to work. I assume that I have written variable declaration and constructor in class MusicLibrary correct. It should be zero-argument constructor which assigns to cdCatalogue an empty instance of a suitable concrete class.
    I think I should also use method printString() from class CD but when I try to do this i get compilation error non-static method cannot be refferenced from a static context.

    Please push me in the right direction.
    Regards

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: collections, maps, lists problem

    Code:
    System.out.println(cdCatalogue.get(eachKey))
    This will get the CD object out of the cdCatalogue and then calls it's toString() method to get the string representation of the object and finally print it to stdout. You haven't declared a toString() method and so the default one from the Object class will be used which prints a seemingly meaningless value.

    You have declared a printString() method, should this have been toString() or were you trying to do something else?

    I can't see where you are calling printString() to get a compilation error. Please show the code that is causing this error and the full error message.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Mar 2011
    Posts
    13

    Re: collections, maps, lists problem

    thanks for the reply.


    My task here is to write method displayCDs() that will cause to display details of all the CD s in the library each in new line.I still have no clue how to do it

    thnks for your help.

  4. #4
    Join Date
    Mar 2011
    Posts
    13

    Re: collections, maps, lists problem

    Went back to books and then spent some more time on it and finally got solution

    Code:
     public void displayCDs()
       {
        List<CD> currentCD = new ArrayList<CD>();
        for ( String eachCD : cdCatalogue.keySet())
          {
             currentCD = cdCatalogue.get(eachCD);
             for (CD temp : currentCD)
             {
                temp.printString();
             }
          }
       }
    I'm happy like pig in s***

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured