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;
private Currency myCurrency;
private IntegerValue toPayValue;
private IntegerValue paidValue;
private int payAssessment;
private Customer customer;
private transient Iterator videoIterator;
public RentProcess()
{
super ("RentProcess");
}
protected void setupMachine()
{
myCurrency = (Currency)Shop.getTheShop().getCatalog("DM");
final DataBasket db = getBasket();
final CountingStockImpl cs =
(CountingStockImpl)Shop.getTheShop().getStock("Video-Countingstock");
final TextInputForm tif = new TextInputForm("Customer-ID",
"Customer-ID",
"");
tif.addContentCreator(
new FormSheetContentCreator()
{
protected void createFormSheetContent(FormSheet fs)
{
fs.getButton(FormSheet.BTNID_OK).
setAction(
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.getButton(FormSheet.BTNID_CANCEL).
setAction(
new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
capabilityGate.setNextTransition(
GateChangeTransition.CHANGE_TO_ROLLBACK_GATE);
}
}
);
}
}
);
capabilityGate = new UIGate((FormSheet) tif,
(MenuSheet) null);
selectionGate = new UIGate((FormSheet) null, (MenuSheet) null);
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.getButton(FormSheet.BTNID_OK).
setAction(
new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
selectionGate.setNextTransition(toPayingTransition);
}
}
);
fs.getButton(FormSheet.BTNID_CANCEL).
setAction(
new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
selectionGate.setNextTransition(
GateChangeTransition.CHANGE_TO_ROLLBACK_GATE);
}
}
);
}
}
);
selectionGate.setFormSheet(ttfs);
return selectionGate;
}
};
rentGate = new UIGate(null, null);
toPayingTransition =
new Transition()
{
public Gate perform(SaleProcess pOwner, User usr)
{
final DataBasketCondition dbc =
DataBasketConditionImpl.allStockItemsWithSource(cs);
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.removeButton(FormSheet.BTNID_CANCEL);
fs.getButton(FormSheet.BTNID_OK).
setAction(
new sale.Action()
{
public void doAction (SaleProcess p, SalesPoint sp)
{
try {
paidValue = (IntegerValue)myCurrency.parse(tifFinal.getText());
videoIterator = db.iterator(dbc);
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;
}
};
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(CurrencyImpl.PFENNIG_STCK_1,
toPayValue.getValue().intValue(), pOwner.getBasket());
if (paidValue.compareTo(toPayValue) == 0)
payAssessment = 0;
else
payAssessment = 1;
}
else
payAssessment = -1;
return decisionGate;
}
};
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:
commitTransfer();
return GateChangeTransition.CHANGE_TO_COMMIT_GATE;
case 1:
commitTransfer();
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;
}
}
public void commitTransfer()
{
Object date = Shop.getTheShop().getTimer().getTime();
while (videoIterator.hasNext())
{
CountingStockItemDBEntry cassetteItem =
(CountingStockItemDBEntry)videoIterator.next();
int number = cassetteItem.count();
for (; number > 0; number --)
{
customer.addVideoCassette(new
CassetteStoringStockItem(cassetteItem.getSecondaryKey(), date));
try {
Log.getGlobalLog().log(new MyLoggable(cassetteItem.getSecondaryKey(),
customer.getCustomerID(),
date));
}
catch (LogNoOutputStreamException lnose) {
}
catch (IOException ioe) {
}
}
}
}
};
getChangeGate = new UIGate(null, null);
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;
}
};
}
public Gate getInitialGate()
{
setupMachine();
return capabilityGate;
}
public Gate getLogGate()
{
return getStopGate();
}
}