7 Replies - 2230 Views - Last Post: 29 July 2013 - 07:47 AM Rate Topic: -----

#1 coderzzz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 24-July 13

How can I create dynamic setters and getters (accessors and mutators)

Posted 28 July 2013 - 03:49 PM

Hey guys, how can I create dynamic setters and getters.
Say I am reading an xml file using SAX Parser : Here is the format of xml file
<BookList>
 <BookTitle_1> C++ For Dummies </BookTitle_1>
 <BookAurthor_1> Charles </BookAuthor>
 <BookISBN_1> ISBN -1023-234 </BookISBN_2>
 <BookTitle_2> Java For Dummies </Booktitle_2>
 <BookAuthor_2> Henry </BookAuthor_2>
 <BookISN_2> ISBN - 231-235 </BookISN_2>
</BookList> 



And then I have class call Books:

 public class Book
 {
   private String Name;
   private String Author;
   private String ISBN;

   public void SetName(String Title)
   {
     this.Name = Title;
   }
   
   public String getName()
   {
      return this.Name;
   }
   
   public void setAuthor(String _author)
   {
      this.Author = _author;
   }

   public String getAuthor()
   {

 }


And then I have an ArrayList class of type Book as such:

public class BookList
{
 private List <Book> books ;

  BookList(Book _books)
  {
     books = = new ArrayList<Book>();
     books.add(_books);
  }
}






And lastly I have the main class,
where I parse and read the books from the xml file. Currently when I parse an xml file and say I read the tag <BookTitle_1> I call the SetName() and then call BookList() to create a new arraylist for the new book and add it to the arraylist.

But how can I create a dynamic setters and getters method so that whenever a new book title is read it calls the new setter and getter method to add it to arraylist.

Currently my code over writes the previously stored book and prints that book out. I have heard there is something called reflection. If I should use reflection, please can some one show me an example?

Thank you.

This post has been edited by jon.kiparsky: 28 July 2013 - 04:37 PM
Reason for edit:: Recovered content from closed version of same thread


Is This A Good Question/Topic? 0
  • +

Replies To: How can I create dynamic setters and getters (accessors and mutators)

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: How can I create dynamic setters and getters (accessors and mutators)

Posted 28 July 2013 - 03:51 PM

What do you mean by "dynamic setters and getters?"
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4444
  • View blog
  • Posts: 12,318
  • Joined: 18-April 07

Re: How can I create dynamic setters and getters (accessors and mutators)

Posted 28 July 2013 - 04:04 PM

As far as I know you can't dynamically modify classes at runtime in Java. For instance, I can't simply add a method to set and get the ISBN number. Now what you could instead do is parse the document, create a map structure mapping the node and its value and then create a mechanism for fetching values from it and setting values in it. But they wouldn't be true getter/setter methods.

:)
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,757
  • Joined: 19-March 11

Re: How can I create dynamic setters and getters (accessors and mutators)

Posted 28 July 2013 - 04:45 PM

public class BookList
{
 private List <Book> books ;

  BookList(Book _books)
  {
     books = = new ArrayList<Book>();
     books.add(_books);
  }
}




To begin with, this takes one Book, and returns an instance of Booklist with that one book in it.

To add another book to this you'd need to make an addBook method:
public void addBook(Book book){
  books.add(book);
}


To get a particular book out is a little complicated, since you have no way to query this arraylist by, say, title. You have to examine each book and see if it's got the title you're looking for. If you're smart, you'll check all of them - some books might have the same title. (for example, you might be looking for the title "Of Human Bondage" and get a novel or a guide to playing in your private dungeon)

There are a couple of ways you might approach the access problem. First of all, start by defining what you want to solve. What should I be able to get from your database?
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2071
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: How can I create dynamic setters and getters (accessors and mutators)

Posted 29 July 2013 - 02:14 AM

Could you clarify the question please? What do you mean by dynamic setter? Do you want to be able to access and modify any book object you have already created?

In any case, I think your XML is working against you. Do you have control over it? How about something like this:

<booklist>
  <book>
    <title>C++ For Dummies</title>
    <author>Charles</author>
    <isbn>ISBN -1023-234</isbn>
  </book>
  <book>
    <title>Java For Dummies</title>
    <author>Henry</author>
    <isbn>ISBN - 231-235</isbn>
  </book>
</booklist> 

Was This Post Helpful? 0
  • +
  • -

#6 coderzzz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 24-July 13

Re: How can I create dynamic setters and getters (accessors and mutators)

Posted 29 July 2013 - 07:09 AM

o.k. guys sorry about the confusion here. To clarify myself, what I mean by dnyamic setters and getters is:

I do not know before hand, how many books are in the .xml file.
So say I parse a book with book's title, author, ISBN, etc and then call setName(String Title),
setAuthor(String author), and setISBN(String isbn) accordingly.
And then add that book to the ArrayList.

Now when I read in another book, and use the setters to repeat the same process as above, I end up over writing the previous book details.

Because there is only one setter for title, author, and isbn each.

To answer some other questions that were asked above:
1. No I do not control over the xml files.
2. I will not need to sort or search the books with their titles, authors, or isbn. All I will need to parse the info, store and then print it out.

Someone pointed out that xml may not be right, to answer that, I already have a working xml file. I only pasted a quick snippet of the xml.

Thank you
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,757
  • Joined: 19-March 11

Re: How can I create dynamic setters and getters (accessors and mutators)

Posted 29 July 2013 - 07:40 AM

Quote

Someone pointed out that xml may not be right, to answer that, I already have a working xml file. I only pasted a quick snippet of the xml.


"Working" in what sense? cfoley is right. XML is a tool for structuring data. This is ust a list of unrelated tags. The only way to read this is just to count lines, and the only thing you're going to do with the tags is throw them away.


Quote

2. I will not need to sort or search the books with their titles, authors, or isbn. All I will need to parse the info, store and then print it out.


0. Create a BookList object
1. Read the <booklist> tag. Discard it.
2. While you haven't read the </booklist> tag:
3. Create a new Book object
4. Read three lines, and discard their tags. Use them in order for the title, author, and ISBN fields of the Book object.
5. Add the Book to the BookList
6. Repeat.
Was This Post Helpful? 0
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2071
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: How can I create dynamic setters and getters (accessors and mutators)

Posted 29 July 2013 - 07:47 AM

Oh right, I understand. When you do something like:

Book a = new Book();


You are creating an instance of your class. You need to understand the difference between a class and an object. A class is like a rubber stamp for creating objects. If we ignore the statik keyword for a while, a class itn't much use for anything other than creating objects. If we do something like this:

Book a = new Book();
Book b = new Book();


We have two separate objects. We can call setters and they will have their own setters:

Book a = new Book();
Book b = new Book();

a.setTitle("The Magician's Nephew");
b.setTitle("Schindler's List");

// prints The Magician's Nephew
System.out.println(a.getTitle());

// prints Schindler's List
System.out.println(b.getTitle());


Since you are using a loop, you need to create a new object on each iteration of the loop. Otherwise, as you have found out the hard way, all you are doing is changing the attributes of one Book object.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1