Code:
import java.util.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Inventory2 extends JFrame implements ActionListener { //Utility class for displaying the picture //If we are going to use a class/method/variable inside that class only, we declare it private in that class 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 title; JLabel quantity; JLabel price; JLabel fee; JLabel totalValue; JTextField nameField = new JTextField(20); JTextField numberField = new JTextField(20); JTextField titleField = new JTextField(20); JTextField quantityField = new JTextField(20); JTextField priceField = new JTextField(20); JPanel display; JPanel displayHolder; JPanel panel; boolean locked = false; //Notice how I've used this flag to keep the interface clean public Inventory2() { makeTheDataItems(); setSize(700, 500); setTitle("Inventory Program"); //make the panels display = new JPanel(); JPanel other = new JPanel(); other.setLayout(new GridLayout(2, 1)); JPanel picture = new MyPanel(); JPanel buttons = new JPanel(); JPanel centerPanel = new JPanel(); displayHolder = new JPanel(); display.setLayout(new GridLayout(7, 1)); //other.setLayout(new GridLayout(1, 1)); //make the labels name = new JLabel("Name :"); number = new JLabel("Number :"); title = new JLabel("Title :"); quantity = new JLabel("Quantity :"); price = new JLabel("Price :"); fee = new JLabel("Fee :"); totalValue = new JLabel("Total Value :"); //Use the utility method to make the buttons JButton first = makeButton("First"); JButton next = makeButton("Next"); JButton previous = makeButton("Previous"); JButton last = makeButton("Last"); JButton search = makeButton("Search"); //Other buttons JButton add = makeButton("Add"); JButton modify = makeButton("Modify"); JButton delete = makeButton("Delete"); JButton save = makeButton("Save"); JButton exit = makeButton("Exit"); //Add the labels to the display panel display.add(name); display.add(number); display.add(title); 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(search); //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); forAdd.add(modify); forAdd.add(delete); forAdd.add(save); forAdd.add(exit); 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); } private void makeTheDataItems () { Product p1 = new DVD("The one", 001, 200, 100, "The one"); Product p2 = new DVD("Once upon a time in China V", 002, 500, 10000, "Once upon a time in China V"); Product p3 = new DVD("Rat Race", 003, 100, 3000, "Rat Race"); Product p4 = new DVD("The Man in the Iron Mask", 004, 3000, 9000, "The Man in the Iron Mask"); supplies[0] = p1; supplies[1] = p2; supplies[2] = p3; supplies[3] = p4; } //Utility method for creating and dressing buttons private JButton makeButton(String label) { JButton button = new JButton(label); button.setPreferredSize(new Dimension(100, 25)); button.setActionCommand(label); button.addActionListener(this); return button; } private void addItem() { panel = new JPanel(); JPanel add = new JPanel(); add.setLayout(new GridLayout(7, 2)); JButton addIt = makeButton("Add Item"); JLabel name = new JLabel("Name :"); JLabel title = new JLabel("Title :"); JLabel quantity = new JLabel("Quantity :"); JLabel price = new JLabel("Price :"); add.add(name); add.add(nameField); add.add(title); add.add(titleField); 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); } public static void main( String args[]) { new Inventory2().displayFirst(); //The main method should not have too much code } // end main method public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); //This retrieves the command that we set for the button //Always compare strings using the .equals method and not using == if(command.equals("First")) { if(!locked) { displayFirst(); } } else if(command.equals("Next")) { if(!locked) { displayNext(); } } else if(command.equals("Previous")) { if(!locked) { displayPrevious(); } } else if(command.equals("Last")) { if(!locked) { displayLast(); } } else if(command.equals("Exit")) { this.dispose(); System.exit(0); } else if(command.equals("Add")) { if(!locked) { addItem(); locked = true; } } else if(command.equals("Add Item")) { if(!locked) { addItemToArray(); locked = true; } } else if(command.equals("Modify")) { if(!locked) { modify(); locked = true; } } else if(command.equals("Update")) { if(!locked) { modifyItemInArray(); locked = true; } } else if(command.equals("Delete")) { if(!locked) { DVD dvd = (DVD)supplies[currentIndex]; int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete item "+dvd.getItemNumber()); if(confirm == JOptionPane.YES_OPTION) { removeItemAt(currentIndex); displayFirst(); } } } } private void modify() { DVD dvd = (DVD)supplies[currentIndex]; panel = new JPanel(); JPanel add = new JPanel(); add.setLayout(new GridLayout(7, 2)); JButton update = makeButton("Update"); JLabel number = new JLabel("Number :"); JLabel name = new JLabel("Name :"); JLabel title = new JLabel("Title :"); JLabel quantity = new JLabel("Quantity :"); JLabel price = new JLabel("Price :"); add.add(number); numberField.setText(""+dvd.getItemNumber()); numberField.setEditable(false); add.add(numberField); add.add(name); nameField.setText(dvd.getItemName()); add.add(nameField); titleField.setText(dvd.getTitle()); titleField.setEditable(false); add.add(title); add.add(titleField); add.add(quantity); quantityField.setText(""+dvd.getStockQuantity()); add.add(quantityField); add.add(price); add.add(priceField); priceField.setText(""+dvd.getItemPrice()); panel.add(add); JPanel forAddIt = new JPanel(); forAddIt.add(update); panel.add(forAddIt); displayHolder.remove(display); displayHolder.add(panel); //display = panel; this.setVisible(true); } private void addItemToArray() { Product p = new DVD(nameField.getText(), supplies.length + 1, Long.parseLong(quantityField.getText()), Double.parseDouble(priceField.getText()), titleField.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); } //Utility method to ease the typing and reuse code //This method reduces the number of lines of our code private void displayItemAt(int index) { DVD product = (DVD)supplies[index]; name.setText("Item Name: "+ product.getItemName()); number.setText("Item Number: "+ product.getItemNumber()); title.setText("Title: "+ product.getTitle()); quantity.setText("Quantity In Stock: "+ product.getStockQuantity()); price.setText("Item Price: "+ product.getItemPrice()); totalValue.setText("Total: " + product.calculateInventoryValue()); fee.setText("Fee :"+product.calculateRestockFee()); locked = false; this.repaint(); this.setVisible(true); } private void modifyItemInArray() { Product p = new DVD(nameField.getText(), supplies.length + 1, Long.parseLong(quantityField.getText()), Double.parseDouble(priceField.getText()), titleField.getText()); supplies[currentIndex] = p; displayHolder.remove(panel); displayHolder.add(display); displayItemAt(currentIndex); this.setVisible(false); this.setVisible(true); } private void removeItemAt(int index) { Product[] temp = new Product[supplies.length-1]; int counter = 0; for(int i = 0; i < supplies.length;i++) { if(i == index) { //skip the item to delete } else { temp[counter++] = supplies[i]; } } supplies = temp; } 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 Inventory2
Comment