coding issues

creating a GUI for dvd inventory

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1415 Views - Last Post: 03 May 2009 - 02:28 PM Rate Topic: -----

#1 crynine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-May 09

coding issues

Post icon  Posted 02 May 2009 - 09:40 AM

Here is my code, I am getting one error message that says the directory name and then java:10: ' ;' expected I did not mean to post all my code but I am curious if I even put the thing together right, I think I have to many classes involved or have named them wrong? Every time I change something on this code anything at all it throws more errors. So I don't know what is right or wrong anymore. Can anyone at least point me in a direction so I have a place to start from? Do I even need the paint[] code in there as well? This is susposed to be basic java and does not seem so basic anymore.





import java.io;
import java.util;
import javax.swing;
import java.Jlabel

 public class Inventory_Part4 extends JFrame
{

	// create inventory for the DVD
	private Inventory theInventory;

	// index in the inventory of the currently displayed DVD.
	// the index starts at 0, goes to the number of DVD in the inventory minus 1
	private int index = 0;

	// GUI elements to display currently selected DVD's information
	private JLabel itemNumberLabel = new JLabel("Product Number: ");
	private JTextField itemNumberText;

	private JLabel produdctNameLabel = new JLabel("Product Name: ");
	private JTextField prodnameText;

	private JLabel genreLabel = new JLabel("Product Genre: ");
	private JTextField genreText;

	private JLabel numinstockLabel = new JLabel("Number in Stock: ");
	private JTextField numinstockText;

	private JLabel productPriceLabel = new JLabel("Product Price: ");
	private JTextField productPriceText;

	private JLabel restockingFeeLabel = new JLabel("Restocking Fee: ");
	private JTextField restockingFeeText;

	private JLabel valueLabel = new JLabel("Inventory Value: ");
	private JTextField valueText;

	private JLabel totalValueLabel = new JLabel("Total Inventory Value: ");
	private JLabel totalValueText;

	// setup the buttons in the GUI
	// go to the next product in the list
	private Action nextAction  = new AbstractAction("Next")
	{
		public void actionPerformed(ActionEvent evt) {
			// go forward one product
			index++;

			// check to see if there is a next product
			if ( index == theInventory.getSize() ) {
				// if we're at the last product in the list, then we can't go any further forward
				// so back up one
				index--;
			}

			repaint();
		}
	};
	private JButton nextButton = new JButton(nextAction);


	// Add the product to the underlying inventory object
	public void addDVDToInventory(DVD temp)
	{
		theInventory.addDVD(temp);
		sortInventory();
		repaint();
	}

	// sort the items in the underlying inventory object
	public void sortInventory()
	{
		theInventory.sortInventory();
		repaint();
	}

	// constructor for the GUI, in charge of creating all GUI elements
	public Inventory_Part4()
	{
		// create the inventory object that will hold the product information
		theInventory = new Inventory();

		// setup the GUI
		// add the next button to the top of the GUI
		// setup a panel to collect all the buttons in a FlowLayout
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(nextButton);
		getContentPane().add(buttonPanel, BorderLayout.NORTH);

		// product information
		// setup a panel to collect all the components.
		JPanel centerPanel = new JPanel(new GridLayout(8, 2, 0, 4));

		centerPanel.add(itemNumberLabel);
		itemNumberText = new JTextField("");
		itemNumberText.setEditable(false);
		centerPanel.add(itemNumberText);

		centerPanel.add(produdctNameLabel);
		prodnameText = new JTextField("");
		prodnameText.setEditable(false);
		centerPanel.add(prodnameText);

		centerPanel.add(genreLabel);
		genreText = new JTextField("");
		genreText.setEditable(false);
		centerPanel.add(genreText);

		centerPanel.add(numinstockLabel);
		numinstockText = new JTextField("");
		numinstockText.setEditable(false);
		centerPanel.add(numinstockText);

		centerPanel.add(productPriceLabel);
		productPriceText = new JTextField("");
		productPriceText.setEditable(false);
		centerPanel.add(productPriceText);

		centerPanel.add(restockingFeeLabel);
		restockingFeeText = new JTextField("");
		restockingFeeText.setEditable(false);
		centerPanel.add(restockingFeeText);

		centerPanel.add(valueLabel);
		valueText = new JTextField("");
		valueText.setEditable(false);
		centerPanel.add(valueText);

		// add the overall inventory information to the panel
		centerPanel.add(totalValueLabel);
		totalValueText = new JLabel("");
		centerPanel.add(totalValueText);

		// add the panel to the center of the GUI's window
		getContentPane().add(centerPanel, BorderLayout.CENTER);

	}

	// Get the GUI to show up on the screen
	public void showGUI() {
		this.setDefaultCloseOperation( EXIT_ON_CLOSE );
		this.pack();
		this.setSize(600, 275);
		this.setResizable(false);
		this.setLocationRelativeTo( null );
		this.setVisible(true);

		repaint();
	}

	// repaint the GUI with current product's information
	public void repaint() {

		DVD temp = theInventory.getDVD(index);

		if (temp != null) {

			itemNumberText.setText( ""+temp.getItemNumber() );

			prodnameText.setText( temp.getItemName() );
			genreText.setText( temp.getGenre() );
			productPriceText.setText( String.format("$%.2f", temp.getItemPrice()) );
			restockingFeeText.setText( String.format("$%.2f", temp.getRestockingFee()) );
			numinstockText.setText( ""+temp.getItemsInStock() );

			valueText.setText( String.format("$%.2f", temp.getInventoryValue() ) );

		}

		totalValueText.setText( String.format("$%.2f", theInventory.getTotalValueOfAllInventory() ) );

	}

}  // End Dvd class

/*
 * class which reprsents a collection of DVDs in an inventory.
 */
 public class Inventory
{
	// place to store all the DVDs in the inventory
	DVD[] inventory;

	// create an inventory capable of holding different DVDs
	public Inventory( )
	{
		inventory = new DVD[0];
	}

	// add the DVDs to the end of the inventory
	public void addDVD(DVD new_DVD)
	{
		// create a new array of DVDs that can hold one more than the current array of DVDs
		DVD[] new_inventory = new DVD[this.getSize() + 1];

		//copy all the old ones to the front of the new array
		for (int i = 0; i < this.getSize(); i++) {
			new_inventory[i] = inventory[i];
		}

		// add the new DVD to the end of the new array
		new_inventory[this.getSize()] = new_DVD;

		// replace the old inventory with the new one
		inventory = new_inventory;

	}
	// return the DVDs at the location index in the inventory
	public DVD getDVD(int index)
	{
		return inventory[index];
	}

	// return the number of DVDs in the inventory
	public int getSize()
	{
		return inventory.length;
	}

	// run through all the DVDs in the inventory, and add up their value.
	// this includes the restocking fee
	public double getTotalValueOfAllInventory()
	{
		double tot = 0.00;

		for(int i = 0; i < getSize(); i++)
		{
			tot += inventory[i].getInventoryValue();
		}
		return tot;
	}

	// sort the DVDs in the inventory by movie title
	public void sortInventory()
	{
		Arrays.sort(inventory, 0, getSize());
	}
}



//Class Product

class Product implements Comparable<Product> { //Start the Product class

	String itemNumber; //stores item number
	String name; //stores the CD's name
	int stockQuantity; //stores number of units in stock
	double priceperunit; //stores the price of each unit

	//Constructor method
	public Product(String Item_Number, String Item_Name, int Items_in_Stock, double Item_Price){
		itemNumber = Item_Number;
		name = Item_Name;
		stockQuantity = Items_in_Stock;
		priceperunit = Item_Price;
	}

	public void setItemName(String Item_Name) //Method to set and get the item name
	{
		name = Item_Name;
	}

	public String getItemName() {
		return name;
	}

	public void setItemNumber(String Item_Number) //Method to set and get the item number
	{
		itemNumber = Item_Number;
	}

	public String getItemNumber(){
		return itemNumber;
	}

	public void setItemsInStock(int Items_in_Stock) //Method to set and get the quantity in stock
	{
		stockQuantity = Items_in_Stock;
	}

	public int getItemsInStock(){
		return stockQuantity;
	}

	public void setItemPrice(double Item_Price) //Method to set and get the item price
	{
		priceperunit = Item_Price;
	}

	public double getItemPrice(){
		return priceperunit;
	}

	public double getInventoryValue() //Method to calculate the value of the in stock inventory
	{
		return priceperunit * stockQuantity;
	}

	// returns -1, 0, or 1 depending on if the compared to object should appear before, the same, or after the current item
	public int compareTo (Product p)
	{
		return name.compareTo(p.getItemName());
	}

	public String toString() {
		//Return the product details as a string
		return "Product Number: "+this.getItemNumber() + "\n" +
		"Product Name: "+this.getItemName() + "\n" +
		"Number of Product items in stock: "+this.getItemsInStock() + "\n" +
		"Product price: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getItemPrice()) + "\n" +
		"Inventory Value: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getInventoryValue()) + "\n";
	}
} // end Product Class


class DVD extends Product {
	// Genre of the DVD being added
	private String DVDGenre;
	// Restock fee to add to inventory value
	private double restockingFee;

	// initialize Product constructor
	public DVD(String DVDTitle,  String DVDGenre, String Item_Number,  int Items_in_Stock, double Item_Price) {

		super(Item_Number, DVDTitle, Items_in_Stock, Item_Price);

		this.DVDGenre = DVDGenre;

		// the default restocking fee is 5%
		this.restockingFee = 0.05;
	}

	// getter and setter for the DVD's genre
	public void setGenre(String DVDGenre)
	{
		this.DVDGenre = DVDGenre;
	}
	public String getGenre()
	{
		return DVDGenre;
	}


	// calculates the restocking fee of the DVDs based on a percentage of the value
	public double getRestockingFee() // Figures restocking fee of inventory
	{
		return super.getInventoryValue() * restockingFee;
	}

	// include a restocking fee in the inventory's value
	public double getInventoryValue() // Figures total inventory value including restocking fee
	{
		return  super.getInventoryValue() + getRestockingFee();
	}

	// returns a string representation of the item
	public String toString() {
		//Return the product details as a string
		return "Product Number: "+this.getItemNumber() + "\n" +
		"Product Name: "+this.getItemName() + "\n" +
		"Product Genre			: "  + DVDGenre			+ "\n" +
		"Number of Product items in stock: "+this.getItemsInStock() + "\n" +
		"Product price: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getItemPrice()) + "\n" +
		"Restocking Fee   : "  + NumberFormat.getCurrencyInstance(Locale.US).format(getRestockingFee()) + "\n" +
		"Inventory Value: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getInventoryValue()) + "\n";
	}

}// end DVD

class Inventory_Part4 {

	public static void main(String [] args) //main() method
	{

		InventoryGUI theInventoryGUI = new InventoryGUI();

		// get input from the user until stop is entered as the DVD name
		while(true) {
			System.out.print("Please enter the product number (type \"stop\" to quit): ");

			//Create a new DVD object
			DVD theDVD = new DVD(Item_Name, Item_Genre, Item_Number, Items_in_Stock, Item_Price);

			theInventoryGUI.addDVDToInventory(theDVD);
		}

		// sort the DVDs by name
		theInventoryGUI.sortInventory();

		// Display the inventory of DVD on the screen, one DVD at a time
		theInventoryGUI.showGUI();

	}//end of main() method

}//end of Dvd class




Is This A Good Question/Topic? 0
  • +

Replies To: coding issues

#2 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: coding issues

Posted 02 May 2009 - 09:52 AM

hey
you had declared multiple classes with same name Inventory_part4 thats impossible in the same file please repost your code
Was This Post Helpful? 1

#3 crynine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-May 09

Re: coding issues

Posted 02 May 2009 - 09:54 AM

Ok I thought I would add I have spent all night on this code and have done the code several different ways with many errors each time although I have gotten it down to one error and as many as 73 somehow. My other question to my previous post is did I add to much unnecessary things in my code? I am just needing to display the information one product at a time and this includes the item#, the name of the product, the # of units in stock, the price of the units, and the value of the inventory of that product while being able to have the GUI display the value of the entire inventory and restocking fee. I have read the text but am not sure how to put what they show you in the text in context here as there are several examples but its very very basic. All I am asking for is a point in the right direction, nothing more , don't want you doing my homework but I need to see an example with the dvd in place so I can get the idea and do it on my own, Is this possible? Oh and I am terrible with the curly braces can this be part of my problem it has been in the past?

Thank You for your time.

View Postprajayshetty, on 2 May, 2009 - 08:52 AM, said:

hey
you had declared multiple classes with same name Inventory_part4 thats impossible in the same file please repost your code



Hey Thanks, I thought that might be some of the issue, So I need to first clean up my class names and go from there correct?
Was This Post Helpful? 0
  • +
  • -

#4 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: coding issues

Posted 02 May 2009 - 09:57 AM

i appreciate your idea of doing your self i am just getting what your program is doing once i get the idea i will go to remove the errors have you declared a class called inventory gui in your program??? some where which you have not posted here if yes then please tell

This post has been edited by prajayshetty: 02 May 2009 - 10:02 AM

Was This Post Helpful? 0
  • +
  • -

#5 crynine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-May 09

Re: coding issues

Posted 02 May 2009 - 10:14 AM

I do have the GUIInventory toward the bottom. I changed the classes so that they were seperated. I am a beginner so I hope this is what you needed. I got three errors from changing which I think is a good sign. Here is the new code.

*Edited to remove code that is posted between the correct tags in the next topics

This post has been edited by pbl: 02 May 2009 - 10:40 AM

Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: coding issues

Posted 02 May 2009 - 10:15 AM

Use code tags! :angry:

import java.io;
import java.util;
import javax.swing;
import java.Jlabel

 public class Inventory_Part4 extends JFrame
{

	// create inventory for the DVD
	private Inventory theInventory;

	// index in the inventory of the currently displayed DVD.
	// the index starts at 0, goes to the number of DVD in the inventory minus 1
	private int index = 0;

	// GUI elements to display currently selected DVD's information
	private JLabel itemNumberLabel = new JLabel("Product Number: ");
	private JTextField itemNumberText;

	private JLabel produdctNameLabel = new JLabel("Product Name: ");
	private JTextField prodnameText;

	private JLabel genreLabel = new JLabel("Product Genre: ");
	private JTextField genreText;

	private JLabel numinstockLabel = new JLabel("Number in Stock: ");
	private JTextField numinstockText;

	private JLabel productPriceLabel = new JLabel("Product Price: ");
	private JTextField productPriceText;

	private JLabel restockingFeeLabel = new JLabel("Restocking Fee: ");
	private JTextField restockingFeeText;

	private JLabel valueLabel = new JLabel("Inventory Value: ");
	private JTextField valueText;

	private JLabel totalValueLabel = new JLabel("Total Inventory Value: ");
	private JLabel totalValueText;

	// setup the buttons in the GUI
	// go to the next product in the list
	private Action nextAction  = new AbstractAction("Next")
	{
		public void actionPerformed(ActionEvent evt) {
			// go forward one product
			index++;

			// check to see if there is a next product
			if ( index == theInventory.getSize() ) {
				// if we're at the last product in the list, then we can't go any further forward
				// so back up one
				index--;
			}

			repaint();
		}
	};
	private JButton nextButton = new JButton(nextAction);


	// Add the product to the underlying inventory object
	public void addDVDToInventory(DVD temp)
	{
		theInventory.addDVD(temp);
		sortInventory();
		repaint();
	}

	// sort the items in the underlying inventory object
	public void sortInventory()
	{
		theInventory.sortInventory();
		repaint();
	}//end public class Inventory_Part4

	// constructor for the GUI, in charge of creating all GUI elements
	public Dvd()
	{
		// create the inventory object that will hold the product information
		theInventory = new Inventory();

		// setup the GUI
		// add the next button to the top of the GUI
		// setup a panel to collect all the buttons in a FlowLayout
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(nextButton);
		getContentPane().add(buttonPanel, BorderLayout.NORTH);

		// product information
		// setup a panel to collect all the components.
		JPanel centerPanel = new JPanel(new GridLayout(8, 2, 0, 4));

		centerPanel.add(itemNumberLabel);
		itemNumberText = new JTextField("");
		itemNumberText.setEditable(false);
		centerPanel.add(itemNumberText);

		centerPanel.add(produdctNameLabel);
		prodnameText = new JTextField("");
		prodnameText.setEditable(false);
		centerPanel.add(prodnameText);

		centerPanel.add(genreLabel);
		genreText = new JTextField("");
		genreText.setEditable(false);
		centerPanel.add(genreText);

		centerPanel.add(numinstockLabel);
		numinstockText = new JTextField("");
		numinstockText.setEditable(false);
		centerPanel.add(numinstockText);

		centerPanel.add(productPriceLabel);
		productPriceText = new JTextField("");
		productPriceText.setEditable(false);
		centerPanel.add(productPriceText);

		centerPanel.add(restockingFeeLabel);
		restockingFeeText = new JTextField("");
		restockingFeeText.setEditable(false);
		centerPanel.add(restockingFeeText);

		centerPanel.add(valueLabel);
		valueText = new JTextField("");
		valueText.setEditable(false);
		centerPanel.add(valueText);

		// add the overall inventory information to the panel
		centerPanel.add(totalValueLabel);
		totalValueText = new JLabel("");
		centerPanel.add(totalValueText);

		// add the panel to the center of the GUI's window
		getContentPane().add(centerPanel, BorderLayout.CENTER);

	}

	// Get the GUI to show up on the screen
	public void showGUI() {
		this.setDefaultCloseOperation( EXIT_ON_CLOSE );
		this.pack();
		this.setSize(600, 275);
		this.setResizable(false);
		this.setLocationRelativeTo( null );
		this.setVisible(true);

		repaint();
	}
	// repaint the GUI with current product's information
	public void repaint() {

		DVD temp = theInventory.getDVD(index);

		if (temp != null) {

			itemNumberText.setText( ""+temp.getItemNumber() );

			prodnameText.setText( temp.getItemName() );
			genreText.setText( temp.getGenre() );
			productPriceText.setText( String.format("$%.2f", temp.getItemPrice()) );
			restockingFeeText.setText( String.format("$%.2f", temp.getRestockingFee()) );
			numinstockText.setText( ""+temp.getItemsInStock() );

			valueText.setText( String.format("$%.2f", temp.getInventoryValue() ) );

		}

		totalValueText.setText( String.format("$%.2f", theInventory.getTotalValueOfAllInventory() ) );

	}

}  // End Dvd class

/*
 * class which reprsents a collection of DVDs in an inventory.
 */
 public class Inventory
{
	// place to store all the DVDs in the inventory
	DVD[] inventory;

	// create an inventory capable of holding different DVDs
	public Inventory( )
	{
		inventory = new DVD[0];
	}

	// add the DVDs to the end of the inventory
	public void addDVD(DVD new_DVD)
	{
		// create a new array of DVDs that can hold one more than the current array of DVDs
		DVD[] new_inventory = new DVD[this.getSize() + 1];

		//copy all the old ones to the front of the new array
		for (int i = 0; i < this.getSize(); i++) {
			new_inventory[i] = inventory[i];
		}

		// add the new DVD to the end of the new array
		new_inventory[this.getSize()] = new_DVD;

		// replace the old inventory with the new one
		inventory = new_inventory;

	}
	// return the DVDs at the location index in the inventory
	public DVD getDVD(int index)
	{
		return inventory[index];
	}

	// return the number of DVDs in the inventory
	public int getSize()
	{
		return inventory.length;
	}

	// run through all the DVDs in the inventory, and add up their value.
	// this includes the restocking fee
	public double getTotalValueOfAllInventory()
	{
		double tot = 0.00;

		for(int i = 0; i < getSize(); i++)
		{
			tot += inventory[i].getInventoryValue();
		}
		return tot;
	}

	// sort the DVDs in the inventory by movie title
	public void sortInventory()
	{
		Arrays.sort(inventory, 0, getSize());
	}
}// end class Inventory



// public class Product

class Product implements Comparable<Product> { //Start the Product class

	String itemNumber; //stores item number
	String name; //stores the CD's name
	int stockQuantity; //stores number of units in stock
	double priceperunit; //stores the price of each unit

	//Constructor method
	public Product(String Item_Number, String Item_Name, int Items_in_Stock, double Item_Price){
		itemNumber = Item_Number;
		name = Item_Name;
		stockQuantity = Items_in_Stock;
		priceperunit = Item_Price;
	}

	public void setItemName(String Item_Name) //Method to set and get the item name
	{
		name = Item_Name;
	}

	public String getItemName() {
		return name;
	}

	public void setItemNumber(String Item_Number) //Method to set and get the item number
	{
		itemNumber = Item_Number;
	}

	public String getItemNumber(){
		return itemNumber;
	}

	public void setItemsInStock(int Items_in_Stock) //Method to set and get the quantity in stock
	{
		stockQuantity = Items_in_Stock;
	}

	public int getItemsInStock(){
		return stockQuantity;
	}

	public void setItemPrice(double Item_Price) //Method to set and get the item price
	{
		priceperunit = Item_Price;
	}

	public double getItemPrice(){
		return priceperunit;
	}

	public double getInventoryValue() //Method to calculate the value of the in stock inventory
	{
		return priceperunit * stockQuantity;
	}

	// returns -1, 0, or 1 depending on if the compared to object should appear before, the same, or after the current item
	public int compareTo (Product p)
	{
		return name.compareTo(p.getItemName());
	}

	public String toString() {
		//Return the product details as a string
		return "Product Number: "+this.getItemNumber() + "\n" +
		"Product Name: "+this.getItemName() + "\n" +
		"Number of Product items in stock: "+this.getItemsInStock() + "\n" +
		"Product price: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getItemPrice()) + "\n" +
		"Inventory Value: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getInventoryValue()) + "\n";
	}
} // end class Product

public class Extension
class DVD extends Product {
	// Genre of the DVD being added
	private String DVDGenre;
	// Restock fee to add to inventory value
	private double restockingFee;

	// initialize Product constructor
	public DVD(String DVDTitle,  String DVDGenre, String Item_Number,  int Items_in_Stock, double Item_Price) {

		super(Item_Number, DVDTitle, Items_in_Stock, Item_Price);

		this.DVDGenre = DVDGenre;

		// the default restocking fee is 5%
		this.restockingFee = 0.05;
	}

	// getter and setter for the DVD's genre
	public void setGenre(String DVDGenre)
	{
		this.DVDGenre = DVDGenre;
	}
	public String getGenre()
	{
		return DVDGenre;
	}


	// calculates the restocking fee of the DVDs based on a percentage of the value
	public double getRestockingFee() // Figures restocking fee of inventory
	{
		return super.getInventoryValue() * restockingFee;
	}

	// include a restocking fee in the inventory's value
	public double getInventoryValue() // Figures total inventory value including restocking fee
	{
		return  super.getInventoryValue() + getRestockingFee();
	}

	// returns a string representation of the item
	public String toString() {
		//Return the product details as a string
		return "Product Number: "+this.getItemNumber() + "\n" +
		"Product Name: "+this.getItemName() + "\n" +
		"Product Genre			: "  + DVDGenre			+ "\n" +
		"Number of Product items in stock: "+this.getItemsInStock() + "\n" +
		"Product price: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getItemPrice()) + "\n" +
		"Restocking Fee   : "  + NumberFormat.getCurrencyInstance(Locale.US).format(getRestockingFee()) + "\n" +
		"Inventory Value: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getInventoryValue()) + "\n";
	}

}// end class Extension

public class GUIInventory {

	public static void main(String [] args) //main() method
	{

		InventoryGUI theInventoryGUI = new InventoryGUI();

		// get input from the user until stop is entered as the DVD name
		while(true) {
			System.out.print("Please enter the product number (type \"stop\" to quit): ");

			//Create a new DVD object
			DVD theDVD = new DVD(Item_Name, Item_Genre, Item_Number, Items_in_Stock, Item_Price);

			theInventoryGUI.addDVDToInventory(theDVD);
		}

		// sort the DVDs by name
		theInventoryGUI.sortInventory();

		// Display the inventory of DVD on the screen, one DVD at a time
		theInventoryGUI.showGUI();

	}//end of main() method

}//end class GUIInventory


Was This Post Helpful? 0
  • +
  • -

#7 crynine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-May 09

Re: coding issues

Posted 02 May 2009 - 10:21 AM

Oops sorry on the code tags I am trying. Still very new at this posting here too.



View PostKYA, on 2 May, 2009 - 09:15 AM, said:

Use code tags! :angry:

import java.io;
import java.util;
import javax.swing;
import java.Jlabel

 public class Inventory_Part4 extends JFrame
{

	// create inventory for the DVD
	private Inventory theInventory;

	// index in the inventory of the currently displayed DVD.
	// the index starts at 0, goes to the number of DVD in the inventory minus 1
	private int index = 0;

	// GUI elements to display currently selected DVD's information
	private JLabel itemNumberLabel = new JLabel("Product Number: ");
	private JTextField itemNumberText;

	private JLabel produdctNameLabel = new JLabel("Product Name: ");
	private JTextField prodnameText;

	private JLabel genreLabel = new JLabel("Product Genre: ");
	private JTextField genreText;

	private JLabel numinstockLabel = new JLabel("Number in Stock: ");
	private JTextField numinstockText;

	private JLabel productPriceLabel = new JLabel("Product Price: ");
	private JTextField productPriceText;

	private JLabel restockingFeeLabel = new JLabel("Restocking Fee: ");
	private JTextField restockingFeeText;

	private JLabel valueLabel = new JLabel("Inventory Value: ");
	private JTextField valueText;

	private JLabel totalValueLabel = new JLabel("Total Inventory Value: ");
	private JLabel totalValueText;

	// setup the buttons in the GUI
	// go to the next product in the list
	private Action nextAction  = new AbstractAction("Next")
	{
		public void actionPerformed(ActionEvent evt) {
			// go forward one product
			index++;

			// check to see if there is a next product
			if ( index == theInventory.getSize() ) {
				// if we're at the last product in the list, then we can't go any further forward
				// so back up one
				index--;
			}

			repaint();
		}
	};
	private JButton nextButton = new JButton(nextAction);


	// Add the product to the underlying inventory object
	public void addDVDToInventory(DVD temp)
	{
		theInventory.addDVD(temp);
		sortInventory();
		repaint();
	}

	// sort the items in the underlying inventory object
	public void sortInventory()
	{
		theInventory.sortInventory();
		repaint();
	}//end public class Inventory_Part4

	// constructor for the GUI, in charge of creating all GUI elements
	public Dvd()
	{
		// create the inventory object that will hold the product information
		theInventory = new Inventory();

		// setup the GUI
		// add the next button to the top of the GUI
		// setup a panel to collect all the buttons in a FlowLayout
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(nextButton);
		getContentPane().add(buttonPanel, BorderLayout.NORTH);

		// product information
		// setup a panel to collect all the components.
		JPanel centerPanel = new JPanel(new GridLayout(8, 2, 0, 4));

		centerPanel.add(itemNumberLabel);
		itemNumberText = new JTextField("");
		itemNumberText.setEditable(false);
		centerPanel.add(itemNumberText);

		centerPanel.add(produdctNameLabel);
		prodnameText = new JTextField("");
		prodnameText.setEditable(false);
		centerPanel.add(prodnameText);

		centerPanel.add(genreLabel);
		genreText = new JTextField("");
		genreText.setEditable(false);
		centerPanel.add(genreText);

		centerPanel.add(numinstockLabel);
		numinstockText = new JTextField("");
		numinstockText.setEditable(false);
		centerPanel.add(numinstockText);

		centerPanel.add(productPriceLabel);
		productPriceText = new JTextField("");
		productPriceText.setEditable(false);
		centerPanel.add(productPriceText);

		centerPanel.add(restockingFeeLabel);
		restockingFeeText = new JTextField("");
		restockingFeeText.setEditable(false);
		centerPanel.add(restockingFeeText);

		centerPanel.add(valueLabel);
		valueText = new JTextField("");
		valueText.setEditable(false);
		centerPanel.add(valueText);

		// add the overall inventory information to the panel
		centerPanel.add(totalValueLabel);
		totalValueText = new JLabel("");
		centerPanel.add(totalValueText);

		// add the panel to the center of the GUI's window
		getContentPane().add(centerPanel, BorderLayout.CENTER);

	}

	// Get the GUI to show up on the screen
	public void showGUI() {
		this.setDefaultCloseOperation( EXIT_ON_CLOSE );
		this.pack();
		this.setSize(600, 275);
		this.setResizable(false);
		this.setLocationRelativeTo( null );
		this.setVisible(true);

		repaint();
	}
	// repaint the GUI with current product's information
	public void repaint() {

		DVD temp = theInventory.getDVD(index);

		if (temp != null) {

			itemNumberText.setText( ""+temp.getItemNumber() );

			prodnameText.setText( temp.getItemName() );
			genreText.setText( temp.getGenre() );
			productPriceText.setText( String.format("$%.2f", temp.getItemPrice()) );
			restockingFeeText.setText( String.format("$%.2f", temp.getRestockingFee()) );
			numinstockText.setText( ""+temp.getItemsInStock() );

			valueText.setText( String.format("$%.2f", temp.getInventoryValue() ) );

		}

		totalValueText.setText( String.format("$%.2f", theInventory.getTotalValueOfAllInventory() ) );

	}

}  // End Dvd class

/*
 * class which reprsents a collection of DVDs in an inventory.
 */
 public class Inventory
{
	// place to store all the DVDs in the inventory
	DVD[] inventory;

	// create an inventory capable of holding different DVDs
	public Inventory( )
	{
		inventory = new DVD[0];
	}

	// add the DVDs to the end of the inventory
	public void addDVD(DVD new_DVD)
	{
		// create a new array of DVDs that can hold one more than the current array of DVDs
		DVD[] new_inventory = new DVD[this.getSize() + 1];

		//copy all the old ones to the front of the new array
		for (int i = 0; i < this.getSize(); i++) {
			new_inventory[i] = inventory[i];
		}

		// add the new DVD to the end of the new array
		new_inventory[this.getSize()] = new_DVD;

		// replace the old inventory with the new one
		inventory = new_inventory;

	}
	// return the DVDs at the location index in the inventory
	public DVD getDVD(int index)
	{
		return inventory[index];
	}

	// return the number of DVDs in the inventory
	public int getSize()
	{
		return inventory.length;
	}

	// run through all the DVDs in the inventory, and add up their value.
	// this includes the restocking fee
	public double getTotalValueOfAllInventory()
	{
		double tot = 0.00;

		for(int i = 0; i < getSize(); i++)
		{
			tot += inventory[i].getInventoryValue();
		}
		return tot;
	}

	// sort the DVDs in the inventory by movie title
	public void sortInventory()
	{
		Arrays.sort(inventory, 0, getSize());
	}
}// end class Inventory



// public class Product

class Product implements Comparable<Product> { //Start the Product class

	String itemNumber; //stores item number
	String name; //stores the CD's name
	int stockQuantity; //stores number of units in stock
	double priceperunit; //stores the price of each unit

	//Constructor method
	public Product(String Item_Number, String Item_Name, int Items_in_Stock, double Item_Price){
		itemNumber = Item_Number;
		name = Item_Name;
		stockQuantity = Items_in_Stock;
		priceperunit = Item_Price;
	}

	public void setItemName(String Item_Name) //Method to set and get the item name
	{
		name = Item_Name;
	}

	public String getItemName() {
		return name;
	}

	public void setItemNumber(String Item_Number) //Method to set and get the item number
	{
		itemNumber = Item_Number;
	}

	public String getItemNumber(){
		return itemNumber;
	}

	public void setItemsInStock(int Items_in_Stock) //Method to set and get the quantity in stock
	{
		stockQuantity = Items_in_Stock;
	}

	public int getItemsInStock(){
		return stockQuantity;
	}

	public void setItemPrice(double Item_Price) //Method to set and get the item price
	{
		priceperunit = Item_Price;
	}

	public double getItemPrice(){
		return priceperunit;
	}

	public double getInventoryValue() //Method to calculate the value of the in stock inventory
	{
		return priceperunit * stockQuantity;
	}

	// returns -1, 0, or 1 depending on if the compared to object should appear before, the same, or after the current item
	public int compareTo (Product p)
	{
		return name.compareTo(p.getItemName());
	}

	public String toString() {
		//Return the product details as a string
		return "Product Number: "+this.getItemNumber() + "\n" +
		"Product Name: "+this.getItemName() + "\n" +
		"Number of Product items in stock: "+this.getItemsInStock() + "\n" +
		"Product price: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getItemPrice()) + "\n" +
		"Inventory Value: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getInventoryValue()) + "\n";
	}
} // end class Product

public class Extension
class DVD extends Product {
	// Genre of the DVD being added
	private String DVDGenre;
	// Restock fee to add to inventory value
	private double restockingFee;

	// initialize Product constructor
	public DVD(String DVDTitle,  String DVDGenre, String Item_Number,  int Items_in_Stock, double Item_Price) {

		super(Item_Number, DVDTitle, Items_in_Stock, Item_Price);

		this.DVDGenre = DVDGenre;

		// the default restocking fee is 5%
		this.restockingFee = 0.05;
	}

	// getter and setter for the DVD's genre
	public void setGenre(String DVDGenre)
	{
		this.DVDGenre = DVDGenre;
	}
	public String getGenre()
	{
		return DVDGenre;
	}


	// calculates the restocking fee of the DVDs based on a percentage of the value
	public double getRestockingFee() // Figures restocking fee of inventory
	{
		return super.getInventoryValue() * restockingFee;
	}

	// include a restocking fee in the inventory's value
	public double getInventoryValue() // Figures total inventory value including restocking fee
	{
		return  super.getInventoryValue() + getRestockingFee();
	}

	// returns a string representation of the item
	public String toString() {
		//Return the product details as a string
		return "Product Number: "+this.getItemNumber() + "\n" +
		"Product Name: "+this.getItemName() + "\n" +
		"Product Genre			: "  + DVDGenre			+ "\n" +
		"Number of Product items in stock: "+this.getItemsInStock() + "\n" +
		"Product price: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getItemPrice()) + "\n" +
		"Restocking Fee   : "  + NumberFormat.getCurrencyInstance(Locale.US).format(getRestockingFee()) + "\n" +
		"Inventory Value: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getInventoryValue()) + "\n";
	}

}// end class Extension

public class GUIInventory {

	public static void main(String [] args) //main() method
	{

		InventoryGUI theInventoryGUI = new InventoryGUI();

		// get input from the user until stop is entered as the DVD name
		while(true) {
			System.out.print("Please enter the product number (type \"stop\" to quit): ");

			//Create a new DVD object
			DVD theDVD = new DVD(Item_Name, Item_Genre, Item_Number, Items_in_Stock, Item_Price);

			theInventoryGUI.addDVDToInventory(theDVD);
		}

		// sort the DVDs by name
		theInventoryGUI.sortInventory();

		// Display the inventory of DVD on the screen, one DVD at a time
		theInventoryGUI.showGUI();

	}//end of main() method

}//end class GUIInventory


Was This Post Helpful? 0
  • +
  • -

#8 crynine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-May 09

Re: coding issues

Posted 02 May 2009 - 10:40 AM

Okay , crazy question here but am trying to figure it out for myself. If I took my code and put it in small peices into the compiler to see if there are errors with only parts of the code will it work that way?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: coding issues

Posted 02 May 2009 - 10:45 AM

// repaint the GUI with current product's information
	public void repaint() {




This is NOT a good idea at all
repaint() is a component method that will eventually fired an event that will call back your piant() method with argument a Graphics object

If you decide to overload the repaint() method, your problem, but at least make sur to call the real one at the end

super.repaint();

if you don't do so your paint() method will not be called
Was This Post Helpful? 1

#10 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: coding issues

Posted 02 May 2009 - 10:54 AM

package javaapplication22;

import java.awt.event.ActionEvent;
import java.io.*;

import java.text.NumberFormat;
import java.util.*;;
import javax.swing.*;


 class Inventory_Part4 extends JFrame
{

	// create inventory for the DVD
	private Inventory theInventory;

	// index in the inventory of the currently displayed DVD.
	// the index starts at 0, goes to the number of DVD in the inventory minus 1
	private int index = 0;

	// GUI elements to display currently selected DVD's information
	private JLabel itemNumberLabel = new JLabel("Product Number: ");
	private JTextField itemNumberText;

	private JLabel produdctNameLabel = new JLabel("Product Name: ");
	private JTextField prodnameText;

	private JLabel genreLabel = new JLabel("Product Genre: ");
	private JTextField genreText;

	private JLabel numinstockLabel = new JLabel("Number in Stock: ");
	private JTextField numinstockText;

	private JLabel productPriceLabel = new JLabel("Product Price: ");
	private JTextField productPriceText;

	private JLabel restockingFeeLabel = new JLabel("Restocking Fee: ");
	private JTextField restockingFeeText;

	private JLabel valueLabel = new JLabel("Inventory Value: ");
	private JTextField valueText;

	private JLabel totalValueLabel = new JLabel("Total Inventory Value: ");
	private JLabel totalValueText;

	// setup the buttons in the GUI
	// go to the next product in the list
	private Action nextAction  = new AbstractAction("Next")
	{
		public void actionPerformed(ActionEvent evt) {
			// go forward one product
			index++;

			// check to see if there is a next product
			if ( index == theInventory.getSize() ) {
				// if we're at the last product in the list, then we can't go any further forward
				// so back up one
				index--;
			}

			repaint();
		}
	};
	private JButton nextButton = new JButton(nextAction);


	// Add the product to the underlying inventory object
	public void addDVDToInventory(DVD temp)
	{
		theInventory.addDVD(temp);
		sortInventory();
		repaint();
	}

	// sort the items in the underlying inventory object
	public void sortInventory()
	{
		theInventory.sortInventory();
		repaint();
	}//end public class Inventory_Part4

	// constructor for the GUI, in charge of creating all GUI elements
	
	// Get the GUI to show up on the screen
	public void showGUI() {
		this.setDefaultCloseOperation( EXIT_ON_CLOSE );
		this.pack();
		this.setSize(600, 275);
		this.setResizable(false);
		this.setLocationRelativeTo( null );
		this.setVisible(true);

		repaint();
	}
	// repaint the GUI with current product's information
	public void repaint() {

		DVD temp = theInventory.getDVD(index);

		if (temp != null) {

			itemNumberText.setText( ""+temp.getItemNumber() );

			prodnameText.setText( temp.getItemName() );
			genreText.setText( temp.getGenre() );
			productPriceText.setText( String.format("$%.2f", temp.getItemPrice()) );
			restockingFeeText.setText( String.format("$%.2f", temp.getRestockingFee()) );
			numinstockText.setText( ""+temp.getItemsInStock() );

			valueText.setText( String.format("$%.2f", temp.getInventoryValue() ) );

		}

		totalValueText.setText( String.format("$%.2f", theInventory.getTotalValueOfAllInventory() ) );

	}

}  // End Dvd class

/*
* class which reprsents a collection of DVDs in an inventory.
*/


// public class Product

class Product implements Comparable<Product> { //Start the Product class

	String itemNumber; //stores item number
	String name; //stores the CD's name
	int stockQuantity; //stores number of units in stock
	double priceperunit; //stores the price of each unit

	//Constructor method
	public Product(String Item_Number, String Item_Name, int Items_in_Stock, double Item_Price){
		itemNumber = Item_Number;
		name = Item_Name;
		stockQuantity = Items_in_Stock;
		priceperunit = Item_Price;
	}

	public void setItemName(String Item_Name) //Method to set and get the item name
	{
		name = Item_Name;
	}

	public String getItemName() {
		return name;
	}

	public void setItemNumber(String Item_Number) //Method to set and get the item number
	{
		itemNumber = Item_Number;
	}

	public String getItemNumber(){
		return itemNumber;
	}

	public void setItemsInStock(int Items_in_Stock) //Method to set and get the quantity in stock
	{
		stockQuantity = Items_in_Stock;
	}

	public int getItemsInStock(){
		return stockQuantity;
	}

	public void setItemPrice(double Item_Price) //Method to set and get the item price
	{
		priceperunit = Item_Price;
	}

	public double getItemPrice(){
		return priceperunit;
	}

	public double getInventoryValue() //Method to calculate the value of the in stock inventory
	{
		return priceperunit * stockQuantity;
	}

	// returns -1, 0, or 1 depending on if the compared to object should appear before, the same, or after the current item
	public int compareTo (Product p)
	{
		return name.compareTo(p.getItemName());
	}

	public String toString() {
		//Return the product details as a string
		return "Product Number: "+this.getItemNumber() + "\n" +
		"Product Name: "+this.getItemName() + "\n" +
		"Number of Product items in stock: "+this.getItemsInStock() + "\n" +
		"Product price: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getItemPrice()) + "\n" +
		"Inventory Value: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getInventoryValue()) + "\n";
	}
} // end class Product


class DVD extends Product {
	// Genre of the DVD being added
	private String DVDGenre;
	// Restock fee to add to inventory value
	private double restockingFee;

	// initialize Product constructor
	public DVD(String DVDTitle,  String DVDGenre, String Item_Number,  int Items_in_Stock, double Item_Price) {

		super(Item_Number, DVDTitle, Items_in_Stock, Item_Price);

		this.DVDGenre = DVDGenre;

		// the default restocking fee is 5%
		this.restockingFee = 0.05;
	}

	// getter and setter for the DVD's genre
	public void setGenre(String DVDGenre)
	{
		this.DVDGenre = DVDGenre;
	}
	public String getGenre()
	{
		return DVDGenre;
	}


	// calculates the restocking fee of the DVDs based on a percentage of the value
	public double getRestockingFee() // Figures restocking fee of inventory
	{
		return super.getInventoryValue() * restockingFee;
	}

	// include a restocking fee in the inventory's value
	public double getInventoryValue() // Figures total inventory value including restocking fee
	{
		return  super.getInventoryValue() + getRestockingFee();
	}

	// returns a string representation of the item
	public String toString() {
		//Return the product details as a string
		return "Product Number: "+this.getItemNumber() + "\n" +
		"Product Name: "+this.getItemName() + "\n" +
		"Product Genre			: "  + DVDGenre			+ "\n" +
		"Number of Product items in stock: "+this.getItemsInStock() + "\n" +
		"Product price: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getItemPrice()) + "\n" +
		"Restocking Fee   : "  + NumberFormat.getCurrencyInstance(Locale.US).format(getRestockingFee()) + "\n" +
		"Inventory Value: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getInventoryValue()) + "\n";
	}

}// end class Extension
 class Inventory
{
	// place to store all the DVDs in the inventory
	DVD[] inventory;

	// create an inventory capable of holding different DVDs
	public Inventory( )
	{
		inventory = new DVD[0];
	}

	// add the DVDs to the end of the inventory
	public void addDVD(DVD new_DVD)
	{
		// create a new array of DVDs that can hold one more than the current array of DVDs
		DVD[] new_inventory = new DVD[this.getSize() + 1];

		//copy all the old ones to the front of the new array
		for (int i = 0; i < this.getSize(); i++) {
			new_inventory[i] = inventory[i];
		}

		// add the new DVD to the end of the new array
		new_inventory[this.getSize()] = new_DVD;

		// replace the old inventory with the new one
		inventory = new_inventory;

	}
	// return the DVDs at the location index in the inventory
	public DVD getDVD(int index)
	{
		return inventory[index];
	}

	// return the number of DVDs in the inventory
	public int getSize()
	{
		return inventory.length;
	}

	// run through all the DVDs in the inventory, and add up their value.
	// this includes the restocking fee
	public double getTotalValueOfAllInventory()
	{
		double tot = 0.00;

		for(int i = 0; i < getSize(); i++)
		{
			tot += inventory[i].getInventoryValue();
		}
		return tot;
	}

	// sort the DVDs in the inventory by movie title
	public void sortInventory()
	{
		Arrays.sort(inventory, 0, getSize());
	}
}// end class Inventory


 class Main {

	public static void main(String [] args) //main() method
	{
	 Scanner s = new Scanner(System.in);

		Inventory_Part4 theInventoryGUI = new Inventory_Part4();

		// get input from the user until stop is entered as the DVD name
		for(int i=0;i<10;i++) {
		   

			String Title;
			System.out.println("enter title");
			Title=s.nextLine();
		String DVDGenre;
		System.out.println("enter DvdGenere");
 DVDGenre=s.nextLine();


String Item_Name;
System.out.println("enter item name");
Item_Name=s.nextLine();
String Item_Genre;
System.out.println("enter the item genre");
Item_Genre=s.nextLine();
		String Item_Number;
		System.out.println("enter the item number");
		Item_Number=s.nextLine();
		int Items_in_Stock=10;
		System.out.println("enter the number of item in stock");
		String temp=s.nextLine();
		Items_in_Stock=Integer.parseInt(temp);
		double Item_Price=10.0;
		System.out.println("enter the price of the item");
	   temp=s.nextLine();
	  Item_Price=Double.parseDouble(temp);
			//Create a new DVD object
			DVD theDVD = new DVD(Item_Name, Item_Genre, Item_Number, Items_in_Stock, Item_Price);

			theInventoryGUI.addDVDToInventory(theDVD);
		}

		// sort the DVDs by name
	  theInventoryGUI.sortInventory();

		// Display the inventory of DVD on the screen, one DVD at a time
		theInventoryGUI.showGUI();

	}//end of main() method

}


following code is with no errors
ya i dont know how you want to stop that main loop so i used a for loop
please correct it

next time when you post please take care of your classes names and duplicate class declaration and ya
one more thing please give some description of each variable that you declare you have done for most of them here but i suggest you do it for all

This post has been edited by prajayshetty: 02 May 2009 - 11:03 AM

Was This Post Helpful? 1

#11 crynine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-May 09

Re: coding issues

Posted 02 May 2009 - 11:38 AM

Thank you for that, I usually use a while loop they work well for me. I will remember your advice and all you have done to help me. One last thing is it extremely important to give a description of the variable you declare all the way through? Will that effect the code alot? Again thank you so much.







View Postprajayshetty, on 2 May, 2009 - 09:54 AM, said:

package javaapplication22;

import java.awt.event.ActionEvent;
import java.io.*;

import java.text.NumberFormat;
import java.util.*;;
import javax.swing.*;


 class Inventory_Part4 extends JFrame
{

	// create inventory for the DVD
	private Inventory theInventory;

	// index in the inventory of the currently displayed DVD.
	// the index starts at 0, goes to the number of DVD in the inventory minus 1
	private int index = 0;

	// GUI elements to display currently selected DVD's information
	private JLabel itemNumberLabel = new JLabel("Product Number: ");
	private JTextField itemNumberText;

	private JLabel produdctNameLabel = new JLabel("Product Name: ");
	private JTextField prodnameText;

	private JLabel genreLabel = new JLabel("Product Genre: ");
	private JTextField genreText;

	private JLabel numinstockLabel = new JLabel("Number in Stock: ");
	private JTextField numinstockText;

	private JLabel productPriceLabel = new JLabel("Product Price: ");
	private JTextField productPriceText;

	private JLabel restockingFeeLabel = new JLabel("Restocking Fee: ");
	private JTextField restockingFeeText;

	private JLabel valueLabel = new JLabel("Inventory Value: ");
	private JTextField valueText;

	private JLabel totalValueLabel = new JLabel("Total Inventory Value: ");
	private JLabel totalValueText;

	// setup the buttons in the GUI
	// go to the next product in the list
	private Action nextAction  = new AbstractAction("Next")
	{
		public void actionPerformed(ActionEvent evt) {
			// go forward one product
			index++;

			// check to see if there is a next product
			if ( index == theInventory.getSize() ) {
				// if we're at the last product in the list, then we can't go any further forward
				// so back up one
				index--;
			}

			repaint();
		}
	};
	private JButton nextButton = new JButton(nextAction);


	// Add the product to the underlying inventory object
	public void addDVDToInventory(DVD temp)
	{
		theInventory.addDVD(temp);
		sortInventory();
		repaint();
	}

	// sort the items in the underlying inventory object
	public void sortInventory()
	{
		theInventory.sortInventory();
		repaint();
	}//end public class Inventory_Part4

	// constructor for the GUI, in charge of creating all GUI elements
	
	// Get the GUI to show up on the screen
	public void showGUI() {
		this.setDefaultCloseOperation( EXIT_ON_CLOSE );
		this.pack();
		this.setSize(600, 275);
		this.setResizable(false);
		this.setLocationRelativeTo( null );
		this.setVisible(true);

		repaint();
	}
	// repaint the GUI with current product's information
	public void repaint() {

		DVD temp = theInventory.getDVD(index);

		if (temp != null) {

			itemNumberText.setText( ""+temp.getItemNumber() );

			prodnameText.setText( temp.getItemName() );
			genreText.setText( temp.getGenre() );
			productPriceText.setText( String.format("$%.2f", temp.getItemPrice()) );
			restockingFeeText.setText( String.format("$%.2f", temp.getRestockingFee()) );
			numinstockText.setText( ""+temp.getItemsInStock() );

			valueText.setText( String.format("$%.2f", temp.getInventoryValue() ) );

		}

		totalValueText.setText( String.format("$%.2f", theInventory.getTotalValueOfAllInventory() ) );

	}

}  // End Dvd class

/*
* class which reprsents a collection of DVDs in an inventory.
*/


// public class Product

class Product implements Comparable<Product> { //Start the Product class

	String itemNumber; //stores item number
	String name; //stores the CD's name
	int stockQuantity; //stores number of units in stock
	double priceperunit; //stores the price of each unit

	//Constructor method
	public Product(String Item_Number, String Item_Name, int Items_in_Stock, double Item_Price){
		itemNumber = Item_Number;
		name = Item_Name;
		stockQuantity = Items_in_Stock;
		priceperunit = Item_Price;
	}

	public void setItemName(String Item_Name) //Method to set and get the item name
	{
		name = Item_Name;
	}

	public String getItemName() {
		return name;
	}

	public void setItemNumber(String Item_Number) //Method to set and get the item number
	{
		itemNumber = Item_Number;
	}

	public String getItemNumber(){
		return itemNumber;
	}

	public void setItemsInStock(int Items_in_Stock) //Method to set and get the quantity in stock
	{
		stockQuantity = Items_in_Stock;
	}

	public int getItemsInStock(){
		return stockQuantity;
	}

	public void setItemPrice(double Item_Price) //Method to set and get the item price
	{
		priceperunit = Item_Price;
	}

	public double getItemPrice(){
		return priceperunit;
	}

	public double getInventoryValue() //Method to calculate the value of the in stock inventory
	{
		return priceperunit * stockQuantity;
	}

	// returns -1, 0, or 1 depending on if the compared to object should appear before, the same, or after the current item
	public int compareTo (Product p)
	{
		return name.compareTo(p.getItemName());
	}

	public String toString() {
		//Return the product details as a string
		return "Product Number: "+this.getItemNumber() + "\n" +
		"Product Name: "+this.getItemName() + "\n" +
		"Number of Product items in stock: "+this.getItemsInStock() + "\n" +
		"Product price: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getItemPrice()) + "\n" +
		"Inventory Value: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getInventoryValue()) + "\n";
	}
} // end class Product


class DVD extends Product {
	// Genre of the DVD being added
	private String DVDGenre;
	// Restock fee to add to inventory value
	private double restockingFee;

	// initialize Product constructor
	public DVD(String DVDTitle,  String DVDGenre, String Item_Number,  int Items_in_Stock, double Item_Price) {

		super(Item_Number, DVDTitle, Items_in_Stock, Item_Price);

		this.DVDGenre = DVDGenre;

		// the default restocking fee is 5%
		this.restockingFee = 0.05;
	}

	// getter and setter for the DVD's genre
	public void setGenre(String DVDGenre)
	{
		this.DVDGenre = DVDGenre;
	}
	public String getGenre()
	{
		return DVDGenre;
	}


	// calculates the restocking fee of the DVDs based on a percentage of the value
	public double getRestockingFee() // Figures restocking fee of inventory
	{
		return super.getInventoryValue() * restockingFee;
	}

	// include a restocking fee in the inventory's value
	public double getInventoryValue() // Figures total inventory value including restocking fee
	{
		return  super.getInventoryValue() + getRestockingFee();
	}

	// returns a string representation of the item
	public String toString() {
		//Return the product details as a string
		return "Product Number: "+this.getItemNumber() + "\n" +
		"Product Name: "+this.getItemName() + "\n" +
		"Product Genre			: "  + DVDGenre			+ "\n" +
		"Number of Product items in stock: "+this.getItemsInStock() + "\n" +
		"Product price: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getItemPrice()) + "\n" +
		"Restocking Fee   : "  + NumberFormat.getCurrencyInstance(Locale.US).format(getRestockingFee()) + "\n" +
		"Inventory Value: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getInventoryValue()) + "\n";
	}

}// end class Extension
 class Inventory
{
	// place to store all the DVDs in the inventory
	DVD[] inventory;

	// create an inventory capable of holding different DVDs
	public Inventory( )
	{
		inventory = new DVD[0];
	}

	// add the DVDs to the end of the inventory
	public void addDVD(DVD new_DVD)
	{
		// create a new array of DVDs that can hold one more than the current array of DVDs
		DVD[] new_inventory = new DVD[this.getSize() + 1];

		//copy all the old ones to the front of the new array
		for (int i = 0; i < this.getSize(); i++) {
			new_inventory[i] = inventory[i];
		}

		// add the new DVD to the end of the new array
		new_inventory[this.getSize()] = new_DVD;

		// replace the old inventory with the new one
		inventory = new_inventory;

	}
	// return the DVDs at the location index in the inventory
	public DVD getDVD(int index)
	{
		return inventory[index];
	}

	// return the number of DVDs in the inventory
	public int getSize()
	{
		return inventory.length;
	}

	// run through all the DVDs in the inventory, and add up their value.
	// this includes the restocking fee
	public double getTotalValueOfAllInventory()
	{
		double tot = 0.00;

		for(int i = 0; i < getSize(); i++)
		{
			tot += inventory[i].getInventoryValue();
		}
		return tot;
	}

	// sort the DVDs in the inventory by movie title
	public void sortInventory()
	{
		Arrays.sort(inventory, 0, getSize());
	}
}// end class Inventory


 class Main {

	public static void main(String [] args) //main() method
	{
	 Scanner s = new Scanner(System.in);

		Inventory_Part4 theInventoryGUI = new Inventory_Part4();

		// get input from the user until stop is entered as the DVD name
		for(int i=0;i<10;i++) {
		   

			String Title;
			System.out.println("enter title");
			Title=s.nextLine();
		String DVDGenre;
		System.out.println("enter DvdGenere");
 DVDGenre=s.nextLine();


String Item_Name;
System.out.println("enter item name");
Item_Name=s.nextLine();
String Item_Genre;
System.out.println("enter the item genre");
Item_Genre=s.nextLine();
		String Item_Number;
		System.out.println("enter the item number");
		Item_Number=s.nextLine();
		int Items_in_Stock=10;
		System.out.println("enter the number of item in stock");
		String temp=s.nextLine();
		Items_in_Stock=Integer.parseInt(temp);
		double Item_Price=10.0;
		System.out.println("enter the price of the item");
	   temp=s.nextLine();
	  Item_Price=Double.parseDouble(temp);
			//Create a new DVD object
			DVD theDVD = new DVD(Item_Name, Item_Genre, Item_Number, Items_in_Stock, Item_Price);

			theInventoryGUI.addDVDToInventory(theDVD);
		}

		// sort the DVDs by name
	  theInventoryGUI.sortInventory();

		// Display the inventory of DVD on the screen, one DVD at a time
		theInventoryGUI.showGUI();

	}//end of main() method

}


following code is with no errors
ya i dont know how you want to stop that main loop so i used a for loop
please correct it

next time when you post please take care of your classes names and duplicate class declaration and ya
one more thing please give some description of each variable that you declare you have done for most of them here but i suggest you do it for all

Was This Post Helpful? 0
  • +
  • -

#12 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: coding issues

Posted 02 May 2009 - 11:44 AM

no see what happens is that remeber even if your programmig alone it is better to give description to each varaible whenever your programming always document your code well and give as many as description that you give so that next time after some days when you read you dont have to waste time in understanding the code and one more thing at what conditon do u want that while loop to stop?

This post has been edited by prajayshetty: 02 May 2009 - 11:46 AM

Was This Post Helpful? 0
  • +
  • -

#13 crynine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-May 09

Re: coding issues

Posted 02 May 2009 - 12:38 PM

View Postprajayshetty, on 2 May, 2009 - 10:44 AM, said:

no see what happens is that remeber even if your programmig alone it is better to give description to each varaible whenever your programming always document your code well and give as many as description that you give so that next time after some days when you read you dont have to waste time in understanding the code and one more thing at what conditon do u want that while loop to stop?



I want the program to stop when the information has been entered or actually when "stop" has been entered. I usually do my programming that way.. what I got out of my text was this and wanted to implement it somehow in my program. It is this code.....// Set it so that it terminates the program when the window is closed.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

I also am wondering if I save my program right? I saved my program under Inventory_Part4 but when I do this it throws an error over the * sign? The one for this code[import javax.swing.*;
It says that an interface or class is expected? Am I saving this wrong?
Was This Post Helpful? 0
  • +
  • -

#14 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: coding issues

Posted 02 May 2009 - 03:13 PM

package javaapplication22;

import java.awt.event.ActionEvent;
import java.io.*;

import java.text.NumberFormat;
import java.util.*;;
import javax.swing.*;


class Inventory_Part4 extends JFrame
{

	// create inventory for the DVD
	public  Inventory theInventory=new Inventory();

	// index in the inventory of the currently displayed DVD.
	// the index starts at 0, goes to the number of DVD in the inventory minus 1
	private int index = 0;

	// GUI elements to display currently selected DVD's information
	private JLabel itemNumberLabel = new JLabel("Product Number: ");
	private JTextField itemNumberText;

	private JLabel produdctNameLabel = new JLabel("Product Name: ");
	private JTextField prodnameText;

	private JLabel genreLabel = new JLabel("Product Genre: ");
	private JTextField genreText;

	private JLabel numinstockLabel = new JLabel("Number in Stock: ");
	private JTextField numinstockText;

	private JLabel productPriceLabel = new JLabel("Product Price: ");
	private JTextField productPriceText;

	private JLabel restockingFeeLabel = new JLabel("Restocking Fee: ");
	private JTextField restockingFeeText;

	private JLabel valueLabel = new JLabel("Inventory Value: ");
	private JTextField valueText;

	private JLabel totalValueLabel = new JLabel("Total Inventory Value: ");
	private JLabel totalValueText;

	// setup the buttons in the GUI
	// go to the next product in the list
	private Action nextAction  = new AbstractAction("Next")
	{
		public void actionPerformed(ActionEvent evt) {
			// go forward one product
			index++;

			// check to see if there is a next product
			if ( index == theInventory.getSize() ) {
				// if we're at the last product in the list, then we can't go any further forward
				// so back up one
				index--;
			}

			repaint();
		}
	};
	private JButton nextButton = new JButton(nextAction);


	// Add the product to the underlying inventory object
	public void addDVDToInventory(DVD temp)
	{
		theInventory.addDVD(temp);
		sortInventory();
		repaint();
	}

	// sort the items in the underlying inventory object
	public void sortInventory()
	{
		theInventory.sortInventory();
		repaint();
	}//end public class Inventory_Part4

	// constructor for the GUI, in charge of creating all GUI elements

	// Get the GUI to show up on the screen
	public void showGUI() {
		this.setDefaultCloseOperation( EXIT_ON_CLOSE );
		this.pack();
		this.setSize(600, 275);
		this.setResizable(false);
		this.setLocationRelativeTo( null );
		this.setVisible(true);

		repaint();
	}
	// repaint the GUI with current product's information
   
	public void repaint() {

		DVD temp = theInventory.getDVD(index);

		if (temp != null) {

			itemNumberText.setText( ""+temp.getItemNumber() );

			prodnameText.setText( temp.getItemName() );
			genreText.setText( temp.getGenre() );
			productPriceText.setText( String.format("$%.2f", temp.getItemPrice()) );
			restockingFeeText.setText( String.format("$%.2f", temp.getRestockingFee()) );
			numinstockText.setText( ""+temp.getItemsInStock() );

			valueText.setText( String.format("$%.2f", temp.getInventoryValue() ) );

		}

		totalValueText.setText( String.format("$%.2f", theInventory.getTotalValueOfAllInventory() ) );

	}

}  // End Dvd class

/*
* class which reprsents a collection of DVDs in an inventory.
*/


// public class Product

class Product implements Comparable<Product> { //Start the Product class

	String itemNumber; //stores item number
	String name; //stores the CD's name
	int stockQuantity; //stores number of units in stock
	double priceperunit; //stores the price of each unit

	//Constructor method
	public Product(String Item_Number, String Item_Name, int Items_in_Stock, double Item_Price){
		itemNumber = Item_Number;
		name = Item_Name;
		stockQuantity = Items_in_Stock;
		priceperunit = Item_Price;
	}

	public void setItemName(String Item_Name) //Method to set and get the item name
	{
		name = Item_Name;
	}

	public String getItemName() {
		return name;
	}

	public void setItemNumber(String Item_Number) //Method to set and get the item number
	{
		itemNumber = Item_Number;
	}

	public String getItemNumber(){
		return itemNumber;
	}

	public void setItemsInStock(int Items_in_Stock) //Method to set and get the quantity in stock
	{
		stockQuantity = Items_in_Stock;
	}

	public int getItemsInStock(){
		return stockQuantity;
	}

	public void setItemPrice(double Item_Price) //Method to set and get the item price
	{
		priceperunit = Item_Price;
	}

	public double getItemPrice(){
		return priceperunit;
	}

	public double getInventoryValue() //Method to calculate the value of the in stock inventory
	{
		return priceperunit * stockQuantity;
	}

	// returns -1, 0, or 1 depending on if the compared to object should appear before, the same, or after the current item
	public int compareTo (Product p)
	{
		return name.compareTo(p.getItemName());
	}

	public String toString() {
		//Return the product details as a string
		return "Product Number: "+this.getItemNumber() + "\n" +
		"Product Name: "+this.getItemName() + "\n" +
		"Number of Product items in stock: "+this.getItemsInStock() + "\n" +
		"Product price: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getItemPrice()) + "\n" +
		"Inventory Value: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getInventoryValue()) + "\n";
	}
} // end class Product


class DVD extends Product {
	// Genre of the DVD being added
	private String DVDGenre;
	// Restock fee to add to inventory value
	private double restockingFee;

	// initialize Product constructor
	public DVD(String DVDTitle,  String DVDGenre, String Item_Number,  int Items_in_Stock, double Item_Price) {

		super(Item_Number, DVDTitle, Items_in_Stock, Item_Price);

		this.DVDGenre = DVDGenre;

		// the default restocking fee is 5%
		this.restockingFee = 0.05;
	}

	// getter and setter for the DVD's genre
	public void setGenre(String DVDGenre)
	{
		this.DVDGenre = DVDGenre;
	}
	public String getGenre()
	{
		return DVDGenre;
	}


	// calculates the restocking fee of the DVDs based on a percentage of the value
	public double getRestockingFee() // Figures restocking fee of inventory
	{
		return super.getInventoryValue() * restockingFee;
	}

	// include a restocking fee in the inventory's value
	public double getInventoryValue() // Figures total inventory value including restocking fee
	{
		return  super.getInventoryValue() + getRestockingFee();
	}

	// returns a string representation of the item
	public String toString() {
		//Return the product details as a string
		return "Product Number: "+this.getItemNumber() + "\n" +
		"Product Name: "+this.getItemName() + "\n" +
		"Product Genre			: "  + DVDGenre			+ "\n" +
		"Number of Product items in stock: "+this.getItemsInStock() + "\n" +
		"Product price: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getItemPrice()) + "\n" +
		"Restocking Fee   : "  + NumberFormat.getCurrencyInstance(Locale.US).format(getRestockingFee()) + "\n" +
		"Inventory Value: "+NumberFormat.getCurrencyInstance(Locale.US).format(this.getInventoryValue()) + "\n";
	}

}// end class Extension
class Inventory
{
	// place to store all the DVDs in the inventory
	DVD[] inventory;

	// create an inventory capable of holding different DVDs
	public Inventory( )
	{
		inventory = new DVD[0];
	}

	// add the DVDs to the end of the inventory
	public void addDVD(DVD new_DVD)
	{
		// create a new array of DVDs that can hold one more than the current array of DVDs
		DVD[] new_inventory = new DVD[this.getSize() + 1];

		//copy all the old ones to the front of the new array
		for (int i = 0; i < this.getSize(); i++) {
			new_inventory[i] = inventory[i];
		}

		// add the new DVD to the end of the new array
		new_inventory[this.getSize()] = new_DVD;

		// replace the old inventory with the new one
		inventory = new_inventory;

	}
	// return the DVDs at the location index in the inventory
	public DVD getDVD(int index)
	{
		return inventory[index];
	}

	// return the number of DVDs in the inventory
	public int getSize()
	{
		return inventory.length;
	}

	// run through all the DVDs in the inventory, and add up their value.
	// this includes the restocking fee
	public double getTotalValueOfAllInventory()
	{
		double tot = 0.00;

		for(int i = 0; i < getSize(); i++)
		{
			tot += inventory[i].getInventoryValue();
		}
		return tot;
	}

	// sort the DVDs in the inventory by movie title
	public void sortInventory()
	{
		Arrays.sort(inventory, 0, getSize());
	}
}// end class Inventory


class Main {

	public static void main(String [] args) //main() method
	{
	 Scanner s = new Scanner(System.in);
String check;
		Inventory_Part4 theInventoryGUI = new Inventory_Part4();

		// get input from the user until stop is entered as the DVD name
		do {
System.out.println("enter any string other than exit to continue");
 check=s.nextLine();
			String Title;
			System.out.println("enter title");
			Title=s.nextLine();
		String DVDGenre;
		System.out.println("enter DvdGenere");
DVDGenre=s.nextLine();


String Item_Name;
System.out.println("enter item name");
Item_Name=s.nextLine();
String Item_Genre;
System.out.println("enter the item genre");
Item_Genre=s.nextLine();
		String Item_Number;
		System.out.println("enter the item number");
		Item_Number=s.nextLine();
		int Items_in_Stock=10;
		System.out.println("enter the number of item in stock");
		String temp=s.nextLine();
		Items_in_Stock=Integer.parseInt(temp);
		double Item_Price=10.0;
		System.out.println("enter the price of the item");
	   temp=s.nextLine();
	  Item_Price=Double.parseDouble(temp);
			//Create a new DVD object
			DVD theDVD = new DVD(Item_Name, Item_Genre, Item_Number, Items_in_Stock, Item_Price);

			theInventoryGUI.addDVDToInventory(theDVD);
		}while(!check.equalsIgnoreCase("exit"));

		// sort the DVDs by name
	  theInventoryGUI.sortInventory();

		// Display the inventory of DVD on the screen, one DVD at a time
		theInventoryGUI.showGUI();

	}//end of main() method

}


program will terminate when exit is typed

This post has been edited by prajayshetty: 02 May 2009 - 03:19 PM

Was This Post Helpful? 0
  • +
  • -

#15 crynine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 02-May 09

Re: coding issues

Posted 02 May 2009 - 09:51 PM

I no matter what I try still get the same error code. The code is just one and it says . 'class' or 'interface' expected import javax.swing.*;

I don't know why it is doing that could it be the way I am saving this file? I have changed curly braces, renamed names just to see if it would work and it still says the same one code. I am stuck why would it do that could it still be a curly brace in the wrong spot?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2