daniel_lawson9999

InventoryMain

May 3rd, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.01 KB | None | 0 0
  1. package com.inventory.daniel;
  2.  
  3. /**
  4.  * Created by danie on 5/3/2017.
  5.  */
  6. import javax.swing.*;
  7. import javax.swing.border.Border;
  8. import java.awt.*;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.MouseAdapter;
  12. import java.awt.event.MouseEvent;
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.io.FileWriter;
  16. import java.util.ArrayList;
  17. import java.util.Scanner;
  18.  
  19.  
  20. public class InventoryMain extends JFrame
  21. {
  22.     private ArrayList<ArrayList<ArrayList<Thing>>> item = new ArrayList<ArrayList<ArrayList<Thing>>>();
  23.     private ArrayList<String> categories = new ArrayList<>();
  24.     private ArrayList<ArrayList<String>> subCategories = new ArrayList<ArrayList<String>>();
  25.     private JList<String> categoryList, subcategoryList, itemList;
  26.     private DefaultListModel<String> categoryModel, subcategoryModel;
  27.     private DefaultListModel<String> itemModel;
  28.     private MouseHandler mh;
  29.     private JLabel image;
  30.     private JButton save;
  31.     private int categoryP = -1;
  32.     private int subP = -1;
  33.     private int itemP = -1;
  34.  
  35.     private JPanel contents; // contents pane
  36.     //Region panels
  37.     private JPanel panelCenter;
  38.     private JPanel panelEast;
  39.     private JPanel panelSouth;
  40.     private JPanel panelWest;
  41.     private JPanel panelSearch,panelWestSearch, panelEastSearch;
  42.     // Borders:
  43.     private Border borderContents = BorderFactory.createEmptyBorder(10,10,10,10);
  44.     Color c = new Color(56, 66, 66);
  45.     private Border borderRegion = BorderFactory.createLineBorder(c,1);
  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 InventoryMain()throws FileNotFoundException {
  57.         super("Inventory");
  58.         setFonts();
  59.  
  60.         File f = new File("Inventory3.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(220,300));
  106.         panelEast.setBackground(Color.lightGray);
  107.         panelEast.add(new JLabel("Items                               "));
  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("Categories"));
  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.         checkInButton = new JButton("Check-in");
  133.         checkInButton.setFont(smallFontLabels);
  134.         checkOutButton = new JButton("Check-out");
  135.         checkOutButton.setFont(smallFontLabels);
  136.  
  137.         save = new JButton("Save");
  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.  
  148.         Box buttonBar = Box.createHorizontalBox();
  149.         buttonBar.add(locationLabel);
  150.         buttonBar.add(Box.createHorizontalStrut(20));
  151.  
  152.         Box otherBar = Box.createHorizontalBox();
  153.         otherBar.add(quantityLabel);
  154.         otherBar.add(Box.createHorizontalStrut(40));
  155.  
  156.  
  157.  
  158.  
  159.         panelEastSearch = new JPanel();
  160.         panelEastSearch.setBorder(subBorderRegion);
  161.         panelEastSearch.setPreferredSize(new Dimension(160,100));
  162.         // panelEastSearch.add(locationLabel);
  163.         panelEastSearch.add(buttonBar);
  164.         //panelEastSearch.add(quantityLabel);
  165.         panelEastSearch.add(otherBar);
  166.         panelEastSearch.add(save);
  167.         panelEastSearch.setBackground(Color.lightGray);
  168.  
  169.  
  170.         panelSearch = new JPanel();
  171.         panelSearch.setPreferredSize(new Dimension(300,100));
  172.         panelSearch.setBackground(Color.lightGray);
  173.         panelSearch.add(panelWestSearch,BorderLayout.WEST);
  174.         panelSearch.add(panelEastSearch, BorderLayout.EAST);
  175.         panelSearch.setBorder(borderRegion);
  176.         contents.add(panelSearch,BorderLayout.NORTH);
  177.  
  178.         ButtonListener bl = new ButtonListener();
  179.         checkInButton.addActionListener(bl);
  180.         checkOutButton.addActionListener(bl);
  181.         save.addActionListener(bl);
  182.  
  183.         setResizable(false);
  184.         pack();
  185.         setVisible (true);
  186.     }
  187.     private void setFonts()
  188.     {
  189.         UIManager.put("Label.font", fontLabels);
  190.     }
  191.     private class ButtonListener implements ActionListener{
  192.         public void actionPerformed(ActionEvent e) {
  193.             if (e.getSource() == save) {
  194.  
  195.                 /*try{
  196.             FileWriter fw=new FileWriter("C:\\Users\\danie\\Documents\\thetest.txt");
  197.             String s = "test\n";
  198.             fw.write("Welcome to javaTpoint.\n" + String.format("%n") );
  199.             fw.flush();
  200.             fw.write("test\n\n"+ s + "tstfas",6,7);
  201.             fw.close();
  202.         }catch(Exception e){System.out.println(e);}*/
  203.                 try {
  204.                     FileWriter fw = new FileWriter("com/inventory/daniel/Inventory3.txt");
  205.                     for (int i = 0; i < item.size(); i++) {
  206.                         fw.write("- " + categories.get(i).toString() + String.format("%n"));
  207.                         System.out.println("- " + categories.get(i).toString() + String.format("%n"));
  208.                         //System.out.println(categories.get(i).toString());
  209.                         for (int j = 0; j < item.get(i).size(); j++) {
  210.                             fw.write("+ " + subCategories.get(i).get(j).toString() + String.format("%n"));
  211.                             for (int k = 0; k < item.get(i).get(j).size(); k++) {
  212.                                 fw.write("| " + item.get(i).get(j).get(k).getName() + " " + item.get(i).get(j).get(k).getLocation() + " " + item.get(i).get(j).get(k).getInventory() + String.format("%n"));
  213.                             }
  214.                         }
  215.                     }
  216.                     fw.close();
  217.                 }
  218.                 catch (Exception ex){
  219.                     System.out.println(ex);
  220.                 }
  221.                 dispose();
  222.             } else {
  223.                 if (itemHighlighted) {
  224.                     int quantity = 0;
  225.                     Scanner s = new Scanner(enterTextField.getText());
  226.                     if (s.hasNextInt()) {
  227.                         int number = s.nextInt();
  228.                         quantity = item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).getInventory();
  229.                         if (e.getSource() == checkInButton) {
  230.                             item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).changeInventory(number);
  231.                             quantityLabel.setText("Quantity: " + (quantity + number));
  232.                         }
  233.                         if (e.getSource() == checkOutButton && quantity > 0) {
  234.                             item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).changeInventory(-number);
  235.                             quantityLabel.setText("Quantity: " + (quantity - number));
  236.                         }
  237.                     } else {
  238.                         System.out.println("error");
  239.                     }
  240.                 }
  241.  
  242.             }
  243.         }
  244.     }
  245.     public static void main (String [] args) throws FileNotFoundException{
  246.         InventoryMain gui = new InventoryMain();
  247.         gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  248.     }
  249.  
  250.     private class MouseHandler  extends MouseAdapter{
  251.         public void mouseClicked(MouseEvent me) {
  252.  
  253.             if (categoryP != categoryList.getSelectedIndex()) {
  254.                 subP = -1;
  255.                 itemHighlighted = false;
  256.                 if(itemP != -1){
  257.                     contents.remove(image);
  258.                     setVisible(true);
  259.                 }
  260.                 subcategoryModel.removeAllElements();
  261.                 itemModel.removeAllElements();
  262.  
  263.                 for (int i = 0; i < subCategories.get(categoryList.getSelectedIndex()).size(); i++) {
  264.                     subcategoryModel.addElement(subCategories.get(categoryList.getSelectedIndex()).get(i));
  265.                 }
  266.  
  267.                 categoryP = categoryList.getSelectedIndex();
  268.             }
  269.  
  270.             else if (subP != subcategoryList.getSelectedIndex()) {
  271.                 System.out.println(subP + " " +subcategoryList.getSelectedIndex());
  272.                 itemHighlighted =false;
  273.                 // System.out.println(itemP);
  274.                 if(itemP != -1){
  275.                     panelSouth.remove(image);
  276.                     setVisible(true);
  277.                 }
  278.                 itemModel.removeAllElements();
  279.  
  280.                 for (int i = 0; i < item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).size(); i++) {
  281.                     itemModel.addElement(item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(i).getName());
  282.                 }
  283.  
  284.                 subP = subcategoryList.getSelectedIndex();
  285.  
  286.             } else if (itemP != itemList.getSelectedIndex()) {
  287.                 if(itemP != -1)panelSouth.remove(image);
  288.                 itemHighlighted = true;
  289.                 quantityLabel.setText("Quantity: " +item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).getInventory());
  290.                 locationLabel.setText("Location: " +item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).getLocation());
  291.                 String name = item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).getName();
  292.                 System.out.println(("images" + name  + ".JPG"));
  293.                 image = new JLabel(new ImageIcon(name + ".JPG"));
  294.                 Dimension test = new Dimension(200,200);
  295.                 image.setMinimumSize(test);
  296.                 image.setPreferredSize(test);
  297.                 image.setMaximumSize(test);
  298.                 //setSpecificSize
  299.                 panelSouth.add(image);
  300.                 setVisible(true);
  301.                 itemP = itemList.getSelectedIndex();
  302.             }
  303.  
  304.         }
  305.     }
  306.  
  307.     private static void readContents(Scanner s, ArrayList<ArrayList<ArrayList<Thing>>> list, ArrayList<ArrayList<String>> subCategories, ArrayList<String> categories) {
  308.         while (s.hasNextLine()) {
  309.             String line = s.nextLine();
  310.             Scanner l = new Scanner(line);
  311.             String type = "";
  312.             if(l.hasNext()) type =l.next();
  313.             if (type.equals("-")) {
  314.                 categories.add(l.next());
  315.                 list.add(new ArrayList<ArrayList<Thing>>());
  316.                 subCategories.add(new ArrayList<String>());
  317.             } else if (type.equals("+")) {
  318.                 subCategories.get(subCategories.size() - 1).add(l.next());
  319.                 list.get(list.size() - 1).add(new ArrayList<Thing>());
  320.             } else if (type.equals("|")) {
  321.                 int index = list.get(list.size() - 1).size() - 1;
  322.                 String word = l.next();
  323.                 int number = 0;
  324.                 String location = "Not Found";
  325.                 if(l.hasNext()){
  326.                     location = l.next();
  327.                 }
  328.                 if(l.hasNextInt()){
  329.                     number = l.nextInt();
  330.                 }
  331.                 list.get(list.size() - 1).get(index).add(new Thing(word, location, number));
  332.             }
  333.         }
  334.     }
  335. }
Advertisement
Add Comment
Please, Sign In to add comment