Skip to content

Instantly share code, notes, and snippets.

@AnnaBoro
Created January 28, 2016 21:42
Show Gist options
  • Save AnnaBoro/b5f7fbe746d17444b1c9 to your computer and use it in GitHub Desktop.
Save AnnaBoro/b5f7fbe746d17444b1c9 to your computer and use it in GitHub Desktop.
+ menuBar
package lesson6_9.adapter.mvc.shop;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
public class StoreUI1 implements ActionListener{
private Store store;
private Report1 report;
private ButtonGroup group = new ButtonGroup();
private JPanel tablePanel;
private JPanel sellingPanel;
private JFrame frame;
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem menuItem;
public StoreUI1(Store store) {
this.store = store;
report = new Report1(store);
frame = new JFrame("STORE");
frame.setMinimumSize(new Dimension(800, 600));
frame.setLocation(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuBar = new JMenuBar();
menu = new JMenu("File");
menuItem = new JMenuItem("Buy");
menuItem.addActionListener(this);
menu.add(menuItem);
menuBar.add(menu);
menuBar.setBackground(Color.MAGENTA);
frame.setJMenuBar(menuBar);
frame.getContentPane().add(new PurchaseTable().createTablePanel());
tablePanel.setVisible(true);
sellingPanel = createSellingPanel();
frame.pack();
frame.setVisible(true);
}
private JPanel createSellingPanel() {
sellingPanel = new JPanel();
sellingPanel.setBackground(Color.CYAN);
GridBagLayout gbl = new GridBagLayout();
sellingPanel.setLayout(gbl);
GridBagConstraints c = new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
JLabel jName = new JLabel("Enter name: ");
final JTextField jTField = new JTextField(30);
gbl.setConstraints(jName, c);
c = new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
gbl.setConstraints(jTField, c);
sellingPanel.add(jName);
sellingPanel.add(jTField);
final List<Bird> birds = store.getBirds();
JLabel jBirds = new JLabel("Birds");
JPanel jPBirds = new JPanel();
jPBirds.setBorder(BorderFactory.createLineBorder(Color.BLUE));
jPBirds.setLayout(new GridLayout(birds.size(), 0));
for (Bird b : birds) {
JRadioButton birdButton = new JRadioButton(b.getName() + " " + b.getPrice());
birdButton.setActionCommand(b.getName());
jPBirds.add(birdButton);
group.add(birdButton);
}
c = new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
gbl.setConstraints(jBirds, c);
c = new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(30, 0, 30, 0), 0, 0);
gbl.setConstraints(jPBirds, c);
sellingPanel.add(jBirds);
sellingPanel.add(jPBirds);
JLabel jNumber = new JLabel("Enter quantity: ");
NumberFormat nf = NumberFormat.getInstance();
final JFormattedTextField jTField2 = new JFormattedTextField(nf);
jTField2.setValue(1);
c = new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
gbl.setConstraints(jNumber, c);
c = new GridBagConstraints(1, 2, 1, 1, 0, 0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
gbl.setConstraints(jTField2, c);
sellingPanel.add(jNumber);
sellingPanel.add(jTField2);
final JButton buyButton = new JButton("BUY");
c = new GridBagConstraints(1, 3, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(30, 0, 0, 0), 0, 0);
gbl.setConstraints(buyButton, c);
buyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Buyer b = new Buyer();
if (!jTField.getText().isEmpty()) {
b.setName(jTField.getText());
if (group.getSelection() != null) {
String birdSTR = group.getSelection().getActionCommand();
int num = Integer.parseInt(jTField2.getText());
store.sell(birdSTR, num, b);
}
else System.out.println("Choose a bird, please");
}
else System.out.println("Enter your name, please");
}
});
sellingPanel.add(buyButton);
sellingPanel.setVisible(false);
return sellingPanel;
}
@Override
public void actionPerformed(ActionEvent e) {
if (tablePanel.isVisible()) {
tablePanel.setVisible(false);
frame.getContentPane().add(sellingPanel);
frame.getContentPane().remove(tablePanel);
sellingPanel.setVisible(true);
menuItem.setText("Purchases");
}
else if (sellingPanel.isVisible()) {
sellingPanel.setVisible(false);
frame.getContentPane().remove(sellingPanel);
frame.getContentPane().add(new PurchaseTable().createTablePanel());
tablePanel.setVisible(true);
menuItem.setText("Buy");
}
// frame.revalidate();
frame.repaint();
}
private class PurchaseTable {
Object[][] purchaseView;
List<Purchase> purchases;
JTable myTable;
MyTableModel myTableModel;
String[] columns;
SimpleDateFormat c = new SimpleDateFormat("d MMM yyyy HH:mm:ss", Locale.ENGLISH);
public PurchaseTable() {
}
public JPanel createTablePanel() {
tablePanel = new JPanel();
myTableModel = new MyTableModel();
columns = new String[]{"ID", "Date", "Bird", "Number", "Buyer"};
myTableModel.setColumnsName(columns);
purchases = report.getListTodayPurchases();
purchaseView = new Object[purchases.size()][];
for (int i = 0; i < purchases.size(); i++) {
purchaseView[i] = new Object[]{i + 1, c.format(purchases.get(i).getDate()), purchases.get(i).getBird().getName(),
purchases.get(i).getNumber(), purchases.get(i).getBuyer().getName()};
}
myTableModel.setPurchases(purchases);
myTableModel.setPurchaseView(purchaseView);
myTable = new JTable(myTableModel);
JScrollPane scrollPane = new JScrollPane(myTable);
myTable.setPreferredScrollableViewportSize(new Dimension(250, 100));
tablePanel.add(scrollPane, BorderLayout.CENTER);
myTable.setVisible(true);
tablePanel.setVisible(true);
return tablePanel;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment