开发者

Modify the Inventory Program to use a GUI

The GUI should display all of the items in the inventory and include 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. All dollar values should be displayed as currency (i.e. $D,DDD.CC).

When I compile my code I received the following error messages, which I am not sure how to correct.

C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:162: class, interface, or enum expected
import java.awt.BorderLayout;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:163: class, interface, or enum expected
import java.awt.FlowLayout;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:164: class, interface, or enum expected
import java.awt.GridLayout;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:165: class, interface, or enum expected
import java.awt.event.ActionEvent;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:166: class, interface, or enum expected
import java.awt.event.ActionListener;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:167: class, interface, or enum expected
import java.text.DecimalFormat;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:169: class, interface, or enum expected
import javax.swing.BorderFactory;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:170: class, interface, or enum expected
import javax.swing.JButton;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:171: class, interface, or enum expected
import javax.swing.JFrame;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:172: class, interface, or enum expected
import javax.swing.JLabel;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:173: class, interface, or enum expected
import javax.swing.JPanel;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:174: class, interface, or enum expected
import javax.swing.JTextField;
^
12 errors

Tool completed with exit code 1

Here is my code:

/**
 * Represents a product.
 */
public class Television {
    private int itemNumber;
    private String productName;
    private int unitsInStock;
    private double price;

    /**
     * Default constructor
     */
    public Television() {
    }

    /**
     * Handy constructor to initialize all attributes of the product.
     */
    public Television(int itemNumber, String productName, int unitsInStock,
            double price) {
        this.itemNumber = itemNumber;
        this.productName = productName;
        this.unitsInStock = unitsInStock;
        this.price = price;
    }

    /**
     * @return The value of the inventory (the number of units in stock
     *         multiplied by the price of each unit).
     */
    public double calculateInventory() {
        return this.unitsInStock * this.price;
    }

    /**
     * @return the itemNumber
     */
    public int getItemNumber() {
        return itemNumber;
    }

    /**
     * @return the product name
     */
    public String getProductName() {
        return productName;
    }

    /**
     * @return the unitsInStock
     */
    public int getUnitsInStock() {
        return unitsInStock;
    }

    /**
     * @return the price
     */
    public double getPrice() {
        return price;
    }

    /**
     * @param itemNumber
     *            the itemNumber to set
     */
    public void setItemNumber(int itemNumber) {
        this.itemNumber = itemNumber;
    }

    /**
     * @param name
     *            the product name to set
     */
    public void setName(String productName) {
        this.productName = productName;
    }

    /**
     * @param unitsInStock
     *            the unitsInStock to set
     */
    public void setUnitsInStock(int unitsInStock) {
        this.unitsInStock = unitsInStock;
    }

    /**
     * @param price
     *            the price to set
     */
    public void setPrice(double price) {
        this.price = price;
    }
}


/**
 * This class should inherit from the Product class. Recall that the extends
 * keyword is used in java to represent inheritance
 */
public class Supplier extends Computer {
    /** Supplier Name */
    private String supplierName;

    /**
     * Constructor should have 5 parameters:
     *
     * - Item Number
     *
     * - Product Name
     *
     * - Number of Units in Stock
     *
     * - Price of each Unit
     *
     * - Supplier Name
     */
    public Supplier(int itemNumber, String productName, int unitsInStock,
            double price, String supplierName) {
        /*
         * Note you will use the super keyword to invoke the constructor in your
         * Product class.
         */
        super(itemNumber, productName, unitsInStock, price);
        this.supplierName = supplierName;
    }

    /**
     * This method returns the product of price and available units multiplied
     * by 5% ((price * product) * .05);
     */
    public double calculateRestockFee() {
        return super.calculateInventory() * .05;
    }

    /**
     * This method returns the product of price and available units plus the
     * restock fee.
     */
    public double calculateInventory() {
        return super.calculateInventory() + calculateRestockFee();
    }

    /**
     * @return the supplierName
     */
    public String getSupplierName() {
        return supplierName;
    }

    /**
     * @param supplierName
     *            the supplierName to set
     */
    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

}


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class InventoryPart4 exten开发者_Go百科ds JFrame {
    private static DecimalFormat currency = new DecimalFormat("$#,##0.00");

    // Declares the TextFields used to display the value of each attribute of
    // the current product.
    private JTextField itemNumberTF;
    private JTextField productNameTF;
    private JTextField unitsInStockTF;
    private JTextField priceTF;
    private JTextField supplierNameTF;
    private JTextField restockFeeTF;
    private JTextField valueOfInventoryTF;

    // Declares a TextField to display the value of the entire inventory
    private JTextField totalValueOfInventoryTF;

    // Declare buttons to navigate through the products in the inventory
    private JButton priorBT;
    private JButton nextBT;

    // This array holds all products in the inventory.
    private Supplier[] products;

    // Indicates the index of the product displayed onto the screen.
    private int current = 0;

    /**
     * Starts the application
     *
     * @param args
     *            Not used by this application
     */
    public static void main(String[] args) {
        // Creates and displays the GUI
        new InventoryPart4();
    }

    /**
     * Creates a new instance of the GUI and displays the frame.
     */
    public InventoryPart4() {
        super("Inventory Part 4");
        setSize(500, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // Creates the array with 5 elements
        products = new Supplier[5];

        // Creates some products
        products[0] = new Supplier(0001, " Samsung UN46D6400",9,1599.99);
        products[1] = new Supplier(0002, " Vizio XVT553SV",6,1299.00);
        products[2] = new Supplier(0003, " Panasonic Viera TC-P50VT25",2,2079.99);
        products[3] = new Supplier(0004, " Sony Bravia KDL-55EX720",8, 1889.99);
        products[4] = new Supplier(0005, " LG Infinia 47LX9500",2,2099.00);

        // Sorts products by name
        sortArray();

        // Creates the visual components
        createComponents();

        // Shows the GUI
        setVisible(true);

        // Displays the first product
        updateFields();
    }

    private void createComponents() {
        JPanel p = new JPanel();
        p.setLayout(new BorderLayout());
        p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        p.add(createFieldsPanel(), BorderLayout.CENTER);
        p.add(createButtonsPanel(), BorderLayout.SOUTH);

        setContentPane(p);
    }

    private JPanel createButtonsPanel() {
        JPanel p = new JPanel();
        p.setLayout(new FlowLayout(FlowLayout.RIGHT));

        priorBT = new JButton("Prior");
        priorBT.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (current > 0) {
                    current--;
                    updateFields();
                }
            }
        });
        p.add(priorBT);

        nextBT = new JButton("Next");
        nextBT.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (current < products.length - 1) {
                    current++;
                    updateFields();
                }
            }
        });
        p.add(nextBT);

        return p;
    }

    /**
     * Updates the fields to reflect the current product.
     */
    protected void updateFields() {
        Supplier s = products[current];

        itemNumberTF.setText(String.valueOf(s.getItemNumber()));
        productNameTF.setText(s.getProductName());
        unitsInStockTF.setText(String.valueOf(s.getUnitsInStock()));
        priceTF.setText(currency.format(s.getPrice()));
        supplierNameTF.setText(s.getSupplierName());
        restockFeeTF.setText(currency.format(s.calculateRestockFee()));
        valueOfInventoryTF.setText(currency.format(s.calculateInventory()));

        totalValueOfInventoryTF.setText(currency.format(calculateInventory()));
    }

    private JPanel createFieldsPanel() {
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(0, 2, 5, 5));

        p.add(new JLabel("Item Number"));
        itemNumberTF = new JTextField();
        p.add(itemNumberTF);

        p.add(new JLabel("Product Name"));
        productNameTF = new JTextField();
        p.add(productNameTF);

        p.add(new JLabel("Units In Stock"));
        unitsInStockTF = new JTextField();
        p.add(unitsInStockTF);

        p.add(new JLabel("Unit Price"));
        priceTF = new JTextField();
        p.add(priceTF);

        p.add(new JLabel("Supplier Name"));
        supplierNameTF = new JTextField();
        p.add(supplierNameTF);

        p.add(new JLabel("Restock Fee"));
        restockFeeTF = new JTextField();
        p.add(restockFeeTF);

        p.add(new JLabel("Value Of Inventory"));
        valueOfInventoryTF = new JTextField();
        p.add(valueOfInventoryTF);

        p.add(new JLabel(""));
        p.add(new JLabel(""));

        p.add(new JLabel("Value Of The Entire Inventory"));
        totalValueOfInventoryTF = new JTextField();
        p.add(totalValueOfInventoryTF);

        return p;
    }

    /**
     * A method to calculate the value of the entire inventory. This method
     * should take in an array of type Television and should
     * traverse through all the elements of the array and calculate the
     * inventory.
     *
     * @return The value of the entire inventory.
     */
    public double calculateInventory() {
        double value = 0;
        for (int i = 0; i < products.length; i++) {
            value += products[i].calculateInventory();
        }
        return value;
    }

    /**
     * Sorts the products by name, using the Bubble Sort algorithm.

     */
    public void sortArray() {
        int n = products.length; // size;
        boolean swapped;
        do {
            swapped = false;
            for (int i = 0; i < n - 1; i++) {
                String name1 = products[i].getProductName();
                String name2 = products[i + 1].getProductName();
                if (name1.compareToIgnoreCase(name2) > 0) {
                    // swap
                    Supplier temp = products[i];
                    products[i] = products[i + 1];
                    products[i + 1] = temp;
                    swapped = true;
                }
            }
            n = n - 1;
        } while (swapped);
    }
}


It looks like you are trying to do your imports halfway through the file. Imports should be at the top of your file. I think that will solve the particular errors you are getting.


@Ben Torell's answer is partially correct. Imports must be at the top of the class. I'm assuming these aren't all in one file, though. Once I made a .java file for each (and generated a Computer class that you left out), the only remaining problem I see is that your constructor calls in the array of Suppliers are missing arguments for the last parameter, supplierName.

Edit: Okay, per the comments below, there are actually a few more issues. Each of the get... methods is not defined in Television. So the calls to getItemNumber(), et al are also invalid. If you're using a decent IDE (say, Eclipse) it will offer to generate these for you. Otherwise, define those methods in Television, add the missing argument to the constructor calls, and then you should be GTG.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜