Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Using Accessor methods to access data from another class

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    16
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question Using Accessor methods to access data from another class

    I am relatively new to java and learning to program, this being my 8th week at uni. I have been fiddling around with my code for the past day and I have been searching for the past hour or two for a similar question that could help answer my problem but have not found one that helps my particular situation, at least not that I could understand.

    For an assignment, I have been asked to write a program with 3 classes(an interface, a store and a product class) and I have been going ok until I need to display data on the interface that is held in the product class. At the moment the code will compile fine but when I run the program and try to use the writeOutput() method I get a stack overflow error. Thanks to a friend, I realise now that it is because of a non-terminating recursive call, however I can not think of a solution on how to fix the problem. And just to clarify, what I am aiming to do is have the MatesTerminal class display the data for name that is stored in the Product class(I have no way of determining which product to display at this time, so I would like to be able to display the data for all three products if possible). Anyway this is what I have so far:

    The method from the MatesTerminal Class:

    private void writeOutput()
    {
        int productChoice;
     
        Scanner console = new Scanner(System.in);
        System.out.println("Please choose a product (1),(2),(3)");
        productChoice = console.nextInt();
        System.out.println("The records of product " +productChoice+ " are:");
        System.out.println("Name: "+matesStore.getName());

    This is one of the methods from the Store class:

    public String getName()
    {
            return getName();            
    }

    I'll include the getter and setter from the Product class just in case:

    public void setName(String newName) 
    {
        name = newName;
    }    
    public String getName()
    {
        return name;        
    }



    Here is all three classes to help people who wish to help me Just keep in mind that I am nowhere near finishing and my code is probably riddled with problems. Hopefully it's not too messy for you guys to understand. And sorry for not writing many comments, it's something i need to work on.

    import java.util.*;
    public class MatesInterface
    {
        Store matesStore = new Store();
     
        private void run()
        {      
            showInterface();
            chooseOption();
        }
        private void showInterface()
        {
            System.out.println("What would you like to do?:");
            System.out.println("(1)Input data for the product");
            System.out.println("(2)Show data from one product");
            System.out.println("(3)Show the replenishment strategy for a product");
            System.out.println("(0)Exit the program"); 
        }
        private void chooseOption()
        {        
            int option;
            boolean flag = false;  
     
            Scanner console = new Scanner(System.in);          
            System.out.print("Please choose an option: ");
            option = console.nextInt();
     
            if(option==1)
            {
                readInput();                            
            }
            else if(option==2)
            {
                writeOutput();
            }
            else if (option==3)
            {
     
            }
            else if(option==0)
            {
                System.exit(0);
            }
            else
            {
                flag = true;
            }
     
     
            while (flag)
            {
                System.out.println("That is not a valid option.");
                System.out.println("Please choose an option: ");
                option = console.nextInt();
                flag = false;
            }   
     
        }
     
        private void readInput()
        {
            Store matesStore = new Store();
            String name;
            int productChoice, demandRate;
            double setupCost, unitCost, inventoryCost, sellingPrice;
     
            Scanner console = new Scanner(System.in);
            System.out.println("Please choose a product (1), (2) or (3): ");
            productChoice = console.nextInt();
            System.out.println("Please enter the product's name: ");
            name = console.next();
            System.out.println("Please enter the product's demand rate: ");
            demandRate = console.nextInt();
            System.out.println("Please enter the product's setup cost: ");
            setupCost = console.nextDouble();
            System.out.println("Please enter the product's unit cost: ");
            unitCost = console.nextDouble();
            System.out.println("Please enter the product's inventory cost: ");
            inventoryCost = console.nextDouble();
            System.out.println("Please enter the product's selling price: ");
            sellingPrice = console.nextDouble();
            matesStore.addData(productChoice, name, demandRate, setupCost, unitCost, inventoryCost, sellingPrice);
            chooseOption();
        }
        private void writeOutput()
        {
            int productChoice;
     
            Scanner console = new Scanner(System.in);
            System.out.println("Please choose a product (1),(2),(3)");
            productChoice = console.nextInt();
            System.out.println("The records of product " +productChoice+ " are:");
            System.out.println("Name: "+matesStore.getName());
        }
     
        public static void main (String[] args)
        {        
            MatesInterface intFace = new MatesInterface();
            intFace.run();
        }
    }

    public class Store
    {
        // instance variables - replace the example below with your own
        private Product product1, product2, product3;
     
        public Store()
        {
            product1 = new Product();
            product2 = new Product();
            product3 = new Product();
        }
     
        public void addData(int option, String newName, int newDemand, double newSetup, double newUnit, double newInventory, double newPrice)
        {
            if (option==1) setData(product1, newName, newDemand, newSetup, newUnit, newInventory, newPrice);
            else if (option==2) setData(product2, newName, newDemand, newSetup, newUnit, newInventory, newPrice);
            else setData(product3, newName, newDemand, newSetup, newUnit, newInventory, newPrice);
        }
        private void setData(Product product, String name, int demandRate, double setupCost, double unitCost, double inventoryCost, double sellingPrice)
        {
            product.setName(name);
            product.setDemand(demandRate);
            product.setSetup(setupCost);
            product.setUnit(unitCost);
            product.setInventory(inventoryCost);
            product.setPrice(sellingPrice);        
        }
     
        public String getName()
        {
                return getName();            
        }
    }

    public class Product
    {
        private String name;
        private int demandRate;
        //private final int REPLENISHMENTRATE=0;
        private double setupCost;
        private double unitCost;
        private double inventoryCost;
        private double sellingPrice; 
     
        //Constructors for the class Product
        public Product()
        {
            name = "No name yet.";
            demandRate = 0;
            unitCost = 0;
            setupCost = 0;
            inventoryCost = 0;
            sellingPrice = 0;        
        }       
        public Product(String newName, int newDemand, double newSetup, double newUnit, double newInventory, double newPrice)
        {
            name = newName;
            demandRate = newDemand;
            unitCost = newUnit;
            setupCost = newSetup;
            inventoryCost = newInventory;
            sellingPrice = newPrice;
        }
     
        // Accessor and mutator methods to access and modify data on a Product object
       public void setName(String newName) 
       {
            name = newName;
       }    
       public String getName()
       {
            return name;        
       }
     
       public void setDemand(int newDemand)
       {
            demandRate = newDemand;
       }    
       public int getDemand()
       {
           return demandRate;
       }
     
       public void setUnit(double newUnit)
       {
           unitCost = newUnit;
       }   
       public double getUnit()
       {
           return unitCost;
       }
     
       public void setSetup(double newSetup)
       {
           setupCost = newSetup;
       }    
       public double getSetup()
       {
           return setupCost;
       }
     
       public void setInventory(double newInventory)
       {
           inventoryCost = newInventory;
       }   
       public double getInventory()
       {
           return inventoryCost;
       }
     
       public void setPrice(double newPrice)
       {
           sellingPrice = newPrice;
       }   
       public double getPrice()
       {
           return sellingPrice;
       }
       }

    Hopefully this is not too messy for you guys and has enough information to help.

    Cheers Cale


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,812
    My Mood
    Mellow
    Thanks
    177
    Thanked 626 Times in 613 Posts

    Default Re: Using Accessor methods to access data from another class

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

    It's more difficult to help someone with uncommented code. Not only do we not know what the code is supposed to do, but it indicates that the author does not know what the code is supposed to do.

    As an example, the code below is either purposefully silly or an accident. If purposefully silly, can you please explain what it's supposed to be doing and why you wrote it this way. If you've already tried to do that, I apologize for not being able to pull that from what you've posted:
        public String getName()
        {
            return getName();            
        }
    BTW - If you knew this method was the source of the error, posting it with the error and what you intended the code to accomplish is all that would have been necessary to help you.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    MrPigeon (May 7th, 2014)

  4. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Using Accessor methods to access data from another class

    Quote Originally Posted by MrPigeon View Post
    At the moment the code will compile fine but when I run the program and try to use the writeOutput() method I get a stack overflow error. Thanks to a friend, I realise now that it is because of a non-terminating recursive call, however I can not think of a solution on how to fix the problem.
    I take it you're saying that you realise that
    public String getName()
    {
            return getName();            
    }
    is the non-terminating recursive call, right?

    Quote Originally Posted by MrPigeon View Post
    And just to clarify, what I am aiming to do is have the MatesTerminal class display the data for name that is stored in the Product class (I have no way of determining which product to display at this time, so I would like to be able to display the data for all three products if possible).
    I interpret this as when MatesInterface's writeOutput()method is called, this method makes a further call, "System.out.println("Name: " + matesStore.getName());".

    Store's getName() method at the moment is as above (i.e., the recursive call), and you do not know how to replace the recursive call with the right calls to the Product objects that contain the product names, right?

    If so, then since Store already have product1, product2 and product3 as fields (instance variables), i.e.,
    class Store
    {
        // instance variables - replace the example below with your own
        private Product product1, product2, product3;
    what's stopping Store's getName() method from (temporarily) constructing a String of product names? E.g.,
    public String getName()
    {
        String productNames =
            product1.getName() + ", " + product2.getName() + ", " + product3.getName();
     
        return productNames;
    }

    Note that even if you change the getName() method as suggested above, it will print out "No name yet." for all the products. This occurs even if you have earlier entered data for the product(s). The source of this problem lies with MatesInterface's
    private void readInput()
    {
        Store matesStore = new Store();
        ...
    I'll leave this with you for now to work out the reason the above is problematic.

  5. The Following User Says Thank You to jashburn For This Useful Post:

    MrPigeon (May 7th, 2014)

  6. #4
    Junior Member
    Join Date
    May 2014
    Posts
    16
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Using Accessor methods to access data from another class

    Thank you both for your helpful answers. And I agree, it was rather silly of me to ask a rather vague question without even a few comments in my code. I solved my problem as I had not declared the variables of the Product class to be static. So I was able to change the getName() method in the Store class to
    readName() // changed the name slightly to not confuse it with accessor method in the Product class
    {
         return Product.getName(); 
    }

    However, I much prefer your method Jashburn.

    public static String readName()
    {
        String productNames =
            product1.getName() + ", " + product2.getName() + ", " + product3.getName();
     
        return productNames;
    }

Similar Threads

  1. Replies: 8
    Last Post: May 6th, 2013, 07:53 AM
  2. Replies: 21
    Last Post: November 27th, 2012, 09:58 PM
  3. List methods add(int k, Data data), set(int k, Data data), remove(int k)
    By Enirox in forum Object Oriented Programming
    Replies: 3
    Last Post: September 20th, 2012, 06:43 AM
  4. Need Help with Accessor and Mutator Methods
    By ankur_032 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 9th, 2011, 09:00 AM
  5. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM

Tags for this Thread