I'm a CS undergrad, and I'm working on a group project for class. We're building an inventory/register system for a store. Essentially, there is a main screen, where the user will see the current transaction and its items, as well as buttons to see/search the store's inventory, or add a new item to the inventory.
Of course, this is a group project at college, so the work has been shoddy and haphazard. It's basically been one guy so far with me helping out, but now I'm attempting to take his work and organize it.
Here's his code for the main screen (the data is just dummy info he wanted to test with):
` public GUI() implements ActionListener {
frame = new JFrame();
inventory = new JButton("Inventory");
additem = new JButton("Add Item");
inventory.setPreferredSize(new Dimension(100,100));
additem.setPreferredSize(new Dimension(100,100));
inventory.setAlignmentX(50);
inventory.setAlignmentY(50);
inventory.addActionListener(this);
additem.addActionListener(this);
String[] column = {"Qty.", "Item", "Price"};
String[][] data = {{"1", "brewski", "1.08"}, {"1", "water", "10.73"}, {"2", "doritos", "6.75"}, {"1", "Guitar", "1000"}, {"2", "hat", "40"}, {"1", "lighter", "1.75"}};
ItemList = new JTable(data, column);
ItemList.setPreferredScrollableViewportSize(new Dimension(1680, 1280));
ItemList.setFillsViewportHeight(false);
jps = new JScrollPane(ItemList);
panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(175, 250, 200,250));
panel.add(inventory);
panel.add(additem);
panel.add(jps);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Presley Register v1.0");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
}
public void actionPerformed(ActionEvent e) { //performs action when inventory button is clicked
if(e.getSource()==inventory) {
JFrame f = new JFrame("Inventory");
try {
f.add(new SearchInJtable());
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
f.setSize(800, 200);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
else if(e.getSource()==additem){
try {
new NewItem();
} catch (IOException ex) {
ex.printStackTrace();
}
}
`
Some things I noticed I wanted to fix:
- The class implements actionListener. I think it would be better practice to use anonymous listeners for each button, though I'm not sure.
- Obviously the data is dummy, but I wasn't sure where this would actually be written. It's the model, so it should be separated from the view, but with how this is written, I'm not sure how to even go about that.
- The two classes created from the actionListener, NewItem and SearchInJtable are custom classes he wrote that contain the functionalities of searching inventory or adding a new item, each in a separate Frame.
- The code just isn't modular at all
I know this is a very vague question, but I wanted to at least reach out and ask. Thanks!