Is it possible to reduce casting by toString() method?
eg. *VendingMachine class uses ItemType class*
public class VendingMachine{
public VendingMachine{ }
if(inputamount < ((ItemType)list.get(i)).getName())
|________________________________|
ArrayList list = new ArrayList(); //collection of object reference of ItemType
}
VendingMachine maintains the collection of ItemType objects.
How can I reduce the above expression by using toString()?
_______________________________________________________________________________
Thank you very much for any help!
Murray - 24 May 2004 18:41 GMT
> Is it possible to reduce casting by toString() method?
>
[quoted text clipped - 14 lines]
>
> How can I reduce the above expression by using toString()?
____________________________________________________________________________
___
> Thank you very much for any help!
Your code is a bit confusing ..
inputamount is a number I assume, and getName() looks like it would be a
String? And you're using < ? *confused*
Disregarding that, I guess you could technically use toString() to avoid a
cast, but that's not what it's designed for. In your example above, you
could make your toString() return getName(), then you could do
list.get(i).toString() without having to cast to ItemType. But I would never
do that.
What's wrong with casting anyway? Apart from it looking a bit ugly. Maybe
you should wait for Java 1.5 Generics :-)
Murray - 24 May 2004 18:41 GMT
> Is it possible to reduce casting by toString() method?
>
[quoted text clipped - 14 lines]
>
> How can I reduce the above expression by using toString()?
____________________________________________________________________________
___
> Thank you very much for any help!
Your code is a bit confusing ..
inputamount is a number I assume, and getName() looks like it would be a
String? And you're using < ? *confused*
Disregarding that, I guess you could technically use toString() to avoid a
cast, but that's not what it's designed for. In your example above, you
could make your toString() return getName(), then you could do
list.get(i).toString() without having to cast to ItemType. But I would never
do that.
What's wrong with casting anyway? Apart from it looking a bit ugly. Maybe
you should wait for Java 1.5 Generics :-)
Eric - 24 May 2004 23:38 GMT
> Is it possible to reduce casting by toString() method?
>
[quoted text clipped - 14 lines]
>
> How can I reduce the above expression by using toString()?
____________________________________________________________________________
___
> Thank you very much for any help!
When you toString() a Collection (e.g., ArrayList), it will apply invoke
each object's toString() as follows: [name1, name2, ...]. Not very useful
for a comparison though. Live with the cast (at least until 1.5 is part of
normal life) to get what you need (lots of IDE's will even help you generate
it with minimal typing).
Hope that helps, -Eric
Woebegone - 29 May 2004 04:28 GMT
> if(inputamount < ((ItemType)list.get(i)).getName())
> |________________________________|
[quoted text clipped - 6 lines]
>
> How can I reduce the above expression by using toString()?
____________________________________________________________________________
___
> Thank you very much for any help!
As the previous posters pointed out, toString probably isn't what you want.
If you're determined to use your list like an array, the following will
create an array from your list with the correct type:
ItemType[] items = (ItemType[])list.toArray(new ItemType[list.size()]);
Now, you can write code like "items[i].getName()" if you prefer. There's a
cost to doing it this way though.

Signature
Sean.
Woebegone - 29 May 2004 04:44 GMT
> Is it possible to reduce casting by toString() method?
>
[quoted text clipped - 14 lines]
>
> How can I reduce the above expression by using toString()?
____________________________________________________________________________
___
> Thank you very much for any help!
Another possibility is to implement a class that uses the list but
simplifies the interface. If you know that it only ever will hold ItemTypes,
make an ItemTypeList, something like the example below (this is more work in
the short run, but could save a lot of grief over a large system).
Basically, write an adapter for the list so that all you can ever do is
add/remove/etc. ItemTypes. Then give some methods like "getNameAt(int)" so
you don't have to "get(i).getName()" (assuming this is a common operation --
the one you wanted to streamline the call to).
Finally, you could provide a custom iterator that wraps an Iterator from the
item list but returns ItemTypes with no casting. There's some overhead cost,
but then using the thing is much easier.
HTH! Regards,
Sean
<code>
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
interface ItemType {
public String getName();
public double getPrice();
}
class ItemTypeList {
private List list;
public ItemTypeList() {
list = new ArrayList();
}
public boolean add(ItemType item) {
return list.add(item);
}
public String getNameAt(int i) {
return get(i).getName();
}
public ItemType get(int i) {
return (ItemType)list.get(i);
}
public ItemTypeIterator iterator() {
return new ItemTypeIterator(list.iterator());
}
}
class ItemTypeIterator {
private Iterator iterator;
public ItemTypeIterator(Iterator iterator) {
this.iterator = iterator;
}
public boolean hasNext() {
return iterator.hasNext();
}
public ItemType next() {
return (ItemType)iterator.next();
}
}
public class VendingMachine {
private ItemTypeList items;
public ItemType buy(String name, double amount) {
ItemTypeIterator i = items.iterator();
while (i.hasNext()) {
ItemType item = i.next();
if (item.getName().equals(name)) {
if (item.getPrice() <= amount) {
return item;
}
else {
// throw too little money exception
}
}
}
// should throw sold out exception
// instead just return null for e.g.
return null;
}
}
</code
Qu?bec - 29 May 2004 12:00 GMT
Maybe I am wrong but ,
class ItemTypeList {
should implements the interface ItemType... ?
The newbe.
Woebegone - 29 May 2004 14:07 GMT
> Maybe I am wrong but ,
>
[quoted text clipped - 3 lines]
>
> The newbe.
In a more sophisticated context you might want something like that, but it
would imply the possibility of a recursive relationship where each list item
might be an ItemType _or_ an ItemLypeList. For this example, I really just
wanted to demonstrate how encapsulation could simplify use of the
Collection.
I think that what you 're describing is the "Composite" design pattern --
more than the example called for.
Or, do you mean because I provide no implementation of ItemType? I used only
an interface for ItemType because it's implementation doesn't matter for
this example, and would just have been extra baggage. To actually use the
code would require a concrete ItemType though, you're right. I'm assuming
the OP has one already to plug in. :-)

Signature
Thanks,
Sean.