Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default yet another N00b

    Okay, I'm working diligently on this inventory program too, with so many other lost souls. This is where I'm at with part 5. The only things I have left is, I can't get the "first" and "last" buttons to show up and work as they should to jump to the first and last item in the array when clicked, and I can't get my icon to show near the buttons on the bottom.

    All three files compile and get along nicely with each other.

    I've been through several books and docs outside the course material to get this far and am soooooo close to being done. Thanks in advance for pointing me in the direction of the solution.

    Java Code:
    //  InventoryProgram8c.java
    
    import java.text.DecimalFormat;
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.event.*;
    import java.awt.event.*;
    
    public class InventoryProgram8c extends JApplet
     {
    	
    	private JLabel itemNum = new JLabel("Item Number");
    	private JTextField itemNumValue = new JTextField();
    	private JLabel itemID = new JLabel("Item ID");
    	private JTextField itemIDValue = new JTextField();
    	private JLabel name = new JLabel("Name");
    	private JTextField nameValue = new JTextField();
    	private JLabel unit = new JLabel("Unit");
    	private JTextField unitValue = new JTextField();
    	private JLabel price = new JLabel("Price");
    	private JTextField priceValue = new JTextField();
    	private JLabel restockingFee = new JLabel("Restocking Fee");
    	private JTextField rfValue = new JTextField();
    	private JLabel invent = new JLabel("Inventory Value");
    	private JTextField inventValue = new JTextField();
    	private JLabel totalValue = new JLabel();
    	private JButton first = new JButton("First");
    	private JButton previous = new JButton("Previous");
    	private JButton next = new JButton("Next");
    	private JButton last = new JButton("Last");
    	
    	private AddItemID[] products;
    	private int current = 0;
    	private int total = 0;
    	
    	class ButtonListener implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			JButton button = (JButton) e.getSource();
    			
    			if (button == first) current = (current) % total;
    			if (button == previous) current = (current + total - 1) % total;
    			if (button == next) current = (current + 1) % total;
    			if (button == last) current = (current +4) % total;
    			
    			
    			display();
    		}
    		// display Icon
    		 private class MyPanel extends JPanel
         {
              ImageIcon image = new ImageIcon("logo.jpg");
              int width = image.getIconWidth();
              int height = image.getIconHeight();
              long angle = 0;
              public MyPanel()
              {
                   super();
              }
    
              public void paintComponent(Graphics g)
              {
                   super.paintComponent(g);
                   Graphics2D g2d = (Graphics2D)g;
                   g2d.rotate (Math.toRadians(angle), 200+width/2, 90+height/2);
                   g2d.drawImage(image.getImage(), 5, 5, this);
                   g2d.dispose();
              }
    
         }//end class Icon
    	}
    	
    	private void display() {
    		AddItemID product= (AddItemID)products[current];
    		DecimalFormat df=new DecimalFormat("$#.00");
    		itemNumValue.setText(product.getItemNum()+""); 
    		itemNumValue.setEditable(false);
    		itemIDValue.setText(product.getItemID());
    		itemIDValue.setEditable(false);
    		nameValue.setText(product.getName()); 
    		nameValue.setEditable(false);
    		unitValue.setText(product.getUnit()+""); 
    		unitValue.setEditable(false);
    		priceValue.setText(df.format(product.getPrice())); 
    		priceValue.setEditable(false);
    		rfValue.setText(df.format(product.getRestockingFee())); 
    		rfValue.setEditable(false);
    		inventValue.setText(df.format(product.calculateInventory()));
    		inventValue.setEditable(false);
    		totalValue.setText("The total inventory value is "+df.format(calculateAll()));
    	}
    	
    	private void bubbleSort() {
    		// Sort the product by their names
    		for (int i=0; i<products.length; i++) {
    			for (int j=i+1; j<products.length; j++) {
    				String name1 = products[i].getName();
    				String name2 = products[j].getName();
    				if (name1.compareTo(name2)>0) {
    					AddItemID temp = products[i];
    					products[i] = products[j]; 
    					products[j] = temp;
    				}
    			}
    		}
    	}
    	
    	private double calculateAll() {
    		// Calculate the value of all inventory
    		double value = 0.0;
    		for (int i=0; i<products.length; i++) {
    			value = value + products[i].calculateInventory();
    		}
    		return value;
    	}
    	
    	public void init() {
    		products = new AddItemID[5];
    		
    		// Add hard-coded data into the list
    		products[0] = new AddItemID(1, "PC0133", 5, 47.02, "SD_Memory");
    		products[1] = new AddItemID(2, "PC3400", 12, 21.70, "DDRMemory");
    		products[2] = new AddItemID(3, "PC2700", 8, 25.02, "DDRMemory");
    		products[3] = new AddItemID(4, "PC2100", 9, 34.06, "DDRMemory");
    		products[4] = new AddItemID(5, "PC3200", 8, 22.04, "DDRMemory");
    		total = 5;
    		
    		// Sort the products by their names
    		bubbleSort();
    		
    		// Show the inventory in the text area
    		ButtonListener buttonListener = new ButtonListener();
            previous.addActionListener(buttonListener);
            next.addActionListener(buttonListener);
            
            JPanel up = new JPanel();
            up.setLayout(new GridLayout(7,2));
            up.add(itemNum); up.add(itemNumValue);
            up.add(itemID); up.add(itemIDValue);
            up.add(unit); up.add(unitValue);
            up.add(name); up.add(nameValue);
            up.add(price); up.add(priceValue);
            up.add(restockingFee); up.add(rfValue);
            up.add(invent); up.add(inventValue);
            display();
            JPanel middle = new JPanel();
            middle.setLayout(new FlowLayout());
            middle.add(previous); middle.add(next);
            JPanel down = new JPanel();
            down.setLayout(new BorderLayout());
            down.add(BorderLayout.CENTER, totalValue);
            
            JPanel all = new JPanel();
            all.setLayout(new BoxLayout(all, BoxLayout.Y_AXIS));
            all.add(up);
            all.add(down);
            all.add(middle);
            
            Container cp = getContentPane();
            cp.add(BorderLayout.NORTH, all);
    	}
    	
    	public static void main(String[] args) {
    		JApplet applet = new InventoryProgram8c();
    		JFrame frame = new JFrame("Inventory Program Week 8");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(true); 
    		frame.getContentPane().add(applet);
    		frame.setSize(410,350);
    		applet.init();
    		frame.setVisible(true);
    	}
    }
    Java Code:
    // AddItemID.java
    
    public class AddItemID extends Product {
    	private String itemID;
    	
    	// Constructor with 5 arguments
    	public AddItemID(int itemNum, String name, int unit, double price, String itemID) {
    		super(itemNum, name, unit, price);
    		this.itemID = itemID;
    	}
    	
    	// Getter and Setter methods
    	public void setItemID(String itemID) {
    		this.itemID = itemID;
    	}
    	public String getItemID() {
    		return itemID;
    	}
    	
    	// Calculate the restocking fee
    	public double getRestockingFee() {
    		return getPrice() * 0.05;
    	}
    	
    	// Get the value of the inventory: override the method calculateInventory
    	public double calculateInventory() {
    		return getUnit() * getPrice();
    	}
    }
    Java Code:
    // Product.java
    
    class Product {
    	private int itemNum;	// item number
    	private String name;	// item name
    	private int unit;		// number of units of the item
    	private double price;	// price of each unit
    	
    	public Product() {}		// Default constructor
    	public Product(int itemNum, String name, int unit, double price) {
    		// Constructor with input
    		this.itemNum = itemNum;
    		this.name = name;
    		this.unit = unit;
    		this.price = price;
    	}
    	
    	// Getter and Setter methods
    	public void setItemNum(int itemNum) {
    		this.itemNum = itemNum;
    	}
    	public int getItemNum() {
    		return itemNum;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setUnit(int unit) {
    		this.unit = unit;
    	}
    	public int getUnit() {
    		return unit;
    	}
    	public void setPrice(double price) {
    		this.price = price;
    	}
    	public double getPrice() {
    		return price;
    	}
    	
    	// Get the value of the inventory
    	public double calculateInventory() {
    		return unit * price;
    	}
    }

  2. #2
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    Okay, after a few hours sleep I got the buttons all sorted out and working correctly.

    I would still appreciate a little help with getting an icon to display in the lower left corner beneath the buttons.
    I'm becoming delirious from reading and searching and can't seem to make the compiler happy with anything I've tried. I had it working once before but can't remember how I did it.

  3. #3
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Talking what compiler are you using?

    Quote Originally Posted by java_student_4now View Post
    Okay, after a few hours sleep I got the buttons all sorted out and working correctly.
    Be careful when just awoke in last few hours not to go on a deletion spree.
    Quote Originally Posted by java_student_4now View Post
    I would still appreciate a little help with getting an icon to display in the lower left corner beneath the buttons.
    Put a jpeg in each in class Product, use it in the JLabel ( probably, have not looked at all the code )
    Quote Originally Posted by java_student_4now View Post
    I'm becoming delirious from reading and searching and can't seem to make the compiler happy with anything I've tried. I had it working once before but can't remember how I did it.
    You sound pretty sane to me. Try thus:
    Java Code:
    application/vnd.ms-excel
    written from a server to Sue B. Good at Cuttham, Burnham and Runn requires additional skills which will be forthcoming in your next nightmare.

    I try to revise my code while in nightmares, it gets innovative insights in scope and is much easier that recovering all the lost snippets - I call my index page Cold Slab Bank.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  4. #4
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the response.
    I'm using JCreator to code and compile.
    I won't be deleting anything, as a matter of fact I just copied my files off onto a laptop because we just had a brief power loss that may have just shaved a few years off my life as my desktop shut down.
    My buttons are working great, all I need is a jpg. to show up within the main window of the applet, hopefully in the lower left corner. Then, hopefully I'll awake from this nightmare I'm still having.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    ImageIcon image = new ImageIcon("logo.jpg");
    Is this the image you are trying to show?
    You''ve defined it as local so it will not be available in the rest of the class. Where in your code do you try to display it?

    Why are you using the JApplet class with a main() method? Is this supposed to work both as an applet and as an application?

  6. #6
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default Thanks Norm

    Quote Originally Posted by Norm View Post
    Is this the image you are trying to show?
    You''ve defined it as local so it will not be available in the rest of the class. Where in your code do you try to display it?

    Why are you using the JApplet class with a main() method? Is this supposed to work both as an applet and as an application?
    Thanks for the response Norm.
    I got the image to work by changing the display image code to:


    Java Code:
     //icon add image
            JLabel jlbLabel1;
            ImageIcon icon = new ImageIcon("logo.jpg");
    		// Creating an Icon
    	
    		jlbLabel1 = new JLabel( icon, JLabel.LEFT);
    		jlbLabel1.setVerticalTextPosition(JLabel.BOTTOM);
    		jlbLabel1.setHorizontalTextPosition(JLabel.LEFT);
    			
    		add(jlbLabel1);
    		
    		//end creating an Icon
    To answer your question, the project started out as an application and then had to be converted to an applet. Now, this week I have to add more action buttons with the ability to add, delete, modify to the array. A save button which executes saving present and new array contents as a local file is also a requirement.

    Sorry about my slow response to your assistance, we had a two day power outage here due to the hurricane remnants passing through and wreaking havoc on the local utilities.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Come on back if you've got more questions.

    PS. I also had a day w/o power as Ike passed by to the east of us.

Similar Threads

  1. n00b help with java2D api
    By michcio in forum Java 2D
    Replies: 7
    Last Post: 10-30-2008, 07:18 PM
  2. n00b: confused about java's RIA/Rich Client Story
    By bryancostanich in forum New To Java
    Replies: 2
    Last Post: 05-02-2008, 02:16 AM
  3. n00b at JSP - Need help
    By sylo18 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-04-2008, 08:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •