Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,275 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,247 people online right now. Registration is fast and FREE... Join Now!




TRying to modify java program

 
Reply to this topicStart new topic

TRying to modify java program, Help with modification

colec
24 Aug, 2008 - 07:33 PM
Post #1

New D.I.C Head
*

Joined: 22 Aug, 2008
Posts: 3

I am now trying to modify my 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.

This is what I have and receive 2 errors in the GUI and 5 errors in the inventory. The CD and Product files come back fine. There are four files total, each broke with ______________

Thanks for your help, it is really and truly appreciated.

Craig

CODE

public class CD extends Product {

public static void main(String[] args) {
}
    
    // title of the CD
    private String title;
    
    // constructor
    public CD(int item, String name, int units, double price, String title) {
        super(item,name,units, price);
        this.title = title;
    }
    
    public String getTitle() {
        return title;
    }
    
    // total value with the 5% fee
    public double value() {
        return super.value()*1.05;
    }
}


_______________________________________________________

CODE

import java.util.*;

public class Inventory {
    // Stores multiple products...

public static void main(String[] args) {
}
    
    private CD[] prods; // field
    
    // Constructor, based on the size of the array we want
    public Inventory(int size) {
        prods = new CD[size];
    }
    
    // Add a product
    public void add(int loc, CD p) {
        prods[loc] = p;
    }
    
    // getter for a product
    public CD get(int loc) {
        return prods[loc];
    }
    
    // sort the array!
    public void sort() {
        Arrays.sort(prods);
    }
    
    public int size() {
        return prods.length;
    }
    
    // Total the value of all the products
    public double totalValue() {
        double val = 0.0;
        for (int i = 0; i < prods.length; i++) {
            val += prods[i].value();
        }
        return val;
    }
}

___________________________________________________________
CODE

import javax.swing.*;

import java.awt.event.*;

public class GUI1 extends JFrame implements ActionListener {

    private JTextArea output;
    private int current = 0;
    private Inventory inv;
    private JButton next;

    public GUI1() {
        super("Product GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // end the program when we are done

        // number of products...
        int number = Integer.parseInt(JOptionPane.showInputDialog("Please tell me the number of products to enter"));

        // Set up the inventory
        inv = new Inventory(number);

        // enter the product info using a series of dialogs
        for (int i = 0; i < number; i++) {
            int item = Integer.parseInt(JOptionPane.showInputDialog("Enter the item's number"));

            String name = JOptionPane.showInputDialog("Enter the item's name");

            int units = Integer.parseInt(JOptionPane.showInputDialog("Enter the units in stock"));

            double price = Double.parseDouble(JOptionPane.showInputDialog("Enter the price of each item"));

            String title = JOptionPane.showInputDialog("Enter the title");

            // make the object
            CD p = new CD(item, name, units, price, title);

            // put it in the inventory
            inv.add(i,p);
        }

        // Sort the inv
        inv.sort();

        // now popup the "real" gui for the display of the products...
        JPanel content = new JPanel();
        setContentPane(content);

        content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));

        output = new JTextArea(20,50);
        output.setEditable(false);
        content.add(output);

        next = new JButton("Next");
        next.setActionCommand("Next");
        next.addActionListener(this);
        content.add(next);

        if (inv.size() > 0)
            display(inv.get(0));
        else {
            next.setText("End");
            next.setEnabled(false);
        }
        if (current == inv.size()-1) {
            next.setText("End");
            next.setEnabled(false);
        }

    }

    // button pushes...
    public void actionPerformed(ActionEvent evt) {
        if (evt.getActionCommand().equals("Next")) {
            if (current < inv.size()-1) {
                current++;
                CD p = inv.get(current);
                display(p);
            }
            if (current == inv.size()-1) {
                next.setText("End");
                next.setEnabled(false);
            }
        }
    }

    // show a record
    public void display(CD p) {
        String str = "";
        str += "Item number: " + p.getItem() + "\n";
        str += "Item name: " + p.getName() + "\n";
        str += "Items in stock: " + p.getUnits() + "\n";
        str += "Price: $" + p.getPrice() + "\n";
        str += "Fee: $" + (p.value() - p.value()/1.05) + "\n";
        str += "Value (including the fee): $" + p.value() + "\n";

        // total value of it
        str += "Total fee: $" + (inv.totalValue() - inv.totalValue()/1.05) + "\n";
        str += "Total value (including the fee): $" + inv.totalValue() + "\n";
        output.setText(str);
    }

    public static void main(String args[]) {
        // setup and display the gui
        GUI1 g = new GUI1();
        g.pack();
        g.setVisible(true);
    }
}

__________________________________________________________

CODE

// Stores a Product
public class Product implements Comparable {

public static void main(String[] args) {
            
        }

    // fields
    private int item;
    private String name;
    private int units;
    private double price;
    
    // constructor
    public Product(int item, String name, int units, double price) {
        this.item = item;
        this.name = name;
        this.units = units;
        this.price = price;
    }
    
    // total value
    public double value() {
        return units*price;
    }
    
    // getters and setters

    public int getItem() {
        return item;
    }

    public void setItem(int item) {
        this.item = item;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    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;
    }
    
    // for the sorting
    public int compareTo(Object p) {
        final Product o = (Product)p; // make it a product
        return (this.getName().compareToIgnoreCase(o.getName())); // compare the names
    }
    
}


Edited to use the [ code] tags so we can see where we are going

This post has been edited by pbl: 24 Aug, 2008 - 09:41 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: TRying To Modify Java Program
24 Aug, 2008 - 09:45 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Don't you think it would be helpful to us if you actually posted the error messages you are getting instead of trying sift through all your code?

And if your CD and product classes are fine, why post them? Let use some common sense here. smile.gif
User is offlineProfile CardPM
+Quote Post

colec
RE: TRying To Modify Java Program
25 Aug, 2008 - 03:59 AM
Post #3

New D.I.C Head
*

Joined: 22 Aug, 2008
Posts: 3

Sorry for posting all files. I was not sure if all the sourse code was needed or not. Now I know.

I went back to compile the files again and they worked. I am unsure what is happening here. Any insight as to why the program failed last night, yet worked this morning? I am running NetBeans 6.1.

thanks

Craig
User is offlineProfile CardPM
+Quote Post

ajaymatrix
RE: TRying To Modify Java Program
28 Aug, 2008 - 05:49 AM
Post #4

D.I.C Regular
Group Icon

Joined: 15 May, 2007
Posts: 389



Thanked: 1 times
Dream Kudos: 100
My Contributions
strange..
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 05:11AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month