Results 1 to 2 of 2
  1. #1
    MikeJ39 is offline Member
    Join Date
    Oct 2011
    Posts
    38
    Rep Power
    0

    Default Getting errors in my Imports

    This is my code and this is what i need to do for my assignment:

    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 department in which the product belongs, 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.

    Java Code:
    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;


    import java.util.Arrays;
    import java.util.Comparator;

    public class CameraInventory3
    {



    public static void main(String[] args)
    {


    Camera[] camera = new Camera[3];
    camera[0] = new Cam(1, "Kodak", 45, 15.0, 2);
    camera[1] = new Cam(2, "Sony", 50, 16.0, 32.1);
    camera[2] = new Cam(3, "Olympus", 10, 23.0, 53.8);

    Inventory invent = new Inventory(camera);


    invent.sortProd();

    for (int i = 0; i < invent.getSize(); i++)
    {

    Cam prod = (Cam)invent.get(i);


    System.out.println("Item Number: " + prod.getItemNumber());
    System.out.println("Product Name: " + prod.getName());
    System.out.println("Number Of Units: " + prod.getUnits());
    System.out.println("Number Of Megapixels: " + prod.getMegapx());
    System.out.printf("Price Per Unit: $%,.2f\n", prod.getPrice());
    System.out.printf("The value of the inventory: $%,.2f\n\n", prod.inventoryValue());
    System.out.printf("The value of the restocking fee: $%,.2f\n\n", prod.getRestockingFee());
    }

    System.out.printf("The total value: $%,.2f\n", invent.totalValue());


    }

    }

    class Camera
    {

    private int itemNumber;
    private String name;
    private int units;
    private double price;
    Cam cam;



    public Camera(Cam cam)
    {
    this.cam = cam;
    }


    public Camera()
    {
    itemNumber = 0;
    name = "";
    units = 0;
    price = 0;
    }


    public Camera(int itemNumber, String name, int units, double price, double megapx)
    {
    this.itemNumber = itemNumber;
    this.name = name;
    this.units = units;
    this.price = price;
    }


    public double inventoryValue()
    {
    return units * price;
    }


    public int getItemNumber()
    {
    return itemNumber;
    }

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

    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;
    }

    }

    class Cam extends Camera
    {

    private double megapx;


    public Cam()
    {
    super();
    megapx = 0;
    }


    public Cam(int itemNumber, String name, int units, double price, double megapx)
    {
    super(itemNumber, name, units, price, megapx);
    this.megapx = megapx;
    }


    public double getRestockingFee()
    {
    return getPrice() * getUnits() * 0.05;
    }


    public double inventoryValue()
    {
    return getPrice() * getUnits();
    }


    public double getMegapx()
    {
    return megapx;
    }


    public void setMegapx(double megapx)
    {
    this.megapx = megapx;
    }

    }
    class Inventory
    {
    private Camera[] camera;



    public Inventory(Camera[] camera)
    {
    this.camera = camera;
    }


    public double totalValue()
    {
    double total = 0;
    for (int i = 0; i < camera.length; i++)
    total += camera[i].inventoryValue();
    return total;
    }


    public void sortProd()
    {
    Comparator<Camera> comp = new Comparator<Camera>() {
    public int compare(Camera o1, Camera o2) {
    return o1.getName().compareTo(o2.getName());
    }
    };

    Arrays.sort(camera, comp);
    }


    public int getSize()
    {
    return camera.length;
    }


    public Camera get(int idx)
    {
    return camera[idx];
    }
    }

    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;


    public class InventoryPart4 extends JFrame {
    private static DecimalFormat currency = new DecimalFormat("$#,##0.00");


    private JTextField itemNumberTF;
    private JTextField productNameTF;
    private JTextField unitsInStockTF;
    private JTextField priceTF;
    private JTextField supplierNameTF;
    private JTextField restockFeeTF;
    private JTextField valueOfInventoryTF;

    private JTextField totalValueOfInventoryTF;

    private JButton priorBT;
    private JButton nextBT;

    private Supplier[] products;

    private int current = 0;


    public static void main(String[] args) {

    new InventoryPart4();
    }


    public InventoryPart4() {
    super("Inventory Part 4");
    setSize(500, 300);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Camera[] camera = new Camera[3];
    camera[0] = new Cam(1, "Kodak", 45, 15.0, 2);
    camera[1] = new Cam(2, "Sony", 50, 16.0, 32.1);
    camera[2] = new Cam(3, "Olympus", 10, 23.0, 53.8);

    sortArray();

    createComponents();

    setVisible(true);

    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;
    }

    protected void updateFields() {
    Supplier s = products[current];

    itemNumberTF.setText(String.valueOf(s.getItemNumbe r()));
    productNameTF.setText(s.getProductName());
    unitsInStockTF.setText(String.valueOf(s.getUnitsIn Stock()));
    priceTF.setText(currency.format(s.getPrice()));
    supplierNameTF.setText(s.getSupplierName());
    restockFeeTF.setText(currency.format(s.calculateRe stockFee()));
    valueOfInventoryTF.setText(currency.format(s.calcu lateInventory()));

    totalValueOfInventoryTF.setText(currency.format(ca lculateInventory()));
    }

    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;
    }



    public double calculateInventory() {
    double value = 0;
    for (int i = 0; i < products.length; i++) {
    value += products[i].calculateInventory();
    }
    return value;
    }


    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) {

    Supplier temp = products[i];
    products[i] = products[i + 1];
    products[i + 1] = temp;
    swapped = true;
    }
    }
    n = n - 1;
    } while (swapped);
    }
    }

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    3

    Default Re: Getting errors in my Imports

    Look at this line:
    Java Code:
    import java.awt.BorderLayout;{
    Why is there a curly brace?

Similar Threads

  1. Get the list of imports
    By kfranic in forum Advanced Java
    Replies: 9
    Last Post: 03-17-2011, 11:25 AM
  2. Security Measures with Imports
    By Moncleared in forum New To Java
    Replies: 0
    Last Post: 03-20-2009, 09:13 PM
  3. Imports
    By whosadork in forum New To Java
    Replies: 5
    Last Post: 09-07-2008, 01:32 AM
  4. cannot find imports..
    By little_polarbear in forum New To Java
    Replies: 4
    Last Post: 08-25-2008, 03:57 AM
  5. How to use Static Imports
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •