zadanie 1

This commit is contained in:
2023-01-14 16:45:35 +01:00
parent 6659f96257
commit b6a0ca935a
2 changed files with 24 additions and 8 deletions

View File

@@ -60,14 +60,16 @@ public class BookSellerAgent extends Agent
} }
//invoked from GUI, when a new book is added to the catalogue //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() addBehaviour(new OneShotBehaviour()
{ {
public void action() public void action()
{ {
catalogue.put(title, new Integer(price)); final int totalPrice = price + shippingPrice;
System.out.println(getAID().getLocalName() + ": " + title + " put into the catalogue. Price = " + price);
catalogue.put(title, new Integer(totalPrice));
System.out.println(getAID().getLocalName() + ": " + title + " put into the catalogue. Price = " + price + " + shippingPrice: " + shippingPrice + " = totalPrice: " + totalPrice);
} }
} ); } );
} }

View File

@@ -10,7 +10,7 @@ class BookSellerGui extends JFrame
{ {
private BookSellerAgent myAgent; private BookSellerAgent myAgent;
private JTextField titleField, priceField; private JTextField titleField, priceField, shippingPriceField;
BookSellerGui(BookSellerAgent a) BookSellerGui(BookSellerAgent a)
{ {
@@ -19,28 +19,42 @@ class BookSellerGui extends JFrame
myAgent = a; myAgent = a;
JPanel p = new JPanel(); JPanel p = new JPanel();
p.setLayout(new GridLayout(2, 2)); p.setLayout(new GridLayout(3, 2));
p.add(new JLabel("Title:")); p.add(new JLabel("Title:"));
titleField = new JTextField(15); titleField = new JTextField(15);
p.add(titleField); p.add(titleField);
p.add(new JLabel("Price:")); p.add(new JLabel("Price:"));
priceField = new JTextField(15); priceField = new JTextField(15);
p.add(priceField); p.add(priceField);
p.add(new JLabel("Shipping cost:"));
shippingPriceField = new JTextField(15);
p.add(shippingPriceField);
getContentPane().add(p, BorderLayout.CENTER); getContentPane().add(p, BorderLayout.CENTER);
JButton addButton = new JButton("Add"); JButton addButton = new JButton("Add");
addButton.addActionListener( new ActionListener() addButton.addActionListener( new ActionListener()
{ {
public void actionPerformed(ActionEvent ev) { public void actionPerformed(ActionEvent ev)
{
try try
{ {
String title = titleField.getText().trim(); String title = titleField.getText().trim();
String price = priceField.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(""); titleField.setText("");
priceField.setText(""); priceField.setText("");
shippingPriceField.setText("");
} }
catch (Exception e) { catch (Exception e)
{
JOptionPane.showMessageDialog(BookSellerGui.this, "Invalid values. " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(BookSellerGui.this, "Invalid values. " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
} }
} }