From b6a0ca935a1da43cc3f7f884d4f67d5208b35569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Wiewi=C3=B3ra?= Date: Sat, 14 Jan 2023 16:45:35 +0100 Subject: [PATCH] zadanie 1 --- src/BookSellerAgent.java | 8 +++++--- src/BookSellerGui.java | 24 +++++++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/BookSellerAgent.java b/src/BookSellerAgent.java index de48955..3265df1 100644 --- a/src/BookSellerAgent.java +++ b/src/BookSellerAgent.java @@ -60,14 +60,16 @@ public class BookSellerAgent extends Agent } //invoked from GUI, when a new book is added to the catalogue - public void updateCatalogue(final String title, final int price) + public void updateCatalogue(final String title, final int price, final int shippingPrice) { addBehaviour(new OneShotBehaviour() { public void action() { - catalogue.put(title, new Integer(price)); - System.out.println(getAID().getLocalName() + ": " + title + " put into the catalogue. Price = " + price); + final int totalPrice = price + shippingPrice; + + catalogue.put(title, new Integer(totalPrice)); + System.out.println(getAID().getLocalName() + ": " + title + " put into the catalogue. Price = " + price + " + shippingPrice: " + shippingPrice + " = totalPrice: " + totalPrice); } } ); } diff --git a/src/BookSellerGui.java b/src/BookSellerGui.java index 319faa7..494fd78 100644 --- a/src/BookSellerGui.java +++ b/src/BookSellerGui.java @@ -10,7 +10,7 @@ class BookSellerGui extends JFrame { private BookSellerAgent myAgent; - private JTextField titleField, priceField; + private JTextField titleField, priceField, shippingPriceField; BookSellerGui(BookSellerAgent a) { @@ -19,28 +19,42 @@ class BookSellerGui extends JFrame myAgent = a; JPanel p = new JPanel(); - p.setLayout(new GridLayout(2, 2)); + p.setLayout(new GridLayout(3, 2)); + p.add(new JLabel("Title:")); titleField = new JTextField(15); p.add(titleField); + p.add(new JLabel("Price:")); priceField = new JTextField(15); p.add(priceField); + + p.add(new JLabel("Shipping cost:")); + shippingPriceField = new JTextField(15); + p.add(shippingPriceField); + + getContentPane().add(p, BorderLayout.CENTER); JButton addButton = new JButton("Add"); addButton.addActionListener( new ActionListener() { - public void actionPerformed(ActionEvent ev) { + public void actionPerformed(ActionEvent ev) + { try { String title = titleField.getText().trim(); String price = priceField.getText().trim(); - myAgent.updateCatalogue(title, Integer.parseInt(price)); + String shippingPrice = shippingPriceField.getText().trim(); + + myAgent.updateCatalogue(title, Integer.parseInt(price), Integer.parseInt(shippingPrice)); + titleField.setText(""); priceField.setText(""); + shippingPriceField.setText(""); } - catch (Exception e) { + catch (Exception e) + { JOptionPane.showMessageDialog(BookSellerGui.this, "Invalid values. " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } }