001 package videoautomat;
002
003 import java.util.Iterator;
004
005 import sale.*;
006 import users.User;
007 import videoautomat.gui.HandBack;
008 import data.IntegerValue;
009 import data.NotEnoughMoneyException;
010 import data.NumberValue;
011 import data.QuoteValue;
012 import data.StockItem;
013 import data.StoringStock;
014 import data.Value;
015 import data.ooimpl.MoneyBagImpl;
016 import data.ooimpl.StoringStockImpl;
017 import data.stdforms.TwoTableFormSheet;
018 import data.stdforms.twotableformsheet.SSSSStrategy;
019
020 /**
021 * This class implements a <code>SaleProcess</code> to hand back the videos.
022 *
023 */
024 public class SaleProcessHandBack extends SaleProcess {
025
026 /*
027 * The initial Gate of this process at which the user can choose from his/her rented videos.
028 */
029 private UIGate uig_video = new UIGate(null, null);
030
031 /*
032 * The Gate where the user will get informed about his/her change money.
033 */
034 private UIGate uig_change = new UIGate(null, null);
035
036 /*
037 * A Stock to store temporary videos.
038 */
039 private StoringStock ss_temp =
040 new StoringStockImpl("temp", VideoShop.getVideoCatalog());
041
042 /*
043 * A MoneyBag to store temporary the change money
044 */
045 private MoneyBagImpl mb_temp = new MoneyBagImpl("temp", VideoShop.getCurrency());
046
047 /*
048 * A NumberValue representing the change money
049 */
050 private NumberValue nv_sum = new IntegerValue(0);
051
052 /**
053 * Constructs a new <code>SaleProcessHandBack</code>
054 *
055 */
056 public SaleProcessHandBack() {
057 super("SaleProcessGiveback");
058 }
059
060 /**
061 * Implementation of the inherited abstract method.
062 *
063 * @return the <code>Gate</code> where the user will see his/her current rented videos.
064 *
065 * @see sale.SaleProcess#getInitialGate()
066 */
067 protected Gate getInitialGate() {
068 TwoTableFormSheet ttfs =
069 HandBack.getRentedVideosFormSheet(
070 ((AutomatUser) ((SalesPoint) this.getContext()).getUser()).getVideoStock(),
071 ss_temp,
072 getBasket(),
073 uig_video,
074 null,
075 null,
076 new TEDVideoCassette(),
077 new TEDVideoCassette(),
078 new SSSSStrategy());
079
080 ttfs.addContentCreator(new FormSheetContentCreator() {
081 public void createFormSheetContent(FormSheet fs) {
082 fs.getButton(HandBack.FB_GIVEBACK).setAction(new Action() {
083 public void doAction(SaleProcess p, SalesPoint sp) {
084 uig_video.setNextTransition(getCalculateSumTransition());
085 }
086 });
087 fs.getButton(HandBack.FB_CANCEL).setAction(new Action() {
088 public void doAction(SaleProcess p, SalesPoint sp) {
089 uig_video.setNextTransition(
090 new GateChangeTransition(getRollbackGate()));
091 }
092 });
093 }
094 });
095
096 uig_video.setFormSheet(ttfs);
097 return uig_video;
098 }
099
100 /**
101 * @return the <code>Gate</code> where the user will see his/her change money.
102 *
103 */
104 private Gate getChangeGate() {
105 String s = VideoShop.getCurrency().toString(nv_sum);
106 FormSheet fs = HandBack.getChangeFormSheet(mb_temp, uig_change, getBasket(), s);
107
108 fs.addContentCreator(new FormSheetContentCreator() {
109 public void createFormSheetContent(FormSheet fs) {
110 fs.getButton(FormSheet.BTNID_OK).setAction(new Action() {
111 public void doAction(SaleProcess p, SalesPoint sp) {
112 uig_change.setNextTransition(
113 GateChangeTransition.CHANGE_TO_COMMIT_GATE);
114 }
115 });
116 }
117 });
118
119 uig_change.setFormSheet(fs);
120 return uig_change;
121 }
122
123 /**
124 * @return a <code>Transition</code> that calculates the money the user get back, temporary re-adds the handed
125 * back videos to the {@link VideoShop}s stock and change to the
126 * {@link SaleProcessHandBack#getChangeGate()}. If there is not enough change money in the shop, a
127 * {@link DisplayMoneyStockError}opens and it returns the sale.SaleProcess#getRollbackGate()
128 *
129 */
130 private Transition getCalculateSumTransition() {
131 return new Transition() {
132 public Gate perform(SaleProcess p, User u) {
133 if (ss_temp.size(getBasket()) == 0)
134 return getInitialGate();
135
136 Iterator it = ss_temp.iterator(getBasket(), false);
137 while (it.hasNext()) {
138 StockItem ssi = (StockItem) it.next();
139 Value v = null;
140 v = ((QuoteValue) ssi.getAssociatedItem(null).getValue()).getOffer();
141 nv_sum =
142 (NumberValue) nv_sum.add(
143 v.subtract(((VideoCassette) ssi).getCost()));
144
145 VideoShop.getVideoStock().add(ssi.getName(), 1, getBasket());
146 }
147 try{
148 VideoShop.getMoneyBag().transferMoney(mb_temp, getBasket(), nv_sum);
149 }catch(NotEnoughMoneyException e){
150 DisplayMoneyStockError dmse = new DisplayMoneyStockError();
151 return getRollbackGate();
152 }
153
154 return getChangeGate();
155 }
156 };
157 }
158 }