001 package market.stdform;
002
003 import java.awt.GridBagConstraints;
004 import java.awt.GridBagLayout;
005 import java.awt.GridLayout;
006 import java.awt.event.KeyAdapter;
007 import java.awt.event.KeyEvent;
008
009 import javax.swing.Box;
010 import javax.swing.BoxLayout;
011 import javax.swing.ButtonGroup;
012 import javax.swing.JLabel;
013 import javax.swing.JPanel;
014 import javax.swing.JRadioButton;
015 import javax.swing.JTextField;
016
017 import market.Conversions;
018 import market.VCDummy;
019 import market.VCPositiveDouble;
020 import market.swing.ComponentFactory;
021 import market.swing.JTFCheckable;
022 import sale.FormSheet;
023 import sale.FormSheetContentCreator;
024 import data.Value;
025
026 /**
027 * This FormSheet displays the bill of a customer`s buy
028 */
029 public class FSSellerBill extends FormSheet {
030
031 /**
032 * ID for serialization.
033 */
034 private static final long serialVersionUID = -1249220937998935774L;
035
036 public static final int JTFC_DISCOUNT = 0;
037 public static final int JTFC_ENDSUM = 1;
038
039 private Value v_sum;
040 private double discount;
041 private Value v_endSum;
042 private JTFCheckable jtfc_discount;
043 private JTFCheckable jtfc_endSum;
044
045 /**
046 * @param v the value of customer's current purchase.
047 * @param d the customer's current discount, it covers values from 0 to 1.
048 */
049 public FSSellerBill(Value v, double d) {
050 super("Rechnung", null);
051 v_sum = v;
052 discount = d;
053 setEndSum(discount);
054 this.addContentCreator(getFSCC());
055 jtfc_discount.addKeyListener(new KeyAdapter(){
056 public void keyPressed(KeyEvent e) {
057 if(e.getKeyCode()==KeyEvent.VK_ENTER){
058 setNewDiscount();
059 }
060 }
061 });
062 }
063
064 /**
065 * @return a new {@link FSCheckable} that uses a FSSellerBill as argument.
066 *
067 * @param v the value of customer's current purchase.
068 * @param d the customer's current discount, it covers values from 0 to 1.
069 */
070 public static FSCheckable create(Value v, double d){
071 FormSheet fs = new FSSellerBill(v, d);
072 return new FSCheckable(fs);
073 }
074
075 /**
076 * @return the FormSheetContentCreator of this FormSheet class.
077 */
078 private FormSheetContentCreator getFSCC(){
079 return new FormSheetContentCreator() {
080 private static final long serialVersionUID = -309759057338864367L;
081 protected void createFormSheetContent(FormSheet fs) {
082
083 //JPanel Initialization
084 JPanel jp_content = new JPanel();
085 JPanel jp_main = new JPanel();
086 JPanel jp_amounts = new JPanel();
087 JPanel jp_paymentMode = new JPanel();
088
089 GridBagLayout gridbag = new GridBagLayout();
090 GridBagConstraints c = new GridBagConstraints();
091 jp_content.setLayout(gridbag);
092 c.gridy = 1;
093 c.weighty = 1;
094 gridbag.setConstraints(jp_main, c);
095
096 jp_main.setLayout(new BoxLayout(jp_main, BoxLayout.Y_AXIS));
097 jp_amounts.setLayout(new GridLayout(3,3));
098 jp_amounts.setAlignmentX(Box.CENTER_ALIGNMENT);
099 jp_paymentMode.setLayout(new GridLayout(2,1));
100 jp_paymentMode.setBorder(ComponentFactory.createInsetBorder("Zahlungsart"));
101 jp_paymentMode.setAlignmentX(Box.CENTER_ALIGNMENT);
102
103 //TextField Initialisation
104 JTextField jtf_sum = new JTextField(Conversions.valueToCurrency(v_sum));
105 jtfc_discount = new JTFCheckable(JTFC_DISCOUNT, new VCPositiveDouble("Rabatt", false), 3);
106 jtfc_discount.setText(Conversions.fixedDecimal(100 * discount, 3));
107 jtfc_endSum = new JTFCheckable(JTFC_ENDSUM, new VCDummy("Endsumme"), 7);
108 jtfc_endSum.setText(Conversions.valueToCurrency(v_endSum));
109 jtf_sum.setEditable(false);
110 jtfc_endSum.setEditable(false);
111
112 //ButtonGroup to select mode of payment
113 JRadioButton jrb_cash = new JRadioButton("Bar", true);
114 JRadioButton jrb_card = new JRadioButton("Geldkarte", false);
115 ButtonGroup group = new ButtonGroup();
116 group.add(jrb_cash);
117 group.add(jrb_card);
118
119 //Adds Components to JPanels
120 jp_amounts.add(new JLabel("Summe: "));
121 jp_amounts.add(jtf_sum);
122 jp_amounts.add(new JLabel(" Euro"));
123 jp_amounts.add(new JLabel("Rabatt: "));
124 jp_amounts.add(jtfc_discount);
125 jp_amounts.add(new JLabel(" %"));
126 jp_amounts.add(new JLabel("Rechnungsbetrag: "));
127 jp_amounts.add(jtfc_endSum);
128 jp_amounts.add(new JLabel(" Euro"));
129
130 jp_paymentMode.add(jrb_cash);
131 jp_paymentMode.add(jrb_card);
132
133 jp_main.add(jp_amounts);
134 jp_main.add(Box.createVerticalStrut(50));
135 jp_main.add(jp_paymentMode);
136
137 jp_content.add(jp_main);
138
139 //Adds components and Buttons to the FormSheet
140 fs.setComponent(jp_content);
141 fs.removeAllButtons();
142 fs.addButton("Zahlung", ButtonIDs.BTN_ACCEPT, null);
143 fs.addButton("Zurück", ButtonIDs.BTN_BACK, null);
144 }
145 };
146 }
147
148 /**
149 * Updates the TextFields containing discount and end-sum, using entry in field discount.
150 */
151 private void setNewDiscount(){
152 if(jtfc_discount.hasValidValue()){
153 double oldDisc = discount;
154 discount = Conversions.round(
155 Double.valueOf(Conversions.convertComma(
156 jtfc_discount.getText())).doubleValue()/100, 5);
157 if (discount > 1) { //do not give more than 100 percent discount
158 discount = oldDisc;
159 }
160 setEndSum(discount);
161 jtfc_discount.setText(Conversions.fixedDecimal(100 * discount, 3));
162 jtfc_endSum.setText(Conversions.valueToCurrency(v_endSum));
163 } else {
164 jtfc_discount.setText(Conversions.fixedDecimal(100 * discount, 3));
165 }
166 }
167
168 /**
169 * Sets new end-sum using sum and given discount.
170 */
171 private void setEndSum(double discount){
172 if(discount==0) v_endSum = v_sum;
173 else v_endSum = v_sum.subtract(v_sum.multiply(discount));
174 }
175
176 /**
177 * @return the final value the customer has to pay.
178 */
179 public Value getEndSum(){
180 return v_endSum;
181 }
182 }