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