Inventory program Part 4

Help with modifying my program to use a GUI

Page 1 of 1

7 Replies - 19447 Views - Last Post: 20 July 2009 - 08:33 AM Rate Topic: -----

#1 patti1272000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-November 07

Inventory program Part 4

Post icon  Posted 08 December 2007 - 07:51 PM

I am having a lot of trouble with the next part of my program. This is the code that I have so far and I am supposed to modify this 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. Any help would be greatly appreciated.




import java.util.*;

/*=======================================*/
/*	  File: Inventory3.java			*/
/*=======================================*/
/*   Modify the Inventory Program by creating a subclass of the product class that uses one
additional unique feature of the product you chose (for the DVDs subclass, you could use
movie title, for example). In the subclass, create a method to calculate the value of the
inventory of a product with the same name as the method previously created for the
product class. The subclass method should also add a 5% restocking fee to the value of
the inventory of that product.?Modify the output to display this additional feature you have chosen and the restocking
fee.

/*=======================================*/
/*  Author: Patricia Lonergan			*/
/*  Class: IT/315 - Java Programming	 */
/*  Date: November 29, 2007			  */
/*=======================================*/


public class Inventory3 {

	public static void main(String args []) {
		Inventory invent = new Inventory();
		Movie dvd;

		dvd = new Movie("Movie1", 1, "Poseidon", 5, 14.95);
		invent.add(dvd);
		dvd = new Movie("Movie2", 2, "The Departed", 10, 12.99);
		invent.add(dvd);
		dvd = new Movie("Movie3", 3, "Material Girls", 6, 19.99);
		invent.add(dvd);
		dvd = new Movie("Movie4", 4, "The Brothers Grimm", 3, 10.99);
		invent.add(dvd);

		invent.display();
	} //end main

} // end class Inventory3


class DVD {
	private int dvdItem;
	private String dvdTitle;
	private int dvdStock;
	private double dvdPrice;

	public DVD(int item, String title, int stock, double price) {
		dvdItem  = item;
		dvdTitle = title;
		dvdStock = stock;
		dvdPrice = price;
	} //end four-argument constructor

	// set DVD Item
	public void setDvdItem(int item) {
		dvdItem = item;
	} //end method  set Dvd Item

	//return DVD Item
	public int getDvdItem() {
		return dvdItem;
	} //end method get Dvd Item

	//set DVD Title
	public void setDvdTitle(String title) {
		dvdTitle = title;
	} //end method set Dvd Title

	//return Dvd Title
	public String getDvdTitle() {
		return dvdTitle;
	} //end method get Dvd Title

	public void setDvdStock(int stock) {
		dvdStock = stock;
	} //end method set Dvd Stock

	//return dvd Stock
	public int getDvdStock() {
		return dvdStock;
	} //end method get Dvd Stock

	public void setDvdPrice(double price) {
		dvdPrice = price;
	} //end method setdvdPrice

	//return DVD Price
	public double getDvdPrice() {
		return dvdPrice;
	} //end  method get Dvd Price

	//calculate inventory value
	public double value() {
		return dvdPrice * dvdStock;
	} //end method value

	public String toString() {
		return String.format("item=%3d   title=%-20s   units=%3d   price=$%6.2f   value=$%7.2f",
							  dvdItem, dvdTitle, dvdStock, dvdPrice, value());
	}

} //end class DVD


class Movie extends DVD {
	private String movieTitle;

	public Movie(String title, int item, String dtitle, int stock, double price) {
		super(item, dtitle,  stock, price);
		movieTitle = title;
	}

	public double value() {
		double value = getDvdPrice() * getDvdStock();
		value = 1.05 * value;
		return value;
	} //end method value

	public String toString() {
		String s = String.format("Movie title=%-12s", movieTitle);
		s = s + " " + super.toString();
		return s;
	}

} // end class Movie


class Inventory {
	private DVD[] dvds;
	private int count;

	Inventory() {
		dvds = new DVD[10];
		count = 0;
	}

	public void add(DVD dvd) {
		dvds[count] = dvd;
		++count;
		sort();
	}

	public double entireValue() {
		double value = 0;
		for (int i = 0; i < count; i++) {
			value = value + dvds[i].value();
		}
		return value;
	}

	public void sort() {
		for (int index = 1; index < count; index++) {
			DVD key = dvds[index];
			int position = index;

			//  Shift larger values to the right
			while (position > 0 && key.getDvdTitle().compareTo(dvds[position-1].getDvdTitle()) < 0) {
				dvds[position] = dvds[position-1];
				position--;
			}
			dvds[position] = key;
		}
	}

	public void display() {
		System.out.println("\nThe inventory contains " + count + " DVDs\n");
		for (int i = 0; i < count; i++)
			System.out.printf("%3d  %s\n", i, dvds[i]);
		System.out.printf("\nThe total inventory value is $%.2f\n\n", entireValue());
	}

} // end class Inventory



Is This A Good Question/Topic? 0
  • +

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: Inventory program Part 4

Posted 08 December 2007 - 08:29 PM

Well it looks like you have most of the stuff put together already so that is good. The next step will be to simply put all that aside and work on first getting some kind of GUI up and working. So create a new file and new class which will implement JFrame. This is using the Swing library which comes in the package javax.swing.* package.

To get you started I have created a basic swing application for you.

// Import the java swing package so we can use swing controls like JFrame.
import javax.swing.*;		

public class HelloWorldSwing {
	public static void main(String[] args) {
		// Create a JFrame (application window with the title "HelloWorldSwing")
		JFrame frame = new JFrame("HelloWorldSwing");
	
		// Set it so that it terminates the program when the window is closed.
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Set the windows size and then show it.
		frame.setSize(300,300);
		frame.setVisible(true);
	}
}



If you compile and run this, you should see a nice little GUI window appear. It doesn't do much because you don't have any controls placed on it yet, but it is a good start.

As you can imagine there are virtually all the standard controls there for swing like JButton, JLabel, JMenu etc. Each of these controls can be found in the Java documentation online.

So to start you create a control, configure its properties and add it to the JFrame which I have helped start for you. Once you got this, you simply make your classes you wrote output to these controls. Example, when you startup the form you would create your inventory class. When you click an add button, the button's ActionListener would then create a product and pass it to the inventory.

You can read up on events at the following link....

Java tutorial: Writing Event Listeners

I hope you find the information helpful. Enjoy! :)

This post has been edited by Martyr2: 08 December 2007 - 08:31 PM

Was This Post Helpful? 1

#5 b_h  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-December 07

Re: Inventory program Part 4

Posted 09 December 2007 - 10:15 AM

I've helped people with a similar question before. You can check out the code at: ***edit: link removed***

*1lacca: Either help, or advertise in the right topic, but we don't ask for money here, ty.
Was This Post Helpful? 0
  • +
  • -

#6 otwhitty1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-December 07

Re: Inventory program Part 4

Posted 23 December 2007 - 01:00 PM

We are needing help. Not answers that we have to pay for.
Was This Post Helpful? 0
  • +
  • -

#7 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Inventory program Part 4

Posted 24 December 2007 - 02:33 AM

Quote

We are needing help. Not answers that we have to pay for.

Did Martyr2's answer help? It's not really clear from your post, but I think he
has just given you what you've asked for.
Was This Post Helpful? 0
  • +
  • -

#8 Beachgirl029  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-July 09

Re: Inventory program Part 4

Posted 19 July 2009 - 11:36 AM

Martyr2, I am greatfull to you in your way of explaining the Java Programming so people get a better concept of how everything works and to better understand the proper usage of the code. I do need your help. I am in the Inventory part 4, and I do not know where to begin. I have never experimented with GUI, I need help with a good explanation of where to start. My program up to this point is running GREAT. I am not sure how to start this current modification. Let me know if you need me to send you what i have so far. Thank you for any advice you can give me.


View PostMartyr2, on 8 Dec, 2007 - 07:29 PM, said:

Well it looks like you have most of the stuff put together already so that is good. The next step will be to simply put all that aside and work on first getting some kind of GUI up and working. So create a new file and new class which will implement JFrame. This is using the Swing library which comes in the package javax.swing.* package.

To get you started I have created a basic swing application for you.

// Import the java swing package so we can use swing controls like JFrame.
import javax.swing.*;		

public class HelloWorldSwing {
	public static void main(String[] args) {
		// Create a JFrame (application window with the title "HelloWorldSwing")
		JFrame frame = new JFrame("HelloWorldSwing");
	
		// Set it so that it terminates the program when the window is closed.
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Set the windows size and then show it.
		frame.setSize(300,300);
		frame.setVisible(true);
	}
}



If you compile and run this, you should see a nice little GUI window appear. It doesn't do much because you don't have any controls placed on it yet, but it is a good start.

As you can imagine there are virtually all the standard controls there for swing like JButton, JLabel, JMenu etc. Each of these controls can be found in the Java documentation online.

So to start you create a control, configure its properties and add it to the JFrame which I have helped start for you. Once you got this, you simply make your classes you wrote output to these controls. Example, when you startup the form you would create your inventory class. When you click an add button, the button's ActionListener would then create a product and pass it to the inventory.

You can read up on events at the following link....

Java tutorial: Writing Event Listeners

I hope you find the information helpful. Enjoy! :)

This post has been edited by Beachgirl029: 19 July 2009 - 11:41 AM

Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Inventory program Part 4

Posted 19 July 2009 - 08:32 PM

@Beach

hey there, if you need help, create your own topic with the proper title. Someone will help you im sure. Whether it is advice on what you should do or if it is errors you need help getting rid of and can't do it yourself. We are a very helpful bunch, just go through proper procedure.
1. Own topic (not nice to hi-jack others :P)
2. Post your code in the code tags (find those in the rules of posting)
3. Post Question, whether it be advice errors or logic help needed

Goodluck!
Was This Post Helpful? 1

#11 Beachgirl029  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-July 09

Re: Inventory program Part 4

Posted 20 July 2009 - 08:33 AM

Thank you Fuzzyness I am new to the proper procedures. ;)

View PostFuzzyness, on 19 Jul, 2009 - 07:32 PM, said:

@Beach

hey there, if you need help, create your own topic with the proper title. Someone will help you im sure. Whether it is advice on what you should do or if it is errors you need help getting rid of and can't do it yourself. We are a very helpful bunch, just go through proper procedure.
1. Own topic (not nice to hi-jack others :P)
2. Post your code in the code tags (find those in the rules of posting)
3. Post Question, whether it be advice errors or logic help needed

Goodluck!

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1