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
coding issuescreating a GUI for dvd inventory
22 Replies - 1063 Views - Last Post: 03 May 2009 - 02:28 PM
#1
coding issues
Posted 02 May 2009 - 09:40 AM
Replies To: coding issues
#2
Re: coding issues
Posted 02 May 2009 - 09:52 AM
you had declared multiple classes with same name Inventory_part4 thats impossible in the same file please repost your code
#3
Re: coding issues
Posted 02 May 2009 - 09:54 AM
Thank You for your time.
prajayshetty, on 2 May, 2009 - 08:52 AM, said:
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?
#4
Re: coding issues
Posted 02 May 2009 - 09:57 AM
This post has been edited by prajayshetty: 02 May 2009 - 10:02 AM
#5
Re: coding issues
Posted 02 May 2009 - 10:14 AM
*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
#6
Re: coding issues
Posted 02 May 2009 - 10:15 AM
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
#7
Re: coding issues
Posted 02 May 2009 - 10:21 AM
KYA, on 2 May, 2009 - 09:15 AM, said:
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
#8
Re: coding issues
Posted 02 May 2009 - 10:40 AM
#9
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
#10
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
#11
Re: coding issues
Posted 02 May 2009 - 11:38 AM
prajayshetty, 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
#12
Re: coding issues
Posted 02 May 2009 - 11:44 AM
This post has been edited by prajayshetty: 02 May 2009 - 11:46 AM
#13
Re: coding issues
Posted 02 May 2009 - 12:38 PM
prajayshetty, on 2 May, 2009 - 10:44 AM, said:
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?
#14
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
#15
Re: coding issues
Posted 02 May 2009 - 09:51 PM
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?
|
|

New Topic/Question
Reply




MultiQuote



|