Problem accessing methods in collcetions

Hi I have a class which is passed a HashMap collection called BookingCollection this is assigned as BC withinbg the class
There is a method of the class that writes out to a file.

When I try to compile , it says that the method size() (or any of the collection methods ) doesnt exist

Here is a code sample

Ive omitted the actual filewrting bit

public class FileWriterBooking {
   
   
    public BookingCollection bc ;
 
    // constructor
    public FileWriterBooking(BookingCollection bookingColl)
    {  
     
     BookingCollection bc = new BookingCollection();
     bc = (BookingCollection) bookingColl ;
     
     
    }
   
    public void createFile(String fName)
    {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy, hh:mm a");
        try
        {
           
            FileWriter bookingFile = new FileWriter(fName);
           
            PrintWriter bookingWriter = new PrintWriter(bookingFile);
             
            Booking nextBooking;
             
            int noOfBookings = bc.size();
       
tinybearAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sciuriwareCommented:
>>>      BookingCollection bc = new BookingCollection();
delete that line, it is inactive.

Where and how did you declare 'BookingCollection'???

;JOOP!
0
sciuriwareCommented:
I bet that BookingCollection is NOT a HashMap, but contains one.
Add a method to your class that returns the size() of the internal HashMap.

BookingCollection is not a collection itself, I think.

;JOOP!
0
tinybearAuthor Commented:
I have a class called BookingCollection declared as follows.

public class BookingCollection
{
   
    public HashMap bookingColl;    
   
    public BookingCollection()
    {
        bookingColl = new HashMap();

This holds objects of type booking from a Bookings class
There is a cliient which instatiates the Booking Collections etc and calls the FileWriterBoooking class passing in the BookingCollection to the constructor
Hope that makes sense
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

sciuriwareCommented:
Well, that's clear: BookingCollection doesn't have a 'size()', bookingColl has one.

Add a method to BookingCollection:

    public int contentSize()
    {
        return(bookingColl.size());
    }

;JOOP!
0
sciuriwareCommented:
In your program:

>>> int noOfBookings = bc.size();

becomes:

   int noOfBookings = bc.contentSize();

;JOOP!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tinybearAuthor Commented:
that makes sense.

I then need to be able to access every item in the bookingColl from WITHIN the FileWriter class.

But as I had passed in bookingColl to the fileWriter I thought Id have access to it.

0
tinybearAuthor Commented:
so how can I access the HashMap objects from within FileWriter?

e.g i was going to use the following but that is prob not possible.

 if (noOfBookings > 0)
            {
           
                Iterator it = bc.values().iterator();
         
                while (it.hasNext())
                {
             
                    nextBooking = (Booking) it.next();
                   
                    bookingWriter.println(nextBooking.bookingRef+"\n");
                    bookingWriter.println(formatter.format(nextBooking.dateObj)+"\n");
                    bookingWriter.println(nextBooking.name+"\n");
                    bookingWriter.println(nextBooking.location+"\n");
                    bookingWriter.println(nextBooking.destination+"\n");
                    bookingWriter.println();
             
                }
               
            }
0
CEHJCommented:
Provide an accessor method, then you can call:

Iterator it = bc.getBookingsMap().values().iterator();


............


public class BookingCollection
{
   
    private HashMap bookingColl;

    public HashMap getBookingsMap() {
        return bookingColl;
    }
   
0
Mayank SAssociate Director - Product EngineeringCommented:
>> so how can I access the HashMap objects

Add an accessor (setter) for the hash-map in your BookingCollection class.

public class BookingCollection
{
  ..
  public HashMap getBookingColl ()
  {
    return bookingColl ;
  }
}

Now you can iterate through it. BTW, why is the BookingCollection class needed? It seems to be nothing but a needless wrapper over a HashMap.
0
CEHJCommented:
>>Add an accessor (setter) for the hash-map in your BookingCollection class.

Isn't that what i said  5 minutes earlier? ;-)

>>It seems to be nothing but a needless wrapper over a HashMap.

That's not necessarily the complete code perhaps...

0
Mayank SAssociate Director - Product EngineeringCommented:
If you just need to access the values, you can well use a simple loop:

Object[] array = bc.getBookingColl ().values ().toArray () ;

for ( int i = 0 ; i < array.length ; i ++ )
{
  Booking nextBooking = ( Booking ) array[i] ) ;
  System.out.println ( nextBooking.bookingRef ) ;

// BTW, you should make these data-members like bookingRef private and use an accessor getBookingRef ()

}
0
tinybearAuthor Commented:
It is part of an overall exercise. The BookingCollection class and the Booking class were aprt of one exercise, then used again with the fileWriter & fileReader classes added in.


Ive done what you said, and all works fine now..

Many thanks, I couldnt see the wood for the trees.

Excellent repsonses all
0
Mayank SAssociate Director - Product EngineeringCommented:
>> Isn't that what i said  5 minutes earlier? ;-)

Yes, CEHJ. I hope you realize the fact that I had several questions open and was posting on them.... yours wasn't on this one when I was posting mine.

>> That's not necessarily the complete code perhaps...

Correct, which is why I asked what else does the class do.
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.