I am trying to put together a simple inventory program for my class, but I keep running into one error that I just cannot seem to correct.

Here is my code:

public class DVD extends Product {

    // channel that the show was on
    private String channel;

    // constructor
    public DVD(int item, String name, int units, double price, String channel) {
        super(item,name,units, price);
        this.channel = channel;
    }

    // getter
    public String getChannel() {
        return channel;
    }


    // setter
    public void setChannel(String channel) {
        this.channel = channel;
    }

    // total value with the 5% fee
    public double value() {
        return super.value()*1.05;
    }

    // the 5% fee
    public double fee() {
        return super.value()*0.05;
    }

    public String toString () {
        return super.toString() + "\n" +
        "Channel: " + channel +
        "\nFee (already included in value): " +
        String.format("$%.2f", fee());
    }
}

public class Inventory {

    private DVD[] list;


    // constructor
    public Inventory(int size) {
        list = new DVD[size];
    }

    public int size() {
        return list.length;
    }

    // complete value
    public double totalValue() {
        double val = 0.0;
        for (int i = 0; i < list.length; i++) {
            val += list[i].value();
        }
        return val;
    }

    // add an item
    public void add(DVD d, int p) {
        list[p] = d;
    }

    // get an item
    public DVD get(int i) {
        return list[i];
    }

    // sort by name
    public void sort() {
        // bubble sort
        int n = list.length;
        for (int search = 1; search < n; search++) {
            for (int i = 0; i < n-search; i++) {
                if (list[i].getdvdName().compareToIgnoreCase(list[i+1].getdvdName()) > 0) {
                    // swap
                    DVD temp = list[i];
                    list[i] = list[i+1];
                    list[i+1] = temp;
                }
            }
        }
    }


}


import java.awt.event.*
import javax.swing.*;
// stores TV DVD's
class InventoryProgram extends JFrame {

    private JTextArea txt;
    private Inventory obj;
    private int current = 0;
    private JButton next;

    public static void main(String args[]) {
        InventoryProgram gui = new InventoryProgram();
        gui.pack();
        gui.setVisible(true);
    }
    public InventoryProgram() {

        super("TV DVD's");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //make products
        DVD product1 = new DVD(1,"Smallville", 11,16.99,"CW");
        DVD product2 = new DVD(2,"Heroes", 25,21.99,"NBC");
        DVD product3 = new DVD(3,"Scrubs", 2,14.99,"Fox");

        // inventory object
        obj = new Inventory(3);
        obj.add(product1, 0);
        obj.add(product2, 1);
        obj.add(product3, 2);

        obj.sort();


        /// output

        for (int i = 0; i < obj.size(); i++) {
            System.out.println(obj.get(i));
            System.out.println();
        }

        // total val
        System.out.printf("Total value: $%.2f", obj.totalValue());

        // gui stuff
        JPanel panel = new JPanel();
        txt = new JTextArea(15,20);
        txt.setEditable(false);//user shouldn't change it
        panel.add(txt);

        next = new JButton("Next");
        next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (current < obj.size()-1) {
                    current++;
                    showMusic();
                    if (current == obj.size()-1) {
                        InventoryProgram.this.next.setEnabled(false);
                    }
                }
            }
        });
        panel.add(next);

        getContentPane().add(panel);

        showMusic();



    }

    public void showMusic() {
        txt.setText("DVD Details:\n");
        txt.append(obj.get(current) + "\n");


        txt.append("Total value: $" + String.format("%.2f",obj.totalValue()));


    }
}


// product class
public class Product {

    // variables
    private int itemNumber;
    private String dvdName;
    private int units;
    private double price;

    // constructor for the product
    public Product(int itemNumber, String dvdName, int units, double price) {
        this.itemNumber = itemNumber;
        this.dvdName = dvdName;
        this.units = units;
        this.price = price;
    }

    // total value
    public double value() {
        return units*price;
    }

    // getters and setters

    public int getitemNumber() {
        return itemNumber;
    }

    public void setitemNumber(int itemNumber) {
        this.itemNumber = itemNumber;
    }

    public String getdvdName() {
        return dvdName;
    }

    public void setdvdName(String dvdName) {
        this.dvdName = dvdName;
    }

    public int getUnits() {
        return units;
    }

    public void setUnits(int units) {
        this.units = units;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String toString () {
        return "Item number: " + itemNumber
        + "\nName: " + dvdName+ "\nUnits: " + units
        + "\nPrice: " + String.format("$%.2f", price)
        + "\nValue: " + String.format("$%.2f", value());
    }

}

And the error I get is:

C:\Documents and Settings\Administrator\Desktop\InventoryProgram.java:94: class, interface, or enum expected
import java.awt.event.*
^
1 error

Tool completed with exit code 1

What am I missing here? I've tried several things and nothing works.

Recommended Answers

All 3 Replies

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

1. No semicolon on the end of 1st import.
2. You add imports only on the top of document, not in the middle. In the middle are 'class', interfaces or enum expected'.


PS. http://www.daniweb.com/forums/announcement9-3.html

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

1. No semicolon on the end of 1st import.
2. You add imports only on the top of document, not in the middle. In the middle are 'class', interfaces or enum expected'.


PS. http://www.daniweb.com/forums/announcement9-3.html

When I put the semicolon in, it gives me 2 errors. Not sure what you mean by adding it at the "top of a document".

public class DVD extends Product

He means they have to go above where you make the class declaration. (Also note that if you have a package declaration, they should go below that). Example:

package whatever;
import whatever.*;

public class Other{

}

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.