import sale.*;
import sale.stdforms.*;
import data.*;
import data.ooimpl.*;
import data.stdforms.*;
import data.events.*;
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 GiveBackProcess extends SaleProcess
{
protected UIGate capabilityGate;
protected UIGate selectionGate;
protected UIGate giveRestGate;
protected Transition toSelectionTransition;
protected Transition toGetMoneyTransition;
protected data.Currency myCurrency;
private int toPayBackValue;
private IntegerValue paidValue;
private Customer customer;
private int rentPrice = 300;
public GiveBackProcess()
{
super ("GiveBackProcesss");
}
protected void setupMachine()
{
myCurrency = (Currency)Shop.getTheShop().getCatalog("DM");
final CatalogImpl videoCatalog =
(CatalogImpl)Shop.getTheShop().getCatalog("Video-Catalog");
capabilityGate = new UIGate(null, null);
final TextInputForm tif = new TextInputForm("Customer-ID",
"Customer-ID",
"");
selectionGate = new UIGate(null, null);
final DataBasket db = getBasket();
toSelectionTransition = new Transition()
{
public Gate perform(SaleProcess pOwner, User usr)
{
StoringStock ss = customer.getStoringStock();
TwoTableFormSheet ttfs = TwoTableFormSheet.create(
"Give Back",
ss,
db,
selectionGate
);
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(toGetMoneyTransition);
}
});
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;
}
};
giveRestGate = new UIGate(null, null);
toGetMoneyTransition = new Transition()
{
public Gate perform(SaleProcess pOwner, User usr)
{
DataBasketCondition dbc =
DataBasketConditionImpl.ALL_STOCK_ITEMS;
int sellValue = 0;
int rentValue = 0;
CountingStockImpl videoStock =
(CountingStockImpl)Shop.getTheShop().getStock("Video-Countingstock");
Object date = Shop.getTheShop().getTimer().getTime();
Iterator i = db.iterator(dbc);
while (i.hasNext()) {
StoringStockItemDBEntry cassetteItem =
(StoringStockItemDBEntry)i.next();
CassetteStoringStockItem cassette =
(CassetteStoringStockItem)cassetteItem.getValue();
try {
sellValue =
((NumberValue)((QuoteValue)(videoCatalog.get(
cassette.getName(), null, false).getValue())).getBid()
).getValue().intValue();
}
catch (VetoException ve) {}
if ((((Long)date).intValue()
- ((Long)cassette.getDate()).intValue()) > 0) {
rentValue = (((Long)date).intValue()
- ((Long)cassette.getDate()).intValue())
* rentPrice;
if (rentValue < sellValue) {
toPayBackValue = toPayBackValue + (sellValue - rentValue);
videoStock.add(cassette.getName(), 1, null);
try {
Log.getGlobalLog().log(new GiveBackLoggable(
cassetteItem.getSecondaryKey(),
customer.getCustomerID(), date));
}
catch (LogNoOutputStreamException lnose) {}
catch (IOException ioe) {}
}
}
if ((((Long)date).intValue()
- ((Long)cassette.getDate()).intValue()) == 0) {
toPayBackValue = toPayBackValue + (sellValue - rentPrice);
videoStock.add(cassette.getName(), 1, null);
try {
Log.getGlobalLog().log(new GiveBackLoggable(
cassetteItem.getSecondaryKey(),
customer.getCustomerID(), date));
}
catch (LogNoOutputStreamException lnose) {}
catch (IOException ioe) {}
}
}
try {
if (toPayBackValue > 0 ) {
((CountingStock)Shop.getTheShop().getStock(
"coin slot")).remove("1-Pfennig-Stueck",
toPayBackValue,
pOwner.getBasket());
}
}
catch (VetoException ve) {}
MsgForm mf = new MsgForm(
"Give Rest",
"You get " +
myCurrency.toString(new IntegerValue(toPayBackValue)) +
" back");
mf.addContentCreator(new FormSheetContentCreator()
{
public void createFormSheetContent(FormSheet fs)
{
fs.getButton(FormSheet.BTNID_OK).setAction(new Action()
{
public void doAction(SaleProcess p, SalesPoint sp)
{
giveRestGate.setNextTransition(
GateChangeTransition.CHANGE_TO_COMMIT_GATE);
}
});
}
});
giveRestGate.setFormSheet(mf);
return giveRestGate;
}
};
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) {}
if (!isNotAnInteger && (new Integer(customerID)).intValue() > 0) {
try {
boolean customerExists = false;
Set customerSet = VideoMachine.getAllCustomer();
Iterator i = customerSet.iterator();
while (i.hasNext() && !customerExists){
Customer myCustomer = (Customer)i.next();
if (myCustomer.getCustomerID().equals(customerID)) {
customer = myCustomer;
customerExists = true;
}
}
if (customerExists) {
capabilityGate.setNextTransition(
toSelectionTransition);
}
else {
JOptionPane.showMessageDialog(null,
"This CustomerID doesn't exist!");
capabilityGate.setNextTransition(
new GateChangeTransition(capabilityGate));
}
}
catch (NullPointerException ne) {
JOptionPane.showMessageDialog(null,
"This CustomerID doesn't exist!");
capabilityGate.setNextTransition(
new GateChangeTransition(capabilityGate));
}
}
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);
}
protected void logSalesPointClosed()
{
}
public Gate getInitialGate()
{
setupMachine();
return capabilityGate;
}
public Gate getLogGate()
{
return getStopGate();
}
}
class GiveBackLoggable implements Loggable
{
String name;
String customerID;
Object date;
public GiveBackLoggable(String name,
String customerID, Object date)
{
super();
this.name = name;
this.customerID = customerID;
this.date = date;
}
public GiveBackLoggable(StoringStockItemDBEntry cassetteItem,
Customer customer, Object date)
{
super();
name = cassetteItem.getSecondaryKey();
this.customerID = customerID;
this.date = date;
}
public LogEntry getLogData()
{
return new GiveBackLogEntry(name, customerID, date);
}
}
class GiveBackLogEntry extends LogEntry
{
String name;
String customerID;
Object date;
public GiveBackLogEntry(String name, String customerID, Object date)
{
super();
this.name = name;
this.customerID = customerID;
this.date = date;
}
public String toString()
{
return name +
" gave back by customer " + customerID +
" (ID) at turn " + date;
}
}