my dog learned polymorphism
The moose likes Java in General and the fly likes Constructor Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Constructor Problem" Watch "Constructor Problem" New topic

Constructor Problem

Mervin Chan

Joined: Dec 03, 2004
Posts: 24
I have a problem. I am modelling a factory environment.

I have 3 classes, Machine and Store and Product.

In my program, a "Machine" Object is supposed to pass an "Product" object to a "Buffer" Object. In my Machine constructor, i have indicated which Buffer to pass the Product to. ie

Store A = new Store();


Machine aMachine= new Machine(A);

//Send output of Machine to Store A

Constructor for Machine : Machine (Store aStoreName) { ..... }

the problem is this, I intend to read a string from a textfile indicating the Buffer that Machine is targeting. ie String aStoreName.

How to i convert String aStoreName, so that i can be used in the Machine Constructor? I tried casting, did not work!

I hope I've described my problem well... thanks for the help!
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Did you post this before and then delete the topic? I seem to remember answering this not 5 minutes ago...but I can't find the other topic

Anyway, this sounds like a job for a java.util.Map. Simply store the String bufferName as the key and then the Buffer object as the value. Whenever you get a string, just look up that string in the Map and get the correct Buffer object.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: Constructor Problem
It's not a secret anymore!