Hello everyone and thanks in advance for your help. I am having trouble with a piece of java code for class and have asked the insructor for help and he has only helped in confusing me further. The assignment was due Sunday and I have been in the hospital since last Wed. I get no credit if it is not turned in by tomorrow and I need credit for this assignment to maintain my GPA.This is an inventory program and I am not sure if it was the meds I was on that made me lose the grasp on the assignment or the fact I just hate GUIs and programming them. (I never learned) Here is the assignment from the class syllabus: • "Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee."
The additional attribute is the DVD title. I chose to use a DVD inventory of only 4 DVDs the instructor has accepted this so here is my code. It is copied straight out of Netbeans IDE 6.8. I did exactly what the instructor asked me to do which was add the API and compile it. I also added the compiled .java file to this post. I can not get it to work correctly. PLease help. Again thank you for your help.

package InventoryProgram3;

/**
 *
 * @author Ardus
 */



import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

 public class Main {



     public static void main(String args []) {

         Inventory invent = new Inventory();// tells the program to enter the information for each dvd into the inventory array for future use

         Movie dvd;



         dvd = new Movie("Movie1", 1, "Batman", 5, 14.95);

         invent.add(dvd);

         dvd = new Movie("Movie2", 2, "Wanted", 10, 19.99);

         invent.add(dvd);

         dvd = new Movie("Movie3", 3, "Star Wars Collection I-VI", 6, 49.99);

         invent.add(dvd);

         dvd = new Movie("Movie4", 4, "Death Race", 3, 19.99);

         invent.add(dvd);



         invent.display();

     } //end main



 } // end class Inventory3





 class DVD {

     private int dvdItem;

     private String dvdTitle;//title of dvd

     private int dvdStock;//number in stock

     private double dvdPrice;//price per dvd



     public DVD(int item, String title, int stock, double price) {

         dvdItem  = item;

         dvdTitle = title;//title of dvd

         dvdStock = stock;// number in stock

         dvdPrice = price;// price per dvd

     } //end four-argument constructor



     // set DVD Item

     public void setDvdItem(int item) {

         dvdItem = item;

     } //end method  set Dvd Item



     //return DVD Item

     public int getDvdItem() {

         return dvdItem;

     } //end method get Dvd Item



     //set DVD Title

     public void setDvdTitle(String title) {

         dvdTitle = title;

     } //end method set Dvd Title



     //return Dvd Title

     public String getDvdTitle() {

         return dvdTitle;

     } //end method get Dvd Title



     public void setDvdStock(int stock) {

         dvdStock = stock;

     } //end method set Dvd Stock



     //return dvd Stock

     public int getDvdStock() {

         return dvdStock;

     } //end method get Dvd Stock



     public void setDvdPrice(double price) {

         dvdPrice = price;

     } //end method setdvdPrice



     //return DVD Price

     public double getDvdPrice() {

         return dvdPrice;

     } //end  method get Dvd Price



     //calculate inventory value

     public double value() {

         return dvdPrice * dvdStock;

     } //end method value



     public String toString() {

         return String.format("item=%3d   title=%-20s   units=%3d   price=$%6.2f   value=$%7.2f",

                               dvdItem, dvdTitle, dvdStock, dvdPrice, value());

     }



 } //end class DVD





 class Movie extends DVD {

     private String movieTitle;



     public Movie(String title, int item, String dtitle, int stock, double price) {

         super(item, dtitle,  stock, price);

         movieTitle = title;

     }



     public double value() {

         double value = getDvdPrice() * getDvdStock();

         value = 1.05 * value; //5% restocking fee automatically calculated

         return value;

     } //end method value



     public String toString() {

         String s = String.format("Movie title=%-12s", movieTitle);

         s = s + " " + super.toString();

         return s;

     }


 } // end class Movie





 class Inventory {

     private DVD[] dvds;

     private int count;



     Inventory() {

         dvds = new DVD[10];

         count = 0;

     }



     public void add(DVD dvd) {

         dvds[count] = dvd;

         ++count;

         sort();

     }



     public double entireValue() {
        double value = 0;

         for (int i = 0; i < count; i++) {

             value = value + dvds[i].value();
         }

         return value;

     }



     public void sort() {

         for (int index = 1; index < count; index++) {

             DVD key = dvds[index];

             int position = index;



             //  Shift larger values to the right
             while (position > 0 && key.getDvdTitle().compareTo(dvds[position-1].getDvdTitle()) < 0) {

                 dvds[position] = dvds[position-1];

                 position--;

             }

             dvds[position] = key;

         }

     }



     public void display() {

        // System.out.println("\nThe inventory contains " + count + " DVDs\n");

         for (int i = 0; i < count; i++)

            // System.out.printf("%3d  %s\n", i, dvds[i]);

         //System.out.printf("\nThe total inventory value is $%.2f\n\n", entireValue());
         {


} // end class Inventory

 class GUI extends JFrame {
    /*Following Decalres Labels ,TextBoxes,Buttons and Panels that are used in the gui
     * 
     * 
     * 
     * 
     * 
     */
    private JLabel item;
    private JLabel title;
    private JLabel stock;
    private JLabel price;
    private JLabel entireValue;
    javax.swing.JTextArea ta = new javax.swing.JTextArea(10,20);
    {
    ta.append("\nThe inventory contains " + count + " DVDs\n");

    }
           for (int i = 0; i < count; i++)

               ta.append("%3d  %s\n", i, dvds[i]);

    ta.append("\nThe total inventory value is $%.2f\n\n", entireValue());

    javax.swing.JFrame frame = new javax.swing.JFrame();

    frame.getContentPane().add(new javax.swing.JScrollPane(ta));

    frame.pack();

    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);
    }
 }
     }

Recommended Answers

All 9 Replies

Please put posted code in code tags. Unformatted code is hard to read.

What are your problems? Can you ask specific questions about them?

Member Avatar for

Please put posted code in code tags. Unformatted code is hard to read.

What are your problems? Can you ask specific questions about them?

My problem is that my code in netbeans says "package ta does not exist" and "package frame does not exist" it will not produce a GUI as far as I can see and I am trying to get it to produce a GUI. I will try to repost the code in the code tags. Sorry it took me a bit to respond. My account some how was erased or corrupted. I could not log into DeniWeb and it said I was banned and this was the only post I have ever made.

package InventoryProgram3;

/**
 *
 * @author Ardus
 */



import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

 public class Main {



     public static void main(String args []) {

         Inventory invent = new Inventory();// tells the program to enter the information for each dvd into the inventory array for future use

         Movie dvd;



         dvd = new Movie("Movie1", 1, "Batman", 5, 14.95);

         invent.add(dvd);

         dvd = new Movie("Movie2", 2, "Wanted", 10, 19.99);

         invent.add(dvd);

         dvd = new Movie("Movie3", 3, "Star Wars Collection I-VI", 6, 49.99);

         invent.add(dvd);

         dvd = new Movie("Movie4", 4, "Death Race", 3, 19.99);

         invent.add(dvd);



         invent.display();

     } //end main



 } // end class Inventory3





 class DVD {

     private int dvdItem;

     private String dvdTitle;//title of dvd

     private int dvdStock;//number in stock

     private double dvdPrice;//price per dvd



     public DVD(int item, String title, int stock, double price) {

         dvdItem  = item;

         dvdTitle = title;//title of dvd

         dvdStock = stock;// number in stock

         dvdPrice = price;// price per dvd

     } //end four-argument constructor



     // set DVD Item

     public void setDvdItem(int item) {

         dvdItem = item;

     } //end method  set Dvd Item



     //return DVD Item

     public int getDvdItem() {

         return dvdItem;

     } //end method get Dvd Item



     //set DVD Title

     public void setDvdTitle(String title) {

         dvdTitle = title;

     } //end method set Dvd Title



     //return Dvd Title

     public String getDvdTitle() {

         return dvdTitle;

     } //end method get Dvd Title



     public void setDvdStock(int stock) {

         dvdStock = stock;

     } //end method set Dvd Stock



     //return dvd Stock

     public int getDvdStock() {

         return dvdStock;

     } //end method get Dvd Stock



     public void setDvdPrice(double price) {

         dvdPrice = price;

     } //end method setdvdPrice



     //return DVD Price

     public double getDvdPrice() {

         return dvdPrice;

     } //end  method get Dvd Price



     //calculate inventory value

     public double value() {

         return dvdPrice * dvdStock;

     } //end method value



     public String toString() {

         return String.format("item=%3d   title=%-20s   units=%3d   price=$%6.2f   value=$%7.2f",

                               dvdItem, dvdTitle, dvdStock, dvdPrice, value());

     }



 } //end class DVD





 class Movie extends DVD {

     private String movieTitle;



     public Movie(String title, int item, String dtitle, int stock, double price) {

         super(item, dtitle,  stock, price);

         movieTitle = title;

     }



     public double value() {

         double value = getDvdPrice() * getDvdStock();

         value = 1.05 * value; //5% restocking fee automatically calculated

         return value;

     } //end method value



     public String toString() {

         String s = String.format("Movie title=%-12s", movieTitle);

         s = s + " " + super.toString();

         return s;

     }


 } // end class Movie





 class Inventory {

     private DVD[] dvds;

     private int count;



     Inventory() {

         dvds = new DVD[10];

         count = 0;

     }



     public void add(DVD dvd) {

         dvds[count] = dvd;

         ++count;

         sort();

     }



     public double entireValue() {
        double value = 0;

         for (int i = 0; i < count; i++) {

             value = value + dvds[i].value();
         }

         return value;

     }



     public void sort() {

         for (int index = 1; index < count; index++) {

             DVD key = dvds[index];

             int position = index;



             //  Shift larger values to the right
             while (position > 0 && key.getDvdTitle().compareTo(dvds[position-1].getDvdTitle()) < 0) {

                 dvds[position] = dvds[position-1];

                 position--;

             }

             dvds[position] = key;

         }

     }



     public void display() {

        // System.out.println("\nThe inventory contains " + count + " DVDs\n");

         for (int i = 0; i < count; i++)

            // System.out.printf("%3d  %s\n", i, dvds[i]);

         //System.out.printf("\nThe total inventory value is $%.2f\n\n", entireValue());
         {


} // end class Inventory

 class GUI extends JFrame {
    /*Following Decalres Labels ,TextBoxes,Buttons and Panels that are used in the gui
     * 
     * 
     * 
     * 
     * 
     */
    private JLabel item;
    private JLabel title;
    private JLabel stock;
    private JLabel price;
    private JLabel entireValue;
    javax.swing.JTextArea ta = new javax.swing.JTextArea(10,20);
    {
    ta.append("\nThe inventory contains " + count + " DVDs\n");

    }
           for (int i = 0; i < count; i++)

               ta.append("%3d  %s\n", i, dvds[i]);
    
    ta.append("\nThe total inventory value is $%.2f\n\n", entireValue());
   
    javax.swing.JFrame frame = new javax.swing.JFrame();

    frame.getContentPane().add(new javax.swing.JScrollPane(ta));

    frame.pack();

    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);
    }
 }
     }

package ta does not exist" and "package frame does not exist"

Can you copy and post here the full text of the error messages.
I don't see any import statements for ta or frame.

Can you copy and post here the full text of the error messages.
I don't see any import statements for ta or frame.

Sorry it took me a while to get you these errors. I was at church. I teach the children on Wed nights so I have to go in early. Here is the errors I get when I run the code in netbeans. I had to copy it and set a new project in netbeans because I had compiled it like the instructor told me to and I could not get anything but build successful.

run:
java.lang.NoClassDefFoundError: inventoryprogram4/Main
Caused by: java.lang.ClassNotFoundException: inventoryprogram4.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: inventoryprogram4.Main. Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

OK so looking at these errors I see where they are comming from. I fixed them by erasing my inventoryprogram3 project in netbeans and renaming my package. Yes it took me an hour to realize thes errors were not the ones I was seeing before. I just copied and pasted without reading them. I was getting ready for bed when I checked for an answer. Ok so after following my instructors advice and compiling I can not get the errors to show up again on the reporting area of net beans. Did I mention before I hate building GUIs? If you copy and paste the code into net beans you will see the red exclamation points and errors associated with them. Now that the code is compiled all I get now in the reporting area is:
"run:
BUILD SUCCESSFUL (total time: 1 second)"
I do not know how to use net beans to see if the GU is showing up right. Or if it is showing up at all. The errors on the exclamation points are what I posted in my first post. If the GUI is supposed to show up in the reporting box when I hit run it is not showing anything at all. Please help thelast day to turn it in is tomorrow and I am not sure what I am missing in the code. All the help I have gotten so far has been vague and not helpful at all. The next part of my assignment which was due today was to build a button onto the GUI to cycle through the list. I beleive I can do this after the GUI is working.
Thanks again.
As far as the other comment earlier that you see no import for ta or frame my instructor said all I needed was the javax import. This import is not in my book as far as I can find. Where would I put it and how would it look? Also is my GUI in the right place? I really have no idea what I am doing with GUIs at all.

OK so I have been dabbling with the code a little more. I did find a little info on the TA and another error. But it did not get rid of them. Let me post the new code for you.

package InventoryProgram4;

/**
 *
 * @author Ardus
 */



import javax.swing.*;
import javax.swing.JTextArea.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.text.DecimalFormat.*;
import java.text.MessageFormat;
import java.lang.String;

 public class Main {



     public static void main(String args []) {

         Inventory invent = new Inventory();// tells the program to enter the information for each dvd into the inventory array for future use

         Movie dvd;



         dvd = new Movie("Movie1", 1, "Batman", 5, 14.95);

         invent.add(dvd);

         dvd = new Movie("Movie2", 2, "Wanted", 10, 19.99);

         invent.add(dvd);

         dvd = new Movie("Movie3", 3, "Star Wars Collection I-VI", 6, 49.99);

         invent.add(dvd);

         dvd = new Movie("Movie4", 4, "Death Race", 3, 19.99);

         invent.add(dvd);



         invent.display();

     } //end main



 } // end class Inventory4





 class DVD {

     private int dvdItem;

     private String dvdTitle;//title of dvd

     private int dvdStock;//number in stock

     private double dvdPrice;//price per dvd



     public DVD(int item, String title, int stock, double price) {

         dvdItem  = item;

         dvdTitle = title;//title of dvd

         dvdStock = stock;// number in stock

         dvdPrice = price;// price per dvd

     } //end four-argument constructor



     // set DVD Item

     public void setDvdItem(int item) {

         dvdItem = item;

     } //end method  set Dvd Item



     //return DVD Item

     public int getDvdItem() {

         return dvdItem;

     } //end method get Dvd Item



     //set DVD Title

     public void setDvdTitle(String title) {

         dvdTitle = title;

     } //end method set Dvd Title



     //return Dvd Title

     public String getDvdTitle() {

         return dvdTitle;

     } //end method get Dvd Title



     public void setDvdStock(int stock) {

         dvdStock = stock;

     } //end method set Dvd Stock



     //return dvd Stock

     public int getDvdStock() {

         return dvdStock;

     } //end method get Dvd Stock



     public void setDvdPrice(double price) {

         dvdPrice = price;

     } //end method setdvdPrice



     //return DVD Price

     public double getDvdPrice() {

         return dvdPrice;

     } //end  method get Dvd Price



     //calculate inventory value

     public double value() {

         return dvdPrice * dvdStock;

     } //end method value



     public String toString() {

         return String.format("item=%3d   title=%-20s   units=%3d   price=$%6.2f   value=$%7.2f",

                               dvdItem, dvdTitle, dvdStock, dvdPrice, value());

     }



 } //end class DVD





 class Movie extends DVD {

     private String movieTitle;



     public Movie(String title, int item, String dtitle, int stock, double price) {

         super(item, dtitle,  stock, price);

         movieTitle = title;

     }



     public double value() {

         double value = getDvdPrice() * getDvdStock();

         value = 1.05 * value; //5% restocking fee automatically calculated

         return value;

     } //end method value



     public String toString() {

         String s = String.format("Movie title=%-12s", movieTitle);

         s = s + " " + super.toString();

         return s;

     }


 } // end class Movie





 class Inventory {

     private DVD[] dvds;

     private int count;



     Inventory() {

         dvds = new DVD[10];

         count = 0;

     }



     public void add(DVD dvd) {

         dvds[count] = dvd;

         ++count;

         sort();

     }



     public double entireValue() {
        double value = 0;

         for (int i = 0; i < count; i++) {

             value = value + dvds[i].value();
         }

         return value;

     }



     public void sort() {

         for (int index = 1; index < count; index++) {

             DVD key = dvds[index];

             int position = index;



             //  Shift larger values to the right
             while (position > 0 && key.getDvdTitle().compareTo(dvds[position-1].getDvdTitle()) < 0) {

                 dvds[position] = dvds[position-1];

                 position--;

             }

             dvds[position] = key;

         }

     }



     public void display() {

        // System.out.println("\nThe inventory contains " + count + " DVDs\n");

         for (int i = 0; i < count; i++)

            // System.out.printf("%3d  %s\n", i, dvds[i]);

         //System.out.printf("\nThe total inventory value is $%.2f\n\n", entireValue());
         {


} // end class Inventory

 class GUI extends JFrame {
    /*Following Decalres Labels ,TextBoxes,Buttons and Panels that are used in the gui
     *
     *
     *
     *
     *
     */
    private JLabel item;
    private JLabel title;
    private JLabel stock;
    private JLabel price;
    private JLabel entireValue;
    javax.swing.JTextArea ta = new javax.swing.JTextArea(10,20);
    {
    ta.append("\nThe inventory contains " + count + " DVDs\n");

    }
    {   
    for (int i = 0; i < count; i++)

               ta.append("%3d  %s\n", i, dvds[i]);

    ta.append("\nThe total inventory value is $%.2f\n\n", entireValue());
    
    javax.swing.JFrame frame = new javax.swing.JFrame();

    frame.getContentPane().add(new javax.swing.JScrollPane(ta));

    frame.pack();

    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);
    }
 }
     }
 }

ON lines 341 and 343 the main errors I am having trouble with are as follows: method append in class javax.swing.JtextArea cannot be applied to given types required: java.lang.String
found java.lang.String,int,InventoryProgram4.DVD
On line 343 it says the exact same with the exception in the line found it says:
found java.lang.String,double

these are the errors at the exclamation points.

Bumping this because it is due in 6 hours and I have been trying to fix this for 7 days. The instructor says this code should work but it does not. I do not get it. Please help. If this code should work maybe I am using netbeans wrong and not able to see the GUI. I do not know how to make GUIs and this is my very first attempt. Please help I do not know what I am doing wrong.

Still hoping and praying for some help as the next three assignments depend on this GUI working corrrectly. As does my final assignment which is the completed program. Can someone please help? I am at wits end as I have tried everything that I know to try.

Check the API for the JTextArea append method. It takes exactly one parameter, a String.
You are calling it with multiple arguments, just like the errors say.
If you want to append some complex formatted text you must build this up in a String first, then append that String.
(ps this is NOT an answer to any prayer, but hoping does sometimes work!)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.