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