Question:
I need a serious Java programmer to please help me!!!!!!! This inventory program will not compile why?
robpurpleblazekamp
2007-03-30 20:08:48 UTC
/*
* Main.java
*
* Created on March 31, 2007, 2:15 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package inventory11;

/**
*
* @author Rob
*/
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

class Product implements Comparable
{
String name; // class variable that stores the item name
double number; // class variable that stores the item number
long stockQuantity; // class variable that stores the quantity in stock
double price; // class variable that stores the item price
public Product()
{
name = "";
number = 0.0;
stockQuantity = 0L;
price = 0.0;
}

public Product(String name, int number, long stockQuantity, double price)
{
this.name = name;
this.number = number;
this.stockQuantity = stockQuantity;
this.price = price;
}

public void setItemName(String name)
{
this.name = name;
}

public String getItemName()
{
return name;
}

public void setItemNumber(double number)
{
this.number = number;
}

public double getItemNumber()
{
return number;
}

public void setStockQuantity(long quantity)
{
stockQuantity = quantity;
}

public long getStockQuantity()
{
return stockQuantity;
}

public void setItemPrice(double price)
{
this.price = price;
}

public double getItemPrice()
{
return price;
}

public double calculateInventoryValue()
{
return getItemPrice() * getStockQuantity();
}

public int compareTo (Object o)
{
Product p = (Product)o;
return name.compareTo(p.getItemName());
}

public String toString()
{
return "\nName: "+getItemName() + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
}
}//end Product

class MobilePhone extends Product implements Comparable
{
private String brand;
public MobilePhone()
{
super(); //call the constructor in Product
brand = ""; //add the additonal attribute
}

public MobilePhone(String name, int number, long stockQuantity, double price, String brand)
{
super(name, number, stockQuantity, price); //call the constructor in Product
this.brand = brand; //add the additonal attribute
}

public void setBrand(String brand)
{
this.brand = brand;
}

public String getBrand()
{
return brand;
}

public double calculateInventoryValue()
{
return getItemPrice() * getStockQuantity();
}

public double calculateRestockFee()
{
return getItemPrice() * 0.05;
}

public int compareTo (Object o)
{
Product p = (Product)o;
return getItemName().compareTo(p.getItemName());
}

public String toString()
{
return "\nName: "+getItemName() + "\nNumber: "+getItemNumber()+"\nPrice: $"+getItemPrice()+"\nQuantity: "+getStockQuantity() +"\nBrand: "+getBrand()+"\nValue: $"+calculateInventoryValue();
}
}//end MobilePhone

public class Inventory11 extends JFrame implements ActionListener
{
//utility class for displaying the picture
private class MyPanel extends JPanel
{
ImageIcon image = new ImageIcon("Sample.jpg");
int width = image.getIconWidth();
int height = image.getIconHeight();
long angle = 30;
public MyPanel()
{
super();
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.rotate (Math.toRadians(angle), 60+width/2, 60+height/2);
g2d.drawImage(image.getImage(), 60, 60, this);
g2d.dispose();
}

}//end class MyPanel

int currentIndex; //currently displayed Item
Product[] supplies = new Product[4];
JLabel name ;
JLabel number;
JLabel brand;
JLabel quantity;
JLabel price;
JLabel fee;
JLabel totalValue;
JTextField nameField = new JTextField(20);
JTextField numberField = new JTextField(20);
JTextField brandField = new JTextField(20);
JTextField quantityField = new JTextField(20);
JTextField priceField = new JTextField(20);

JPanel display;
JPanel displayHolder;
JPanel panel;

public Inventory11()
{
makeTheDataItems();
setSize(500, 500);
setTitle("Rob's Inventory Program v1.0");

//make the panels
display = new JPanel();
JPanel other = new JPanel();
JPanel picture = new MyPanel();
JPanel buttons = new JPanel();
JPanel centerPanel = new JPanel();
displayHolder = new JPanel();
display.setLayout(new GridLayout(3, 3));
other.setLayout(new GridLayout(2, 1));

//make the labels
name = new JLabel("Name :");
number = new JLabel("Number :");
brand = new JLabel("Brand :");
quantity = new JLabel("Quantity :");
price = new JLabel("Price :");
fee = new JLabel("Fee :");
totalValue = new JLabel("Total Value :");

//make the buttons
JButton first = makeButton("First");
JButton next = makeButton("Next");
JButton previous = makeButton("Previous");
JButton last = makeButton("Last");
JButton exit = makeButton("Exit");

//other buttons
JButton add = makeButton("Add");

//add the labels to the display panel
display.add(name);
display.add(number);
display.add(brand);
display.add(quantity);
display.add(price);
display.add(fee);

//add the buttons to the buttonPanel
buttons.add(first);
buttons.add(previous);
buttons.add(next);
buttons.add(last);
buttons.add(exit);

//add the picture panel and display to the centerPanel
displayHolder.add(display);
centerPanel.setLayout(new GridLayout(2, 1));
centerPanel.add(picture);
centerPanel.add(displayHolder);
other.add(buttons);
JPanel forAdd = new JPanel(); // add the other buttons to this panel
forAdd.add(add);
other.add(forAdd);

//add the panels to the frame
getContentPane().add(centerPanel, "Center");
getContentPane().add(other, "South");
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setVisible(true);

}//end Inventory10

private void makeTheDataItems ()
{
supplies[0] = new MobilePhone("3310", 001, 200, 200, "Nokia");
supplies[1] = new MobilePhone("T720", 002, 500, 250, "Motorola");
supplies[2] = new MobilePhone("5110C", 003, 35, 149, "LG");
supplies[3] = new MobilePhone("9400D", 004, 90, 300, "Samsung");
}

//method for creating and dressing buttons
private JButton makeButton(String label)
{
JButton button = new JButton(label);
button.setActionCommand(label);
button.addActionListener(this);
return button;
}

private void addItem()
{
System.out.println("eeeeeeeeee");
panel = new JPanel();
JPanel add = new JPanel();
add.setLayout(new GridLayout(2, 1));
add.setLayout(new GridLayout(4, 4));
JButton addIt = makeButton("Add Item");
JLabel name = new JLabel("Name :");
//JLabel number = new JLabel("Number :");
JLabel brand = new JLabel("Brand :");
JLabel quantity = new JLabel("Quantity :");
JLabel price = new JLabel("Price :");
add.add(name); add.add(nameField);
//add.add(number); add.add(numberField);
add.add(brand); add.add(brandField);
add.add(quantity); add.add(quantityField);
add.add(price); add.add(priceField);
panel.add(add);
JPanel forAddIt = new JPanel();
forAddIt.add(addIt);
panel.add(forAddIt);
displayHolder.remove(display);
displayHolder.add(panel);
//display = panel;
this.setVisible(true);
}//end addItem

// main methods begins execution of java application
public static void main( String args[])
{
Inventory11 object = new Inventory11();
} // end main method

public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand(); //retrieves command set for the button

if(command.equals("First"))
{
displayFirst();
}
else if(command.equals("Next"))
{
displayNext();
}
else if(command.equals("Previous"))
{
displayPrevious();
}
else if(command.equals("Last"))
{
displayLast();
}
else if(command.equals("Exit"))
{
this.dispose();
System.exit(0);
}
else if(command.equals("Add"))
{
addItem();
}
else if(command.equals("Add Item"))
{
addItemToArray();
}

}//end action method

private void addItemToArray()
{
Product p = new MobilePhone(nameField.getText(), supplies.length -2, Long.parseLong(quantityField.getText()),
Double.parseDouble(priceField.getText()), brandField.getText());

//extend size of array by one first
Product[] ps = new Product[supplies.length + 1];
for(int i = 0; i < ps.length-1; i++)
{
ps[i] = supplies[i];
}
ps[supplies.length] = p;
supplies = ps;
displayHolder.remove(panel);
displayHolder.add(display);
displayLast();
this.setVisible(false);
this.setVisible(true);
}//end addItem

//utility method to reduce lines of code
private void displayItemAt(int index)
{
MobilePhone product = (MobilePhone)supplies[index];
name.setText("Item Name: "+ product.getItemName());
number.setText("Item Number: "+ product.getItemNumber());
brand.setText("Brand: "+ product.getBrand());
quantity.setText("Quantity In Stock: "+ product.getStockQuantity());
price.setText("Item Price: "+ product.getItemPrice());
totalValue.setText("Total: " + product.calculateInventoryValue());
fee.setText("Fee :"+product.calculateRestockFee());
this.setVisible(true);
}

public void displayFirst()
{
displayItemAt(0);
currentIndex = 0;
}

public void displayNext()
{
if(currentIndex == supplies.length-1)
{
displayFirst();
currentIndex = 0;
}
else
{
displayItemAt(currentIndex + 1);
currentIndex++;
}
}

public void displayPrevious()
{
if(currentIndex == 0)
{
displayLast();
currentIndex = supplies.length-1;
}
else
{
displayItemAt(currentIndex - 1);
currentIndex--;
}
}

public void displayLast()
{
displayItemAt(supplies.length-1);
currentIndex = supplies.length-1;
}
}//end class Inventory11
}

}
Three answers:
jguru
2007-04-02 05:11:43 UTC
dear friend

will u plz provide ur src code to me, so that i can help, i am a experience java programmer working in java for 2 years,

ur code doesnt make any sense, and its difficult fo read from web like u have posted ur code. here is my yahoo id ,i am always in yahoo. saiful_raju@yahoo.com. if i am not plz give me offline message. i will help u. this is so simple problem.

cheers

raju







here is ur answer



hi Rob , ur code is working, there was no bug in ur code , but the problem is there is no package named inventory11,but declared package . there was two main method, u implement product,MobilePhone,Inventory11 inside the main method, but u didnt execute them, so the program will not run, in order to run u have to create the instance of Invertory class. its not good to implement classes inside the main method,rahter u just implement each class in separate file. i have attached two version of ur code one is everything is same file and another is my own version where each class is in different file , u have to have a image file named sample.jpg in ur class path (put this file where Main.java is located)

stay in touch

bye



here is the right code, u may find another zip version in ur yahoo mail





/*

* Main.java

*

* Created on March 31, 2007, 2:15 AM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/



package inventory11;



/**

*

* @author Rob

*/

import java.util.*;

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class Main {



/** Creates a new instance of Main */

public Main() {

}



/**

* @param args the command line arguments

*/

public static void main(String[] args) {

System.out.println("wrong");



class Product implements Comparable

{

String name; // class variable that stores the item name

double number; // class variable that stores the item number

long stockQuantity; // class variable that stores the quantity in stock

double price; // class variable that stores the item price

public Product()

{

name = "";

number = 0.0;

stockQuantity = 0L;

price = 0.0;

}



public Product(String name, int number, long stockQuantity, double price)

{

this.name = name;

this.number = number;

this.stockQuantity = stockQuantity;

this.price = price;

}



public void setItemName(String name)

{

this.name = name;

}



public String getItemName()

{

return name;

}



public void setItemNumber(double number)

{

this.number = number;

}



public double getItemNumber()

{

return number;

}



public void setStockQuantity(long quantity)

{

stockQuantity = quantity;

}



public long getStockQuantity()

{

return stockQuantity;

}



public void setItemPrice(double price)

{

this.price = price;

}



public double getItemPrice()

{

return price;

}



public double calculateInventoryValue()

{

return getItemPrice() * getStockQuantity();

}



public int compareTo (Object o)

{

Product p = (Product)o;

return name.compareTo(p.getItemName());

}



public String toString()

{

return "\nName: "+getItemName() + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();

}

}//end Product



class MobilePhone extends Product implements Comparable

{

private String brand;

public MobilePhone()

{

super(); //call the constructor in Product

brand = ""; //add the additonal attribute

}



public MobilePhone(String name, int number, long stockQuantity, double price, String brand)

{

super(name, number, stockQuantity, price); //call the constructor in Product

this.brand = brand; //add the additonal attribute

}



public void setBrand(String brand)

{

this.brand = brand;

}



public String getBrand()

{

return brand;

}



public double calculateInventoryValue()

{

return getItemPrice() * getStockQuantity();

}



public double calculateRestockFee()

{

return getItemPrice() * 0.05;

}



public int compareTo (Object o)

{

Product p = (Product)o;

return getItemName().compareTo(p.getItemName());

}



public String toString()

{

return "\nName: "+getItemName() + "\nNumber: "+getItemNumber()+"\nPrice: $"+getItemPrice()+"\nQuantity: "+getStockQuantity() +"\nBrand: "+getBrand()+"\nValue: $"+calculateInventoryValue();

}

}//end MobilePhone



class Inventory11 extends JFrame implements ActionListener

{

//utility class for displaying the picture

class MyPanel extends JPanel

{

ImageIcon image = new ImageIcon("Sample.jpg");

int width = image.getIconWidth();

int height = image.getIconHeight();

long angle = 30;

public MyPanel()

{

super();

}



public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2d = (Graphics2D)g;

g2d.rotate (Math.toRadians(angle), 60+width/2, 60+height/2);

g2d.drawImage(image.getImage(), 60, 60, this);

g2d.dispose();

}



}//end class MyPanel



int currentIndex; //currently displayed Item

Product[] supplies = new Product[4];

JLabel name ;

JLabel number;

JLabel brand;

JLabel quantity;

JLabel price;

JLabel fee;

JLabel totalValue;

JTextField nameField = new JTextField(20);

JTextField numberField = new JTextField(20);

JTextField brandField = new JTextField(20);

JTextField quantityField = new JTextField(20);

JTextField priceField = new JTextField(20);



JPanel display;

JPanel displayHolder;

JPanel panel;



public Inventory11()

{

makeTheDataItems();

setSize(500, 500);

setTitle("Rob's Inventory Program v1.0");



//make the panels

display = new JPanel();

JPanel other = new JPanel();

JPanel picture = new MyPanel();

JPanel buttons = new JPanel();

JPanel centerPanel = new JPanel();

displayHolder = new JPanel();

display.setLayout(new GridLayout(3, 3));

other.setLayout(new GridLayout(2, 1));



//make the labels

name = new JLabel("Name :");

number = new JLabel("Number :");

brand = new JLabel("Brand :");

quantity = new JLabel("Quantity :");

price = new JLabel("Price :");

fee = new JLabel("Fee :");

totalValue = new JLabel("Total Value :");



//make the buttons

JButton first = makeButton("First");

JButton next = makeButton("Next");

JButton previous = makeButton("Previous");

JButton last = makeButton("Last");

JButton exit = makeButton("Exit");



//other buttons

JButton add = makeButton("Add");



//add the labels to the display panel

display.add(name);

display.add(number);

display.add(brand);

display.add(quantity);

display.add(price);

display.add(fee);



//add the buttons to the buttonPanel

buttons.add(first);

buttons.add(previous);

buttons.add(next);

buttons.add(last);

buttons.add(exit);



//add the picture panel and display to the centerPanel

displayHolder.add(display);

centerPanel.setLayout(new GridLayout(2, 1));

centerPanel.add(picture);

centerPanel.add(displayHolder);

other.add(buttons);

JPanel forAdd = new JPanel(); // add the other buttons to this panel

forAdd.add(add);

other.add(forAdd);



//add the panels to the frame

getContentPane().add(centerPanel, "Center");

getContentPane().add(other, "South");

this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

setVisible(true);



}//end Inventory10



private void makeTheDataItems ()

{

supplies[0] = new MobilePhone("3310", 001, 200, 200, "Nokia");

supplies[1] = new MobilePhone("T720", 002, 500, 250, "Motorola");

supplies[2] = new MobilePhone("5110C", 003, 35, 149, "LG");

supplies[3] = new MobilePhone("9400D", 004, 90, 300, "Samsung");

}



//method for creating and dressing buttons

private JButton makeButton(String label)

{

JButton button = new JButton(label);

button.setActionCommand(label);

button.addActionListener(this);

return button;

}



private void addItem()

{

System.out.println("eeeeeeeeee");

panel = new JPanel();

JPanel add = new JPanel();

add.setLayout(new GridLayout(2, 1));

add.setLayout(new GridLayout(4, 4));

JButton addIt = makeButton("Add Item");

JLabel name = new JLabel("Name :");

//JLabel number = new JLabel("Number :");

JLabel brand = new JLabel("Brand :");

JLabel quantity = new JLabel("Quantity :");

JLabel price = new JLabel("Price :");

add.add(name); add.add(nameField);

//add.add(number); add.add(numberField);

add.add(brand); add.add(brandField);

add.add(quantity); add.add(quantityField);

add.add(price); add.add(priceField);

panel.add(add);

JPanel forAddIt = new JPanel();

forAddIt.add(addIt);

panel.add(forAddIt);

displayHolder.remove(display);

displayHolder.add(panel);

//display = panel;

this.setVisible(true);

}//end addItem



// main methods begins execution of java application

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

// {

// Inventory11 object = new Inventory11();

// } // end main method





public void actionPerformed(ActionEvent event)

{

String command = event.getActionCommand(); //retrieves command set for the button



if(command.equals("First"))

{

displayFirst();

}

else if(command.equals("Next"))

{

displayNext();

}

else if(command.equals("Previous"))

{

displayPrevious();

}

else if(command.equals("Last"))

{

displayLast();

}

else if(command.equals("Exit"))

{

this.dispose();

System.exit(0);

}

else if(command.equals("Add"))

{

addItem();

}

else if(command.equals("Add Item"))

{

addItemToArray();

}



}//end action method



private void addItemToArray()

{

Product p = new MobilePhone(nameField.getText(), supplies.length -2, Long.parseLong(quantityField.getText()),

Double.parseDouble(priceField.getText()), brandField.getText());



//extend size of array by one first

Product[] ps = new Product[supplies.length + 1];

for(int i = 0; i < ps.length-1; i++)

{

ps[i] = supplies[i];

}

ps[supplies.length] = p;

supplies = ps;

displayHolder.remove(panel);

displayHolder.add(display);

displayLast();

this.setVisible(false);

this.setVisible(true);

}//end addItem



//utility method to reduce lines of code

private void displayItemAt(int index)

{

MobilePhone product = (MobilePhone)supplies[index];

name.setText("Item Name: "+ product.getItemName());

number.setText("Item Number: "+ product.getItemNumber());

brand.setText("Brand: "+ product.getBrand());

quantity.setText("Quantity In Stock: "+ product.getStockQuantity());

price.setText("Item Price: "+ product.getItemPrice());

totalValue.setText("Total: " + product.calculateInventoryValue());

fee.setText("Fee :"+product.calculateRestockFee());

this.setVisible(true);

}



public void displayFirst()

{

displayItemAt(0);

currentIndex = 0;

}



public void displayNext()

{

if(currentIndex == supplies.length-1)

{

displayFirst();

currentIndex = 0;

}

else

{

displayItemAt(currentIndex + 1);

currentIndex++;

}

}



public void displayPrevious()

{

if(currentIndex == 0)

{

displayLast();

currentIndex = supplies.length-1;

}

else

{

displayItemAt(currentIndex - 1);

currentIndex--;

}

}



public void displayLast()

{

displayItemAt(supplies.length-1);

currentIndex = supplies.length-1;

}

}//end class Inventory11



Inventory11 inventory11 = new Inventory11();

}



}
v_2tbrow
2007-03-31 03:15:15 UTC
It could be that it's littered with invalid syntax... e.g.:



button.setActionCommand(label)...

button.addActionListener(this)...



three dots in a row mean nothing to java
josh.weissbock
2007-03-31 03:40:17 UTC
What does the errors say?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...