Performance of Java Object Referece

Hi all,

I have a Performance question want to ask. The original code are shown in the below:

publicstatic String[] itemDetails =

{"item ID","item Title","item Description","item Price","quantity in stock","quantity in order","release day"};

publicstatic String[] cdDetails =

{"CD type","artist name","number of Tracks","Duration"};

publicstatic String[] bookDetails =

{"Book type","ISBN","author name","publisher"};

public List addItem(){

boolean isCD =true;

List list =new List();

BookStoreUtility bsUtil =new BookStoreUtility();

CD cd =new CD();

Book book =new Book();

for (int i = 0; i < itemDetails.length; i++ ){

input = Keyboard.readStr();// get input from users

if ( i == 0 ){

if ( ( input.charAt(0) =='c' || input.charAt(0) =='C') )

isCD =true;

else

isCD =false;

}

if (isCD)

PopulateCD(i, input, cd);

else

PopulateBook(i, input, book);

}

list.add(cd);

return list;

}

publicvoid PopulateCD(int i, String paramValue, CD cd){

try{

switch (i){

case 0: cd.setItemID(paramValue);break;

case 1: cd.setItemTitle(paramValue);break;

case 2: cd.setItemDes(paramValue);break;

case 3: cd.setItemPrice(Float.parseFloat(paramValue));break;

case 4: cd.setQuanInStock(Long.parseLong(paramValue));break;

case 5: cd.setQuanInOrder(Long.parseLong(paramValue));break;

case 6: cd.setRelDate(df.parse(paramValue));break;

case 7: cd.setType(paramValue.charAt(0));break;

case 8: cd.setArtist(paramValue);break;

case 9: cd.setNum_of_tracks(Integer.parseInt(paramValue));break;

case 10: cd.setDuration(Float.parseFloat(paramValue));break;

}

}catch (Exception e){};

}

publicvoid PopulateBook(int i, String paramValue, Book book){

try{

switch (i){

case 0: book.setItemID(paramValue);break;

case 1: book.setItemTitle(paramValue);break;

case 2: book.setItemDes(paramValue);break;

case 3: book.setItemPrice(Float.parseFloat(paramValue));break;

case 4: book.setQuanInStock(Long.parseLong(paramValue));break;

case 5: book.setQuanInOrder(Long.parseLong(paramValue));break;

case 6: book.setRelDate(df.parse(paramValue));break;

case 7: book.setType(paramValue.charAt(0));break;

case 8: book.setAuthor(paramValue);break;

case 9: book.setISBN(paramValue);break;

case 10: book.setPublisher(paramValue);break;

}

}catch (Exception e){};

}

I have a addItem method that get inputs from user. I have 3 classes(Object), they are Book, CD and Item. Book and CD objects are extending from the Item object. At first, the DataEntry Person would need to input the ItemID. If the ItemID is started with "C", that means he/she is inserting CD's information, and all the inputs will be populated to CD object, and it works the same way for Book Object, ...etc. All the input parameters names will be stored in a String[], such as itemDetails, cdDetails, etc.

However, the program doesn't know the User is inserting whether the Book or CD information at the beginning, so by default i will just use a forLoop to read all the parameters from String[] itemDetails. After the user entered the first parameter, the program knows the user is inserting wheter CD or Book. I would like to concat the cdDetails or bookDetails String array to the default itemDetails String array so that all the required parameters can be read from user.

I would like to get some nice suggestions on how to implement this thing. Someone said it's not oo enough. I want to learn how to design fast, dynamic and portable codes. Please help me out.

Thanks in advance

Transistor

[7705 byte] By [popohomaa] at [2007-10-2 7:19:03]
# 1
You're already getting answers to this on the Java Ranch forum. Don't waste our time by cross-posting it here, please.
DrClapa at 2007-7-16 20:54:36 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
You really consider that a cross-post?
_dnoyeBa at 2007-7-16 20:54:36 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

I would consider it a cross-post. If the individual is receiving responses from a Java forum already, there is no need to post the same content or issue to a second Java forum that mostly shares the same membership. In my opinion, it is unprofessional behavior of a software developer.

Now, if it is a limey technical recruiter that is trying to find a potential candidate for an open position. That is a different story, but it is still unprofessional behavior.

Master_Consultanta at 2007-7-16 20:54:36 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
> You really consider that a cross-post?Yes, I do. It may waste the time of different people than if it were cross-posted twice in the same forum, but it has just as much potential for time-wasting.
DrClapa at 2007-7-16 20:54:36 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
Only a foo-l would write code like this:catch (Exception e) {};%
duffymoa at 2007-7-16 20:54:36 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

"I would like to get some nice suggestions on how to implement this thing. Someone said it's not oo enough."

This is a fair statement.

You don't post either of the Book or CD classes. Those might be pertinent.

I think switch statements like those you presented are bad signs 99% of the time.They tend to be used by neophytes who aren't using OO to full advantage.

I'd prefer to have a static valueOf class that would take a String input, parse the data appropriately, and return an instance of Book or CD instead of the arrays of Strings that you have. I think they're clumsy.

Anything common about Books and CDs? Title, author, publisher, etc? Does that suggest a superclass or interface that might be useful?

%

duffymoa at 2007-7-16 20:54:36 > top of Java-index,Other Topics,Patterns & OO Design...
# 7
"Anything common about Books and CDs? Title, author, publisher, etc? Does that suggest a superclass or interface that might be useful?"I know! We'll call it Item!Yes, learning to think in terms of abstractions is what object-oriented programming is all about.%
duffymoa at 2007-7-16 20:54:36 > top of Java-index,Other Topics,Patterns & OO Design...
# 8
Another hint: quantity in stock should not be an attribute of either Book or CD. That should be something that an InventoryManager should keep track of.%
duffymoa at 2007-7-16 20:54:36 > top of Java-index,Other Topics,Patterns & OO Design...