import javax.swing.JFrame; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.text.NumberFormat; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; import javax.swing.JFrame; //AWT graphical Jframe start public class Inventory extends JFrame { //All of the private strings private Container cp = getContentPane(); private static int itemNum[] = new int[4]; private static String name[] = new String[4]; private static int units[] = new int[4]; private static double price[] = new double[4]; private static int i = 0; //Creating Inventory public Inventory(){ setLayout(new FlowLayout()); } //Setting Item number, name, how many units in stock, and the price of each public Inventory(int _itemNum, String _name, int _units, double _price) { itemNum[i] = _itemNum; name[i] = _name; units[i] = _units; price[i] = _price; i = i + 1; } //Item number public static int getItemNum(int k) { return itemNum[k]; } //Ithem Name public static String getItemName(int k) { return name[k]; } //Number of units public static int getItemUnits(int k) { return units[k]; } //Item price public static double getItemPrice(int k) { return price[k]; } //Value of Inventory public static double valueOfInventory(double p,int u) { return p*u; } public static void swap(int j, int min) { String tmp; tmp = name[j]; name[j] = name[min]; name[min] = tmp; int temp = itemNum[j]; itemNum[j] = itemNum[min]; itemNum[min] = temp; temp = units[j]; units[j] = units[min]; units[min] = temp; double temp1 = price[j]; price[j] = price[min]; price[min] = temp1; } //Value of inventory public double showValueofInventory() { double totalVal = 0; for (int j = 0; j < i; j++) { totalVal = totalVal + valueOfInventory(price[j], units[j]); } return totalVal; } } // Start of DVD Class class DVD extends Inventory { // Subclass to add to DVD Movie static String genre[] = new String[4]; // Restock fee to add to inventory value static double restockingFee[] = new double[4]; static int i; static double TotalVal; private Container cp = getContentPane(); public DVD(String genre, double restockingFee, int Item_Number, String Item_Name, int Items_in_Stock, double Item_Price) { super(Item_Number, Item_Name, Items_in_Stock, Item_Price); this.genre[i] = genre; this.restockingFee[i] = restockingFee; i = i + 1; } //Returning string public String toString() { StringBuffer sb = new StringBuffer("\nGenre: ").append(genre).append("\n"); sb.append(super.toString()); return sb.toString(); } //Restocking value public static double getRestockFee(int val) { return restockingFee[val]; } public void ShowInvent() { JFrame f=new JFrame(); Container container = f.getContentPane(); container.add(getPanel()); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(650, 450); f.setVisible(true); } private JPanel getPanel() { //Value of inventory before and after sorting JLabel lbl = new JLabel("Total Value Of Inventory"); JLabel lblBeforeSorting = new JLabel("Before Sorting"); JLabel lblAfterSorting = new JLabel("After Sorting"); JPanel pnl = new JPanel(new GridLayout(5, 1)); pnl.add(lblBeforeSorting); pnl.add(getScrollPane()); //Sorting for (int j = 0; j < 4; j++) { int min = j; for (int k = 0; k < 4; k++) { if (Inventory.getItemName(k).compareToIgnoreCase(Inventory.getItemName(min)) > 0) { min = k; Inventory.swap(j, min); String rtemp = genre[j]; genre[j] = genre[min]; genre[min] = rtemp; } } } //Sorting Ends lbl.setText("Total Value Of Inventory :: " + String.format("%3.2f", TotalVal)); pnl.add(lblAfterSorting); pnl.add(getScrollPane()); pnl.add(lbl); return pnl; } public static JScrollPane getScrollPane() { Object rows[][] = new Object[4][7]; String titles[] = new String[] { "Genre", "Item #", "Name", "Quantity in Stock", "Unit Price", "Restock Fee", "Value Of Inventory" }; for (int j = 0; j < 4; j++) { rows[j][0] = genre[j]; rows[j][1] = Inventory.getItemNum(j); rows[j][2] = Inventory.getItemName(j); rows[j][3] = Inventory.getItemUnits(j); rows[j][4] = "$" + Inventory.getItemPrice(j); rows[j][5] = "$" + String.format("%3.2f", Products.valueOfRestockFee(Inventory.getItemPrice(j), getRestockFee(j))); rows[j][6] = "$" + String.format("%3.2f", Products.valueOfInventory(Inventory.getItemPrice(j), Inventory.getItemUnits(j), getRestockFee(j))); TotalVal += Products.valueOfInventory(Inventory.getItemPrice(j), Inventory.getItemUnits(j), getRestockFee(j)); } JTable tableTrade = new JTable(rows, titles); JScrollPane scrTabel1 = new JScrollPane(tableTrade); return scrTabel1; } } // End DVD Class class Products { public static double valueOfInventory(double p, double u,double rf) { double vOfI = (p * u) + (p*u*rf); return (vOfI ); } public static double valueOfRestockFee(double p, double rf) { double percent = 0; percent = (p * 5) / 100; return percent; } } class InventoryMain extends JFrame { public static void main( String[] args){ double restockFee = 0.05; DVD p = new DVD("Drama" ,restockFee, 1,"House MD" , 8, 12.99); DVD q = new DVD("Comedy", restockFee, 2, "Ghostbusters", 7, 15.99); DVD r = new DVD("Action", restockFee, 3, "Fight Club", 9, 24.99); DVD s = new DVD("Drama", restockFee, 4, "Lost", 4, 19.99); p.ShowInvent(); } }