Question
Refactoring /Code Review Assignment You are working in a software team. Your primary job is to fix PCRs (Problem Reports) on a section of legacy
Refactoring /Code Review Assignment You are working in a software team. Your primary job is to fix PCRs (Problem Reports) on a section of legacy code. The software lead has assigned you to a Buddy Code Review for a fellow programmer Jing Hsan. Jing was fixing a PCR regarding inserting a check for empty name fields. She was supposed to pop up a message box asking the user to insert a valid name if the user clicked any of the buttons without first entering a name. During your code review you notice that the ActionPerformed() is longer than 100 lines. You also notice some other things suggesting Jing Hsan ought to refactor the code. While you dont usually unit test for a fellow programmer, you notice that Jing Hsan has introduced another error while implementing her fixshe broke the Update Button logic. You are encouraged to paste this code into an IDE like Eclipse to help you find how the program can be improved. Your assignment is to write the buddy code review (emails with the following): 1.) Your advice for Jing on how to fix the update problem. 2.) At least 8 specific suggestions on how to refactor the code. Paste your refactored code at the end of this document, and follow it with a list of the changes that you made. Here is the source code Jing asked you to code review (you can cut and paste from this document): /** * Refactoring Example */ import java.awt.*; import java.awt.event.*; import static java.awt.BorderLayout.*; import javax.swing.*; import java.util.*; public class PhoneBook1 extends JFrame implements ActionListener { private JTextField jN, jPh; private JButton jbtAdd, jD, jbtUpd, jF; private ArrayList nL, pL; // updated for v5.0 private int lastF = -1; // Main method public static void main(String[] args) { int x1 = 300; int y1 = 130; PhoneBook1 frame = new PhoneBook1(); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(x1, y1); frame.centerOnScreen(x1, y1); frame.setVisible(true); } // Constructor public PhoneBook1() { nL = new ArrayList(20); // initially set for up to 20 entries; upd for v5.0 pL = new ArrayList(20); // initially set for up to 20 entries; upd for v5.0 setTitle("Phone Book"); // Create panel p to hold the text field and button JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.add(new JLabel("Name: ")); p.add(jN = new JTextField(20)); p.add(new JLabel("Phone: ")); p.add(jPh = new JTextField(20)); p.add(jbtAdd = new JButton("Add")); p.add(jD = new JButton("Delete")); p.add(jbtUpd = new JButton("Update")); p.add(jF = new JButton("Find")); // Set FlowLayout for the frame and add components in it setLayout(new BorderLayout()); // updated for v5.0 add(p, CENTER); // updated for v5.0 // Register listeners jbtAdd.addActionListener(this); jD.addActionListener(this); jbtUpd.addActionListener(this); jF.addActionListener(this); } public void centerOnScreen(int x1, int y1) { int twidth, lhigh, x, y; // Get the screen dimension Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // Determine the location for the twidth lhigh corner of the frame x = (screenSize.width - x1)/2; y = (screenSize.height - y1)/2; twidth = (x < 0) ? 0 : x; lhigh = (y < 0) ? 0 : y; // Position the frame to the specified location this.setLocation(twidth, lhigh); } // ActionEvent handler public void actionPerformed(ActionEvent e) { String nEntr, pEntr; int index; nEntr = jN.getText(); pEntr = jPh.getText(); if(e.getSource() == jbtAdd) // user clicked Add { if(nEntr.length() == 0) //is length over zero { lastF = -1; JOptionPane.showMessageDialog(this,"Please enter a name.", "Invalid entry. Try again.", JOptionPane.ERROR_MESSAGE); jN.requestFocus(); } else { lastF = -1; if(pEntr.length() > 0) { try { addME(nEntr, pEntr); JOptionPane.showMessageDialog(this,"Entry added to phone book.", "Success!", JOptionPane.INFORMATION_MESSAGE); jN.setText(""); jPh.setText(""); jN.requestFocus(); } catch (Exception ex) { JOptionPane.showMessageDialog(this, ex.getMessage(), "Add failed.", JOptionPane.ERROR_MESSAGE); } } // end iflength greater than zero else //length not greater than zero { JOptionPane.showMessageDialog(this,"Please enter a phone number.", "Invalid entry. Try again.", JOptionPane.ERROR_MESSAGE); jPh.requestFocus(); } //end if is length over zero } //end if add if(e.getSource() == jbtUpd) // user clicked Update { if(nEntr.length() == 0) //is length over zero { // lastF = -1; JOptionPane.showMessageDialog(this,"Please enter a name.", "Invalid entry. Try again.", JOptionPane.ERROR_MESSAGE); jN.requestFocus(); } else { if(lastF != -1) { try { updME(nEntr, pEntr, lastF); JOptionPane.showMessageDialog(this,"Entry updated in phone book.", "Success!", JOptionPane.INFORMATION_MESSAGE); lastF = -1; jN.setText(""); jPh.setText(""); jN.requestFocus(); } catch (Exception ex) { JOptionPane.showMessageDialog(this, ex.getMessage(), "Update failed.", JOptionPane.ERROR_MESSAGE); } } else JOptionPane.showMessageDialog(this, "You must find the entry before updating.", "Update failed.", JOptionPane.ERROR_MESSAGE); } //end if lastF not -1 } // end if length not zero } //end update if(e.getSource() == jD) // user clicked Delete { if(nEntr.length() == 0) //is length over zero { lastF = -1; JOptionPane.showMessageDialog(this,"Please enter a name.", "Invalid entry. Try again.", JOptionPane.ERROR_MESSAGE); jN.requestFocus(); } else { lastF = -1; try { delME(nEntr); JOptionPane.showMessageDialog(this,"Entry deleted from phone book.", "Success!", JOptionPane.INFORMATION_MESSAGE); jN.setText(""); jPh.setText(""); jN.requestFocus(); } catch (Exception ex) { JOptionPane.showMessageDialog(this, ex.getMessage(), "Delete failed.", JOptionPane.ERROR_MESSAGE); jN.requestFocus(); } } } else { if(e.getSource() == jF) // user clicked Find { if(nEntr.length() == 0) //is length over zero { lastF = -1; JOptionPane.showMessageDialog(this,"Please enter a name.", "Invalid entry. Try again.", JOptionPane.ERROR_MESSAGE); jN.requestFocus(); } else { try { index = whereIsME(nEntr); jN.setText(nL.get(index)); // updated for v5.0 jPh.setText(pL.get(index)); // updated for v5.0 lastF = index; jN.requestFocus(); } catch (Exception ex) { JOptionPane.showMessageDialog(this, ex.getMessage(), "Find failed.", JOptionPane.ERROR_MESSAGE); jN.requestFocus(); } } } } // end if user clicked delete } //end actionPerformed private int getME(String namey) throws Exception { int index = 0; index = nL.indexOf(namey.trim()); if(index < 0) throw new Exception("Name not in phone book."); return index; } private void addME(String namey, String fred) throws Exception { if(!nL.contains(namey.trim())) { nL.add(namey.trim()); pL.add(fred.trim()); } else throw new Exception("Name already in phone book."); } private void updME(String namey, String fred, int index) throws Exception { nL.get(index); // to verify index; throws Exception if namey not in book nL.set(index, namey.trim()); pL.set(index, fred.trim()); } private void newME(String namey, String fred, int index) throws Exception { nL.get(index); // to verify index; throws Exception if namey not in book nL.set(index, namey.trim()); pL.set(index, fred.trim()); } private void delME(String namey) throws Exception { int index = 0; index = nL.indexOf(namey.trim()); if(index < 0) throw new Exception("Name not in phone book."); else { nL.remove(index); pL.remove(index); } } private int whereIsME(String namey) throws Exception { int index = 0; index = nL.indexOf(namey.trim()); if(index < 0) throw new Exception("Name not in this phone book."); return index; } } PASTE YOUR REFACTORED CODE HERE: LIST YOUR CHANGES HERE:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started