Results 1 to 5 of 5

Thread: Referencing an object from a method

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    Oxford, England
    Posts
    13

    Question 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

  2. #2
    Lively Member
    Join Date
    Feb 2002
    Posts
    103
    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).

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    Oxford, England
    Posts
    13
    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

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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"); 
     }
    }

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    Oxford, England
    Posts
    13
    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
  •  



Click Here to Expand Forum to Full Width