4 Replies - 1227 Views - Last Post: 12 February 2011 - 08:16 PM Rate Topic: -----

#1 Guest_Sarah*


Reputation:

Inventory Program part 4

Posted 12 February 2011 - 04:04 PM

Below is my code for inventory program part 4. We have to• 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). There should not be any output in the DOS console.

• Source code should adhere to the coding conventions outlined for this course

When I run this it just terminates out of it. It does not show me any errors. I don't understand what I am doing wrong.

public class Laptop4
{
	int LaptopNumber;
	String LaptopName;
	int quantity;
	double price;

	
	public Laptop4(int number, String name, int quantity, double price)
	{
		LaptopNumber = number;
		LaptopName = name;
		this.quantity = quantity;
		this.price = price; 
	}

	public int getLaptopNumber()
	{
		return LaptopNumber;
	}

	public void setlaptopNumber(int value)
	{
		LaptopNumber = value; 
	}

	public String getlaptopName()
	{
		return LaptopName;
	}

	public int getQuantity()
	{
		return quantity;
	}
	public void setQuantity(int value)
	{
		quantity = value;
	}

	public double getPrice()
	{
		return price;
	}

	public void setPrice(double value)
	{
		price = value;
	}

	public double getInventoryValue()
	{
		return getPrice() * getQuantity();
	}

	public String toString()
	{
		return String.format("\nINVENTORY INFO\n%s %s\n%s\t %s\n%s\t %d\n%s\t $%,.2f\n%s\t $%,.2f","Product Number: ", getLaptopNumber(),
				"Product Name: ", getlaptopName(), "Units In Stock:", getQuantity(),
				"Price per Unit:", getPrice(), "Total Inventory Value:", getInventoryValue());
	}
}
public class Inventory {

	private ExtendedLaptop[] list;

	
	public Inventory(int size) {
		list = new ExtendedLaptop[size];
	}
	
	public int size() {
		return list.length;
	}




	public void add(ExtendedLaptop d, int p) {
		list[p] = d;
	}


	public Laptop4 get(int i) {
		return list[i];
	}

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

	public void pack() {
		// TODO Auto-generated method stub
		
	}

	public void setVisible(boolean B)/> {
		// TODO Auto-generated method stub
		
	}

	public Object totalValue() {
		// TODO Auto-generated method stub
		return null;
	}
}

import javax.swing.*;

import java.awt.event.*;
public class InventoryPart4 extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final Inventory i;
	private final JTextArea txt;
	private static int view = 0;

	public static void main(final String[] args) {
		final Inventory gui = new Inventory(view);
		gui.pack();
		gui.setVisible(true);
	}
	public void showLaptop() {
		txt.setText("Laptop Details:\n");
			txt.append(i.get(view) + "\n");
		
		txt.append(String.format("\nTotal value = $%.2f", i.totalValue()));
		

	}
	
	public InventoryPart4() {
		super("Laptop");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // quit when closed

		
		final ExtendedLaptop p1 = new ExtendedLaptop(0, "Elite", 5, 349.99, "Compaq");
		final ExtendedLaptop p2 = new ExtendedLaptop(1, "Core i3", 14, 749.99, "Hewlett Packard");
		final ExtendedLaptop p3 = new ExtendedLaptop(2, "Aspire", 22, 299, "Acer");
		final ExtendedLaptop p4 = new ExtendedLaptop(3, "Inspirion", 11, 799.99, "Dell");
		final ExtendedLaptop p5 = new ExtendedLaptop(4, "Aspire 2", 5, 499, "Acer");
		
		i = new Inventory(3);
		i.add(p1, 0);
		i.add(p2, 1);
		i.add(p3, 2);
		i.add(p4, 3);
		i.add(p5, 4);
		
		i.sort();

		
		for (int k = 0; k < 3; k++) {
			System.out.println(i.get(k));
		}
		
		System.out.println();
		
		
		System.out.printf("Total value = $%.2f", i.totalValue());
		
		
		
		
	
		final JPanel panel = new JPanel();
		txt = new JTextArea(15,20);
		txt.setEditable(false);
		panel.add(txt);
		
		final JButton next = new JButton("Next");
		next.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				if (view < i.size()-1) view++; 
				showLaptop();
			}
		});
		panel.add(next);
		
		getContentPane().add(panel);
		
		showLaptop();
	}

}
class ExtendedLaptop extends Laptop4 {
	
	private String manufacturer = "";
	
	public ExtendedLaptop(int item, String name, int units, double price, String manufacturer) {
		super(item, name, units, price);
		this.manufacturer = manufacturer;
	}

	public String getManufacturer() {
		return manufacturer;
	}

	public void setManufacturer(String Manufacturer, String manufacturer) {
	}
	

	public double getInventoryValue() {
		return 1.05*getQuantity()*getPrice();
	}
	

	public double fee() {
		return 0.05*getQuantity()*getPrice();
	}
	

	public String toString()
	{
		return String.format("\nINVENTORY INFO\n%s %s\n%s\t %s\n%s\t %d\n%s\t $%,.2f\n%s\t%s\n%s\t $%,.2f \n%s\t$%,.2f","Product Number: ", getLaptopNumber(),
				"Product Name: ", getlaptopName(), "Units In Stock:", getQuantity(),
				"Price per Unit:", getPrice(), 
				"Manufacturer:", getManufacturer(),
				"Price:", "Total Inventory Value:", getInventoryValue());
	}
	
}




Is This A Good Question/Topic? 0

Replies To: Inventory Program part 4

#2 moobler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 224
  • Joined: 21-January 11

Re: Inventory Program part 4

Posted 12 February 2011 - 06:40 PM

You create an instance of your Inventory class called 'gui' and then you call gui.setVisible to attempt to show it. The problem is that the Inventory class is not set up as a JFrame or any other type of window. Calling setVisible does absolutely nothing.
Was This Post Helpful? 0
  • +
  • -

#3 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Inventory Program part 4

Posted 12 February 2011 - 06:48 PM

Your creating an instance of the Inventory class when that class doesn't even contain a frame?

So all your doing is invoking these method which do not contain any logic within their bodies which you do not need and this is why your code compiles and then stops.

	public void pack() {
		// TODO Auto-generated method stub
		
	}

	public void setVisible(boolean B)/> {
		// TODO Auto-generated method stub
		
	}


Was This Post Helpful? 1
  • +
  • -

#4 snicole  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-February 11

Re: Inventory Program part 4

Posted 12 February 2011 - 08:09 PM

View Postmoobler, on 12 February 2011 - 06:40 PM, said:

You create an instance of your Inventory class called 'gui' and then you call gui.setVisible to attempt to show it. The problem is that the Inventory class is not set up as a JFrame or any other type of window. Calling setVisible does absolutely nothing.


So I should delete the setVisible, and how do I create an instance in my inventory called gui?
Was This Post Helpful? 0
  • +
  • -

#5 moobler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 224
  • Joined: 21-January 11

Re: Inventory Program part 4

Posted 12 February 2011 - 08:16 PM

If you don't know how to create a GUI you should take a look at this tutorial: Basic GUI In Java (using JFrames)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1