|
-
Jun 6th, 2003, 09:18 AM
#1
Thread Starter
New Member
Referencing an object from a method
Hi
I'm trying to put together a simple supermarket simulation program in Java (which I don't really know so I'm sort of learning it as I go along) and have hit a problem.
I've got a line that sets up a few shelves when the program first executes which I'm then trying to reference from a method in the same class like so:
Code:
public class MainShop
{
Shelf a = new Shelf(5);
public static void WarehouseOp()
{
char opt;
//Warehouse menu
do {
System.out.println("\t\t\t\t::Warehouse::\n");
System.out.println("\tEnter a key to run an operation in the warehouse\n");
System.out.println("\tV: View the items in the warehouse");
System.out.println("\tM: Move the items from the warehouse into the shop");
System.out.println("\tQ: Go back into the store");
opt = Input.readChar("\tInput:");
opt = Character.toUpperCase(opt);
if (opt == 'V') {
Trying to access functions in the Shelf class from here > wanted something like "a.Showproducts();" but doesn't work
}
} while (opt != 'Q');
}
I know the code is sloppy (well the structuring is horrid) because it's an alteration job of something that was incomplete.
Any help would be great. Though I can see I'm gonna go wrong with another part pretty soon too
-
Jun 6th, 2003, 09:52 AM
#2
Lively Member
this is my first attempt to help out someone in the forum, after getting much help for my own stuff. you could try:
public class MainShop
{
Shelf a;
public static void WarehouseOp()
{
char opt;
a = new Shelf(5);
//Warehouse menu
do {
System.out.println("\t\t\t\t::Warehouse::\n");
System.out.println("\tEnter a key to run an operation in the warehouse\n");
System.out.println("\tV: View the items in the warehouse");
System.out.println("\tM: Move the items from the warehouse into the shop");
System.out.println("\tQ: Go back into the store");
opt = Input.readChar("\tInput:");
opt = Character.toUpperCase(opt);
if (opt == 'V') {
a.Showproducts();
}
...
or another possibly simpler solution would be to just remove the 'static' keyword (if desirable).
-
Jun 6th, 2003, 10:16 AM
#3
Thread Starter
New Member
Well I've changed everything static to non-static since I wrote the above.
Code is still the same as I had it before in that the:
Shelf a = new Shelf(5);
Is all still on the same line.
Now the problem equates to it not being able to resolve the 'a' symbol:
MainShop.java:28: cannot resolve symbol
symbol : method Showproducts ()
location: class Shelf
a.Showproducts();
^
1 error
I'm guessing this is because I'm trying to connect to it outside of the method? I still don't know how to fix this
-
Jun 6th, 2003, 11:57 AM
#4
Dazed Member
Code:
import java.io.*;
public class MainShop {
public static void main(String[] args){
String opt = null;
BufferedReader buff = null;
System.out.println("\t\t\t\t::Warehouse::\n");
System.out.println("\tEnter a key to run an operation in the warehouse\n");
System.out.println("\tV: View the items in the warehouse");
System.out.println("\tM: Move the items from the warehouse into the shop");
System.out.println("\tQ: Go back into the store");
try{
buff = new BufferedReader(new InputStreamReader(System.in));
opt = buff.readLine();
if((opt.equals("V")) || (opt.equals("v"))) {
Shelf.ShowProducts();
}
}catch(IOException e){;}
}
}
class Shelf{
public static void ShowProducts(){
System.out.println("Rice Krispers");
}
}
-
Jun 8th, 2003, 01:40 PM
#5
Thread Starter
New Member
Well thanks for the replies but after loads of effort to try and make it work with the rest of the program, it's becoming evident that I'm not going to get it working the way I want it to any time soon.
Thanks for trying to help though!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|