import sale.*;
import sale.stdforms.*;
import data.*;
import data.ooimpl.*;
import data.stdforms.*;
import users.*;
import log.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.io.*;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
public class RentProcess extends SaleProcess
{
protected UIGate capabilityGate;
protected UIGate selectionGate;
protected UIGate rentGate;
protected Gate decisionGate;
protected UIGate getChangeGate;
protected Transition toSelectionTransition;
protected Transition toPayingTransition;
protected Transition toDecisionTransition;
protected Transition toGetChangeTransition;
protected Currency myCurrency;
private IntegerValue toPayValue;
private IntegerValue paidValue;
private int payAssessment;
private Customer customer;
public RentProcess()
{
super ("RentProcess");
}
protected void setupMachine()
{
myCurrency = (Currency)Shop.getTheShop().getCatalog("DM");
capabilityGate = new UIGate(null, null);
final TextInputForm tif = new TextInputForm("Customer-ID",
"Customer-ID",
"");
selectionGate = new UIGate(null, null);
rentGate = new UIGate(null, null);
getChangeGate = new UIGate(null, null);
final DataBasket db = getBasket();
final CountingStockImpl cs =
(CountingStockImpl)Shop.getTheShop().getStock("Video-Countingstock");
toSelectionTransition = new Transition()
{
public Gate perform(SaleProcess pOwner, User usr)
{
TwoTableFormSheet ttfs = TwoTableFormSheet.create(
"Make your selection",
cs,
db,
selectionGate,
null,
null,
false,
new OfferTED(true),
null,
null
);
ttfs.addContentCreator(new FormSheetContentCreator()
{
protected void createFormSheetContent(FormSheet fs)
{
fs.removeAllButtons();
fs.addButton ("Ok", 100, new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
selectionGate.setNextTransition(toPayingTransition);
}
});
fs.addButton ("Cancel", 101, new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
selectionGate.setNextTransition(
GateChangeTransition.CHANGE_TO_ROLLBACK_GATE);
}
});
}
});
selectionGate.setFormSheet(ttfs);
return selectionGate;
}
};
toGetChangeTransition = new Transition()
{
public Gate perform(SaleProcess pOwner, User usr)
{
MsgForm mf = new MsgForm(
"Get Change",
"You get " +
myCurrency.toString((IntegerValue)paidValue.subtract(toPayValue)) +
" Change.");
mf.addContentCreator(new FormSheetContentCreator()
{
public void createFormSheetContent(FormSheet fs)
{
fs.getButton(FormSheet.BTNID_OK).setAction(new Action()
{
public void doAction(SaleProcess p, SalesPoint sp)
{
getChangeGate.setNextTransition(
GateChangeTransition.CHANGE_TO_COMMIT_GATE);
}
});
}
});
getChangeGate.setFormSheet(mf);
return getChangeGate;
}
};
decisionGate = new Gate()
{
public Transition getNextTransition(SaleProcess pOwner, User usr)
throws InterruptedException
{
switch(payAssessment) {
case -1:
FormSheet mf = new MsgForm("Error",
"You have to pay more!",
false);
pOwner.getContext().popUpFormSheet(pOwner, mf);
return new GateChangeTransition(rentGate);
case 0:
return GateChangeTransition.CHANGE_TO_COMMIT_GATE;
case 1:
return toGetChangeTransition;
default:
FormSheet mf2 = new MsgForm("Error",
"Internal error at Decision Gate. " +
"Will quit process.",
false);
pOwner.getContext().popUpFormSheet (pOwner, mf2);
return GateChangeTransition.CHANGE_TO_QUIT_GATE;
}
}
};
toDecisionTransition = new Transition()
{
public Gate perform (SaleProcess pOwner, User usr)
{
if (paidValue.compareTo (toPayValue) >= 0) {
if (toPayValue.getValue().intValue() > 0)
((CountingStock)Shop.getTheShop().getStock(
"coin slot")).add("1-Pfennig-Stück",
toPayValue.getValue().intValue(),
pOwner.getBasket());
if (paidValue.compareTo(toPayValue) == 0)
payAssessment = 0;
else
payAssessment = 1;
}
else
payAssessment = -1;
return decisionGate;
}
};
toPayingTransition = new Transition()
{
public Gate perform(SaleProcess pOwner, User usr)
{
final DataBasketCondition dbc =
new DataBasketConditionImpl (DataBasketEntry.STOCK_ITEM_MAIN_KEY,
null, cs, null, null);
BasketEntryValue bev = BasketEntryValues.ONLY_STOCK_ITEMS;
QuoteValue qvSum =
new QuoteValue(new IntegerValue(0), new IntegerValue(0));
pOwner.getBasket().sumBasket (dbc, bev, qvSum);
toPayValue = (IntegerValue)qvSum.getBid();
FormSheet tif =
new TextInputForm("Paying",
"You have to pay " +
myCurrency.toString (toPayValue) + ".",
myCurrency.toString (toPayValue)
);
tif.addContentCreator(new FormSheetContentCreator()
{
protected void createFormSheetContent(FormSheet fs)
{
final TextInputForm tifFinal = (TextInputForm)fs;
fs.removeAllButtons();
fs.addButton("Ok", 100, new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
try {
paidValue =
(IntegerValue)myCurrency.parse(tifFinal.getText());
final Object date = Shop.getTheShop().getTimer().getTime();
Iterator i = db.iterator(dbc);
while (i.hasNext()) {
final CountingStockItemDBEntry cassetteItem =
(CountingStockItemDBEntry)i.next();
int number = cassetteItem.count();
for (; number > 0; number --) {
try {
Log.getGlobalLog().log(new MyLoggable(
cassetteItem.getSecondaryKey(),
customer.getCustomerID(), date));
}
catch (LogNoOutputStreamException lnose) {}
catch (IOException ioe) {}
customer.addVideoCassette(new CassetteStoringStockItem(
cassetteItem.getSecondaryKey(), date));
}
}
rentGate.setNextTransition(toDecisionTransition);
}
catch(ParseException pexc) {
MsgForm mf = new MsgForm ("Error",
"The specified amount does " +
"not have an appropriate format.");
try {
p.getContext().popUpFormSheet (p, mf);
}
catch(InterruptedException iexc) {
}
}
}
});
fs.addButton ("Back", 101, new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
rentGate.setNextTransition(
new GateChangeTransition(selectionGate));
}
});
fs.addButton ("Cancel", 102, new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
rentGate.setNextTransition(
GateChangeTransition.CHANGE_TO_ROLLBACK_GATE);
}
});
}
});
rentGate.setFormSheet(tif);
return rentGate;
}
};
tif.addContentCreator(new FormSheetContentCreator()
{
protected void createFormSheetContent(FormSheet fs)
{
fs.removeAllButtons();
fs.addButton ("Ok", 100, new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
String customerID = tif.getText();
boolean isNotAnInteger = true;
try {
new Integer(customerID);
isNotAnInteger = false;
}
catch(NumberFormatException nfe) {
isNotAnInteger = true;
}
if (!isNotAnInteger && (new Integer(customerID)).intValue() > 0) {
try {
customer = new Customer(customerID);
VideoMachine.addCustomer(customer);
}
catch (DuplicateKeyException dke) {
customer = VideoMachine.getCustomerByID(customerID);
}
capabilityGate.setNextTransition(toSelectionTransition);
}
else {
JOptionPane.showMessageDialog(null,
"CustomerID must be a positive Number!");
capabilityGate.setNextTransition(
new GateChangeTransition(capabilityGate));
}
}
});
fs.addButton ("Cancel", 101, new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
capabilityGate.setNextTransition(
GateChangeTransition.CHANGE_TO_ROLLBACK_GATE);
}
});
}
});
capabilityGate.setFormSheet(tif);
}
public Gate getInitialGate()
{
setupMachine();
return capabilityGate;
}
public Gate getLogGate()
{
return getStopGate();
}
}
class MyLoggable implements Loggable
{
String name;
String customerID;
Object date;
public MyLoggable(String name,
String customerID, Object date)
{
super();
this.name = name;
this.customerID = customerID;
this.date = date;
}
public LogEntry getLogData()
{
return new MyLogEntry(name, customerID, date);
}
}
class MyLogEntry extends LogEntry
{
String name;
String customerID;
Object date;
public MyLogEntry(String name, String customerID, Object date)
{
super();
this.name = name;
this.customerID = customerID;
this.date = date;
}
public String toString()
{
return name +
" rent by customer " + customerID +
" (ID) at turn " + date;
}
}