Skip to main content

Supermarket Simulation program --- Help Required Please

4 replies [Last post]
tasscott
Offline
Joined: 2008-02-14

Hello.

I'm developing a Super Market simulation program at the moment for a project and I need some help.

I have to use a linked list implementation to store Customer objects (and other objects for Products etc) and then I will need to get Customers from the list in order to call methods etc for various task.

Now for my problem:

I've put the Customer lists in the iterator and when I call the customerIterator.next() method it returns a Object however I can't call methods on the Object.

So, i really need help with how to get the object that is returned working so I can call methods defined in the customer class.

I think i'm right in thinking the linked list will store the reference to the object, so I really need to know how to obtain that reference from the list and then I can use it. At the moment, the Object returned when I call customerIterator.next() can't be used to call objects such as getX() in the customer class

public Customer nearestCustomer(int x, int y) {
// find and return the nearest customer to the point (x,y),
// disregarding any that are at a distance of 20 or more

lists.Iterator customerIterator = new Iterator(customers);
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kirillcool
Offline
Joined: 2004-11-17

If you're using Java 1.4, cast that Object to your customer class. If you're using Java 1.5 and up, define your list as generic to contain only customer instances.

tasscott
Offline
Joined: 2008-02-14

I've been playing around with, wrote a tiny method that returns the object:

public Customer returnCust(){
Customer cust = it.next();
return cust;
}

but it won't compile, error says:

incompatible types - found java.lang.Object but expected practicalA.customer

but the objects that I am putting in are practicalA.Customers....

can anyone help?

tarbo
Offline
Joined: 2006-12-18

What Kirill said: cast your object to the correct class.

Here's what's going on under the hood. Suppose you have the following code snippet:

[code]String first(List strings) {
return strings.get(0);
}[/code]
It's not nice code, it's not good code, don't ever write code like this -- it's just an example.

The compiler will balk here. Why? Because List.get is defined as:
[code]Object get(int index);[/code]

Note that it returns an instance of [i]Object[/i]. There is no way for the compiler to know whether there are strings in that list. (There is no way for the compiler to know there is [b]anything[/b] in that list.) So how do we deal with this?

Either we tell the compiler that we are expecting a String (1), or we use generics (2).

[code]// (1) With cast.
String first(List strings) {
return (String) strings.get(0);
}

// (2) With generics.
String first(List strings) {
return strings.get(0); // Cast behind the scenes
}[/code]

Use the generics if you can, or if you know how -- this will allow the compiler to provide extra type-checking for you; generics were introduced in Java 1.5. If you'd rather not touch generics, or if you are using 1.4.2 or earlier, you have no option but to cast as in (1). See the [url=http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html]Java Tutorial[/url] on Casting Objects (bottom of the page).

Good luck,
Jonathan

tasscott
Offline
Joined: 2008-02-14

Ok thanks guys.

I've got it working correctly. Thanks once again!