daniel_lawson9999

InventoryProject

Apr 13th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.39 KB | None | 0 0
  1. //Inventory5
  2. /**
  3.  * Write a description of class BorderLayoutManager here.
  4.  *
  5.  * @author (your name)
  6.  * @version (a version number or a date)
  7.  */
  8.  
  9. import javax.swing.border.*;
  10. import java.awt.Font;
  11. import javax.swing.*;
  12. import javax.swing.border.*;
  13. import javax.swing.event.ListSelectionEvent;
  14. import javax.swing.event.ListSelectionListener;
  15. import java.awt.*;
  16. import java.awt.event.*;
  17. import java.util.ArrayList;
  18. import java.util.*;
  19. import java.io.*;
  20.  
  21.  
  22. public class Inventory5 extends JFrame
  23. {
  24.     private ArrayList<ArrayList<ArrayList<Thing>>> item = new ArrayList<ArrayList<ArrayList<Thing>>>();
  25.     private ArrayList<String> categories = new ArrayList<>();
  26.     private ArrayList<ArrayList<String>> subCategories = new ArrayList<ArrayList<String>>();
  27.     private JList<String> categoryList, subcategoryList, itemList;
  28.     private DefaultListModel<String> categoryModel, subcategoryModel;
  29.     private DefaultListModel<String> itemModel;
  30.     private MouseHandler mh;
  31.     private JLabel image;
  32.     private int categoryP = -1;
  33.     private int subP = -1;
  34.     private int itemP = -1;
  35.  
  36.     private JPanel contents; // contents pane
  37.     //Region panels
  38.     private JPanel panelCenter;
  39.     private JPanel panelEast;
  40.     private JPanel panelSouth;
  41.     private JPanel panelWest;
  42.     private JPanel panelSearch,panelWestSearch, panelEastSearch;
  43.     // Borders:
  44.     private Border borderContents = BorderFactory.createEmptyBorder(10,10,10,10);
  45.     private Border borderRegion = BorderFactory.createLineBorder(Color.GRAY,2);
  46.     private Border subBorderRegion = BorderFactory.createEmptyBorder(0,0,0,0);
  47.     //Colors:
  48.     private Color colorContents = Color.WHITE;
  49.     private JButton checkOutButton,checkInButton;
  50.     private JLabel locationLabel, quantityLabel;
  51.     private JTextField enterTextField;
  52.     //Fonts:
  53.     private Font fontLabels = new Font(Font.DIALOG, Font.BOLD,16);
  54.     private Font smallFontLabels = new Font(Font.DIALOG,Font.BOLD,12);
  55.     private Boolean itemHighlighted = false;
  56.     public Inventory5 ()throws FileNotFoundException {
  57.         super("Inventory");
  58.         setFonts();
  59.  
  60.         File f = new File("C:\\Users\\danie\\Documents\\inventory.txt");
  61.         Scanner s = new Scanner(f);
  62.         readContents(s, item, subCategories, categories);
  63.  
  64.         mh = new MouseHandler();
  65.  
  66.         categoryModel = new DefaultListModel<>();
  67.         for (String str : categories) {
  68.             categoryModel.addElement(str);
  69.         }
  70.         categoryList = new JList<>(categoryModel);
  71.         categoryList.setBackground(Color.lightGray);
  72.  
  73.  
  74.         subcategoryModel = new DefaultListModel<>();
  75.         subcategoryList = new JList<>(subcategoryModel);
  76.         subcategoryList.setBackground(Color.lightGray);
  77.  
  78.  
  79.         itemModel = new DefaultListModel<>();
  80.         itemList = new JList<String>(itemModel);
  81.         itemList.setBackground(Color.lightGray);
  82.  
  83.  
  84.         categoryList.addMouseListener(mh);
  85.         subcategoryList.addMouseListener(mh);
  86.         itemList.addMouseListener(mh);
  87.  
  88.         contents = new JPanel();
  89.         contents.setLayout(new BorderLayout());
  90.         contents.setBorder(borderContents);
  91.         contents.setBackground(colorContents);
  92.         setContentPane(contents);
  93.  
  94.         panelCenter = new JPanel();
  95.         panelCenter.setPreferredSize(new Dimension(125,300));
  96.         panelCenter.setBorder(borderRegion);
  97.         panelCenter.setBackground(Color.lightGray);
  98.         subcategoryList.setAlignmentX(LEFT_ALIGNMENT);
  99.         panelCenter.add(new JLabel("Sub-Categories"));
  100.         panelCenter.add(subcategoryList);
  101.         contents.add(panelCenter, BorderLayout.CENTER);
  102.  
  103.         panelEast = new JPanel();
  104.         panelEast.setBorder(borderRegion);
  105.         panelEast.setPreferredSize(new Dimension(100,300));
  106.         panelEast.setBackground(Color.lightGray);
  107.         panelEast.add(new JLabel("Categories"));
  108.         panelEast.add(itemList);
  109.         contents.add(panelEast, BorderLayout.EAST);
  110.  
  111.         panelWest = new JPanel();
  112.         panelWest.setBorder(borderRegion);
  113.         panelWest.setPreferredSize(new Dimension(100,300));
  114.         panelWest.setBackground(Color.lightGray);
  115.         panelWest.add(new JLabel("Items"));
  116.         panelWest.add(categoryList);
  117.         contents.add(panelWest, BorderLayout.WEST);
  118.  
  119.         panelSouth = new JPanel();
  120.         panelSouth.setBorder(borderRegion);
  121.         panelSouth.setPreferredSize(new Dimension(300,250));
  122.         panelSouth.setBackground(Color.lightGray);
  123.         contents.add(panelSouth, BorderLayout.SOUTH);
  124.  
  125.         ///*
  126.         locationLabel = new JLabel("Location: Not Available");
  127.         quantityLabel = new JLabel("Quantity: Not Found");
  128.         locationLabel.setFont(smallFontLabels);
  129.         quantityLabel.setFont(smallFontLabels);
  130.         enterTextField = new JTextField(8);
  131.  
  132.  
  133.         checkInButton = new JButton("Check-in");
  134.         checkInButton.setFont(smallFontLabels);
  135.         checkOutButton = new JButton("Check-out");
  136.         checkOutButton.setFont(smallFontLabels);
  137.  
  138.  
  139.         panelWestSearch = new JPanel();
  140.         panelWestSearch.setBorder(subBorderRegion);
  141.         panelWestSearch.setPreferredSize(new Dimension(150,100));
  142.         panelWestSearch.setBackground(Color.lightGray);
  143.         panelWestSearch.add(enterTextField);
  144.         panelWestSearch.add(checkInButton);
  145.         panelWestSearch.add(checkOutButton);
  146.  
  147.         panelEastSearch = new JPanel();
  148.         panelEastSearch.setBorder(subBorderRegion);
  149.         panelEastSearch.setPreferredSize(new Dimension(160,100));
  150.         panelEastSearch.add(locationLabel);
  151.         panelEastSearch.add(new Box(0));
  152.         panelEastSearch.add(quantityLabel);
  153.         panelEastSearch.setBackground(Color.lightGray);
  154.  
  155.  
  156.         panelSearch = new JPanel();
  157.         panelSearch.setPreferredSize(new Dimension(300,100));
  158.         panelSearch.setBackground(Color.lightGray);
  159.         panelSearch.add(panelWestSearch,BorderLayout.WEST);
  160.         panelSearch.add(panelEastSearch, BorderLayout.EAST);
  161.         panelSearch.setBorder(borderRegion);
  162.         contents.add(panelSearch,BorderLayout.NORTH);
  163.  
  164.         ButtonListener bl = new ButtonListener();
  165.         checkInButton.addActionListener(bl);
  166.         checkOutButton.addActionListener(bl);
  167.         //*/
  168.  
  169.         setResizable(false);
  170.         pack();
  171.         setVisible (true);
  172.     }
  173.     private void setFonts()
  174.     {
  175.         UIManager.put("Label.font", fontLabels);
  176.     }
  177.     private class ButtonListener implements ActionListener{
  178.         public void actionPerformed(ActionEvent e){
  179.             int quantity = 0;
  180.             if(itemHighlighted) {
  181.                 Scanner s = new Scanner(enterTextField.getText());
  182.                 if( s.hasNextInt()) {
  183.                     int number = s.nextInt();
  184.                     quantity = item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).getInventory();
  185.                     if (e.getSource() == checkInButton) {
  186.                         item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).changeInventory(number);
  187.                         quantityLabel.setText("Quantity: " + (quantity + number));
  188.                     }
  189.                     if (e.getSource() == checkOutButton && quantity > 0) {
  190.                         item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).changeInventory(-number);
  191.                         quantityLabel.setText("Quantity: " + (quantity - number));
  192.                     }
  193.                 }
  194.                 else{
  195.                     System.out.println("error");
  196.                 }
  197.             }
  198.         }
  199.  
  200. }
  201.     public static void main (String [] args) throws FileNotFoundException{
  202.         Inventory5 gui = new Inventory5();
  203.         gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  204.     }
  205.  
  206.     private class MouseHandler  extends MouseAdapter{
  207.         public void mouseClicked(MouseEvent me) {
  208.  
  209.             if (categoryP != categoryList.getSelectedIndex()) {
  210.                 itemHighlighted = false;
  211.                 if(itemP != -1){
  212.                     contents.remove(image);
  213.                     setVisible(true);
  214.                 }
  215.                 subcategoryModel.removeAllElements();
  216.                 itemModel.removeAllElements();
  217.  
  218.                 for (int i = 0; i < subCategories.get(categoryList.getSelectedIndex()).size(); i++) {
  219.                     subcategoryModel.addElement(subCategories.get(categoryList.getSelectedIndex()).get(i));
  220.                 }
  221.  
  222.                 categoryP = categoryList.getSelectedIndex();
  223.             }
  224.  
  225.             else if (subP != subcategoryList.getSelectedIndex()) {
  226.                 itemHighlighted =false;
  227.                 System.out.println(itemP);
  228.                 if(itemP != -1){
  229.                     panelSouth.remove(image);
  230.                     setVisible(true);
  231.                 }
  232.                 itemModel.removeAllElements();
  233.  
  234.                 for (int i = 0; i < item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).size(); i++) {
  235.                     itemModel.addElement(item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(i).getName());
  236.                 }
  237.  
  238.                 subP = subcategoryList.getSelectedIndex();
  239.             } else if (itemP != itemList.getSelectedIndex()) {
  240.                 if(itemP != -1)panelSouth.remove(image);
  241.                 itemHighlighted = true;
  242.                 quantityLabel.setText("Quantity " +item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).getInventory());
  243.                 String name = item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).getName();
  244.                 System.out.println(("C:\\Users\\danie\\Documents\\inventoryPhotos\\" + name  + ".PNG"));
  245.                 image = new JLabel(new ImageIcon("C:\\Users\\danie\\Documents\\inventoryPhotos\\" + name + ".PNG"));
  246.                 Dimension test = new Dimension(200,200);
  247.                 image.setMinimumSize(test);
  248.                 image.setPreferredSize(test);
  249.                 image.setMaximumSize(test);
  250.                 //setSpecificSize
  251.                 panelSouth.add(image);
  252.                 setVisible(true);
  253.                 itemP = itemList.getSelectedIndex();
  254.             }
  255.  
  256.         }
  257.     }
  258.  
  259.     private static void readContents(Scanner s, ArrayList<ArrayList<ArrayList<Thing>>> list, ArrayList<ArrayList<String>> subCategories, ArrayList<String> categories) {
  260.         while (s.hasNextLine()) {
  261.             String line = s.nextLine();
  262.             Scanner l = new Scanner(line);
  263.             String type = "";
  264.             if(l.hasNext()) type =l.next();
  265.             if (type.equals("-")) {
  266.                 categories.add(l.next());
  267.                 list.add(new ArrayList<ArrayList<Thing>>());
  268.                 subCategories.add(new ArrayList<String>());
  269.             } else if (type.equals("+")) {
  270.                 subCategories.get(subCategories.size() - 1).add(l.next());
  271.                 list.get(list.size() - 1).add(new ArrayList<Thing>());
  272.             } else if (type.equals("|")) {
  273.                 int index = list.get(list.size() - 1).size() - 1;
  274.                 String word = l.next();
  275.                 int number = 0;
  276.                 String location = "Not Found";
  277.                 if(l.hasNextInt()){
  278.                     number = l.nextInt();
  279.                 }
  280.                 if(l.hasNext()){
  281.                     location = l.next();
  282.                 }
  283.                 list.get(list.size() - 1).get(index).add(new Thing(word, location, number));
  284.             }
  285.         }
  286.     }
  287. }
  288. --------------------------
  289. //thing class
  290. public class Thing
  291. {
  292.     private String name;
  293.     private String location;
  294.     private int inventory;
  295.  
  296.     public Thing(String n, String l, int i)
  297.     {
  298.         name = n;
  299.         location = l;
  300.         inventory = i;
  301.     }
  302.  
  303.     public String getName()
  304.     {
  305.         return name;
  306.     }
  307.  
  308.     public String getLocation()
  309.     {
  310.         return location;
  311.     }
  312.  
  313.     public int getInventory()
  314.     {
  315.         return inventory;
  316.     }
  317.  
  318.     public void changeName(String n)
  319.     {
  320.         name = n;
  321.     }
  322.  
  323.     public void changeLocation(String l)
  324.     {
  325.         location = l;
  326.     }
  327.  
  328.     public void changeInventory(int i)
  329.     {
  330.         inventory = inventory + i;
  331.     }
  332. }
Advertisement
Add Comment
Please, Sign In to add comment