Repository navigation
Expand file tree
/
Copy pathShoppingScreen.java
More file actions
155 lines (127 loc) · 6.74 KB
/
Copy pathShoppingScreen.java
File metadata and controls
155 lines (127 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class ShoppingScreen extends JFrame {
public ShoppingScreen() {
setTitle("EEB Shopping");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // place frame to center on the screen
String[] columnNames = {"Product Name", "Color", "Category", "Stock", "Weight", "Description"};
// Create model and set non-editable
DefaultTableModel model;
model = new DefaultTableModel(columnNames, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
// Acces file and read products
FileManager<Product> productManager = new FileManager<>("products.txt", new Product("", "", "", 0, 0, ""));
List<Product> products = productManager.loadFromFile();
// Add each product to table
for (Product p : products) {
Object[] rowData = {
p.getProductName(),
p.getProductColor(),
p.getProductCategory(),
p.getStockQuantity(),
p.getProductWeight(),
p.getProductDescription()
};
model.addRow(rowData);
}
// Table ve Scroll Pane ve oluştur
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
// Create buttonPanel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10)); // buttons are centered with flow layout
// Buttonları oluştur
JButton addFavoriteButton = new JButton("Add Favorite");
JButton removeFavoriteButton = new JButton("Remove Favorite");
JButton orderButton = new JButton("Order Button");
// Action Listener for addFavoriteButton
addFavoriteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
Product selectedProduct = new Product(
model.getValueAt(selectedRow, 0).toString(), // name
model.getValueAt(selectedRow, 1).toString(), // color
model.getValueAt(selectedRow, 2).toString(), // category
Integer.parseInt(model.getValueAt(selectedRow, 3).toString()), // stock
Double.parseDouble(model.getValueAt(selectedRow, 4).toString()), // weight
model.getValueAt(selectedRow, 5).toString() // description
);
Session.getCurrentUser().favoriteProduct(selectedProduct);
JOptionPane.showMessageDialog(null, "You Favorited this Product: " + selectedProduct.getProductName());
}
}});
// Action Listener for removeFavoriteButton
removeFavoriteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
Product selectedProduct = new Product(
model.getValueAt(selectedRow, 0).toString(), // name
model.getValueAt(selectedRow, 1).toString(), // color
model.getValueAt(selectedRow, 2).toString(), // category
Integer.parseInt(model.getValueAt(selectedRow, 3).toString()), // stock
Double.parseDouble(model.getValueAt(selectedRow, 4).toString()), // weight
model.getValueAt(selectedRow, 5).toString() // description
);
Session.getCurrentUser().unfavoriteProduct(selectedProduct);
JOptionPane.showMessageDialog(null, "You Unfavorited this Product: " + selectedProduct.getProductName());
}
}
});
// ActionListener for orderButton
orderButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
Product selectedProduct = new Product(
model.getValueAt(selectedRow, 0).toString(), // name
model.getValueAt(selectedRow, 1).toString(), // color
model.getValueAt(selectedRow, 2).toString(), // category
Integer.parseInt(model.getValueAt(selectedRow, 3).toString()), // stock
Double.parseDouble(model.getValueAt(selectedRow, 4).toString()), // weight
model.getValueAt(selectedRow, 5).toString() // description
);
String input = JOptionPane.showInputDialog(null, "Enter Quantity: ");
if (input != null) {
try {
int quantity = Integer.parseInt(input);
Session.getCurrentUser().orderProduct(selectedProduct, quantity);
JOptionPane.showMessageDialog(null, "Succesfully Ordered!");
// Update Stock and Model
int currentStock = Integer.parseInt(model.getValueAt(selectedRow, 3).toString());
int newStock = currentStock - quantity;
model.setValueAt(newStock, selectedRow, 3);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Invalid number entered!");
} catch (IllegalArgumentException ex1) {
JOptionPane.showMessageDialog(null, "Not Enoug Stock!");
}
}
}
}
});
// Add Scroll Pane
add(scrollPane, BorderLayout.CENTER);
// Add buttons to buttonPanel
buttonPanel.add(addFavoriteButton);
buttonPanel.add(removeFavoriteButton);
buttonPanel.add(orderButton);
// Add buttonPanel to frame
add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
}
}