2323import java .awt .GridBagLayout ;
2424import java .awt .GridLayout ;
2525import java .awt .Insets ;
26- import java .awt .event .ActionEvent ;
2726import java .awt .event .InputEvent ;
2827import java .awt .event .KeyEvent ;
2928import java .io .File ;
3029import java .text .NumberFormat ;
3130import java .util .Currency ;
3231import java .util .Locale ;
32+ import java .util .Objects ;
3333
3434import javax .swing .ImageIcon ;
3535import javax .swing .JButton ;
@@ -89,29 +89,28 @@ public static void main(String[] args) {
8989 }
9090
9191 private JPanel contentPane ;
92- private UsersPanel comptesP ;
92+ private UsersPanel accountsP ;
9393 private TransactionsPanel transactionsP ;
9494 private ArticlesPanel articlesP ;
9595 /** THE textfield for everything */
9696 private JTextField saisieField ;
9797 /** user name */
98- private JLabel nomLabel ;
98+ private JLabel lblName ;
9999 /** user balance */
100- private JLabel soldeLabel ;
100+ private JLabel lblBalance ;
101101 /** name of cash assistant on duty */
102102 private JLabel lblVendorName ;
103103 /** text of the ticket */
104104 private JTextArea ticketTextArea ;
105105 private JLabel taskToDoLabel ;
106- private JLabel enCours ;
106+ private JLabel lblInProgress ;
107107
108108 /** Stock alerts */
109109 private JTextArea alertTextArea ;
110110 private JLabel lblTotalTicket ;
111- private JLabel lblSoldeApres ;
111+ private JLabel lblBalanceAfter ;
112112
113113 private JFileChooser fileChooser ;
114- public Locale locale ;
115114
116115 public Currency currency ;
117116 private Ticket currentTicket ;
@@ -132,8 +131,8 @@ public static void main(String[] args) {
132131 */
133132 public MainGUI2 () {
134133 // Localization
135- locale = Locale .getDefault ();
136- currency = Currency .getInstance (locale );
134+ setLocale ( Locale .getDefault () );
135+ currency = Currency .getInstance (getLocale () );
137136
138137 // Database filling
139138 fillTheDB ();
@@ -219,15 +218,15 @@ public MainGUI2() {
219218 mnExport .add (mntmExportUsersLegacy );
220219
221220 JMenuItem mntmExportUsersCSV = new JMenuItem ("Users to CSV" , convertIcon );
222- mntmExportUsersCSV .addActionListener (( e ) -> exportCSV (users ));
221+ mntmExportUsersCSV .addActionListener (e -> exportCSV (users ));
223222 mnExport .add (mntmExportUsersCSV );
224223
225224 JMenuItem mntmExportArticlesCSV = new JMenuItem ("Articles to CSV" , convertIcon );
226- mntmExportArticlesCSV .addActionListener (( e ) -> exportCSV (catalogue ));
225+ mntmExportArticlesCSV .addActionListener (e -> exportCSV (catalogue ));
227226 mnExport .add (mntmExportArticlesCSV );
228227
229228 JMenuItem mntmExportTransCSV = new JMenuItem ("Transactions to CSV" , convertIcon );
230- mntmExportTransCSV .addActionListener (( e ) -> exportCSV (journal ));
229+ mntmExportTransCSV .addActionListener (e -> exportCSV (journal ));
231230 mnExport .add (mntmExportTransCSV );
232231
233232 JMenu mnHelp = new JMenu ("Help" );
@@ -297,13 +296,13 @@ public MainGUI2() {
297296 gbc_lblN .gridy = 0 ;
298297 panel .add (lblN , gbc_lblN );
299298
300- nomLabel = new JLabel ("no user selected" );
301- nomLabel .setFont (new Font ("Tahoma" , Font .BOLD , 14 ));
299+ lblName = new JLabel ("no user selected" );
300+ lblName .setFont (new Font ("Tahoma" , Font .BOLD , 14 ));
302301 GridBagConstraints gbc_nomLabel = new GridBagConstraints ();
303302 gbc_nomLabel .insets = new Insets (0 , 0 , 5 , 5 );
304303 gbc_nomLabel .gridx = 1 ;
305304 gbc_nomLabel .gridy = 0 ;
306- panel .add (nomLabel , gbc_nomLabel );
305+ panel .add (lblName , gbc_nomLabel );
307306
308307 JButton chooser = new JButton ("Select" );
309308 chooser .setToolTipText ("Select user" );
@@ -319,8 +318,8 @@ public MainGUI2() {
319318 currentUserAtDesk = nextCUser ;
320319
321320 if (currentUserAtDesk != null ) {
322- nomLabel .setText (currentUserAtDesk .getNameWithPromo ());
323- soldeLabel .setText (NumberFormat .getCurrencyInstance ().format (currentUserAtDesk .getBalance ()));
321+ lblName .setText (currentUserAtDesk .getNameWithPromo ());
322+ lblBalance .setText (NumberFormat .getCurrencyInstance ().format (currentUserAtDesk .getBalance ()));
324323
325324 if (currentTicket == null ) {
326325 currentTicket = new Ticket (currentUserAtDesk );
@@ -344,8 +343,8 @@ public MainGUI2() {
344343 btnX .addActionListener (arg0 -> {
345344 currentUserAtDesk = null ;
346345
347- nomLabel .setText ("default (00)" );
348- soldeLabel .setText (NumberFormat .getCurrencyInstance ().format (0.00 ));
346+ lblName .setText ("default (00)" );
347+ lblBalance .setText (NumberFormat .getCurrencyInstance ().format (0.00 ));
349348
350349 if (currentTicket == null ) {
351350 currentTicket = new Ticket (currentUserAtDesk );
@@ -366,13 +365,13 @@ public MainGUI2() {
366365 gbc_lblSolde .gridy = 1 ;
367366 panel .add (lblSolde , gbc_lblSolde );
368367
369- soldeLabel = new JLabel ("--" );
370- soldeLabel .setFont (new Font ("Tahoma" , Font .BOLD , 14 ));
368+ lblBalance = new JLabel ("--" );
369+ lblBalance .setFont (new Font ("Tahoma" , Font .BOLD , 14 ));
371370 GridBagConstraints gbc_soldeLabel = new GridBagConstraints ();
372371 gbc_soldeLabel .insets = new Insets (0 , 0 , 5 , 5 );
373372 gbc_soldeLabel .gridx = 1 ;
374373 gbc_soldeLabel .gridy = 1 ;
375- panel .add (soldeLabel , gbc_soldeLabel );
374+ panel .add (lblBalance , gbc_soldeLabel );
376375
377376 JLabel lblVendor = new JLabel ("Vendor:" );
378377 GridBagConstraints gbc_lblVendor = new GridBagConstraints ();
@@ -405,14 +404,14 @@ public MainGUI2() {
405404 taskToDoLabel .setHorizontalAlignment (SwingConstants .CENTER );
406405 panel_2 .add (taskToDoLabel );
407406
408- enCours = new JLabel ("" );
409- enCours .setHorizontalAlignment (SwingConstants .CENTER );
410- panel_2 .add (enCours );
407+ lblInProgress = new JLabel ("" );
408+ lblInProgress .setHorizontalAlignment (SwingConstants .CENTER );
409+ panel_2 .add (lblInProgress );
411410
412411 saisieField = new JTextField ();
413412 saisieField .setHorizontalAlignment (SwingConstants .RIGHT );
414413 saisieField .setFont (new Font ("Tahoma" , Font .BOLD , 20 ));
415- saisieField .addActionListener (( ActionEvent e ) -> {
414+ saisieField .addActionListener (e -> {
416415 inputLogic ();
417416 saisieField .setText (null );
418417 });
@@ -458,7 +457,7 @@ public void removeUpdate(DocumentEvent e) {
458457 if (currentState == TicketState .TICKET_IDLE ) {
459458 currentArticleAtDesk = c ;
460459 currentItemAtDesk = new TicketItem (c );
461- enCours .setText ("? x " + c .getName ());
460+ lblInProgress .setText ("? x " + c .getName ());
462461
463462 if (c .getNumberOfPrices () > 1 ) {
464463 taskToDoLabel .setText ("Select fee" );
@@ -467,7 +466,7 @@ public void removeUpdate(DocumentEvent e) {
467466 // Only 1 price id, quantity to set
468467 taskToDoLabel .setText ("Select quantity" );
469468 currentState = TicketState .QUANTITY ;
470- enCours .setText ("? x " + currentArticleAtDesk .getName ());
469+ lblInProgress .setText ("? x " + currentArticleAtDesk .getName ());
471470 } else {
472471 // Only 1 price id, no quantity to set: add item to ticket
473472 currentItemAtDesk .computeAmount ();
@@ -507,18 +506,18 @@ public void removeUpdate(DocumentEvent e) {
507506 String nbAsText = String .valueOf (i % 10 );
508507 JButton btn = new JButton (nbAsText );
509508 btn .setMnemonic (nbAsText .charAt (0 ));
510- btn .addActionListener (( arg0 ) -> saisieField .setText (saisieField .getText () + nbAsText ));
509+ btn .addActionListener (arg0 -> saisieField .setText (saisieField .getText () + nbAsText ));
511510 panel_pavenum .add (btn );
512511 }
513512
514513 JButton btnMetre = new JButton ("Dozen" );
515514 btnMetre .setToolTipText ("12 units" );
516- btnMetre .addActionListener (( ActionEvent arg0 ) -> saisieField .setText (String .valueOf (12 )));
515+ btnMetre .addActionListener (arg0 -> saisieField .setText (String .valueOf (12 )));
517516 panel_pavenum .add (btnMetre );
518517
519518 JButton btnClear = new JButton ("Clear" );
520519 btnClear .setToolTipText ("Clear any input (not the ticket!)" );
521- btnClear .addActionListener (( ActionEvent arg0 ) -> saisieField .setText (null ));
520+ btnClear .addActionListener (arg0 -> saisieField .setText (null ));
522521 panel_pavenum .add (btnClear );
523522
524523 JPanel panel_3 = new JPanel ();
@@ -558,13 +557,13 @@ public void removeUpdate(DocumentEvent e) {
558557 gbc_lblSoldeApresText .gridy = 1 ;
559558 panel_3 .add (lblSoldeApresText , gbc_lblSoldeApresText );
560559
561- lblSoldeApres = new JLabel ();
560+ lblBalanceAfter = new JLabel ();
562561 GridBagConstraints gbc_lblSoldeApres = new GridBagConstraints ();
563562 gbc_lblSoldeApres .insets = new Insets (0 , 0 , 5 , 5 );
564563 gbc_lblSoldeApres .fill = GridBagConstraints .BOTH ;
565564 gbc_lblSoldeApres .gridx = 1 ;
566565 gbc_lblSoldeApres .gridy = 1 ;
567- panel_3 .add (lblSoldeApres , gbc_lblSoldeApres );
566+ panel_3 .add (lblBalanceAfter , gbc_lblSoldeApres );
568567
569568 JPanel panel_PaymentMethods = new JPanel ();
570569 panel_PaymentMethods .setBorder (
@@ -611,8 +610,8 @@ public void removeUpdate(DocumentEvent e) {
611610 // ********************************************************************************************
612611 // ********************************************************************************************
613612 // ********************************************************************************************
614- comptesP = new UsersPanel (users );
615- tabbedPane .addTab ("Accounts" , null , comptesP , "Accounts management" );
613+ accountsP = new UsersPanel (users );
614+ tabbedPane .addTab ("Accounts" , null , accountsP , "Accounts management" );
616615
617616 // ********************************************************************************************
618617 // ********************************************************************************************
@@ -686,20 +685,20 @@ public void finalizeTicket() {
686685 // Clear GUI
687686 currentTicket = null ;
688687 taskToDoLabel .setText ("Choose user" );
689- enCours .setText (null );
688+ lblInProgress .setText (null );
690689 ticketTextArea .setText (null );
691690 lblTotalTicket .setText (null );
692- lblSoldeApres .setText (null );
691+ lblBalanceAfter .setText (null );
693692 currentUserAtDesk = null ;
694693 currentItemAtDesk = null ;
695- nomLabel .setText ("no user selected" );
696- soldeLabel .setText ("--" );
694+ lblName .setText ("no user selected" );
695+ lblBalance .setText ("--" );
697696
698697 // Change current state
699698 currentState = TicketState .IDLE ;
700699
701700 // Refresh JTables
702- comptesP .refreshTable ();
701+ accountsP .refreshTable ();
703702 transactionsP .refreshTable ();
704703 articlesP .refreshTable ();
705704 }
@@ -709,7 +708,7 @@ public void finalizeTicket() {
709708 */
710709 private void finalizeTicketItem () {
711710 currentTicket .addArticle (currentItemAtDesk );
712- enCours .setText (String .valueOf (currentItemAtDesk .getQuantity ()) + " x " + currentArticleAtDesk .getName ());
711+ lblInProgress .setText (String .valueOf (currentItemAtDesk .getQuantity ()) + " x " + currentArticleAtDesk .getName ());
713712 currentItemAtDesk = null ;
714713 currentArticleAtDesk = null ;
715714 printTicketToScreen (currentTicket );
@@ -737,12 +736,12 @@ private void inputLogic() {
737736 // More than 1 price id
738737 taskToDoLabel .setText ("Select fee" );
739738 currentState = TicketState .PRICE ;
740- enCours .setText ("? x " + currentArticleAtDesk .getName ());
739+ lblInProgress .setText ("? x " + currentArticleAtDesk .getName ());
741740 } else if (!currentArticleAtDesk .isCountable ()) {
742741 // Only 1 price id, quantity to set
743742 taskToDoLabel .setText ("Select quantity" );
744743 currentState = TicketState .QUANTITY ;
745- enCours .setText ("? x " + currentArticleAtDesk .getName ());
744+ lblInProgress .setText ("? x " + currentArticleAtDesk .getName ());
746745 } else {
747746 // Only 1 price id, no quantity to set: add item to ticket
748747 currentItemAtDesk .computeAmount ();
@@ -815,7 +814,7 @@ private void inputLogic() {
815814 }
816815 } catch (NumberFormatException nfe ) {
817816 JOptionPane .showMessageDialog (null , "Invalid input! Cannot parse a number." , "Error" ,
818- JOptionPane .WARNING_MESSAGE );
817+ JOptionPane .ERROR_MESSAGE );
819818 nfe .printStackTrace ();
820819 }
821820 }
@@ -840,9 +839,9 @@ private void printTicketToScreen(Ticket t) {
840839
841840 // Same for balance after purchase
842841 if (currentUserAtDesk != null ) {
843- lblSoldeApres .setText (NumberFormat .getCurrencyInstance ().format (t .getBalanceAfterTicket ()));
842+ lblBalanceAfter .setText (NumberFormat .getCurrencyInstance ().format (t .getBalanceAfterTicket ()));
844843 } else {
845- lblSoldeApres .setText ("--" );
844+ lblBalanceAfter .setText ("--" );
846845 }
847846 }
848847 }
@@ -856,27 +855,28 @@ public void setCurrentVendor(Vendor currentVendor) {
856855 }
857856
858857 /**
859- * Refreshes the component containing the stock alerts TODO: partly rewrite for
860- * optimization
858+ * Refreshes the component containing the stock alerts
861859 */
862860 public void updateStockAlerts () {
863861 alertTextArea .setText (null ); // Clean UI component
864- if (catalogue != null ) {
862+ StringBuilder bob = new StringBuilder ();
863+
864+ if (Objects .nonNull (catalogue )) {
865865 for (Article a : catalogue .getArray ()) {
866866 if (a != null && a .hasStockMgmtEnabled () && a .hasStockAlertEnabled ()) {
867867 int stock = a .getStock ();
868868 if (stock <= a .getLimitStock ()) {
869- alertTextArea . setText ( alertTextArea . getText () + " \n There are only "
870- + String .valueOf ( a . getStock ()) + " " + a . getName () + " left! Consider refilling" );
869+ bob . append (
870+ String .format ( " \n There are only %d %s left! Consider refilling", stock , a . getName ()) );
871871 if (stock < 0 ) {
872- alertTextArea .setText (
873- alertTextArea .getText () + " or disabling inventory management for this article." );
872+ bob .append (" or disabling inventory management for this article." );
874873 } else {
875- alertTextArea . setText ( alertTextArea . getText () + "! " );
874+ bob . append ( "! " );
876875 }
877876 }
878877 }
879878 }
879+ alertTextArea .setText (bob .toString ());
880880 }
881881 }
882882
0 commit comments