HotDogHouse

HotDogHouse.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
/*
 * Mark Hesser
 * HesserCAN 
 * [email protected]
 * www.hessercan.com
 */
 
package hotdoghouse;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.io.*;
import java.nio.file.*;
import static java.nio.file.StandardOpenOption.*; 
 
/**
 * @author mark
 */
public class HotDogHouse extends JFrame implements ActionListener
{
    // <editor-fold defaultstate="collapsed" desc="Delcarations">
    private final int FHEIGHT = 700;
    private final int FWIDTH = 900;
    private final String TITLE = "Mark's Hot Dog House";
    private final String COPYRIGHT = "Copyright © HesserCAN 2018";
 
    private final Dimension MENUBTND, ATOBTND, TENDERBTND, TENDERQBTND;
 
    private final JMenuBar mainBar = new JMenuBar();
    private final JMenu fileMenu = new JMenu("File");
    private final JMenuItem file_exit = new JMenuItem("Exit");
 
    private final JLabel titleLbl;
 
    GridBagConstraints c;
    int[][] tenderC;
 
    private JPanel jPTop, jPBottom, jPCenter, jPOrder, jPmodLabel, jPmiBtns, jPmcBtns, jPSelect, jPATO, jPTender;
 
    private final JTextArea orderTA = new JTextArea(38, 40);
    private final JScrollPane orderSP = new JScrollPane(orderTA);
 
    private final JTextField itemSelected = new JTextField(18);
    private final JTextField modSelected = new JTextField(18);
    private StringBuilder updateSB;
 
    //Menu Item Components
    private JButton[] miBtns;
    private final String[] miBtnStr = {"Regular\nHot Dog", "Foot-long\nHot Dog", "Italian\nSausage", "Kielbasa"};
    private final String[] miStr = {"Regular Hot Dog", "Foot-long Hot Dog", "Italian Sausage", "Kielbasa"};
    private final double[] miPrice = {2,3,3.5,3.5};
    private boolean[] miWasPressed;
    private boolean itemIsSelected;
 
    //Modifier Components
    private JButton[] mcBtns;
    private final String[] mcBtnStr = {"Ketchup", "Mustard", "Relish", "Onions", "Chili", "Peppers &\nOnions", "Cheese", "Clear"};
    private final String[] mcStr = {"Ketchup", "Mustard", "Relish", "Onions", "Chili", "Peppers & Onions", "Cheese", "Clear"};
    private final double[] mcPrice = {0,0,0,0,1,1,0.5};
    private boolean[] mcWasPressed;
    private String itemSelectStr, modSelectStr;
 
    //Order Components
    StringBuilder orderSB;
    private final JButton atoBtn = new JButton("Add to Order");
    private final JButton clearBtn = new JButton("Clear Order");
    private final JButton totalBtn = new JButton("Total");
    private int orderNum = 0;
    private String myOrderStr;
    private double subTotal = 0, salesTax = 0, gTotal = 0;
    private final double TAX = .06;
 
    //Tender Componets
    private StringBuilder tenderSB;
    private JTextField tenderTxt;
    double tenderAmt, cashTendered, creditTendered, change;
    boolean isTender, dotWP, endOrder;
    private JButton[] tenderBtns;
    private String[] tenderStr = {"1", "2", "3", "Exact Cash", "Next Dollar", 
        "4", "5", "6", "$5", "$10", "7", "8", "9", "$20", "$50", 
        ".", "0", "Clear", "Enter", "Credit/Debit"};
    private int decimalCounter;
    private String changeStr;
    private JLabel changeLbl;
 
    ArrayList<OrderItem> myOrder;
    //Customer Components
    private String custName;
 
    //Fonts
    private final Font titleFont = new Font("Arial", Font.BOLD, 24);
    private final Font copyFont = new Font("Arial", Font.PLAIN, 14);
    private final Font changeFont = new Font("Arial", Font.BOLD, 24);
    private final Font taFont = new Font("monospaced", Font.PLAIN, 12);
 
    //Write to File Components
    Path file = Paths.get(System.getProperty("user.home") 
                + "/Desktop/Order.txt"); 
    // </editor-fold>
 
    //credit card window
    //Work in Progress
    public Card credit;
 
    /**
     * Default Constructor
     */
    public HotDogHouse()
    {
        Image icon = Toolkit.getDefaultToolkit().getImage("AppIcon.png");
        setIconImage(icon);
        setTitle(TITLE);
        titleLbl = new JLabel(TITLE);
 
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(FWIDTH, FHEIGHT);
        setResizable(false);
        setLayout(new BorderLayout());
 
        //Dimensions
        MENUBTND = new Dimension(125, 50);
        ATOBTND = new Dimension(150, 50);
        TENDERBTND = new Dimension(50, 50);
        TENDERQBTND = new Dimension(100, 50);
 
        //Detects Screen Size
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenH = screenSize.height;
        int screenW = screenSize.width;
        //Places Window in the Center of the Screen
        setLocationRelativeTo(null);
 
        initComponents();
 
        createMenuBar();
        createMenuBtns();
        createTenderBtns();
        createFrame();
        addActionListeners();
 
        clearOrder();
 
        setVisible(true);
    }
 
    private void initComponents()
    {
        c = new GridBagConstraints();
 
        //Init Title
        titleLbl.setFont(titleFont);
 
        //Init Menu Item Components
        jPmiBtns = new JPanel(new GridLayout(1,4));
        miBtns = new JButton[miBtnStr.length];
        miWasPressed = new boolean[miBtnStr.length];
        itemSelectStr = "";
 
        //Init Modifier Components
        jPmcBtns = new JPanel(new GridLayout(2,4));
        mcBtns = new JButton[mcBtnStr.length];
        mcWasPressed = new boolean[mcBtnStr.length];
        modSelectStr = "";
 
        //Init Print Components
        orderSB = new StringBuilder();
        updateSB = new StringBuilder();
 
        //Init Order Components
        jPATO = new JPanel(new FlowLayout());
        atoBtn.setPreferredSize(ATOBTND);
        atoBtn.setFocusPainted(false);
        totalBtn.setPreferredSize(ATOBTND);
        totalBtn.setFocusPainted(false);
        clearBtn.setPreferredSize(ATOBTND);
        clearBtn.setFocusPainted(false);
        jPATO.add(atoBtn);
        jPATO.add(totalBtn);
        jPATO.add(clearBtn);
        orderNum = 100;
        itemIsSelected = false;
        isTender = false;
        endOrder = false;
 
        //Init Customer Info Components
        custName = "";     
 
        //Init Tender Components
        jPTender = new JPanel(new GridBagLayout());
        tenderC = new int[][] 
           {{0,0,1,1},{1,0,1,1},{2,0,1,1},
            {3,0,2,1},{5,0,2,1}, //Row 0
            {0,1,1,1},{1,1,1,1},{2,1,1,1},
            {3,1,2,1},{5,1,2,1}, //Row 1
            {0,2,1,1},{1,2,1,1},{2,2,1,1},
            {3,2,2,1},{5,2,2,1}, //Row 2
            {0,3,1,1},{1,3,1,1},{2,3,1,1},
            {3,3,2,1},{5,3,2,1}  //Row 3
        };
        decimalCounter = 0;
 
        credit = new Card();
        credit.process.addActionListener(this);
        credit.setVisible(false);
 
        //Init Order Array List
        myOrder = new ArrayList<>();
 
        changeLbl = new JLabel();
    }
 
    //Creates the Menu Bar
    private void createMenuBar()    
    {
        setJMenuBar(mainBar);
        mainBar.add(fileMenu);
        fileMenu.add(file_exit);
    }
 
    //Creates all the Menu Item Buttons
    private void createMenuBtns()
    {
        for (int i = 0; i < miBtnStr.length; i++)
        {
            miBtns[i] = new JButton("<html><p align=\"center\">" 
                    + miBtnStr[i].replaceAll("\n", "<br>") + "</p></html>");
            miBtns[i].setOpaque(true);
            miBtns[i].setBackground(Color.WHITE);
            miBtns[i].setPreferredSize(MENUBTND);
            miBtns[i].setFocusPainted(false);
            jPmiBtns.add(miBtns[i]); 
            miWasPressed[i] = false;
        }
 
        for (int i = 0; i < mcBtnStr.length; i++)
        {
            mcBtns[i] = new JButton("<html><p align=\"center\">" 
                    + mcBtnStr[i].replaceAll("\n", "<br>") + "</p></html>");
            mcBtns[i].setOpaque(true);
            mcBtns[i].setBackground(Color.WHITE);
            mcBtns[i].setPreferredSize(MENUBTND);
            mcBtns[i].setFocusPainted(false);
            jPmcBtns.add(mcBtns[i]); 
            mcWasPressed[i] = false;  
        }
    }
 
    //Create All the Tender Buttons
    private void createTenderBtns()
    {
        tenderSB = new StringBuilder();
        tenderTxt = new JTextField(20);
        tenderBtns = new JButton[tenderStr.length];
        for (int i = 0; i < tenderStr.length; i++)
        {
            tenderBtns[i] = new JButton(String.format("%s", tenderStr[i]));
            tenderBtns[i].setOpaque(true);
            tenderBtns[i].setBackground(Color.WHITE);
            tenderBtns[i].setEnabled(false);
            tenderBtns[i].setFocusPainted(false);
 
            if(i == 3 || i == 4 || i == 8 || i == 9 || i == 13 || i == 14 || i == 18 || i == 19)
                tenderBtns[i].setPreferredSize(TENDERQBTND);
            else 
                tenderBtns[i].setPreferredSize(TENDERBTND);
 
            c.gridx = tenderC[i][0];
            c.gridy = tenderC[i][1];
            c.gridwidth = tenderC[i][2];
            c.gridheight = tenderC[i][3];
            jPTender.add(tenderBtns[i], c);
        }
    }
 
    //Adds the Components to the Frame
    private void createFrame()
    {
        //Add Frame Components
        jPTop = new JPanel(new FlowLayout());
        jPTop.add(titleLbl);
        add(jPTop, BorderLayout.NORTH);
        jPCenter = new JPanel(new FlowLayout());
        add(jPCenter, BorderLayout.CENTER);
        jPBottom = new JPanel(new FlowLayout());
        jPBottom.add(new JLabel(COPYRIGHT)).setFont(copyFont);
        add(jPBottom, BorderLayout.SOUTH);
        jPOrder = new JPanel(new FlowLayout());
        jPOrder.add(orderSP);
        add(jPOrder, BorderLayout.EAST);
        orderTA.setFont(taFont);
        orderTA.setEditable(false);
 
        //Add Labels for Selected item
        jPSelect = new JPanel(new GridLayout(2,2));
        itemSelected.setEditable(false);
        modSelected.setEditable(false);
        jPSelect.add(new JLabel("Item Selected"));
        jPSelect.add(new JLabel("Modifier Selected"));
        jPSelect.add(itemSelected);
        jPSelect.add(modSelected);
 
        //Add Center Components
        jPCenter.add(jPSelect);
        jPCenter.add(jPmiBtns);
        jPmodLabel = new JPanel(new FlowLayout());
        jPmodLabel.add(new JLabel("Additional Modifiers"));
        jPCenter.add(jPmodLabel);
        jPCenter.add(jPmcBtns);
        jPCenter.add(jPATO);
        jPCenter.add(tenderTxt);
        tenderTxt.setEditable(false);
        jPCenter.add(jPTender);
 
    }
 
        //Adds Action Listeners to all Components
    private void addActionListeners()
    {
        file_exit.addActionListener(this);
 
        for (JButton miBtn : miBtns)
        {
            miBtn.addActionListener(this);
        }
        for (JButton mcBtn : mcBtns)
        {
            mcBtn.addActionListener(this);
        }
        atoBtn.addActionListener(this);
        totalBtn.addActionListener(this);
        clearBtn.addActionListener(this);
 
        for (JButton tenderBtn : tenderBtns)
        {
            tenderBtn.addActionListener(this);
        }
    }
 
    //Increments the order number by one
    //Clears the Order
    public void newOrder()
    {
        if(orderNum >= 199)
            orderNum = 100;
        else 
            orderNum++;
 
        clearOrder();
    }
 
    //Resets Variables and Creates new order
    private void clearOrder()
    {
        myOrder.clear();        
        custName = "";
        tenderSB.setLength(0);
        tenderTxt.setText("");
        isTender = false;
        dotWP = false;
        decimalCounter = 0;
        tenderAmt = 0;
        cashTendered = 0;
        creditTendered = 0;
 
        resetItemSelection();
        resetModSelection();
        resetTender();
        updateIS();
    }
 
    //Response to a Menu Item button being pressed
    private void itemWasPressed(int index)
    {   
 
        for (int i = 0; i < miWasPressed.length; i++)
        {
            if(i != index)
            {
                miWasPressed[i] = false;
                miBtns[i].setBackground(Color.WHITE);
            }
        }
 
        miWasPressed[index] = !miWasPressed[index];
 
        if (miWasPressed[index])
        {
            itemIsSelected = true;
            itemSelectStr = String.format("\n*%s ($%.2f)\n", miStr[index], miPrice[index]);
            itemSelected.setText(miStr[index]);
            miBtns[index].setBackground(Color.YELLOW);
        }
        else
        {
            itemIsSelected = false;
            itemSelectStr = "";
            itemSelected.setText("");
            miBtns[index].setBackground(Color.WHITE);
 
        }
        resetModSelection();
        updateIS();
        updateOrder();
    }
 
    //Response to a modifier button being pressed
    private void modWasPressed(int index)
    {
        if (index == 7)
        {
            resetModSelection();
            updateIS();
        } else
        {
            mcWasPressed[index] = !mcWasPressed[index];
 
            //Set Mod Selected Text
            if (mcWasPressed[index])
            {
                modSelected.setText(mcStr[index]);
                mcBtns[index].setBackground(Color.YELLOW);
            } else
            {
                modSelected.setText("");
                mcBtns[index].setBackground(Color.WHITE);
            }
        }
        modSelectStr = "";
 
        for (int i = 0; i < mcWasPressed.length - 1; i++)
        {
            if (mcWasPressed[i])
            {
 
                if (mcPrice[i] != 0)
                {
                    modSelectStr += String.format(" - add %s ($%.2f)\n", mcStr[i], mcPrice[i]);
                } else
                {
                    modSelectStr += String.format(" - add %s\n", mcStr[i]);
                }
            }
        }
 
        updateOrder();
    }
 
    //Response to the Total button being pressed
    private void totalWasPressed()
    {
        if(itemIsSelected)
            addToOrder();
 
        resetItemSelection();
        resetModSelection();
        resetTender();
        printMyOrder();
        printTotal();
    }
 
    /**
     * Response to a tender key being pressed
     * @param index relates to the button that was pressed
     */
    private void tenderKey(int index)
    {
        switch(index)
        {
            //Numeric Pad
            case 0:  //1
            case 1:  //2
            case 2:  //3
            case 5:  //4
            case 6:  //5
            case 7:  //6
            case 10: //7
            case 11: //8
            case 12: //9
            case 16: //0
            {
                if(dotWP)
                {
                    if(decimalCounter < 2)
                    {
                        tenderSB.append(tenderStr[index]);
                        decimalCounter++;
                    }
                }
                else
                    tenderSB.append(tenderStr[index]);
                break;
            }
 
            case 15: //Dot
            {
                if(!dotWP)
                {
                    tenderSB.append(tenderStr[index]);
                    dotWP = true;
                }
                break;
            } 
 
            //Tender Quick Actions
            case 3: {
                tenderAmt = gTotal;
                printMyOrder();
                printTotal();
                tenderOrder();
                break;
            } //Exact Cash
            case 4: {
                tenderAmt = Math.ceil(gTotal);
                printMyOrder();
                printTotal();
                tenderOrder();
                break;
            }  //Next Dollar
            case 8: {
                tenderAmt = 5;
                if ((cashTendered % 5) != 0)
                    cashTendered = 0;
                tenderOrder();
                break; 
            } //$5
            case 9: {
                tenderAmt = 10;
                if ((cashTendered % 10) != 0)
                    cashTendered = 0;
                tenderOrder();
                break; 
            } //$10
            case 13: {
                tenderAmt = 20;
                if ((cashTendered % 20) != 0)
                    cashTendered = 0;
                tenderOrder();
                break; 
            } //$20
            case 14: {
                tenderAmt = 50;
                if ((cashTendered % 50) != 0)
                    cashTendered = 0;
                tenderOrder();
                break; 
            } //$50
 
            case 17: {
                dotWP = false;
                decimalCounter = 0;
                tenderSB.setLength(0);
                tenderAmt = 0;
                cashTendered = 0;
                break;
            } //Clear
            case 18: {
                try
                {
                    tenderAmt = Double.parseDouble(tenderSB.toString());
 
                }
                catch(NumberFormatException error)
                {
                    tenderAmt = 0;
                }
 
                tenderOrder();
                break;
            } //Enter
            case 19: {
                credit.setVisible(true);
                break;
            } //Credit (Work in Progess)
            default: break; 
        }
 
        tenderTxt.setText(tenderSB.toString());
    }
 
    /**
     * Uses String Builder to append item being worked on to the Text Area
     */
    private void updateOrder()
    {
        printMyOrder();
        updateSB.setLength(0);
        updateSB.append(itemSelectStr);
        updateSB.append(modSelectStr);
        orderSB.append(updateSB);
        printTotal();
    }
 
    /**
     * Enables and Disables Tender Buttons and Menu Item Buttons
     */
    private void updateIS()
    {
        if (itemIsSelected)
        {
            for (JButton mcBtn : mcBtns)
            {
                mcBtn.setEnabled(true);
            }
            atoBtn.setEnabled(true);
            totalBtn.setEnabled(true);
            clearBtn.setEnabled(true);
        } else if (!itemIsSelected && myOrder.isEmpty())
        {
            for (int i = 0; i < mcBtns.length; i++)
            {
                mcBtns[i].setBackground(Color.WHITE);
                mcBtns[i].setEnabled(false);
                mcWasPressed[i] = false;
            }
            atoBtn.setEnabled(false);
            totalBtn.setEnabled(false);
            clearBtn.setEnabled(false);
 
        } else
        {
            for (int i = 0; i < mcBtns.length; i++)
            {
                mcBtns[i].setBackground(Color.WHITE);
                mcBtns[i].setEnabled(false);
                mcWasPressed[i] = false;
            }
            atoBtn.setEnabled(false);
        }
    }
 
    //Creates a new Order Object based on what buttons were pressed.
    private void addToOrder()
    {
        if (itemIsSelected)
        {
            String in = "";
            double ip = 0;
            for (int i = 0; i < miWasPressed.length; i++)
            {
                if (miWasPressed[i])
                {
                    in = miStr[i];
                    ip = miPrice[i];
                }
            }
 
            int modLength = 0;
            for (boolean mc : mcWasPressed)
            {
                if (mc)
                {
                    modLength++;
                }
            }
            String[] mod = new String[modLength];
            double[] mp = new double[modLength];
 
            int mI = 0;
            for (int i = 0; i < mcWasPressed.length; i++)
            {
                if (mcWasPressed[i])
                {
                    mod[mI] = mcStr[i];
                    mI++;
                }
            }
 
            mI = 0;
            for (int i = 0; i < mcWasPressed.length; i++)
            {
                if (mcWasPressed[i])
                {
                    mp[mI] = mcPrice[i];
                    mI++;
                }
            }
 
            myOrder.add(new OrderItem(in, ip, mod, mp));
        }
 
        printMyOrder();
        printTotal();
        resetItemSelection();
        resetModSelection();
        updateIS();
    }
 
    /**
     * Regenerates the order StringBuilder from the myOrder Array
     * Calculates the sub Total of the Order and sets it to the Text Area
     */
    private void printMyOrder()
    {
        subTotal = 0;
        gTotal = 0;
        orderSB.setLength(0);
 
        //Adds order number to receipt
        orderSB.append(String.format("Order Number: %d\n", orderNum));
 
        if(custName != "") //Adds customer name to receipt
            orderSB.append(String.format("Customer: %s\n", custName));
 
        if(!myOrder.isEmpty())
        {
            myOrder.forEach((oi) ->
            {
                String in = oi.getItemName();
                double ip = oi.getItemPrice();
                String[] mod = oi.getModifers();
                double[] mp = oi.getModPrice();
                subTotal += oi.getPrice();
 
                orderSB.append(String.format("\n%s ($%.2f)\n", in, ip));
                for (int i = 0; i < mod.length; i++)
                {
                    if (mp[i] != 0)
                    {
                        orderSB.append(String.format(" - add %s ($%.2f)\n", mod[i], mp[i]));
                    } else
                    {
                        orderSB.append(String.format(" - add %s\n", mod[i]));
                    }
                }
            });
        }
    }
 
    /**
     * Calculates the Sales Tax and Order Total 
     * and adds it to the order Text Area
     */
    private void printTotal()
    {
        salesTax = subTotal * TAX;
        gTotal = subTotal + salesTax;
 
        if(subTotal != 0)
        {
            orderSB.append(String.format("\nSub Total:\t\t$%.2f", subTotal));
            orderSB.append(String.format("\nSales Tax:\t\t$%.2f\n", salesTax));
            orderSB.append(String.format("\nTotal:    \t\t$%.2f\n", gTotal));
        }
 
        myOrderStr = orderSB.toString();
        orderTA.setText(myOrderStr);
    }
 
    /**
     * Prompts the user to enter a Customer name
     * If none is provided the user is put back to edit order
     * A name is required to tender
     */
    private void promptCustName()
    {
        custName = JOptionPane.showInputDialog(null, "Customer Name: ");
        if(custName.isEmpty())
        {
            custName = "";
            isTender = true;
        }
        else
        {
            updateOrder();
        }
    }
 
    /**
     * Processes the amount tendered and calculates the appropriate response
     */
    private void tenderOrder()
    {
 
        if(credit.isValid)
        {
            creditTendered = gTotal - cashTendered;
 
            if(cashTendered > 0)
                orderSB.append(String.format("\nCash Tendered:\t\t$%.2f\n", cashTendered));
 
            orderSB.append(String.format("\nPAID: Credit (Approved) $%.2f\n", creditTendered));
            orderSB.append("Card Type: ").append(credit.getVendor()).append("\n");
            orderSB.append("Account #: ").append(credit.getCardNumber()).append("\n");
            orderSB.append("\n----------------------------------\n");
            orderTA.setText(orderSB.toString());
            credit.resetCard();
            writeOrderToFile();
            newOrder();
        } else 
        {
            cashTendered += tenderAmt;
 
            if (cashTendered >= gTotal)
            {
                change = gTotal - cashTendered;
 
                //Inverts negative if change is not zero
                if (change == 0)
                {
                    change = 0;
                } else
                {
                    change = -(change);
                }
 
                orderSB.append(String.format("\nCash Tendered:\t\t$%.2f\n", cashTendered));
                orderSB.append(String.format("Change:\t\t\t$%.2f"
                        + "\n----------------------------------\n", change));
                myOrderStr = orderSB.toString();
                orderTA.setText(myOrderStr);
                orderTA.append("end of order");
                writeOrderToFile();
                displayChange();
                newOrder();
            }
            else
            {
                orderTA.append(String.format("\nCash:\t\t\t$%.2f", -(tenderAmt)));
                dotWP = false;
                decimalCounter = 0;
                tenderSB.setLength(0);
            }
        }
    }
 
    /**
     * Displays a dialog with the Change
     */
    private void displayChange()
    {
        changeStr = String.format("Cash Tendered: $%.2f\nChange: $%.2f", cashTendered, change);
        changeLbl.setText("<html><p align=\"right\">" 
                    + changeStr.replaceAll("\n", "<br>") + "</p></html>");
        changeLbl.setFont(changeFont);
        JOptionPane.showMessageDialog(null, changeLbl);
    }
 
    /**
     * Writes the receipt to file and console 
     */
    private void writeOrderToFile()
    {
        orderSB.insert(0, "\n" + TITLE + "\n\n");
        myOrderStr = orderSB.toString();
        try{
            try (OutputStream output = new BufferedOutputStream(Files.newOutputStream(file, CREATE, APPEND)))
            {
                try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output)))
                {
                    writer.write(myOrderStr, 0, myOrderStr.length());
                    writer.newLine();
                }
                output.flush();
            }
        } catch (IOException e)
        {
            System.out.println("Message: " + e);
        }
        System.out.println(orderSB.toString());
    }
 
    /**
     * Resets the Menu Item selection components
     */
    private void resetItemSelection()
    {
        for (int i = 0; i < miWasPressed.length; i++)
        {
            miWasPressed[i] = false;
        }
        for (JButton miB : miBtns)
        {
            miB.setBackground(Color.WHITE);
        }
 
        itemSelected.setText("");
        itemSelectStr = "";
        itemIsSelected = false;
    }
 
    /**
     * Resets the Modifier components
     */
    private void resetModSelection()
    {
 
        for (int i = 0; i < mcWasPressed.length; i++)
        {
            mcWasPressed[i] = false;
        }
        for (JButton mcB : mcBtns)
        {
            mcB.setBackground(Color.WHITE);
        }
        modSelected.setText("");
        modSelectStr = "";
    }
 
    /**
     * Resets the tender and order components based on which is needed
     */
    private void resetTender()
    {
        for (JButton miBtn : miBtns)
        {
            miBtn.setEnabled(!isTender);
        }
        for (JButton tenderBtn : tenderBtns)
        {
            tenderBtn.setEnabled(isTender);
        }
        if(isTender)
        {
            totalBtn.setText("Edit Order");
            tenderSB.setLength(0);
            dotWP = false;
        }
        else
            totalBtn.setText("Total");
    }
 
    @Override
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
 
        if(source == file_exit)
            System.exit(0);
 
        if(source == miBtns[0])
        {
            itemWasPressed(0);
        }
        if(source == miBtns[1])
        {
            itemWasPressed(1);
        }
        if(source == miBtns[2])
        {
            itemWasPressed(2);
        }
        if(source == miBtns[3])
        {
            itemWasPressed(3);
        }
        if(source == mcBtns[0])
        {
            modWasPressed(0);
        }
        if(source == mcBtns[1])
        {
            modWasPressed(1);
        }
        if(source == mcBtns[2])
        {
            modWasPressed(2);
        }
        if(source == mcBtns[3])
        {
            modWasPressed(3);
        }
        if(source == mcBtns[4])
        {
            modWasPressed(4);
        }
        if(source == mcBtns[5])
        {
            modWasPressed(5);
        }
        if(source == mcBtns[6])
        {
            modWasPressed(6);
        }
        if(source == mcBtns[7])
        {
            modWasPressed(7);
        }
        if(source == atoBtn)
        {
            addToOrder();
        }
        if(source == totalBtn)
        {
            if(custName == "")
                promptCustName();
            isTender = !isTender;
            totalWasPressed();
        }
        if(source == clearBtn)
        {
            clearOrder();
            printMyOrder();
            printTotal();
        }
        if(source == tenderBtns[0]) //1
            tenderKey(0);
        if(source == tenderBtns[1]) //2
            tenderKey(1);
        if(source == tenderBtns[2]) //3
            tenderKey(2);
        if(source == tenderBtns[3]) //Exact Cash
            tenderKey(3);
        if(source == tenderBtns[4]) //Next Dollar
            tenderKey(4);
        if(source == tenderBtns[5]) //4
            tenderKey(5);
        if(source == tenderBtns[6]) //5
            tenderKey(6);
        if(source == tenderBtns[7]) //6
            tenderKey(7);
        if(source == tenderBtns[8]) //$5
            tenderKey(8);
        if(source == tenderBtns[9]) //$10
            tenderKey(9);
        if(source == tenderBtns[10]) //7
            tenderKey(10);
        if(source == tenderBtns[11]) //8
            tenderKey(11);
        if(source == tenderBtns[12]) //9
            tenderKey(12);
        if(source == tenderBtns[13]) //$20
            tenderKey(13);
        if(source == tenderBtns[14]) //$50
            tenderKey(14);
        if(source == tenderBtns[15]) //Dot
            tenderKey(15);
        if(source == tenderBtns[16]) //0
            tenderKey(16);
        if(source == tenderBtns[17]) //Clear
            tenderKey(17);
        if(source == tenderBtns[18]) //Enter
            tenderKey(18);
        if(source == tenderBtns[19]) //Credit
            tenderKey(19);
        if(source == credit.process)
        {
            credit.setVisible(false);
            tenderOrder();
        }
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        HotDogHouse dog = new HotDogHouse();
    }
}

Card.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Mark Hesser
 * HesserCAN 
 * [email protected]
 * www.hessercan.com
 */
 
package hotdoghouse;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
 
/**
 * @author mark
 */
public class Card extends JFrame implements ActionListener
{
    // <editor-fold defaultstate="collapsed" desc="Delcarations">
    private final int CWIDTH = 500;
    private final int CHEIGHT = 300;
    private String cardType, name, cardNum, expDate, cvv, zipcode;
 
    private JPanel pane;
    private final String[] labelStr;
 
    private JLabel[] labels;
    private JTextField[] text;
    private JButton charge;
    public JButton process;
 
    public boolean isValid = false;
 
    GridBagConstraints c;
    private int[][] paneC;
    // </editor-fold>
 
    /**
     * Default Constructor
     */
    public Card()
    {
        setDefaultCloseOperation(HIDE_ON_CLOSE);
 
        setSize(CWIDTH, CHEIGHT);
        setResizable(false);
 
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenH = screenSize.height;
        int screenW = screenSize.width;
 
        setLocationRelativeTo(null);
 
        labelStr = new String[]
                {"Name: ", "CC Number: ", "Exp Date: ", "CVV: ", "Zipcode: "};
 
        initCredit(); 
    }
 
    /**
     * Initializes Components
     */
    private void initCredit()
    {
        pane = new JPanel(new GridBagLayout());
        paneC = new int [][]
            {{0,0,1,1},
             {0,1,1,1},
             {0,2,1,1},
             {0,3,1,1},
             {0,4,1,1},
             {1,5,4,2}
            };
        c = new GridBagConstraints();
 
        labels = new JLabel[labelStr.length];
        text = new JTextField[labelStr.length];
        c.fill = GridBagConstraints.NONE;
        for (int i = 0; i < labelStr.length; i++)
        {
            labels[i] = new JLabel(labelStr[i]);
            text[i] = new JTextField(16);
 
            c.anchor = GridBagConstraints.EAST;
            c.gridx = paneC[i][0];
            c.gridy = paneC[i][1];
            c.gridwidth = paneC[i][2];
            c.gridheight = paneC[i][3];
            pane.add(labels[i], c);
            c.anchor = GridBagConstraints.WEST;
            c.gridx = paneC[i][0] + 1;
            c.gridy = paneC[i][1];
            c.gridwidth = paneC[i][2] + 4;
            c.gridheight = paneC[i][3];
            pane.add(text[i],c);
        }
        charge = new JButton("Charge");
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.CENTER;
        c.gridx = paneC[5][0];
        c.gridy = paneC[5][1];
        c.gridwidth = paneC[5][2];
        c.gridheight = paneC[5][3];
        charge.setPreferredSize(new Dimension(150, 50));
        charge.setFocusPainted(false);
        pane.add(charge, c);
 
        process = new JButton("Process");
        charge.addActionListener(this);
 
        add(process);
        process.setVisible(false);
        add(pane);
 
    }
 
    /**
     * Processes the Card
     */
    private void processCharge()
    {
        try
        {
            if (!text[1].getText().isEmpty())
            {
                if (isValid(text[1].getText()))
                {
                    name = text[0].getText();
                    cardNum = text[1].getText();
                    cardType = detectCType(cardNum);
                    expDate = text[2].getText();
                    cvv = text[3].getText();
                    zipcode = text[4].getText();
 
                    if(name.isEmpty() || expDate.isEmpty() || cvv.isEmpty())
                    {
                        JOptionPane.showMessageDialog(null, "Please Fill out all required fields", "Declined", 1);
                    } else
                    {
                        if(checkDate(expDate))
                            isValid = true;
                        else
                        {
                            JOptionPane.showMessageDialog(null, "The Customers Card is Expired.", "Declined", 1);
                        }
                    }
                } else
                {
                    JOptionPane.showMessageDialog(null, "The Customers Card has been Declined.", "Declined", 2);
                }
            }
            else
                {
                    JOptionPane.showMessageDialog(null, "Invalid Card Number", "Error", 2);
                }
        }
        catch (Exception error)
        {
            JOptionPane.showMessageDialog(null, "An Error has Occured", "Error", 2);
        }
    }
 
    /**
     * Returns the Card Type as String
     * @return Card Type
     */
    public String getVendor()
    {
        return cardType;
    }
 
    /**
     * Returns Masked Card Number as String
     * @return Masked Card Number
     */
    public String getCardNumber()
    {
        char[] cc = cardNum.toCharArray();
        return "XXXX XXXX XXXX " + cc[12] + cc[13] + cc[14] + cc[15];
    }
 
    /**
     * Resets Card Components
     */
    public void resetCard()
    {
        for(JTextField t : text)
        {
            t.setText("");
        }
        text[0].requestFocus();
        isValid = false;
        setVisible(false);
    }
 
    /**
    * Validates Card Numbers
    * @param cc Card Number
    * @return isValid boolean
    */
    public boolean isValid(String cc)
    {
        int sum = 0;
        boolean alternate = false;
        for (int i = cc.length() - 1; i >= 0; i--)
        {
            int n = Integer.parseInt(cc.substring(i, i + 1));
            if (alternate)
            {
                n *= 2;
                if (n > 9)
                {
                    n = (n % 10) + 1;
                }
            }
            sum += n;
            alternate = !alternate;
        }
        return (sum % 10 == 0);
    }
 
    /**
     * Compares the Date to today and returns true if the date is not expired
     * @param date Expiration Date
     * @return Date Expiration Status (true/false)
     */
    public boolean checkDate(String date)
    {
        DateTimeFormatter MMyy = DateTimeFormatter.ofPattern("MMyy");
        DateTimeFormatter MMyyyy = DateTimeFormatter.ofPattern("MMyyyy");
        LocalDate now = LocalDate.now();
        YearMonth today = YearMonth.parse(now.format(MMyy), MMyy);
 
        boolean dateOK = false;
        try
        {
            YearMonth expiry = YearMonth.parse(date.replaceAll("/", ""), MMyy);
            if (expiry.getMonthValue() >= today.getMonthValue() && expiry.getYear() >= today.getYear())
            {
                dateOK = true;
            }
 
        } catch (Exception e1)
        {
            try
            {
                YearMonth expiry = YearMonth.parse(date.replaceAll("/", ""), MMyyyy);
                if (expiry.getMonthValue() >= today.getMonthValue() && expiry.getYear() >= today.getYear())
                {
                    dateOK = true;
                }
            }
            catch (Exception e2)
            {
 
            }
        }    
        return dateOK;
    }
 
    /**
    * Automatically Detects the Card Type
    * and returns it as a String
    * @param cc Card Number
    */
    private String detectCType(String cc)
    {
        if (cc.startsWith("4"))
            return "Visa";
        else if (cc.startsWith("5"))
            return "MasterCard";
        else if (cc.startsWith("6"))
            return "Discover";
        else if (cc.startsWith("37"))
            return "American Express";
        else
            return "UNKNOWN";
    }
 
    @Override
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
 
        if(source == charge)
        {
            processCharge();
            if(isValid)
                process.doClick();
        }
    }
}

OrderItem.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Mark Hesser
 * HesserCAN 
 * [email protected]
 * www.hessercan.com
 */
 
package hotdoghouse;
 
/**
 * @author mark
 */
public class OrderItem 
{
    private String itemName;
    private double itemPrice;
    private String[] modifiers;
    private double[] modPrice;
    private double totalPrice;
 
    /**
     *
     * @param in Item Name
     * @param ip Item Price
     * @param mod Modifiers
     * @param mp Modifiers Price
     */
    public OrderItem(String in, double ip, String[] mod, double[] mp)
    {
        itemName = in;
        itemPrice = ip;
        modifiers = mod;
        modPrice = mp;
 
        setTotalPrice();
    }
 
    //sets the total price of the item
    private void setTotalPrice()
    {
        totalPrice = 0;
        totalPrice += itemPrice;
        for (double mp : modPrice)
        {
            totalPrice += mp;
        }
    }
 
    //Returns the total Price of the Item
    public double getPrice()
    {
        return totalPrice;
    }
 
    //Returns the Name of the Item
    public String getItemName()
    {
        return itemName;
    }
 
    //Returns the Price of the Item
    public double getItemPrice()
    {
        return itemPrice;
    }
 
    //Returns the Array of Modifiers
    public String[] getModifers()
    {
        return modifiers;
    }
 
    //Returns the Array of Modifier Prices
    public double[] getModPrice()
    {
        return modPrice;
    }
}