Reformatted code
This commit is contained in:
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
*
|
||||
!build.xml
|
||||
!jade
|
||||
!src
|
||||
!.gitignore
|
||||
@@ -10,57 +10,59 @@ import jade.domain.FIPAException;
|
||||
import jade.domain.FIPAAgentManagement.DFAgentDescription;
|
||||
import jade.domain.FIPAAgentManagement.ServiceDescription;
|
||||
|
||||
public class BookBuyerAgent extends Agent {
|
||||
private BookBuyerGui myGui;
|
||||
private String targetBookTitle;
|
||||
|
||||
//list of found sellers
|
||||
private AID[] sellerAgents;
|
||||
|
||||
protected void setup() {
|
||||
targetBookTitle = "";
|
||||
System.out.println("Hello! " + getAID().getLocalName() + " is ready for the purchase order.");
|
||||
myGui = new BookBuyerGui(this);
|
||||
myGui.display();
|
||||
public class BookBuyerAgent extends Agent
|
||||
{
|
||||
private BookBuyerGui myGui;
|
||||
private String targetBookTitle;
|
||||
|
||||
//list of found sellers
|
||||
private AID[] sellerAgents;
|
||||
|
||||
protected void setup()
|
||||
{
|
||||
targetBookTitle = "";
|
||||
System.out.println("Hello! " + getAID().getLocalName() + " is ready for the purchase order.");
|
||||
myGui = new BookBuyerGui(this);
|
||||
myGui.display();
|
||||
//time interval for buyer for sending subsequent CFP
|
||||
//as a CLI argument
|
||||
int interval = 20000;
|
||||
Object[] args = getArguments();
|
||||
if (args != null && args.length > 0) interval = Integer.parseInt(args[0].toString());
|
||||
addBehaviour(new TickerBehaviour(this, interval)
|
||||
{
|
||||
protected void onTick()
|
||||
{
|
||||
//search only if the purchase task was ordered
|
||||
if (!targetBookTitle.equals(""))
|
||||
{
|
||||
System.out.println(getAID().getLocalName() + ": I'm looking for " + targetBookTitle);
|
||||
//update a list of known sellers (DF)
|
||||
DFAgentDescription template = new DFAgentDescription();
|
||||
ServiceDescription sd = new ServiceDescription();
|
||||
sd.setType("book-selling");
|
||||
template.addServices(sd);
|
||||
try
|
||||
{
|
||||
DFAgentDescription[] result = DFService.search(myAgent, template);
|
||||
System.out.println(getAID().getLocalName() + ": the following sellers have been found");
|
||||
sellerAgents = new AID[result.length];
|
||||
for (int i = 0; i < result.length; ++i)
|
||||
{
|
||||
sellerAgents[i] = result[i].getName();
|
||||
System.out.println(sellerAgents[i].getLocalName());
|
||||
}
|
||||
}
|
||||
catch (FIPAException fe)
|
||||
{
|
||||
fe.printStackTrace();
|
||||
}
|
||||
addBehaviour(new TickerBehaviour(this, interval)
|
||||
{
|
||||
protected void onTick()
|
||||
{
|
||||
//search only if the purchase task was ordered
|
||||
if (!targetBookTitle.equals(""))
|
||||
{
|
||||
System.out.println(getAID().getLocalName() + ": I'm looking for " + targetBookTitle);
|
||||
//update a list of known sellers (DF)
|
||||
DFAgentDescription template = new DFAgentDescription();
|
||||
ServiceDescription sd = new ServiceDescription();
|
||||
sd.setType("book-selling");
|
||||
template.addServices(sd);
|
||||
try
|
||||
{
|
||||
DFAgentDescription[] result = DFService.search(myAgent, template);
|
||||
System.out.println(getAID().getLocalName() + ": the following sellers have been found");
|
||||
sellerAgents = new AID[result.length];
|
||||
for (int i = 0; i < result.length; ++i)
|
||||
{
|
||||
sellerAgents[i] = result[i].getName();
|
||||
System.out.println(sellerAgents[i].getLocalName());
|
||||
}
|
||||
}
|
||||
catch (FIPAException fe)
|
||||
{
|
||||
fe.printStackTrace();
|
||||
}
|
||||
|
||||
myAgent.addBehaviour(new RequestPerformer());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
myAgent.addBehaviour(new RequestPerformer());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//invoked from GUI, when purchase was ordered
|
||||
public void lookForTitle(final String title)
|
||||
@@ -75,99 +77,111 @@ public class BookBuyerAgent extends Agent {
|
||||
});
|
||||
}
|
||||
|
||||
protected void takeDown() {
|
||||
protected void takeDown()
|
||||
{
|
||||
myGui.dispose();
|
||||
System.out.println("Buyer agent " + getAID().getLocalName() + " terminated.");
|
||||
}
|
||||
|
||||
private class RequestPerformer extends Behaviour {
|
||||
private AID bestSeller;
|
||||
private int bestPrice;
|
||||
private int repliesCnt = 0;
|
||||
private MessageTemplate mt;
|
||||
private int step = 0;
|
||||
|
||||
public void action() {
|
||||
switch (step) {
|
||||
case 0:
|
||||
//call for proposal (CFP) to found sellers
|
||||
ACLMessage cfp = new ACLMessage(ACLMessage.CFP);
|
||||
for (int i = 0; i < sellerAgents.length; ++i) {
|
||||
cfp.addReceiver(sellerAgents[i]);
|
||||
}
|
||||
cfp.setContent(targetBookTitle);
|
||||
cfp.setConversationId("book-trade");
|
||||
cfp.setReplyWith("cfp"+System.currentTimeMillis()); //unique value
|
||||
myAgent.send(cfp);
|
||||
mt = MessageTemplate.and(MessageTemplate.MatchConversationId("book-trade"),
|
||||
MessageTemplate.MatchInReplyTo(cfp.getReplyWith()));
|
||||
step = 1;
|
||||
break;
|
||||
case 1:
|
||||
//collect proposals
|
||||
ACLMessage reply = myAgent.receive(mt);
|
||||
if (reply != null) {
|
||||
if (reply.getPerformative() == ACLMessage.PROPOSE) {
|
||||
//proposal received
|
||||
int price = Integer.parseInt(reply.getContent());
|
||||
if (bestSeller == null || price < bestPrice) {
|
||||
//the best proposal as for now
|
||||
bestPrice = price;
|
||||
bestSeller = reply.getSender();
|
||||
}
|
||||
}
|
||||
repliesCnt++;
|
||||
if (repliesCnt >= sellerAgents.length) {
|
||||
//all proposals have been received
|
||||
step = 2;
|
||||
}
|
||||
}
|
||||
else {
|
||||
block();
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
//best proposal consumption - purchase
|
||||
ACLMessage order = new ACLMessage(ACLMessage.ACCEPT_PROPOSAL);
|
||||
order.addReceiver(bestSeller);
|
||||
order.setContent(targetBookTitle);
|
||||
order.setConversationId("book-trade");
|
||||
order.setReplyWith("order"+System.currentTimeMillis());
|
||||
myAgent.send(order);
|
||||
mt = MessageTemplate.and(MessageTemplate.MatchConversationId("book-trade"),
|
||||
MessageTemplate.MatchInReplyTo(order.getReplyWith()));
|
||||
step = 3;
|
||||
break;
|
||||
case 3:
|
||||
//seller confirms the transaction
|
||||
reply = myAgent.receive(mt);
|
||||
if (reply != null) {
|
||||
if (reply.getPerformative() == ACLMessage.INFORM) {
|
||||
//purchase succeeded
|
||||
System.out.println(getAID().getLocalName() + ": " + targetBookTitle + " purchased for " + bestPrice + " from " + reply.getSender().getLocalName());
|
||||
System.out.println(getAID().getLocalName() + ": waiting for the next purchase order.");
|
||||
targetBookTitle = "";
|
||||
//myAgent.doDelete();
|
||||
}
|
||||
else {
|
||||
System.out.println(getAID().getLocalName() + ": purchase has failed. " + targetBookTitle + " was sold in the meantime.");
|
||||
}
|
||||
step = 4; //this state ends the purchase process
|
||||
}
|
||||
else {
|
||||
block();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean done() {
|
||||
if (step == 2 && bestSeller == null) {
|
||||
System.out.println(getAID().getLocalName() + ": " + targetBookTitle + " is not on sale.");
|
||||
}
|
||||
//process terminates here if purchase has failed (title not on sale) or book was successfully bought
|
||||
return ((step == 2 && bestSeller == null) || step == 4);
|
||||
}
|
||||
|
||||
private class RequestPerformer extends Behaviour
|
||||
{
|
||||
private AID bestSeller;
|
||||
private int bestPrice;
|
||||
private int repliesCnt = 0;
|
||||
private MessageTemplate mt;
|
||||
private int step = 0;
|
||||
|
||||
public void action()
|
||||
{
|
||||
switch (step)
|
||||
{
|
||||
case 0:
|
||||
//call for proposal (CFP) to found sellers
|
||||
ACLMessage cfp = new ACLMessage(ACLMessage.CFP);
|
||||
for (int i = 0; i < sellerAgents.length; ++i)
|
||||
{
|
||||
cfp.addReceiver(sellerAgents[i]);
|
||||
}
|
||||
cfp.setContent(targetBookTitle);
|
||||
cfp.setConversationId("book-trade");
|
||||
cfp.setReplyWith("cfp"+System.currentTimeMillis()); //unique value
|
||||
myAgent.send(cfp);
|
||||
mt = MessageTemplate.and(MessageTemplate.MatchConversationId("book-trade"),
|
||||
MessageTemplate.MatchInReplyTo(cfp.getReplyWith()));
|
||||
step = 1;
|
||||
break;
|
||||
case 1:
|
||||
//collect proposals
|
||||
ACLMessage reply = myAgent.receive(mt);
|
||||
if (reply != null)
|
||||
{
|
||||
if (reply.getPerformative() == ACLMessage.PROPOSE)
|
||||
{
|
||||
//proposal received
|
||||
int price = Integer.parseInt(reply.getContent());
|
||||
if (bestSeller == null || price < bestPrice)
|
||||
{
|
||||
//the best proposal as for now
|
||||
bestPrice = price;
|
||||
bestSeller = reply.getSender();
|
||||
}
|
||||
}
|
||||
repliesCnt++;
|
||||
if (repliesCnt >= sellerAgents.length)
|
||||
{
|
||||
//all proposals have been received
|
||||
step = 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
block();
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
//best proposal consumption - purchase
|
||||
ACLMessage order = new ACLMessage(ACLMessage.ACCEPT_PROPOSAL);
|
||||
order.addReceiver(bestSeller);
|
||||
order.setContent(targetBookTitle);
|
||||
order.setConversationId("book-trade");
|
||||
order.setReplyWith("order"+System.currentTimeMillis());
|
||||
myAgent.send(order);
|
||||
mt = MessageTemplate.and(MessageTemplate.MatchConversationId("book-trade"),
|
||||
MessageTemplate.MatchInReplyTo(order.getReplyWith()));
|
||||
step = 3;
|
||||
break;
|
||||
case 3:
|
||||
//seller confirms the transaction
|
||||
reply = myAgent.receive(mt);
|
||||
if (reply != null) {
|
||||
if (reply.getPerformative() == ACLMessage.INFORM) {
|
||||
//purchase succeeded
|
||||
System.out.println(getAID().getLocalName() + ": " + targetBookTitle + " purchased for " + bestPrice + " from " + reply.getSender().getLocalName());
|
||||
System.out.println(getAID().getLocalName() + ": waiting for the next purchase order.");
|
||||
targetBookTitle = "";
|
||||
//myAgent.doDelete();
|
||||
}
|
||||
else {
|
||||
System.out.println(getAID().getLocalName() + ": purchase has failed. " + targetBookTitle + " was sold in the meantime.");
|
||||
}
|
||||
step = 4; //this state ends the purchase process
|
||||
}
|
||||
else {
|
||||
block();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean done()
|
||||
{
|
||||
if (step == 2 && bestSeller == null)
|
||||
{
|
||||
System.out.println(getAID().getLocalName() + ": " + targetBookTitle + " is not on sale.");
|
||||
}
|
||||
//process terminates here if purchase has failed (title not on sale) or book was successfully bought
|
||||
return ((step == 2 && bestSeller == null) || step == 4);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,12 +6,14 @@ import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
|
||||
class BookBuyerGui extends JFrame {
|
||||
class BookBuyerGui extends JFrame
|
||||
{
|
||||
private BookBuyerAgent myAgent;
|
||||
|
||||
private JTextField titleField;
|
||||
|
||||
BookBuyerGui(BookBuyerAgent a) {
|
||||
BookBuyerGui(BookBuyerAgent a)
|
||||
{
|
||||
super(a.getLocalName());
|
||||
|
||||
myAgent = a;
|
||||
@@ -24,14 +26,18 @@ class BookBuyerGui extends JFrame {
|
||||
getContentPane().add(p, BorderLayout.CENTER);
|
||||
|
||||
JButton addButton = new JButton("Search");
|
||||
addButton.addActionListener( new ActionListener() {
|
||||
public void actionPerformed(ActionEvent ev) {
|
||||
try {
|
||||
addButton.addActionListener( new ActionListener()
|
||||
{
|
||||
public void actionPerformed(ActionEvent ev)
|
||||
{
|
||||
try
|
||||
{
|
||||
String title = titleField.getText().trim();
|
||||
myAgent.lookForTitle(title);
|
||||
titleField.setText("");
|
||||
}
|
||||
catch (Exception e) {
|
||||
catch (Exception e)
|
||||
{
|
||||
JOptionPane.showMessageDialog(BookBuyerGui.this, "Invalid values. " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
@@ -40,16 +46,21 @@ class BookBuyerGui extends JFrame {
|
||||
p.add(addButton);
|
||||
getContentPane().add(p, BorderLayout.SOUTH);
|
||||
|
||||
addWindowListener(new WindowAdapter() {
|
||||
public void windowClosing(WindowEvent e) {
|
||||
myAgent.doDelete();
|
||||
addWindowListener(
|
||||
new WindowAdapter()
|
||||
{
|
||||
public void windowClosing(WindowEvent e)
|
||||
{
|
||||
myAgent.doDelete();
|
||||
}
|
||||
}
|
||||
} );
|
||||
);
|
||||
|
||||
setResizable(false);
|
||||
}
|
||||
|
||||
public void display() {
|
||||
public void display()
|
||||
{
|
||||
pack();
|
||||
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
int centerX = (int)screenSize.getWidth() / 2;
|
||||
|
||||
@@ -11,108 +11,131 @@ import jade.domain.FIPAAgentManagement.ServiceDescription;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class BookSellerAgent extends Agent {
|
||||
private Hashtable catalogue;
|
||||
private BookSellerGui myGui;
|
||||
public class BookSellerAgent extends Agent
|
||||
{
|
||||
private Hashtable catalogue;
|
||||
private BookSellerGui myGui;
|
||||
|
||||
protected void setup() {
|
||||
catalogue = new Hashtable();
|
||||
myGui = new BookSellerGui(this);
|
||||
myGui.display();
|
||||
protected void setup()
|
||||
{
|
||||
catalogue = new Hashtable();
|
||||
myGui = new BookSellerGui(this);
|
||||
myGui.display();
|
||||
|
||||
//book selling service registration at DF
|
||||
DFAgentDescription dfd = new DFAgentDescription();
|
||||
dfd.setName(getAID());
|
||||
ServiceDescription sd = new ServiceDescription();
|
||||
sd.setType("book-selling");
|
||||
sd.setName("JADE-book-trading");
|
||||
dfd.addServices(sd);
|
||||
try {
|
||||
DFService.register(this, dfd);
|
||||
}
|
||||
catch (FIPAException fe) {
|
||||
fe.printStackTrace();
|
||||
}
|
||||
|
||||
addBehaviour(new OfferRequestsServer());
|
||||
//book selling service registration at DF
|
||||
DFAgentDescription dfd = new DFAgentDescription();
|
||||
dfd.setName(getAID());
|
||||
ServiceDescription sd = new ServiceDescription();
|
||||
sd.setType("book-selling");
|
||||
sd.setName("JADE-book-trading");
|
||||
dfd.addServices(sd);
|
||||
try
|
||||
{
|
||||
DFService.register(this, dfd);
|
||||
}
|
||||
catch (FIPAException fe)
|
||||
{
|
||||
fe.printStackTrace();
|
||||
}
|
||||
|
||||
addBehaviour(new PurchaseOrdersServer());
|
||||
}
|
||||
addBehaviour(new OfferRequestsServer());
|
||||
|
||||
protected void takeDown() {
|
||||
//book selling service deregistration at DF
|
||||
try {
|
||||
DFService.deregister(this);
|
||||
}
|
||||
catch (FIPAException fe) {
|
||||
fe.printStackTrace();
|
||||
}
|
||||
myGui.dispose();
|
||||
System.out.println("Seller agent " + getAID().getName() + " terminated.");
|
||||
}
|
||||
addBehaviour(new PurchaseOrdersServer());
|
||||
}
|
||||
|
||||
//invoked from GUI, when a new book is added to the catalogue
|
||||
public void updateCatalogue(final String title, final int price) {
|
||||
addBehaviour(new OneShotBehaviour() {
|
||||
public void action() {
|
||||
catalogue.put(title, new Integer(price));
|
||||
System.out.println(getAID().getLocalName() + ": " + title + " put into the catalogue. Price = " + price);
|
||||
}
|
||||
} );
|
||||
}
|
||||
protected void takeDown()
|
||||
{
|
||||
//book selling service deregistration at DF
|
||||
try
|
||||
{
|
||||
DFService.deregister(this);
|
||||
}
|
||||
catch (FIPAException fe)
|
||||
{
|
||||
fe.printStackTrace();
|
||||
}
|
||||
|
||||
myGui.dispose();
|
||||
System.out.println("Seller agent " + getAID().getName() + " terminated.");
|
||||
}
|
||||
|
||||
//invoked from GUI, when a new book is added to the catalogue
|
||||
public void updateCatalogue(final String title, final int price)
|
||||
{
|
||||
addBehaviour(new OneShotBehaviour()
|
||||
{
|
||||
public void action()
|
||||
{
|
||||
catalogue.put(title, new Integer(price));
|
||||
System.out.println(getAID().getLocalName() + ": " + title + " put into the catalogue. Price = " + price);
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
private class OfferRequestsServer extends CyclicBehaviour {
|
||||
public void action() {
|
||||
//proposals only template
|
||||
MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.CFP);
|
||||
ACLMessage msg = myAgent.receive(mt);
|
||||
if (msg != null) {
|
||||
String title = msg.getContent();
|
||||
ACLMessage reply = msg.createReply();
|
||||
Integer price = (Integer) catalogue.get(title);
|
||||
if (price != null) {
|
||||
//title found in the catalogue, respond with its price as a proposal
|
||||
reply.setPerformative(ACLMessage.PROPOSE);
|
||||
reply.setContent(String.valueOf(price.intValue()));
|
||||
}
|
||||
else {
|
||||
//title not found in the catalogue
|
||||
reply.setPerformative(ACLMessage.REFUSE);
|
||||
reply.setContent("not-available");
|
||||
}
|
||||
myAgent.send(reply);
|
||||
}
|
||||
else {
|
||||
block();
|
||||
}
|
||||
}
|
||||
private class OfferRequestsServer extends CyclicBehaviour
|
||||
{
|
||||
public void action()
|
||||
{
|
||||
//proposals only template
|
||||
MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.CFP);
|
||||
ACLMessage msg = myAgent.receive(mt);
|
||||
if (msg != null)
|
||||
{
|
||||
String title = msg.getContent();
|
||||
ACLMessage reply = msg.createReply();
|
||||
Integer price = (Integer) catalogue.get(title);
|
||||
if (price != null)
|
||||
{
|
||||
//title found in the catalogue, respond with its price as a proposal
|
||||
reply.setPerformative(ACLMessage.PROPOSE);
|
||||
reply.setContent(String.valueOf(price.intValue()));
|
||||
}
|
||||
else
|
||||
{
|
||||
//title not found in the catalogue
|
||||
reply.setPerformative(ACLMessage.REFUSE);
|
||||
reply.setContent("not-available");
|
||||
}
|
||||
myAgent.send(reply);
|
||||
}
|
||||
else
|
||||
{
|
||||
block();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class PurchaseOrdersServer extends CyclicBehaviour {
|
||||
public void action() {
|
||||
//purchase order as proposal acceptance only template
|
||||
MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.ACCEPT_PROPOSAL);
|
||||
ACLMessage msg = myAgent.receive(mt);
|
||||
if (msg != null) {
|
||||
String title = msg.getContent();
|
||||
ACLMessage reply = msg.createReply();
|
||||
Integer price = (Integer) catalogue.remove(title);
|
||||
if (price != null) {
|
||||
reply.setPerformative(ACLMessage.INFORM);
|
||||
System.out.println(getAID().getLocalName() + ": " + title + " sold to " + msg.getSender().getLocalName());
|
||||
}
|
||||
else {
|
||||
//title not found in the catalogue, sold to another agent in the meantime (after proposal submission)
|
||||
reply.setPerformative(ACLMessage.FAILURE);
|
||||
reply.setContent("not-available");
|
||||
}
|
||||
myAgent.send(reply);
|
||||
}
|
||||
else {
|
||||
block();
|
||||
private class PurchaseOrdersServer extends CyclicBehaviour
|
||||
{
|
||||
public void action()
|
||||
{
|
||||
//purchase order as proposal acceptance only template
|
||||
MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.ACCEPT_PROPOSAL);
|
||||
ACLMessage msg = myAgent.receive(mt);
|
||||
if (msg != null)
|
||||
{
|
||||
String title = msg.getContent();
|
||||
ACLMessage reply = msg.createReply();
|
||||
Integer price = (Integer) catalogue.remove(title);
|
||||
if (price != null)
|
||||
{
|
||||
reply.setPerformative(ACLMessage.INFORM);
|
||||
System.out.println(getAID().getLocalName() + ": " + title + " sold to " + msg.getSender().getLocalName());
|
||||
}
|
||||
else
|
||||
{
|
||||
//title not found in the catalogue, sold to another agent in the meantime (after proposal submission)
|
||||
reply.setPerformative(ACLMessage.FAILURE);
|
||||
reply.setContent("not-available");
|
||||
}
|
||||
myAgent.send(reply);
|
||||
}
|
||||
else
|
||||
{
|
||||
block();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,12 +6,14 @@ import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
|
||||
class BookSellerGui extends JFrame {
|
||||
class BookSellerGui extends JFrame
|
||||
{
|
||||
private BookSellerAgent myAgent;
|
||||
|
||||
private JTextField titleField, priceField;
|
||||
|
||||
BookSellerGui(BookSellerAgent a) {
|
||||
BookSellerGui(BookSellerAgent a)
|
||||
{
|
||||
super(a.getLocalName());
|
||||
|
||||
myAgent = a;
|
||||
@@ -27,9 +29,11 @@ class BookSellerGui extends JFrame {
|
||||
getContentPane().add(p, BorderLayout.CENTER);
|
||||
|
||||
JButton addButton = new JButton("Add");
|
||||
addButton.addActionListener( new ActionListener() {
|
||||
addButton.addActionListener( new ActionListener()
|
||||
{
|
||||
public void actionPerformed(ActionEvent ev) {
|
||||
try {
|
||||
try
|
||||
{
|
||||
String title = titleField.getText().trim();
|
||||
String price = priceField.getText().trim();
|
||||
myAgent.updateCatalogue(title, Integer.parseInt(price));
|
||||
@@ -45,8 +49,10 @@ class BookSellerGui extends JFrame {
|
||||
p.add(addButton);
|
||||
getContentPane().add(p, BorderLayout.SOUTH);
|
||||
|
||||
addWindowListener(new WindowAdapter() {
|
||||
public void windowClosing(WindowEvent e) {
|
||||
addWindowListener(new WindowAdapter()
|
||||
{
|
||||
public void windowClosing(WindowEvent e)
|
||||
{
|
||||
myAgent.doDelete();
|
||||
}
|
||||
} );
|
||||
@@ -54,12 +60,13 @@ class BookSellerGui extends JFrame {
|
||||
setResizable(false);
|
||||
}
|
||||
|
||||
public void display() {
|
||||
public void display()
|
||||
{
|
||||
pack();
|
||||
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
int centerX = (int)screenSize.getWidth() / 2;
|
||||
int centerY = (int)screenSize.getHeight() / 2;
|
||||
setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user