zadanie 1
This commit is contained in:
@@ -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);
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user