Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Inventory5
- /**
- * Write a description of class BorderLayoutManager here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- import javax.swing.border.*;
- import java.awt.Font;
- import javax.swing.*;
- import javax.swing.border.*;
- import javax.swing.event.ListSelectionEvent;
- import javax.swing.event.ListSelectionListener;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.ArrayList;
- import java.util.*;
- import java.io.*;
- public class Inventory5 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 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);
- private Border borderRegion = BorderFactory.createLineBorder(Color.GRAY,2);
- 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 Inventory5 ()throws FileNotFoundException {
- super("Inventory");
- setFonts();
- File f = new File("C:\\Users\\danie\\Documents\\inventory.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(100,300));
- panelEast.setBackground(Color.lightGray);
- panelEast.add(new JLabel("Categories"));
- 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("Items"));
- 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);
- 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);
- panelEastSearch = new JPanel();
- panelEastSearch.setBorder(subBorderRegion);
- panelEastSearch.setPreferredSize(new Dimension(160,100));
- panelEastSearch.add(locationLabel);
- panelEastSearch.add(new Box(0));
- panelEastSearch.add(quantityLabel);
- 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);
- //*/
- setResizable(false);
- pack();
- setVisible (true);
- }
- private void setFonts()
- {
- UIManager.put("Label.font", fontLabels);
- }
- private class ButtonListener implements ActionListener{
- public void actionPerformed(ActionEvent e){
- int quantity = 0;
- if(itemHighlighted) {
- 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{
- Inventory5 gui = new Inventory5();
- gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- private class MouseHandler extends MouseAdapter{
- public void mouseClicked(MouseEvent me) {
- if (categoryP != categoryList.getSelectedIndex()) {
- 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()) {
- 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());
- String name = item.get(categoryList.getSelectedIndex()).get(subcategoryList.getSelectedIndex()).get(itemList.getSelectedIndex()).getName();
- System.out.println(("C:\\Users\\danie\\Documents\\inventoryPhotos\\" + name + ".PNG"));
- image = new JLabel(new ImageIcon("C:\\Users\\danie\\Documents\\inventoryPhotos\\" + name + ".PNG"));
- 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.hasNextInt()){
- number = l.nextInt();
- }
- if(l.hasNext()){
- location = l.next();
- }
- list.get(list.size() - 1).get(index).add(new Thing(word, location, number));
- }
- }
- }
- }
- --------------------------
- //thing class
- public class Thing
- {
- private String name;
- private String location;
- private int inventory;
- public Thing(String n, String l, int i)
- {
- name = n;
- location = l;
- inventory = i;
- }
- public String getName()
- {
- return name;
- }
- public String getLocation()
- {
- return location;
- }
- public int getInventory()
- {
- return inventory;
- }
- public void changeName(String n)
- {
- name = n;
- }
- public void changeLocation(String l)
- {
- location = l;
- }
- public void changeInventory(int i)
- {
- inventory = inventory + i;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment