import java.awt.Container; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; public abstract class ArrayListPracticeBase extends JFrame { final static String[] CAR_NAMES = {"BMW", "Ferrari", "Jeep"}; final static int MIN_CARS = 2; final static int MAX_CARS = 5; /** Fills the carList with hard-coded Auto objects * The instance variable carList is the ArrayList * to be filled with Auto objects */ abstract void fillWithCars(); /** Prints carList to console, elements are separated by a space * The instance variable carList is the ArrayList to be printed */ abstract void printAutoList(); /** Sets the model of all the elements in carList to parameter value * The instance variable carList is the ArrayList to be modified * @param model the model to assign to all Auto objects in carList */ abstract void setModelValues(String model); /** Finds maximum number of miles driven * Instance variable carList is the ArrayList to search * @return the maximum miles driven by all the Auto objects */ abstract int findMaximumMilesDriven(); /** Finds number of times parameter model is found in the carList * Instance variable carList is the ArrayList in which we search * @param model the model to count * @return the number of times model was found */ abstract int countFound(String model); /** Clears the list * Instance variable carList is the ArrayList to clear */ abstract void clearList(); /** Remove last car from the list * Instance variable carList is the ArrayList to clear * Be sure to handle the case when the list is empty */ abstract void removeLast(); //End of student code ////////////////////////////////////////// ////////////////////////////////////////// // GUI components private JButton fillValues; private JButton printAutoList; private JButton setValues; private JButton findMaximum; private JButton countFrequency; private JButton clearList; private JButton removeLast; private ButtonHandler bh; protected static ArrayList carList; private AutoDisplay ad; private static ArrayListPractice app; public ArrayListPracticeBase() { super("Choose your activity"); this.addWindowListener(new WindowAdapter() { @Override public void windowActivated(WindowEvent we) { repaint(); } @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); Container c = getContentPane(); c.setLayout(new FlowLayout()); fillValues = new JButton("Fill Cars"); c.add(fillValues); printAutoList = new JButton("Print Auto List"); c.add(printAutoList); setValues = new JButton("Set Models"); c.add(setValues); findMaximum = new JButton("Find Maximum Miles"); c.add(findMaximum); countFrequency = new JButton("Count Model Frequency"); c.add(countFrequency); clearList = new JButton("Clear List"); c.add(clearList); removeLast = new JButton("Remove Last"); c.add(removeLast); bh = new ButtonHandler(); fillValues.addActionListener(bh); printAutoList.addActionListener(bh); setValues.addActionListener(bh); findMaximum.addActionListener(bh); countFrequency.addActionListener(bh); clearList.addActionListener(bh); removeLast.addActionListener(bh); setSize(500, 400); carList = new ArrayList(); ad = new AutoDisplay(carList); setVisible(true); ad.setEraseColor(getBackground()); // fill carList with several cars fillWithCars(); } void showDialog(String message) { ad.resetActivity(); JOptionPane.showMessageDialog(null, message); repaint(); } public void startActivity(AutoDisplay.Activity act) { ad.setActivity(act); String answer = ""; String message = ""; switch (act) { case FILL_VALUES: fillWithCars(); ad.setCarList(carList); showDialog("carList filled with new values"); break; case PRINT_LIST: printAutoList(); showDialog("carList printed"); break; case SET_MODELS: answer = JOptionPane.showInputDialog(null, "Enter a car model"); if (answer != null) { ad.resetCurrentModelValuesSet(); ad.setSearchModel(answer); setModelValues(answer); if (ad.getCurrentModelValuesSet()) { message = "\nYour result is correct"; } else { message = "\nYour result is not correct"; } showDialog("car models set to " + answer + message); } break; case FIND_MAX: if (carList.isEmpty()) { JOptionPane.showMessageDialog(null, "The list is empty, there is no maximum."); break; } int a = findMaximumMilesDriven(); if (a == ad.getCurrentMaximumMilesDriven()) { message = "\nYour result is correct"; } else { message = "\nYour result is not correct"; } showDialog("The maximum number of miles driven is " + a + message); break; case COUNT_MODEL: answer = JOptionPane.showInputDialog(null, "Enter a car model"); if (answer != null) { ad.resetCurrentCountModelFound(); ad.setSearchModel(answer); int frequency = countFound(answer); if (frequency == ad.getCurrentCountModelFound()) { message = "\nYour result is correct"; } else { message = "\nYour result is not correct"; } if (frequency > 1) { showDialog(answer + " found " + frequency + " times" + message); } else if (frequency == 1) { showDialog(answer + " found " + frequency + " time" + message); } else { showDialog(answer + " not found" + message); } } break; case CLEAR_LIST: int n = JOptionPane.showConfirmDialog( null, "Erase list?", "Confirm", JOptionPane.YES_NO_OPTION); if (n == JOptionPane.YES_OPTION) { clearList(); ad.setCarList(carList); if (carList.isEmpty()) { showDialog("The list is empty.\nYour result is correct."); } else { showDialog("The list is not empty.\nYour result is incorrect."); } } break; case REMOVE_LAST_CAR: int nRemove = JOptionPane.showConfirmDialog( null, "Remove last car from list?", "Confirm", JOptionPane.YES_NO_OPTION); if (nRemove == JOptionPane.YES_OPTION) { Auto lastCar = getLastCar(); removeLast(); ad.setCarList(carList); if (isLastGone(lastCar)) { showDialog("The last car is gone.\nYour result is correct."); } else { showDialog("The last car is still there.\nYour result is incorrect."); } } break; } enableButtons(); } public Auto getLastCar() { if (carList.size() > 0) { return carList.get(carList.size() - 1); } return null; } public boolean isLastGone(Auto last) { for (Auto car : carList) { if (car == last) { return false; } } return true; } public static ArrayList getCarList() { return carList; } protected void animate(Auto au) { switch (ad.getActivity()) { case PRINT_LIST: case SET_MODELS: try { ad.setCurrentAuto(au); ad.setCurrentIndex(au.getIndex()); repaint(); Thread.sleep(4000); } catch (InterruptedException e) { System.out.println("IE Exception " + e.getMessage()); System.out.println(e.toString()); } break; default: // call to animate has wrong number of arguments JOptionPane.showMessageDialog(null, "Wrong number of arguments to animate method.\n" + "The animate method with 1 argument should only be called for PRINT_LIST and SET_MODELS.\n" + "It was called with " + ad.getActivity() + ".\n" + "Terminating program."); System.exit(1); break; } } protected void animate(Auto au, int studentResult) { switch (ad.getActivity()) { case FIND_MAX: case COUNT_MODEL: try { ad.setCurrentAuto(au); ad.setCurrentIndex(au.getIndex()); ad.setStudentResult(studentResult); repaint(); Thread.sleep(4000); } catch (InterruptedException e) { System.out.println("IE Exception " + e.getMessage()); System.out.println(e.toString()); } break; default: // call to animate has wrong number of arguments JOptionPane.showMessageDialog(null, "Wrong number of arguments to animate method.\n" + "The animate method with 2 arguments should only be called for FIND_MAX and COUNT_MODELS.\n" + "It was called with " + ad.getActivity() + ".\n" + "Terminating program."); System.exit(1); break; } } protected void animate() { switch (ad.getActivity()) { case FILL_VALUES: case CLEAR_LIST: case REMOVE_LAST_CAR: case NONE: repaint(); break; default: JOptionPane.showMessageDialog(null, "Wrong number of arguments to animate method.\n" + "The animate method with no arguments should only be called for " + "FILL_VALUES, CLEAR_LIST, REMOVE_LAST_CAR and NONE.\n" + "It was called with " + ad.getActivity() + ".\n" + "Terminating program."); System.exit(1); break; } } @Override public void paint(Graphics g) { super.paint(g); if (ad.getActivity() != AutoDisplay.Activity.NONE && ad.getCurrentAuto() != null) { ad.updateAutoDisplay(ad.getCurrentAuto(), g); } else { ad.updateAutoDisplay(g); } } public static void main(String[] args) { app = new ArrayListPractice(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void disableButtons() { fillValues.setEnabled(false); printAutoList.setEnabled(false); setValues.setEnabled(false); countFrequency.setEnabled(false); findMaximum.setEnabled(false); clearList.setEnabled(false); removeLast.setEnabled(false); } public void enableButtons() { fillValues.setEnabled(true); printAutoList.setEnabled(true); setValues.setEnabled(true); countFrequency.setEnabled(true); findMaximum.setEnabled(true); clearList.setEnabled(true); removeLast.setEnabled(true); } private class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { printAutoListT t = new printAutoListT(app); if (e.getSource() == fillValues) { disableButtons(); fillValues.requestFocus(); ad.setActivity(AutoDisplay.Activity.FILL_VALUES); t.start(); } else if (e.getSource() == printAutoList) { disableButtons(); printAutoList.requestFocus(); ad.setActivity(AutoDisplay.Activity.PRINT_LIST); t.start(); } else if (e.getSource() == setValues) { disableButtons(); setValues.requestFocus(); ad.setActivity(AutoDisplay.Activity.SET_MODELS); t.start(); } else if (e.getSource() == findMaximum) { disableButtons(); findMaximum.requestFocus(); ad.setActivity(AutoDisplay.Activity.FIND_MAX); t.start(); } else if (e.getSource() == countFrequency) { disableButtons(); countFrequency.requestFocus(); ad.setActivity(AutoDisplay.Activity.COUNT_MODEL); t.start(); } else if (e.getSource() == clearList) { disableButtons(); clearList.requestFocus(); ad.setActivity(AutoDisplay.Activity.CLEAR_LIST); t.start(); } else if (e.getSource() == removeLast) { disableButtons(); removeLast.requestFocus(); ad.setActivity(AutoDisplay.Activity.REMOVE_LAST_CAR); t.start(); } } } private class printAutoListT extends Thread { ArrayList arr; ArrayListPractice s1; public printAutoListT(ArrayListPractice s) { arr = ArrayListPracticeBase.carList; s1 = s; } @Override public void run() { startActivity(ad.getActivity()); } } }