001 package org.cocome.tradingsystem.testdriver; 002 003 import java.io.Serializable; 004 005 import javax.jms.JMSException; 006 import javax.jms.TopicPublisher; 007 import javax.jms.TopicSession; 008 import javax.jms.TopicSubscriber; 009 010 import org.cocome.tradingsystem.cashdeskline.datatypes.KeyStroke; 011 import org.cocome.tradingsystem.cashdeskline.datatypes.PaymentMode; 012 import org.cocome.tradingsystem.cashdeskline.events.CashAmountEnteredEvent; 013 import org.cocome.tradingsystem.cashdeskline.events.CashBoxClosedEvent; 014 import org.cocome.tradingsystem.cashdeskline.events.ChangeAmountCalculatedEvent; 015 import org.cocome.tradingsystem.cashdeskline.events.CreditCardPaymentEnabledEvent; 016 import org.cocome.tradingsystem.cashdeskline.events.PaymentModeEvent; 017 import org.cocome.tradingsystem.cashdeskline.events.SaleFinishedEvent; 018 import org.cocome.tradingsystem.cashdeskline.events.SaleStartedEvent; 019 import org.cocome.tradingsystem.systests.interfaces.ICashBox; 020 021 /** 022 * Glue code for the cash box. 023 * 024 * @author Benjamin Hummel 025 * @author $Author: hummel $ 026 * @version $Rev: 63 $ 027 * @levd.rating GREEN Rev: 63 028 */ 029 public class CashBox extends UpdateReceiver implements ICashBox { 030 031 /** Publisher for sending events. */ 032 private final TopicPublisher publisher; 033 034 /** Session used for creating object messages. */ 035 private final TopicSession session; 036 037 /** Constructor. */ 038 public CashBox(TopicPublisher publisher, TopicSession session, 039 TopicSubscriber subscriber) throws JMSException { 040 super(subscriber, ChangeAmountCalculatedEvent.class); 041 this.publisher = publisher; 042 this.session = session; 043 } 044 045 /** Publish a serializable message. */ 046 private void publish(Serializable message) throws JMSException { 047 publisher.publish(session.createObjectMessage(message)); 048 } 049 050 /** {@inheritDoc} */ 051 public void enterReceivedCash(int centAmount) throws JMSException { 052 // we have to generate multiple events for this one 053 if (centAmount >= 100) { 054 for (char c : Integer.toString(centAmount / 100).toCharArray()) { 055 publish(new CashAmountEnteredEvent(charToStroke(c))); 056 } 057 } 058 059 publish(new CashAmountEnteredEvent(KeyStroke.COMMA)); 060 061 String cents = Integer.toString(centAmount % 100); 062 if (cents.length() < 2) { 063 cents = "0" + cents; 064 } 065 for (char c : cents.toCharArray()) { 066 publish(new CashAmountEnteredEvent(charToStroke(c))); 067 } 068 069 publish(new CashAmountEnteredEvent(KeyStroke.ENTER)); 070 } 071 072 /** Convert characters to keystrokes. */ 073 private KeyStroke charToStroke(char c) { 074 switch (c) { 075 case '0': 076 return KeyStroke.ZERO; 077 case '1': 078 return KeyStroke.ONE; 079 case '2': 080 return KeyStroke.TWO; 081 case '3': 082 return KeyStroke.THREE; 083 case '4': 084 return KeyStroke.FOUR; 085 case '5': 086 return KeyStroke.FIVE; 087 case '6': 088 return KeyStroke.SIX; 089 case '7': 090 return KeyStroke.SEVEN; 091 case '8': 092 return KeyStroke.EIGHT; 093 case '9': 094 return KeyStroke.NINE; 095 default: 096 throw new IllegalArgumentException("Only number supported!"); 097 } 098 } 099 100 /** {@inheritDoc} */ 101 public void finishSale() throws JMSException { 102 publish(new SaleFinishedEvent()); 103 } 104 105 /** {@inheritDoc} */ 106 public void setCashboxStatus(boolean closed) throws JMSException { 107 if (closed) { 108 publish(new CashBoxClosedEvent()); 109 } else { 110 // this signal os not used by the implementation 111 } 112 } 113 114 /** {@inheritDoc} */ 115 public void startCashPayment() throws JMSException { 116 publish(new PaymentModeEvent(PaymentMode.CASH)); 117 } 118 119 /** {@inheritDoc} */ 120 public void startCreditCardPayment() throws JMSException { 121 publish(new PaymentModeEvent(PaymentMode.CREDITCARD)); 122 } 123 124 /** {@inheritDoc} */ 125 public void startNewSale() throws JMSException { 126 publish(new SaleStartedEvent()); 127 } 128 129 /** {@inheritDoc} */ 130 public boolean wasOpenSignalSent() { 131 // this is slightly bogus, but this is how it is implemented in the GUI 132 return getLastRelevantMessage() instanceof ChangeAmountCalculatedEvent; 133 } 134 135 /** {@inheritDoc} */ 136 public void manuallyEnableCreditCardReader() throws JMSException { 137 publish(new CreditCardPaymentEnabledEvent()); 138 } 139 }