GUI JFRAMEFORM WITH OOP MANAGER UI CLASS WITH MAIN METHOD /* * Random number generator - game of chance * Supply your full name correctly - complusory * Play. * Your score allocates you to a community service * Your combined score is used to award you a prize * OOP - all methods must be in the manager class * The UI class has the GUI and the main method */ package mandeladayoop; import javax.swing.JOptionPane; public class MandelaDayUI extends javax.swing.JFrame { private static MandelaDayOOPManager mdm = new MandelaDayOOPManager(); // Global variable private static String name = ""; private static boolean nameProcessed = false; private static boolean playProcessed = false; private static boolean serviceProcessed = false; private static boolean allCorrect = false; public MandelaDayUI() { initComponents(); this.setLocationRelativeTo(this); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // private void initComponents() { pnlPanel = new javax.swing.JPanel(); txfHeading = new javax.swing.JLabel(); txforphanage = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); txfprison = new javax.swing.JLabel(); txfspcanumber = new javax.swing.JTextField(); txforphanagenumber = new javax.swing.JTextField(); txfprisonnumber = new javax.swing.JTextField(); btnplay = new javax.swing.JButton(); btnService = new javax.swing.JButton(); btnPrize = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); txfName = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); txfServices = new javax.swing.JTextField(); btnNext = new javax.swing.JButton(); lblprize = new javax.swing.JLabel(); txfPrize = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); pnlPanel.setBackground(new java.awt.Color(204, 255, 204)); pnlPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder()); txfHeading.setFont(new java.awt.Font("Gadugi", 1, 18)); // NOI18N txfHeading.setText("Mandela Day - 67 minutes of service"); txforphanage.setFont(new java.awt.Font("Gadugi", 1, 14)); // NOI18N txforphanage.setText("Orphanage"); jLabel2.setFont(txforphanage.getFont()); jLabel2.setText("SPCA"); txfprison.setFont(txforphanage.getFont()); txfprison.setText("Prison"); txfspcanumber.setEditable(false); txfspcanumber.setFont(new java.awt.Font("Gadugi", 1, 48)); // NOI18N txfspcanumber.setHorizontalAlignment(javax.swing.JTextField.CENTER); txfspcanumber.setMinimumSize(new java.awt.Dimension(59, 20)); txforphanagenumber.setEditable(false); txforphanagenumber.setFont(new java.awt.Font("Gadugi", 1, 48)); // NOI18N txforphanagenumber.setHorizontalAlignment(javax.swing.JTextField.CENTER); txforphanagenumber.setPreferredSize(new java.awt.Dimension(59, 20)); txfprisonnumber.setEditable(false); txfprisonnumber.setFont(txforphanagenumber.getFont()); txfprisonnumber.setHorizontalAlignment(javax.swing.JTextField.CENTER); txfprisonnumber.setPreferredSize(new java.awt.Dimension(59, 20)); btnplay.setFont(txforphanage.getFont()); btnplay.setText("Play"); btnplay.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnplayActionPerformed(evt); } }); btnService.setFont(txforphanage.getFont()); btnService.setText("Service"); btnService.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnServiceActionPerformed(evt); } }); btnPrize.setFont(txforphanage.getFont()); btnPrize.setText("Prize"); btnPrize.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnPrizeActionPerformed(evt); } }); jLabel1.setFont(txforphanage.getFont()); jLabel1.setText("Name"); txfName.setFont(txforphanage.getFont()); txfName.setToolTipText("First name and last name"); txfName.setNextFocusableComponent(btnplay); jLabel3.setFont(txforphanage.getFont()); jLabel3.setText("Service"); txfServices.setEditable(false); txfServices.setFont(txforphanage.getFont()); btnNext.setFont(txforphanage.getFont()); btnNext.setText("Next"); btnNext.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnNextActionPerformed(evt); } }); lblprize.setFont(txforphanage.getFont()); lblprize.setText("Prize"); txfPrize.setFont(txforphanage.getFont()); javax.swing.GroupLayout pnlPanelLayout = new javax.swing.GroupLayout(pnlPanel); pnlPanel.setLayout(pnlPanelLayout); pnlPanelLayout.setHorizontalGroup( pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlPanelLayout.createSequentialGroup() .addGap(28, 28, 28) .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnlPanelLayout.createSequentialGroup() .addComponent(txforphanage) .addGap(76, 76, 76) .addComponent(jLabel2) .addGap(84, 84, 84) .addComponent(txfprison)) .addGroup(pnlPanelLayout.createSequentialGroup() .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(pnlPanelLayout.createSequentialGroup() .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(pnlPanelLayout.createSequentialGroup() .addComponent(jLabel1) .addGap(26, 26, 26) .addComponent(txfName)) .addGroup(pnlPanelLayout.createSequentialGroup() .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(pnlPanelLayout.createSequentialGroup() .addComponent(btnplay, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(63, 63, 63)) .addGroup(pnlPanelLayout.createSequentialGroup() .addComponent(txforphanagenumber, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(57, 57, 57))) .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(btnService, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(txfspcanumber, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) .addGap(43, 43, 43) .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txfprisonnumber, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnPrize, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnNext, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addComponent(txfHeading)) .addGap(3, 3, 3)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnlPanelLayout.createSequentialGroup() .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlPanelLayout.createSequentialGroup() .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlPanelLayout.createSequentialGroup() .addComponent(lblprize) .addGap(25, 25, 25))) .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txfServices) .addComponent(txfPrize)))) .addContainerGap(39, Short.MAX_VALUE)) ); pnlPanelLayout.setVerticalGroup( pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlPanelLayout.createSequentialGroup() .addComponent(txfHeading, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(4, 4, 4) .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(txfName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnNext)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txforphanage) .addComponent(jLabel2) .addComponent(txfprison)) .addGap(18, 18, 18) .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txforphanagenumber, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE) .addComponent(txfspcanumber, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(txfprisonnumber, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(18, 18, 18) .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(btnplay, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnService, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnPrize, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGap(18, 18, 18) .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(txfServices, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(pnlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblprize) .addComponent(txfPrize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(16, 16, 16)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(pnlPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(pnlPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); }// // Action buttons are always void // First name and lastname must be present before Next is pressed // Buttons must be pressed in order from left to right private void btnplayActionPerformed(java.awt.event.ActionEvent evt) { allCorrect = mdm.correctlyProcessed(nameProcessed, name); if(allCorrect){ playProcessed = true; String strRandom = mdm.generateRandom(); txforphanagenumber.setText(strRandom.substring(0,1)); txfspcanumber.setText(strRandom.substring(2,3)); txfprisonnumber.setText(strRandom.substring(4,5)); } else JOptionPane.showMessageDialog(null, "Enter full name and press next." + "\n" + "Follow the order of the buttons left to right."); } private void btnServiceActionPerformed(java.awt.event.ActionEvent evt) { if(allCorrect && playProcessed == true) { serviceProcessed = true; String strService = mdm.allocateService(); txfServices.setText(strService); } else JOptionPane.showMessageDialog(null, "Enter full name and press next." + "\n" + "Follow the order of the buttons left to right."); } private void btnNextActionPerformed(java.awt.event.ActionEvent evt) { nameProcessed = true; name = (txfName.getText()); mdm.setTheName(name); } private void btnPrizeActionPerformed(java.awt.event.ActionEvent evt) { if(allCorrect && serviceProcessed == true && playProcessed == true){ String prize = mdm.allocatePrize(); txfPrize.setText(prize); } else JOptionPane.showMessageDialog(null, "Enter full name and press next." + "\n" + "Follow the order of the buttons left to right."); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(MandelaDayUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MandelaDayUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MandelaDayUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MandelaDayUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MandelaDayUI().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton btnNext; private javax.swing.JButton btnPrize; private javax.swing.JButton btnService; private javax.swing.JButton btnplay; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel lblprize; private javax.swing.JPanel pnlPanel; private javax.swing.JLabel txfHeading; private javax.swing.JTextField txfName; private javax.swing.JTextField txfPrize; private javax.swing.JTextField txfServices; private javax.swing.JLabel txforphanage; private javax.swing.JTextField txforphanagenumber; private javax.swing.JLabel txfprison; private javax.swing.JTextField txfprisonnumber; private javax.swing.JTextField txfspcanumber; // End of variables declaration } ============================================================================== GUI JFRAMEFORM WITH OOP MANAGER MANAGER CLASS /* * Random number generator - game of chance * Supply your full name correctly - complusory * Play. * Your score allocates you to a community service * Your combined score is used to award you a prize * OOP - All methods must be in the manager class * UI class has the GUI and the main method */ package mandeladayoop; import java.util.Random; public class MandelaDayOOPManager { // Global variables private static String name = null; private static String service = null; private static String prize = null; private static int[] intArr = new int[3]; ============================================================== public void setTheName(String n) { this.name = n; } =============================================================== public String generateRandom() { // Generate 3 random numbers and store in an array Random myRandomNumber = new Random(); String strArray = ""; // The 3 random integers are parsed here and then returned // as a single string for (int i = 0; i < 3; i++) { intArr[i]= (myRandomNumber.nextInt(9) + 1); // 1 to 9 inclusive String temp = ("" + intArr[i]); // parse int to String strArray = strArray + temp + ";"; // build a string equivalent } // end for return strArray; // return string format is "int;int;int;" // returning the array "intArr" also works } // end generateRandom ========================================================================= public String allocateService() { // Biggest of the three numbers indicates the service to be allocated // Odds must favour the orphanage and disadvantage the prison // Local temp variables make the logic below easier to read int firstNumber = intArr[0]; int secondNumber = intArr[1]; int thirdNumber = intArr[2]; int biggest = 0; if (firstNumber > biggest) { biggest = firstNumber; service = name + " you will visit the orphanage"; } if (secondNumber > biggest) { biggest = secondNumber; service = name + " you will visit the SPCA"; } if (thirdNumber > biggest) { biggest = thirdNumber; service = name + " you will visit the Prison"; } return service; } // end allocateService =========================================================================== public String allocatePrize() { // Add the three random numbers together to get the total // Higher the total the bigger the prize int total = 0; for(int i = 0; i < 2; i++) total = total + intArr[i]; if (total <= 10) prize = "R100-00 voucher."; else if (total > 10 && total <= 20) prize = "R200-00 voucher."; else if (total > 20 && total <= 24) prize = "A night at the Hilton Hotel."; else if (total >= 25) prize = "A ticket in the Land Rover draw."; else prize = "Error"; // never a condition for the final else return prize; } // end allocatePrize ================================================================ public boolean correctlyProcessed(boolean np, String n){ // The data is checked and processed in one method in one place // Has the name been entered? // Does the name have a first name and a last name? boolean nameProcessed = np; String theName = n; boolean processedCorrect = false; if(nameProcessed == true && theName.length() > 1 && (theName.indexOf(" ") != -1)){ processedCorrect = true; } return processedCorrect; } // end correctlyProcessed } =============================================================================================