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 Currency myCurrency;
private int toPayBackValue;
private IntegerValue paidValue;
private Customer customer;
private int rentPrice = 300;
public GiveBackProcess()
{
super ("GiveBackProcess");
}
protected void setupMachine()
{
myCurrency = (Currency)Shop.getTheShop().getCatalog("DM");
final CatalogImpl videoCatalog =
(CatalogImpl)Shop.getTheShop().getCatalog("Video-Catalog");
final DataBasket db = getBasket();
final TextInputForm tif = new TextInputForm("Customer-ID",
"Customer-ID",
"");
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, "No videos rent out!");
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 = new UIGate((FormSheet) tif,
(MenuSheet) null);
selectionGate = new UIGate((FormSheet) null, (MenuSheet) null);
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 MyLoggable(cassetteItem,
customer, date));
}
catch (LogNoOutputStreamException lnose) {
}
catch (IOException ioe) {
}
}
else
{
JOptionPane.showMessageDialog(null,
cassette.getName() + " is your's!");
}
}
if ((((Long)date).intValue()
- ((Long)cassette.getDate()).intValue()) == 0)
{
toPayBackValue = toPayBackValue + (sellValue - rentPrice);
videoStock.add(cassette.getName(), 1, null);
try {
Log.getGlobalLog().log(new MyLoggable(cassetteItem,
customer, date));
}
catch (LogNoOutputStreamException lnose) {
}
catch (IOException ioe) {
}
}
}
if (customer.getStoringStock().size(null) == 0)
VideoMachine.removeCustomer(customer);
try
{
if (toPayBackValue > 0 )
{
((CountingStock)Shop.getTheShop().getStock(
"coin slot")).remove(CurrencyImpl.PFENNIG_STCK_1,
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;
}
};
}
public Gate getInitialGate()
{
setupMachine();
return capabilityGate;
}
public Gate getLogGate()
{
return getStopGate();
}
}