Assignment #3 (Due 4/4/2017) As a follow up to Assignment #2, modify the CheckingAccount class to add the following instance members: an array (self-expanding type) or ArrayList of Transaction objects and a int variable transcount (incremented each time a new Transaction object is created). Create a Transaction class that stores an int transnumber (will be the current value of transcount), an int transid (1 = check, 2 = deposit 3 = service charge) and a double transamt. Prompt the user for the choice of a) entering a transaction, b) listing all transations, c) listing all checks, d) listing all deposits.(implement these options with JFrame,JLabel, JRadioButton, ButtonGroup, and a listener) The listings must go to a Dialog box. Add instance methods to the CheckingAccount class like addtrans (put a trans into the account), gettrans (get a transaction from the array) gettranscount(get the current value of transcount class variable) and any other methods you wish to implement. This program is event-driven. You cannot put all the processing steps/logic in main. You can put them in helper methods in the Main class, in the GUI objects class, or in the CheckingAccount class, so they can be called by the listener method. Suggested Reading in Textbook: Chapter 4.7 4.9, 5.6 5.8, 7.1 7.11, 8.1 8.6, 8.8 8.9 Sample Run Download Assignment3SampleRun.pdf from the class website. Code Template The following code template will help with this assignment: New data and methods for the CheckingAccount Class --------------------------------------------------------------------------- private ArrayList<Transaction> translist; // keeps a list of Transaction objects for the account private int transcount; // the count of Transaction objects and used as the ID for each transaction public void addtrans( Transaction newtrans) // adds a transaction object to the translist public int gettranscount() //returns the current value of transcount; public Transaction gettrans(int i) // returns the i-th Transaction object in the list 1
New Transaction class -------------------------------------------------------------------------- public class Transaction private int transnumber; private int transid; private double transamt; public Transaction(int number, int id, double amount) transnumber = number; transid = id; transamt = amount; public int gettransnumber() return transnumber; public int gettransid() return transid; public double gettransamount() return transamt; Sample Code //******************************************************************** // EOptionsPanel.java Author: // Demonstrates the use of radio buttons to capture input options. // and listener to call methods to process the options //******************************************************************** import javax.swing.*; 2
import java.awt.*; import java.awt.event.*; public class EOptionsPanel extends JPanel private JLabel prompt; private JRadioButton one, two, three; //----------------------------------------------------------------- // Sets up a panel with a label and a set of radio buttons // that present options to the user. //----------------------------------------------------------------- public EOptionsPanel() prompt = new JLabel ("Choose your input option?"); prompt.setfont (new Font ("Helvetica", Font.BOLD, 24)); one = new JRadioButton ("input element"); one.setbackground (Color.green); two = new JRadioButton ("list all elements"); two.setbackground (Color.green); three = new JRadioButton ("show element count"); three.setbackground (Color.green); ButtonGroup group = new ButtonGroup(); group.add (one); group.add (two); group.add (three); EOptionListener listener = new EOptionListener(); one.addactionlistener (listener); two.addactionlistener (listener); three.addactionlistener (listener); // add the components to the JPanel add (prompt); add (one); add (two); add (three); 3
setbackground (Color.green); setpreferredsize (new Dimension(400, 100)); //***************************************************************** // Represents the listener for the radio buttons //***************************************************************** private class EOptionListener implements ActionListener //-------------------------------------------------------------- // Calls the method to process the option for which radio // button was pressed. //-------------------------------------------------------------- public void actionperformed (ActionEvent event) Object source = event.getsource(); if (source == one) MyElements2.inputElements(); else if (source == two) MyElements2.listElements(); else String message; message = "The number of elements processed was "+ ChemicalElement.getNumOfElements(); JOptionPane.showMessageDialog (null, message); // MyElements2 Author: // Demonstrates the use of radio buttons with listener // and multiple dialog boxes for user interaction. // Demonstrates the use of the JOptionPane class. 4
import javax.swing.joptionpane; import javax.swing.jframe; public class MyElements2 public static JFrame frame; public static ChemicalElement[] elist; public static void main (String[] args) elist = new ChemicalElement[10]; String message; frame = new JFrame ("Chemical Elements Options"); frame.setdefaultcloseoperation (JFrame.EXIT_ON_CLOSE); EOptionsPanel panel = new EOptionsPanel(); frame.getcontentpane().add (panel); frame.pack(); frame.setvisible(true); public static void listelements() String message, ntabs; int num; System.out.println("\nNumber"+"\t"+"Name"+"\t\t"+"Symbol"+"\t" +"AtomicNumber"+"\n"); for ( num=0; num < ChemicalElement.getNumOfElements();num++) if (elist[num].getname().length() < 8) ntabs="\t\t"; else ntabs="\t"; message = num + "\t"+elist[num].getname()+ntabs+elist[num].getsymbol()+ "\t"+elist[num].getnumber(); System.out.println(message); 5
public static void inputelements() String name, symbol, numstr, message; int number, again, index; frame.setvisible(false); do name = JOptionPane.showInputDialog ("Enter the Element name: "); symbol = JOptionPane.showInputDialog ("Enter the Element symbol: "); numstr = JOptionPane.showInputDialog ("Enter the Element number: "); number = Integer.parseInt(numStr); index = ChemicalElement.getNumOfElements(); elist[index] = new ChemicalElement(name,symbol,number); message = elist[index].tostring(); JOptionPane.showMessageDialog (null, message); again = JOptionPane.showConfirmDialog (null, "Input Another?"); while (again == JOptionPane.YES_OPTION); frame.setvisible(true); // Method added to ChemicalElement class public String tostring() String message; message = "The element name is "+nameofelement+"\n" + "Its atomic symbol is "+chemicalsymbol+"\n" + "Its atomic number is "+atomicnumber; return message; 6