使用Accessor方法访问另一个类的数据

时间:2014-05-07 03:26:32

标签: java class methods output accessor

我相对较新的java和学习编程,这是我在大学的第8周。过去一天我一直在弄乱我的代码,我一直在搜索过去一两个小时的类似问题,这个问题可以帮助解决我的问题,但是没有找到一个可以帮助我的特定情况,至少不是我可以理解。

对于作业,我被要求编写一个包含3个类(一个接口,一个商店和一个产品类)的程序,我一直没事,直到我需要在产品中保存的界面上显示数据类。目前代码编译正常但是当我运行程序并尝试使用writeOutput()方法时,我得到堆栈溢出错误。无论如何,这是我到目前为止:

这是我试图在接口类中工作的方法:

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 String getName()
{
        return getName();            
}

最后,我将在产品类中包含getter和setter以防万一:

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

希望这足以让某人能够帮助我,但如果不是,我很乐意全部上传所有三个课程。

干杯Cale。

编辑:我已经决定添加所有三个课程来帮助那些希望帮助我的人(请记住,我无法完成任务并且我的代码可能充满了问题)希望它不会太乱你们明白了抱歉没有写很多评论,这是我需要处理的事情:)

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)
    {

    }
    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();            
}
}




import static java.lang.Math.*;
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;
}  
}

1 个答案:

答案 0 :(得分:1)

store类中的方法是递归调用自身而没有终止子句,这导致StackOverflowError:

public String getName()
{
    return getName();    // calls itself        
}

而是返回name变量的值(如果它存在于Store类中,或者您想要作为名称返回的任何变量):

public String getName()
{
    // determine the product name you want
    return myProduct.getName();
}