Java programming. Program is not getting information from a subclass
I can not figure out why my output doesn't seem to call from my manufacturer class to output the data. Just looking for advise as what I May have done wrong.
package inventory.program2;
import java.io.*;
import java.text.*;
/**
*
* @author chrischad
*/
public class InventoryProgram2 {
public InventoryProgram2() throws IOException {
Inventory aInventory = new Inventory();
int Count = 0;
//create a reader
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Welcome to the Inventory program\n\n");
while ( Count <100)
{
System.out.print("Enter product Name or Exit to stop: ");
String invName = input.readLine();
if (invName.equalsIgnoreCase("Exit"))
{
break;
}
//prompt for input
System.out.print("Inventory number: " );
String invNumber = input.readLine();
System.out.print("Enter manufacturer: " );
String invManu = input.readLine();
System.out.print("How many in Stock: " );
int invStock = Integer.valueOf(input.readLine()).intValue();
while (invStock < 0 )
{
System.out.println("Error: Please insert a positive number of stock: " );
invStock = Integer.valueOf(input.readLine()).intValue();
}
System.out.print("Enter Price: ");
double invPrice = Double.valueOf(input.readLine()).doubleValue();
while (invPrice < 0.0)
{
System.out.println("Error: Please enter a positive Price: ");
invPrice = Double.valueOf(input.readLine()).doubleValue();
}
//create an inventory
Supply supplies = new Supply(invName, invNumber, invStock, invPrice);
aInventory.addSupply(supplies);
Count++;
System.out.println();
}
NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
aInventory.sortInventory();
//print out inventory
for (int i = 0; i< aInventory.getNumberOfSupply();i++)
{
Supply supplies = aInventory.getSupply(i);
{
System.out.println("Product name: " + supplies.getName());
System.out.println("Inventory number: " + supplies.getInvNumber());
System.out.println("Manufacturer: " + supplies.getManu());
System.out.println("Stock: " + supplies.getInvStock());
System.out.println("Price: " + moneyFormat.format(supplies.getInvPrice()));
System.out.println("Restocking Fee: " + moneyFormat.format(supplies.restockingFee()));
System.out.println("Inventory Value: " + moneyFormat.format(supplies.getInvValue()));
System.out.println();
}
System.out.println("Total Inventory Value: " + moneyFormat.format(aInventory.getTotalValue()));
System.out.println();
System.out.println("Closing Inventory program\n\n");
}
}
public static void main(String[] args) throws IOException{
InventoryProgram2 inventoryProgram2 = new InventoryProgram2();
}
}
public class Supply implements Comparable {
public String name;
public String invNumber;
public int invStock;
public double invPrice;
String getManu;
public Supply( )
{
name = "";
invNumber = "0";
invStock = 0;
invPrice = 0.00;
}
public Supply(String name, String invNumber, int invStock, double invPrice)
{
this.name = name;
this.invNumber = invNumber;
this.invStock = invStock;
this.invPrice = invPrice;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getInvNumber()
{
return invNumber;
}
public void setInvNumber(String invNumber)
{
this.invNumber = invNumber;
}
public int getInvStock()
{
return invStock;
}
public void setInvStock(int invStock)
{
this.invStock = invStock;
}
public double getInvPrice()
{
return invPrice;
}
public void setInvPrice(double invPrice)
{
this.invPrice = invPrice;
}
public double getInvValue()
{
return invStock * invPrice;
}
@Override
public int compareTo(Object object)
{
if ( name == null )
{
return -1;
}
if ( object == null )
{
return 1;
}
return name.compareTo(((Supply)object).getName());
}
}
class Manufacturer extends Supply {
private String manu;
public Manufacturer()
{
super();
manu = "";
}
/**
*
* @param name
* @param invNumber
* @param manu
* @param invStock
* @param invPrice
*/
public Manufacturer(String name, String invNumber, String manu, int invStock, double invPrice)
{
super(name, invNumber, invStock, invPrice);
this.manu = manu;
}
/**
*
* @return
*/
public String getManu(String Manu)
{
return super.getManu;
}
public void setmanu(String manu)
{
this.manu = manu;
}
@Override
public double getInvValue()
{
return super.getInvValue() * 1.05;
}
public double restockingFee()
{
return super.getInvValue() * 0.05;
}
@Override
public String toString(){
return String.format
("Item Number:\t\t%d\nItem Name:\t\t%s\nItem Size:\t\t%d oz\nItems In Stock:\t\t%d\n"+
"Cost Per Item:\t\t$%.2f\nTotal Product Cost:\t$%.2f\nRestock Fee:\t\t$%.2f",
invNumber, name, invStock, manu, invPrice, getInvValue(), restockingFee());
}
}
Re: Java programming. Program is not getting information from a subclass
Can you post the contents of the console from when you execute the program that shows its input and output.
On windows: To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: Java programming. Program is not getting information from a subclass
run:
Welcome to the Inventory program
Enter product Name or Exit to stop: d
Inventory number: 3
Enter manufacturer: jjl
How many in Stock: 4
Enter Price: 4
Enter product Name or Exit to stop: kk
Inventory number: 3
Enter manufacturer: jl
How many in Stock: 3
Enter Price: 2.25
Enter product Name or Exit to stop: Exit
Product name: d
Inventory number: 3
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at inventory.program2.InventoryProgram2.<init>(Invent oryProgram2.java:76)
at inventory.program2.InventoryProgram2.main(Inventor yProgram2.java:93)
Java Result: 1
BUILD SUCCESSFUL (total time: 34 seconds)
I'll fix the code tags and post again in a few minutes. Thanks
Re: Java programming. Program is not getting information from a subclass
Quote:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>at inventory.program2.InventoryProgram2.<init>(Invent oryProgram2.java:76)
That says: You need to fix some compiler errors on line 76.
Re: Java programming. Program is not getting information from a subclass
System.out.println("Manufacturer: " + supplies.getManu()); is line 76. Using netbeans, says can not find method getManu .
System.out.println("Restocking Fee: " + moneyFormat.format(supplies.restockingFee())); is line 79, Says can not find method restockingFee.
Both methods are in my subclass Manufacturer but I can not seem to get them to be called by the main class usless i'm missing something.
Re: Java programming. Program is not getting information from a subclass
Quote:
Both methods are in my subclass Manufacturer
What class is the variable: supplies? Does it have those methods?
Re: Java programming. Program is not getting information from a subclass
Supply has the variable supplies to call the inventory but those two methods are not in that class but in the subclass that should be called by super. I'm just confused as to what i've done wrong at this point.
Re: Java programming. Program is not getting information from a subclass
Quote:
those two methods are not in that class
You need to have a reference to the class that the methods are in to be able to call the methods.
What class are the methods in?
How can the code get a reference to an instance of that class so it can call the methods?
Re: Java programming. Program is not getting information from a subclass
methods are in the subclass Manufacturer but I think if did not invoke the constructor and then invoke the methods.
Re: Java programming. Program is not getting information from a subclass
You need a reference to the class that has the methods to call the methods in the class.
What class has the methods you are trying to call?
Where in the code is there a reference to that class?
Where does the code create any instances of the Manufacturer class? If you don't create any instances, you can not call any of its methods.
Re: Java programming. Program is not getting information from a subclass
Manufacturer manu = new Manufacturer();
{
products[MAX_NUM_OF_PRODUCTS] = manu;
numOfProducts++;
}
I added this instance to my (object) Inventory class and still same problem. The manufacturer class has the methods i'm trying to call.
--- Update ---
By adding that instance created the error 'Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100'
--- Update ---
private Manufacturer manu[] = new Manufacturer[10];
/**
*
* @param inv
*/
private void add(Manufacturer inv) {
manu[numOfProducts] = inv;
numOfProducts++;
}
Is this what you mean?
Re: Java programming. Program is not getting information from a subclass
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Quote:
'Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100'
When you post error messages, please copy the full text and paste it. It shows where rhe error happened. You've left that off.