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 12 of 12

Thread: Java programming. Program is not getting information from a subclass

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    }

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: Java programming. Program is not getting information from a subclass

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: Java programming. Program is not getting information from a subclass

    Both methods are in my subclass Manufacturer
    What class is the variable: supplies? Does it have those methods?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: Java programming. Program is not getting information from a subclass

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default 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.

    '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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: January 24th, 2013, 01:46 AM
  2. Replies: 4
    Last Post: June 27th, 2012, 01:15 PM
  3. Program that will retrieve information from website.
    By minaru in forum Java Theory & Questions
    Replies: 2
    Last Post: December 18th, 2010, 05:50 PM
  4. Save the information from an ArrayList after restarting the program
    By noFear in forum Java Theory & Questions
    Replies: 4
    Last Post: August 14th, 2010, 09:53 AM
  5. Replies: 1
    Last Post: August 7th, 2008, 03:09 AM