Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.inventory.daniel;
- /**
- * Created by danie on 5/3/2017.
- */
- import javax.swing.*;
- import javax.swing.border.Border;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class InventoryMain extends JFrame
- {
- private ArrayList<ArrayList<ArrayList<Thing>>> item = new ArrayList<ArrayList<ArrayList<Thing>>>();
- private ArrayList<String> categories = new ArrayList<>();
- private ArrayList<ArrayList<String>> subCategories = new ArrayList<ArrayList<String>>();
- private JList<String> categoryList, subcategoryList, itemList;
- private DefaultListModel<String> categoryModel, subcategoryModel;
- private DefaultListModel<String> itemModel;
- private MouseHandler mh;
- private JLabel image;
- private JButton save;
- private int categoryP = -1;
- private int subP = -1;
- private int itemP = -1;
- private JPanel contents; // contents pane
- //Region panels
- private JPanel panelCenter;
- private JPanel panelEast;
- private JPanel panelSouth;
- private JPanel panelWest;
- private JPanel panelSearch,panelWestSearch, panelEastSearch;
- // Borders:
- private Border borderContents = BorderFactory.createEmptyBorder(10,10,10,10);
- Color c = new Color(56, 66, 66);
- private Border borderRegion = BorderFactory.createLineBorder(c,1);
- private Border subBorderRegion = BorderFactory.createEmptyBorder(0,0,0,0);
- //Colors:
- private Color colorContents = Color.WHITE;
- private JButton checkOutButton,checkInButton;
- private JLabel locationLabel, quantityLabel;
- private JTextField enterTextField;
- //Fonts:
- private Font fontLabels = new Font(Font.DIALOG, Font.BOLD,16);
- private Font smallFontLabels = new Font(Font.DIALOG,Font.BOLD,12);
- private Boolean itemHighlighted = false;
- public InventoryMain()throws FileNotFoundException {
- super("Inventory");
- setFonts();
- File f = new File("Inventory3.txt");
- Scanner s = new Scanner(f);
- readContents(s, item, subCategories, categories);
- mh = new MouseHandler();
- categoryModel = new DefaultListModel<>();
- for (String str : categories) {
- categoryModel.addElement(str);
- }
- categoryList = new JList<>(categoryModel);
- categoryList.setBackground(Color.lightGray);
- subcategoryModel = new DefaultListModel<>();
- subcategoryList = new JList<>(subcategoryModel);
- subcategoryList.setBackground(Color.lightGray);
- itemModel = new DefaultListModel<>();
- itemList = new JList<String>(itemModel);
- itemList.setBackground(Color.lightGray);
- categoryList.addMouseListener(mh);
- subcategoryList.addMouseListener(mh);
- itemList.addMouseListener(mh);
- contents = new JPanel();
- contents.setLayout(new BorderLayout());
- contents.setBorder(borderContents);
- contents.setBackground(colorContents);
- setContentPane(contents);
- panelCenter = new JPanel();
- panelCenter.setPreferredSize(new Dimension(125,300));
- panelCenter.setBorder(borderRegion);
- panelCenter.setBackground(Color.lightGray);
- subcategoryList.setAlignmentX(LEFT_ALIGNMENT);
- panelCenter.add(new JLabel("Sub-Categories"));
- panelCenter.add(subcategoryList);
- contents.add(panelCenter, BorderLayout.CENTER);
- panelEast = new JPanel();
- panelEast.setBorder(borderRegion);
- panelEast.setPreferredSize(new Dimension(220,300));
- panelEast.setBackground(Color.lightGray);
- panelEast.add(new JLabel("Items "));
- panelEast.add(itemList);
- contents.add(panelEast, BorderLayout.EAST);
- panelWest = new JPanel();
- panelWest.setBorder(borderRegion);
- panelWest.setPreferredSize(new Dimension(100,300));
- panelWest.setBackground(Color.lightGray);
- panelWest.add(new JLabel("Categories"));
- panelWest.add(categoryList);
- contents.add(panelWest, BorderLayout.WEST);
- panelSouth = new JPanel();
- panelSouth.setBorder(borderRegion);
- panelSouth.setPreferredSize(new Dimension(300,250));
- panelSouth.setBackground(Color.lightGray);
- contents.add(panelSouth, BorderLayout.SOUTH);
- ///*
- locationLabel = new JLabel("Location: Not Available");
- quantityLabel = new JLabel("Quantity: Not Found");
- locationLabel.setFont(smallFontLabels);
- quantityLabel.setFont(smallFontLabels);
- enterTextField = new JTextField(8);
- checkInButton = new JButton("Check-in");
- checkInButton.setFont(smallFontLabels);
- checkOutButton = new JButton("Check-out");
- checkOutButton.setFont(smallFontLabels);
- save = new JButton("Save");
- panelWestSearch = new JPanel();
- panelWestSearch.setBorder(subBorderRegion);
- panelWestSearch.setPreferredSize(new Dimension(150,100));
- panelWestSearch.setBackground(Color.lightGray);
- panelWestSearch.add(enterTextField);
- panelWestSearch.add(checkInButton);
- panelWestSearch.add(checkOutButton);
- Box buttonBar = Box.createHorizontalBox();
- buttonBar.add(locationLabel);
- buttonBar.add(Box.createHorizontalStrut(20));
- Box otherBar = Box.createHorizontalBox();
- otherBar.add(quantityLabel);
- otherBar.add(Box.createHorizontalStrut(40));
- panelEastSearch = new JPanel();
- panelEastSearch.setBorder(subBorderRegion);
- panelEastSearch.setPreferredSize(new Dimension(160,100));
- // panelEastSearch.add(locationLabel);
- panelEastSearch.add(buttonBar);
- //panelEastSearch.add(quantityLabel);
- panelEastSearch.add(otherBar);
- panelEastSearch.add(save);
- panelEastSearch.setBackground(Color.lightGray);
- panelSearch = new JPanel();
- panelSearch.setPreferredSize(new Dimension(300,100));
- panelSearch.setBackground(Color.lightGray);
- panelSearch.add(panelWestSearch,BorderLayout.WEST);
- panelSearch.add(panelEastSearch, BorderLayout.EAST);
- panelSearch.setBorder(borderRegion);
- contents.add(panelSearch,BorderLayout.NORTH);
- ButtonListener bl = new ButtonListener();
- checkInButton.addActionListener(bl);
- checkOutButton.addActionListener(bl);
- save.addActionListener(bl);
- setResizable(false);
- pack();
- setVisible (true);
- }
- private void setFonts()
- {
- UIManager.put("Label.font", fontLabels);
- }
- private class ButtonListener implements ActionListener{
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == save) {
- /*try{
- FileWriter fw=new FileWriter("C:\\Users\\danie\\Documents\\thetest.txt");
- String s = "test\n";
- fw.write("Welcome to javaTpoint.\n" + String.format("%n") );
- fw.flush();
- fw.write("test\n\n"+ s + "tstfas",6,7);
- fw.close();
- }catch(Exception e){System.out.println(e);}*/
- try {
- FileWriter fw = new FileWriter("com/inventory/daniel/Inventory3.txt");
- for (int i = 0; i < item.size(); i++) {
- fw.write("- " + categories.get(i).toString() + String.format("%n"));
- System.out.println("- " + categories.get(i).toString() + String.format("%n"));
- //System.out.println(categories.get(i).toString());
- for (int j = 0; j < item.get(i).size(); j++) {
- fw.write("+ " + subCategories.get(i).get(j).toString() + String.format("%n"));
- for (int k = 0; k < item.get(i).get(j).size(); k++) {
- 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"));
- }
- }
- }
- fw.close();
- }
- catch (Exception ex){
- System.out.println(ex);
- }
- dispose();
- } else {
- if (itemHighlighted) {
- int quantity = 0;
- Scanner s = new Scanner(enterTextField.getText());
- if (s.hasNextInt()) {
- int number = s.nextInt();
- quantity = item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).getInventory();
- if (e.getSource() == checkInButton) {
- item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).changeInventory(number);
- quantityLabel.setText("Quantity: " + (quantity + number));
- }
- if (e.getSource() == checkOutButton && quantity > 0) {
- item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).changeInventory(-number);
- quantityLabel.setText("Quantity: " + (quantity - number));
- }
- } else {
- System.out.println("error");
- }
- }
- }
- }
- }
- public static void main (String [] args) throws FileNotFoundException{
- InventoryMain gui = new InventoryMain();
- gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- private class MouseHandler extends MouseAdapter{
- public void mouseClicked(MouseEvent me) {
- if (categoryP != categoryList.getSelectedIndex()) {
- subP = -1;
- itemHighlighted = false;
- if(itemP != -1){
- contents.remove(image);
- setVisible(true);
- }
- subcategoryModel.removeAllElements();
- itemModel.removeAllElements();
- for (int i = 0; i < subCategories.get(categoryList.getSelectedIndex()).size(); i++) {
- subcategoryModel.addElement(subCategories.get(categoryList.getSelectedIndex()).get(i));
- }
- categoryP = categoryList.getSelectedIndex();
- }
- else if (subP != subcategoryList.getSelectedIndex()) {
- System.out.println(subP + " " +subcategoryList.getSelectedIndex());
- itemHighlighted =false;
- // System.out.println(itemP);
- if(itemP != -1){
- panelSouth.remove(image);
- setVisible(true);
- }
- itemModel.removeAllElements();
- for (int i = 0; i < item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).size(); i++) {
- itemModel.addElement(item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(i).getName());
- }
- subP = subcategoryList.getSelectedIndex();
- } else if (itemP != itemList.getSelectedIndex()) {
- if(itemP != -1)panelSouth.remove(image);
- itemHighlighted = true;
- quantityLabel.setText("Quantity: " +item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).getInventory());
- locationLabel.setText("Location: " +item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).getLocation());
- String name = item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).getName();
- System.out.println(("images" + name + ".JPG"));
- image = new JLabel(new ImageIcon(name + ".JPG"));
- Dimension test = new Dimension(200,200);
- image.setMinimumSize(test);
- image.setPreferredSize(test);
- image.setMaximumSize(test);
- //setSpecificSize
- panelSouth.add(image);
- setVisible(true);
- itemP = itemList.getSelectedIndex();
- }
- }
- }
- private static void readContents(Scanner s, ArrayList<ArrayList<ArrayList<Thing>>> list, ArrayList<ArrayList<String>> subCategories, ArrayList<String> categories) {
- while (s.hasNextLine()) {
- String line = s.nextLine();
- Scanner l = new Scanner(line);
- String type = "";
- if(l.hasNext()) type =l.next();
- if (type.equals("-")) {
- categories.add(l.next());
- list.add(new ArrayList<ArrayList<Thing>>());
- subCategories.add(new ArrayList<String>());
- } else if (type.equals("+")) {
- subCategories.get(subCategories.size() - 1).add(l.next());
- list.get(list.size() - 1).add(new ArrayList<Thing>());
- } else if (type.equals("|")) {
- int index = list.get(list.size() - 1).size() - 1;
- String word = l.next();
- int number = 0;
- String location = "Not Found";
- if(l.hasNext()){
- location = l.next();
- }
- if(l.hasNextInt()){
- number = l.nextInt();
- }
- list.get(list.size() - 1).get(index).add(new Thing(word, location, number));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment