Originally posted by stevedub
Inventory program in Java
Collapse
X
-
Ok, I was able to get the OP's code to load properly. I have the same assignment due this Sunday. I think I may be to late to turn in the assignment from last week, the one that I gave the link to. So I guess I will try to work on the assignment for this week. Here is the link for this assingment.
https://ecampus.wintu.e du/afm101/secure/view-attachment.jspa ? ID=2016792&mess ageID=10060298& name=Week%208%2 0checkpoint%202 %20example.docComment
-
Originally posted by stevedubOk, I was able to get the OP's code to load properly. I have the same assignment due this Sunday. I think I may be to late to turn in the assignment from last week, the one that I gave the link to. So I guess I will try to work on the assignment for this week. Here is the link for this assingment.
https://ecampus.wintu.e du/afm101/secure/view-attachment.jspa ? ID=2016792&mess ageID=10060298& name=Week%208%2 0checkpoint%202 %20example.docComment
-
Here is the code:
Code:import java.util.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; class Product implements Comparable { String itemName; // class variable that stores the item name double itemNumber; // 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() { itemName = ""; itemNumber = 0.0; stockQuantity = 0L; price = 0.0; } public Product(String name, int number, long stockQuantity, double price) { this.itemName = name; this.itemNumber = number; this.stockQuantity = stockQuantity; this.price = price; } public void setItemName(String name) { this.itemName = itemName; } public String getItemName() { return itemName; } public void setItemNumber(double number) { this.itemNumber = itemNumber; } public double getItemNumber() { return itemNumber; } 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 itemName.compareTo(p.getItemName()); } public String toString() { return "Title :"+getItemName() + "\nStock Number"+itemNumber+"\nPrice"+price+"\nQuantity"+stockQuantity + "\nValue :"+calculateInventoryValue(); } } class DVD extends Product implements Comparable { private String rating; public DVD() { super(); //Call the constructor in Product rating = ""; //Add the additonal attribute } public DVD(String itemName, int itemNumber, long stockQuantity, double price, String rating) { super(itemName, itemNumber, stockQuantity, price); //Call the constructor in Product this.rating = rating; //Add the additonal attribute } public void setRating(String rating) { this.rating = rating; } public String getRating() { return rating; } public double calculateInventoryValue() { return getItemPrice() * getStockQuantity() + getItemPrice()*getStockQuantity()*0.05; //Had you forgotten to include the restocking fee? } //What happens when we want to change the restocking fee? 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 "Name :"+getItemName() + "\nNumber"+getItemNumber()+"\nPrice"+getItemPrice()+"\nQuantity"+getStockQuantity() +"\nRating :"+getRating()+"\nValue"+calculateInventoryValue(); } }
Comment
-
Code:import java.util.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Inventory1 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("dvd.gif"); 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 itemName ; JLabel itemNumber; JLabel rating; JLabel quantity; JLabel price; JLabel fee; JLabel totalValue; JTextField itemNameField = new JTextField(20); JTextField itemNumberField = new JTextField(20); JTextField ratingField = new JTextField(20); JTextField quantityField = new JTextField(20); JTextField priceField = new JTextField(20); JPanel display; JPanel displayHolder; JPanel panel; public Inventory1() { makeTheDataItems(); setSize(600, 500); setTitle("DVD Inventory Program"); //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(5, 3)); other.setLayout(new GridLayout(2, 1)); //make the labels itemName = new JLabel("Name :"); itemNumber = new JLabel("Number :"); rating = new JLabel("Rating :"); 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 exit = makeButton("Exit"); //Other buttons JButton add = makeButton("Add"); //Add the labels to the display panel display.add(itemName); display.add(itemNumber); display.add(rating); 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); } private void makeTheDataItems () { Product p1 = new DVDTitle("Beerfest", 001, 200, 12.99, "R"); Product p2 = new DVDTitle("The Illusionist", 002, 50, 9.95, "PG-13"); Product p3 = new DVDTitle("Employee of the Month ", 003, 100, 19.95, "R"); Product p4 = new DVDTitle("Pirates of the Caribbean 2 ", 004, 75, 9.99, "PG-13"); 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.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 itemName = new JLabel("Name :"); //JLabel itemNumber = new JLabel("Number :"); JLabel rating = new JLabel("Rating :"); JLabel quantity = new JLabel("Quantity :"); JLabel price = new JLabel("Price :"); add.add(itemName); add.add(itemNameField); //add.add(itemNumber); add.add(itemNumberField); add.add(rating); add.add(ratingField); 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[]) { Inventory1 object = new Inventory1(); //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")) { 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(); } } private void addItemToArray() { Product p = new DVDTitle(itemNameField.getText(), supplies.length -2, Long.parseLong(quantityField.getText()), Double.parseDouble(priceField.getText()), ratingField.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) { DVDTitle product = (DVDTitle)supplies[index]; itemName.setText("Item Name: "+ product.getItemName()); itemNumber.setText("Item Number: "+ product.getItemNumber()); rating.setText("Rating: "+ product.getRating()); 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 Inventory2.1
Comment
-
Code:import java.util.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; class Product implements Comparable { String name; double number; long stockQuantity; double 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 "Name :"+getItemName() + "\nNumber"+number+"\nPrice"+price+"\nQuantity"+stockQuantity + "\nValue :"+calculateInventoryValue(); } } class DVD extends Product implements Comparable { private String title; public DVD() { super(); //Call the constructor in Product title = ""; //Add the additonal attribute } public DVD(String name, int number, long stockQuantity, double price, String title) { super(name, number, stockQuantity, price); //Call the constructor in Product this.title = title; //Add the additonal attribute } public void setTitle(String title) { this.title = title; } public String getTitle() { return title; } public double calculateInventoryValue() { return getItemPrice() * getStockQuantity() + getItemPrice()*getStockQuantity()*0.05; } 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 "Name :"+getItemName() + "\nNumber"+getItemNumber()+"\nPrice"+getItemPrice()+"\nQuantity"+getStockQuantity() +"\nTitle :"+getTitle()+"\nValue"+calculateInventoryValue(); } } 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; public Inventory2() { makeTheDataItems(); setSize(500, 500); setTitle("Inventory Program"); //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(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 exit = makeButton("Exit"); //Other buttons JButton add = makeButton("Add"); //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(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); } private void makeTheDataItems () { Product p1 = new DVD("test", 001, 200, 100, "The one"); Product p2 = new DVD("test", 002, 500, 10000, "test"); Product p3 = new DVD("test", 003, 100, 3000, "test"); Product p4 = new DVD("test", 100, 3000, 9000, "test"); 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.setActionCommand(label); button.addActionListener(this); return button; } private void addItem() { 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 title = new JLabel("Title :"); JLabel quantity = new JLabel("Quantity :"); JLabel price = new JLabel("Price :"); add.add(name); add.add(nameField); //add.add(number); add.add(numberField); 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[]) { Inventory2 object = new Inventory2(); //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")) { 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(); } } private void addItemToArray() { Product p = new DVD(nameField.getText(), supplies.length -2, 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) { MobilePhone product = (MobilePhone)supplies[index]; name.setText("Item Name: "+ product.getItemName()); number.setText("Item Number: "+ product.getItemNumber()); title.setText("Title: "+ 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.repaint(); 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 Inventory2
You should ask where you don't understand and point out where it differs from your requirementsComment
-
Ok, I am getting some errors when trying to compile the edited code. I noticed their was some code about a mobile phone in there too. I will try to sit down and go through it to see if I can see what's wrong.
Thanks again so much for all of your help, you don't know how much I appreciate it.Comment
-
Originally posted by stevedubOk, I am getting some errors when trying to compile the edited code. I noticed their was some code about a mobile phone in there too. I will try to sit down and go through it to see if I can see what's wrong.
Thanks again so much for all of your help, you don't know how much I appreciate it.Comment
-
Originally posted by stevedubI must have something set up wrong in the MyPanel part of the code, am I correct? I just need to figure out how to display the logo.
Put the image in the same folder as the code is in and change the name Sample.jpg in the MyPanel class to the name of your image.Comment
-
Ok r035198x, it is time for my final assignment and I am way lost. I could really use your expertise if you have the time. Here is what the assignment is:
• Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the number of units in stock, and the price of each unit. An item added to the inventory should have an item number one more than the previous last item.
• Add a Save button to the GUI that saves the inventory to a C:\data\invento ry.dat file.
• Use exception handling to create the directory and file if necessary.
• Add a search button to the GUI that allows the user to search for an item in the inventory by the product name. If the product is not found, the GUI should display an appropriate message. If the product is found, the GUI should display that product’s information in the GUI.
If you can help me at all I would greatly appreciate it. This class has proven to be too difficult for a beginner in Java, and it is definately not something that can be taught in 9 weeks.Comment
-
Originally posted by stevedubOk r035198x, it is time for my final assignment and I am way lost. I could really use your expertise if you have the time. Here is what the assignment is:
• Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the number of units in stock, and the price of each unit. An item added to the inventory should have an item number one more than the previous last item.
• Add a Save button to the GUI that saves the inventory to a C:\data\invento ry.dat file.
• Use exception handling to create the directory and file if necessary.
• Add a search button to the GUI that allows the user to search for an item in the inventory by the product name. If the product is not found, the GUI should display an appropriate message. If the product is found, the GUI should display that product’s information in the GUI.
If you can help me at all I would greatly appreciate it. This class has proven to be too difficult for a beginner in Java, and it is definately not something that can be taught in 9 weeks.Comment
Comment