Change Calculator Source Code


// File: ChangeCalculator.java
// Date: Mar 21, 2017
// Author: Nicholas Duchon
// Purpose: solve G&T change calculator problem

import java.util.StringTokenizer;
import java.util.ArrayList;   // list of Denomination's
import java.util.Collections; // sort list
import javax.swing.UIManager;
import java.io.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextArea;

/**
Note about paths:
<ul>
<li>C:\java\bin is in %path%
</ul>
<h3>History:</h3>
<ul>
<li> Mar 21, 2017 - cleaned up code
<li> Jun 20, 2017 - prepare jar file, and fix various links
</ul>
 *
 * @author  Nicholas Duchon
@see <a href="readme.html"> Documentation</a>
@see <a href=code.html>Source code</a>

 */
public class ChangeCalculator extends JFrame {
   public static final long serialVersionUID  = 123;
  
   ArrayList <Denomination> den = new ArrayList <> ();
  
   // Variables declaration - do not modify
  
   private JFrame calcFrame;       // initComponents, initCalcFrame, keypadCalculatorButtonClicked
  
   private JPanel calcButtonPanel; // loadButtonPressed, initCalcFrame
  
   private JTextArea calcChangeTA; // initCalcFrame, calcChangeButtonClicked
   private JTextArea input;        // initComponents, loadButtonPressed
   private JTextArea output;       // initComponents, computeButtonPressed, helpButtonPressed
  
   private JTextField calcAmountDueField;      // initCalcFrame, calcDueButtonClicked, calcChangeButtonClicked
   private JTextField calcAmountTenderedField; // initCalcFrame, calcTendButtonClicked, calcChangeButtonClicked, loadButtonPressed
   private JTextField dueTextField;            // initComponents, computeButtonPressed,
   private JTextField payedTextField;          // initiComponents, computeButtonPressed

   // End of variables declaration//GEN-END:variables

   public static void main (String args []) {
      new ChangeCalculator ();
   } // end main
  
   public ChangeCalculator() {
      initComponents();
      initCalcFrame ();
   } // end no parameter constructor
  
   private void initCalcFrame () {
      javax.swing.JLabel calcChangeLabel;
  
      JButton calcCalcChangeButton;
      JButton calcDueButton;
      JButton calcTendButton;
   
      calcFrame = new JFrame();
     
      calcChangeLabel = new javax.swing.JLabel();
      calcAmountTenderedField = new JTextField();
      calcAmountDueField = new JTextField();
      calcChangeTA = new JTextArea();
      calcButtonPanel = new JPanel();
      calcCalcChangeButton = new JButton();
      calcTendButton = new JButton();
      calcDueButton = new JButton();
     
      calcFrame.setTitle("Change Calculator Window - V2");
      calcFrame.setBackground(new java.awt.Color(204, 255, 255));
      calcFrame.setForeground(java.awt.Color.orange);
      calcFrame.setMaximizedBounds(new java.awt.Rectangle(100, 100, 200, 200));
      calcFrame.setName("Change Calculator Window");
      calcFrame.setLocationRelativeTo (null);
     
      JPanel jpTopCalc = new JPanel ();
      jpTopCalc.setLayout(new java.awt.GridLayout (0, 2));
      calcFrame.add (jpTopCalc, java.awt.BorderLayout.NORTH);
     
      jpTopCalc.add(calcTendButton);
    
      calcAmountTenderedField = new JTextField ();
      jpTopCalc.add (calcAmountTenderedField);
     
      jpTopCalc.add(calcDueButton);
     
      calcAmountDueField = new JTextField ();
      jpTopCalc.add (calcAmountDueField);
     
      JButton jbC = new JButton ("Calculate Change");
      jbC.addActionListener(e -> calcCalcChangeButtonClicked (e));
      jpTopCalc.add (jbC);
     
      calcChangeTA = new JTextArea ();
      calcFrame.add (calcChangeTA, java.awt.BorderLayout.CENTER);
     
      calcAmountTenderedField.setHorizontalAlignment(JTextField.LEFT);
  
      calcButtonPanel.setLayout(new java.awt.GridLayout(5, 0));
  
      calcFrame.add(calcButtonPanel, java.awt.BorderLayout.WEST);
  
      calcCalcChangeButton.setText("Calculate Change");
      calcCalcChangeButton.addActionListener(e -> calcCalcChangeButtonClicked (e));
  
      calcTendButton.setText("Tendered Amount");
      calcTendButton.addActionListener(e -> calcTendButtonClicked (e));
  
      calcDueButton.setText("Amount Due");
      calcDueButton.addActionListener(e -> calcDueButtonClicked (e));
  
      calcFrame.validate ();
   } // end method initCalcFrame
  
   private void initComponents() {
      try {
         UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
      //          UIManager.setLookAndFeel( "javax.swing.plaf.metal.MetalLookAndFeel" ); // crossplatform, color, no copy/paset
      //          UIManager.setLookAndFeel( "javax.swing.plaf.nimbus.NimbusLookAndFeel" ); // color, no copy/paste
      //          UIManager.setLookAndFeel( "com.sun.java.swing.plaf.motif.MotifLookAndFeel" ); // color, no copy/paste
      //          UIManager.setLookAndFeel( "com.apple.laf.AquaLookAndFeel" ); // no color, but copy/paste
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   //       System.out.println ("Looks: " + Arrays.deepToString(UIManager.getInstalledLookAndFeels ()).replace (",", "\n"));
   //       System.out.println ("Current: " + UIManager.getLookAndFeel ());
   //       System.out.println ("Defaults:\n" + UIManager.getLookAndFeelDefaults().toString().replace(",", "\n"));
     
      setTitle ("Change Calculator - V2");
      javax.swing.JLabel dueLabel           = new javax.swing.JLabel("Due:  ");
      javax.swing.JLabel proferredLabel     = new javax.swing.JLabel("Proferred:  ");
  
      JPanel controlPanel      = new JPanel();

      JButton helpButton        = new JButton();
      JButton computeButton     = new JButton();
      JButton loadButton        = new JButton();
      JButton keypadStartButton = new JButton();

      dueTextField      = new JTextField();
      payedTextField    = new JTextField();
     
      proferredLabel.setHorizontalAlignment (javax.swing.SwingConstants.RIGHT);
      dueLabel      .setHorizontalAlignment (javax.swing.SwingConstants.RIGHT);

      input = new JTextArea(10, 10);
      input.setBackground (java.awt.Color.yellow);
      javax.swing.JScrollPane inputScrollPane = new javax.swing.JScrollPane(input);
     
      output = new JTextArea(10, 10);
      output.setBackground (new java.awt.Color (200, 255, 200));
      javax.swing.JScrollPane outputScrollPane = new javax.swing.JScrollPane(output);
     
      javax.swing.JSplitPane jspIO = new javax.swing.JSplitPane (
                     javax.swing.JSplitPane.HORIZONTAL_SPLIT,
                     inputScrollPane, outputScrollPane);
      add (jspIO, java.awt.BorderLayout.CENTER);
  
  
      controlPanel.setLayout(new java.awt.GridLayout(2, 4, 2, 2));
  
      helpButton.setBackground(java.awt.Color.green); // new java.awt.Color(204, 255, 204));
      helpButton.setForeground(new java.awt.Color(0, 0, 0));
      helpButton.setText("Help");
      helpButton.addActionListener(e -> helpButtonActionPerformed (e));
  
      controlPanel.add(helpButton);
  
      controlPanel.add(proferredLabel);
  
      payedTextField.setColumns(30);
      payedTextField.setText("proferred");
      controlPanel.add(payedTextField);
  
      computeButton.setBackground(new java.awt.Color(255, 204, 255));
      computeButton.setText("Compute Change");
      computeButton.addActionListener(e -> computeButtonPressed (e));
  
      controlPanel.add(computeButton);
  
      loadButton.setBackground(new java.awt.Color(204, 255, 255));
      loadButton.setText("Load Denominations");
      loadButton.addActionListener(e -> loadButtonPressed (e));
  
      controlPanel.add(loadButton);
  
      controlPanel.add(dueLabel);
  
      dueTextField.setText("due");
      controlPanel.add(dueTextField);
  
      keypadStartButton.setBackground(new java.awt.Color(204, 204, 255));
      keypadStartButton.setText("Keypad Calculator");
      keypadStartButton.addActionListener(e -> keypadCalculatorButtonClicked (e));
  
      controlPanel.add(keypadStartButton);
  
      add(controlPanel, java.awt.BorderLayout.NORTH);
     
      setSize(1200, 800);
      validate ();
      jspIO.setDividerLocation (400);
      setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo (null);
      setVisible (true);
     
   } // end method initComponents

   private void calcDueButtonClicked(java.awt.event.ActionEvent evt) {
   //GEN-FIRST:event_calcDueButtonClicked
      calcAmountDueField.setText("");
      for (Denomination d: den) {
         d.setOutputField(calcAmountDueField);
      } // end for each button
   }//GEN-LAST:event_calcDueButtonClicked

   private void calcTendButtonClicked(java.awt.event.ActionEvent evt) {
   //GEN-FIRST:event_calcTendButtonClicked
      calcAmountTenderedField.setText("");
      for (Denomination d: den) {
         d.setOutputField(calcAmountTenderedField);
      } // end for each button      
   }//GEN-LAST:event_calcTendButtonClicked

   private void calcCalcChangeButtonClicked(java.awt.event.ActionEvent evt) {
   //GEN-FIRST:event_calcCalcChangeButtonClicked
      Rational payedAmount = parseDenomination(calcAmountTenderedField.getText());
      Rational dueAmount = parseDenomination(calcAmountDueField.getText());
      Rational change = payedAmount.sub(dueAmount);
      calcChangeTA.setText(toValueString (change));
   }//GEN-LAST:event_calcCalcChangeButtonClicked

   private void keypadCalculatorButtonClicked(java.awt.event.ActionEvent evt) {
   //GEN-FIRST:event_keypadCalculatorButtonClicked
      calcFrame.setSize(400, 400);
      calcFrame.setBackground(new java.awt.Color(204, 255, 255));
      calcFrame.setVisible (true);
   }//GEN-LAST:event_keypadCalculatorButtonClicked
  
   private void changeColorHandler (java.awt.event.ActionEvent e) {
   } // end method changeColorHandler

   private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {
   //GEN-FIRST:event_helpButtonActionPerformed
      String st = "";
      st += "Use CNTRL-C and -V for copy/paste, even on a Mac!\n\n";
      st += "\nThis program will make change for any set of denominations";
      st += "\n\nBegin by typing in a set of denominations in the left text area.";
      st += "\n\tThe format should be n/d <name> <abbr>";
      st += "\n\tWhere n and d make a fraction";
      st += "\n\t<name> is the name of the denomination";
      st += "\n\t<abbr> is the abbreviation used for the denomination";
      st += "\nThen click the Load Denominations button.";
      st += "\nNow enter the amount proferred and amount due in the";
      st += "\n\tappropriate fields, each entry should be an integer followed by ";
      st += "\n\tthe appropriate abbreviation, as specified in the <abbr> field.";
      st += "\nNow, push the Computer Change button and ";
      st += "\n\tthe results will be displayed in right hand text area.";
      st += "\nFor example:";
      st += "\n\t1/100 cent c\n\t5/100 nickel n\n\t10/100 dime d\n\t25/100 quarter q";
      st += "\n\t1/1 dollar D\n\t5/1 five F\n\t10/1 ten T\n\t50/1 fifty FF";
      st += "\nTest:\n\tProferred: 236 D 45 c\n\tDue: 225 D 17 c";
      st += "\n\tChange:\n\t\t1 ten(s)\n\t\t1 dollar(s)\n\t\t1 quarter(s)\n\t\t3 cent(s)";
      st += "\nThis program is flexible enough to work with units such as time also";
      st += "\nAs in weeks, days, hours, minutes, and seconds, ";
      st += "\nwhere 1 hour makes a nice base unit:";
      st += "\n\t1/3600 second s\n\t1/60 minute m\n\t1/1 hour h\n\t24/1 day d\n\t168/1 week w";
      output.append (st);
   }//GEN-LAST:event_helpButtonActionPerformed

   private void computeButtonPressed(java.awt.event.ActionEvent evt) {
   //GEN-FIRST:event_computeButtonPressed
      // Add your handling code here:
      output.append ("\n\nCompute button pressed");
      output.append ("\npayed: " + payedTextField.getText());
      output.append ("\ndue: " + dueTextField.getText());
      Rational payedAmount = parseDenomination(payedTextField.getText());
      Rational dueAmount = parseDenomination(dueTextField.getText());
      Rational change = payedAmount.sub(dueAmount);
      output.append ("\nPayed: " + payedAmount);
      output.append ("\nDue: " + dueAmount);
      output.append ("\nChange due: " + change);
      output.append ("\nChange in denominations: " + toValueString (change));
   }//GEN-LAST:event_computeButtonPressed

   private void loadButtonPressed(java.awt.event.ActionEvent evt) {
      den.clear(); // clear the old denominations
      calcButtonPanel.removeAll(); // clear button panel also
     
      String s = input.getText(); // get the new denominations
     
      // the following line tokenizes on / in addition to regular
      // delimiters
      StringTokenizer inString = new StringTokenizer (s, " \t\n\r/");
  
      while (inString.hasMoreTokens()) {
         den.add (new Denomination(
             Integer.parseInt(inString.nextToken()),
             Integer.parseInt(inString.nextToken()),
             inString.nextToken(),
             inString.nextToken(),
             calcAmountTenderedField));
      } // end while there are more denominations to process
     
      output.append ("\nUsing an iterator:\n");
      for (Denomination d: den) {
         output.append (d.toString() + "\n");
      } // end for each denomination
     
      // sort the denominations in descending order, for change calculation
      Collections.sort(den); // smallest to largest
      Collections.reverse(den); // largest to smallest
  
      output.append ("\nSorted list Using an iterator:\n");
      for (Denomination d: den) {
         output.append (d.toString() + "\n");
      } // end for each denomination
     
      for (Denomination d: den) {
         calcButtonPanel.add (d.getButton());
      } // end for each denomination
  
   }//GEN-LAST:event_loadButtonPressed

   public Rational parseDenomination(String si) {
      String str;
      Rational n, r = new Rational();
      Denomination d = null;
      boolean foundFlag;
      StringTokenizer stt = new StringTokenizer(si);
      while (stt.hasMoreTokens()) {
         n = new Rational (Integer.parseInt(stt.nextToken()), 1);
         str = stt.nextToken();
         foundFlag = false;
     
         for (Denomination da: den) {
            if (da.getAbbr().equals(str)) {
               foundFlag = true;
               d = da;
               break;
            } // found the right denomination
         } // end for each denomination
         if (foundFlag) r = r.add(d.mult(n));
      } // end while there is more input
      return r;
   } // end parseDenomination  
  
   public String toValueString(Rational r) {
      String str = "";
      Rational remaining = r;
      int c;
  
      for (Denomination d: den) {
         c = 0;
         while (d.lessThanOrEqual(remaining)) {
            remaining = remaining.sub(d);
            c++;
         } // end while more of this denomination
         if (c > 0)
            str += "\n\t" + c + "\t" + d.getName() + "(s)";
      } // end for every denomination
      return str;
   } // end method toValueString
  
} // end class ChangeCalculator


/*
 * DenominationProperties.java
 *
 * Created on February 22, 2003, 1:08 AM
 */

/**
 *
 * @author  Nicholas Duchon
@see <a href="readme.html"> Documentation</a>
@see <a href=code.html.>Source code</a>
 */
class DenominationProperties extends JFrame {
   public static final long serialVersionUID = 123;
  
   private Denomination den;
  
   /** Creates new form DenominationProperties */
   public DenominationProperties(Denomination d) {
      initComponents();
      den = d;
      denNameField1.setText (den.getName());
      denAbbrField1.setText (den.getAbbr());
      denNumField1 .setText (Integer.toString(den.getNumerator()));
      denDenField1 .setText (Integer.toString(den.getDenominator()));
   } // end constructor
  
   private void initComponents() {//GEN-BEGIN:initComponents
      javax.swing.JLabel denFieldNameLabel1  = new javax.swing.JLabel ("Field name");
      javax.swing.JLabel denFieldValueLabel1 = new javax.swing.JLabel ("Field Value");
      javax.swing.JLabel denNumLabel1        = new javax.swing.JLabel ("Numerator");
      javax.swing.JLabel denDenLabel1        = new javax.swing.JLabel ("Denominator");
      javax.swing.JLabel denNameLabel1       = new javax.swing.JLabel ("Name");
      javax.swing.JLabel denAbbrLabel1       = new javax.swing.JLabel ("Abbreviation");
      javax.swing.JLabel denColorLabel1      = new javax.swing.JLabel ("Color");
     
      JButton denColorButton1    = new JButton ("Change Color");
  
      setLayout(new java.awt.GridLayout(6, 2));
     
      // center labels
      setH (denFieldNameLabel1);
      setH (denFieldValueLabel1);
      setH (denNumLabel1);
      setH (denDenLabel1);
      setH (denNameLabel1);
      setH (denAbbrLabel1);
      setH (denColorLabel1);
  
      // add labels and fields
      add (denFieldNameLabel1);
      add (denFieldValueLabel1);
      add (denNumLabel1);
      add (denNumField1);    // numerator field
      add (denDenLabel1);
      add (denDenField1);    // denominator field
      add (denNameLabel1);
      add (denNameField1);   // name field
      add (denAbbrLabel1);
      add (denAbbrField1);   // abbreviation field
      add (denColorLabel1);
      add (denColorButton1); // change color button
     
      denColorButton1.addActionListener (e -> denChangeColorButtonClicked (e));
  
      pack(); // otherwise get just the window control bar until resized
      setLocationRelativeTo (null);
   }//GEN-END:initComponents
  
   void setH (javax.swing.JLabel jft) {
      jft.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
   } // end setH

   //GEN-FIRST:event_denChangeColorButtonClicked
   private void denChangeColorButtonClicked(java.awt.event.ActionEvent evt) {
      den.setColor (javax.swing.JColorChooser.showDialog(null,
                     "Select button "+den.getName()+" Color",
                     java.awt.Color.orange));
     
   }//GEN-LAST:event_denChangeColorButtonClicked
  
//    public static void main(String args[]) {
//       Denomination d = new Denomination (1, 1, "a", "a");
//       DenominationProperties dp = new DenominationProperties(d);
//       dp.setVisible(true);
//       dp.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
//    } // end main
  
  
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private JTextField denAbbrField1 = new JTextField();
   private JTextField denNumField1  = new JTextField();
   private JTextField denDenField1  = new JTextField();
   private JTextField denNameField1 = new JTextField();
   // End of variables declaration//GEN-END:variables
  
} // end class DenominationProperties



// File: Rational.java
//  this class handles rational numbers
// Author: Nicholas Duchon
// Class: CMSC 230
// History:
//   2/1/03 - class created
//   2/2/03 - class finished
//  12/23/15 - updated for JDK 8


/** This class implements the functions of the rational number arithmetic

@author Nicholas Duchon
@see <a href="readme.html"> Documentation</a>
@see <a href=code.html>Source code</a>
*/

class Rational extends Object
     implements Comparable <Rational> {

   private int n, d;
  
   Rational (int a, int b) {
      setValue (a, b);
   } // end two parameter constructor
  
   Rational (int a) {
      setValue (a, 1);
   } // end one parameter constructor
  
   Rational () {
      setValue (0, 1);
   } // end no parameter constructor
  
   public void setValue (int a, int b) {
      n = a;
      d = b;
      reduce();
   } // end setValue
  
   public int getNumerator () {
      return n;
   } // end method getNumerator
  
   public int getDenominator () {
      return d;
   } // end method getDenominator
  
   void reduce () {
      int a, b, t;
      if (d == 0) { // zero denominator
         n = 0;
         d = 1;
      } // end if bad rat
      if (d < 0) { // negative denominator
         n = -n;
         d = -d;
      } // end if den < 0
      a = n;
      if (a<0) a=-a; // for gcd, use positive numbers
      b = d;
      while (a%b != 0) { // calculate gcd
         t = a%b;
         a = b;
         b = t;
      } // end while recursing for gcd
      n = n/b; // divide by gcd
      d = d/b;
   } // end method reduce
  
   public Rational add (Rational r) {
      return new Rational (n*r.d + d*r.n, d*r.d);
   } // end method add
  
   public Rational sub (Rational r) {
      return new Rational (n*r.d - d*r.n, d*r.d);
   } // end method add
  
   public Rational mult (Rational r) {
      return new Rational (n*r.n, d*r.d);
   } // end method add
  
   public Rational div (Rational r) {
      return new Rational (n*r.d, d*r.n);
   } // end method add
  
   public boolean lessThan (Rational r) {
      return (n*r.d < d*r.n);
   } // end method lessThan
  
   public boolean lessThanOrEqual (Rational r) {
      return (n*r.d <= d*r.n);
   } // end method lessThan
  
   // not required by Comparable interface, but used by CompareTo method
   public int compare (Rational r1, Rational r2) {
      if (r1.equals(r2))
         return 0;
      if (r1.lessThan(r2))
         return -1;
      return 1;
   } // end compare
  
   // override Object.equals!
   public boolean equals (Rational r) {
      if (n == r.n && d == r.d)
         return true;
      return false;
   } // end method equals

   // required by Comparable interface
   public int compareTo (Rational r) {
      return compare (this, r);
   } // end method compareTo

   static Rational readRational (String st) {
      Scanner s = new Scanner (st);
      // allow spaces + / + spaces or initial spaces
      s.useDelimiter ("(\\s*/\\s*)|(^\\s*)");
      return new Rational (s.nextInt(), s.nextInt());
   } // end readRational String

   public String toString () {
      return n + "/" + d;
   } // end toString

//    public static void main (String args []) throws IOException {
//       BufferedReader ins = new BufferedReader (
//          new InputStreamReader(System.in));
//      
//       Rational r1, r2;          
//   
//       System.out.println ("Starting");
//       System.out.print ("Rational: " );
//       r1 = readRational (ins.readLine());
//       System.out.print ("Rational: " );
//       r2 = readRational (ins.readLine());
//       System.out.println ("r1: " + r1);
//       System.out.println ("r2: " + r2);
//       System.out.println ("r3=r1+r2: " + r1.add(r2));
//       System.out.println ("r3=r1-r2: " + r1.sub(r2));
//       System.out.println ("r3=r1*r2: " + r1.mult(r2));
//       System.out.println ("r3=r1/r2: " + r1.div(r2));
//       System.out.println ("r1< r2 : " + r1.lessThan(r2));
//       int c = r1.compareTo (r2);
//       System.out.println ("r1 ? r2: " + c + " - meaning: " + r1 + ((c == 0)?" = ":(c == 1)?" > ":" < ") + r2);
//    } // end method main
} // end class Rational






/*
 * Denomination.java
 *
 * Created on February 16, 2003, 6:28 PM
 */

/**
 *
 * @author  Nicholas Duchon
@see <a href="readme.html"> Documentation</a>
@see <a href=code.html>Source code</a>
 */
class Denomination extends Rational {
    private JButton jbDen; // denButton;
    private JTextField outputField;
    private java.awt.Color color = java.awt.Color.pink; // new java.awt.Color (255, 255, 255);
    private String name;
    private String abbreviation;
   
    /** Creates a new instance of Denomination */
    public Denomination(int num, int den, String n, String b) {
        this.setData (num, den, n, b, null);
    }
   
    public Denomination (int num, int den, String n, String b, JTextField jtf) {
        this.setData (num, den, n, b, jtf);
    } // end 5 parameter constructor
   
    private void setData (int a, int b, String c, String d, JTextField jtf) {
        this.setValue(a, b); // sets value of rational element
        this.name = c;
        this.abbreviation = d;
        outputField = jtf;
        jbDen = new JButton (d);
        jbDen.setBackground(color);
        jbDen.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                denButtonHandler(evt);
            }
        });
    } // end set Data
   
    private void denButtonHandler(java.awt.event.MouseEvent evt) {
        // Add your handling code here:
        if ((evt.getModifiers() & java.awt.event.MouseEvent.BUTTON1_MASK) != 0) {
            System.out.println ("button 1 clicked");
            outputField.setText(outputField.getText()+" 1 "+abbreviation);
        }
        else if ((evt.getModifiers() & java.awt.event.MouseEvent.BUTTON2_MASK) != 0) {
            System.out.println ("Button 2 clicked");
            color = javax.swing.JColorChooser.showDialog(null,
                "Select button "+getNumerator()+" Color",
                java.awt.Color.orange);
            jbDen.setBackground(color);
        }
        else if ((evt.getModifiers() & java.awt.event.MouseEvent.BUTTON3_MASK) != 0) {
            System.out.println("Button 3 clicked");
            DenominationProperties dp = new DenominationProperties (this);
            // dp.setSize (200, 300);
            dp.setVisible(true);
        }
    }
    public String getName () {
        return name;
    } // end method getName
   
    public String getAbbr () {
        return abbreviation;
    } // end getAbbr
   
    public void setColor (java.awt.Color c) {
        color = c;
        jbDen.setBackground(color);
    } // end method setColor
   
    public void setName (String n) {
        name = n;
    } // end setName
   
    public void setAbbr (String a) {
        abbreviation = a;
    } // end setAbbr
   
    public void setOutputField (JTextField o) {
        outputField = o;
    } // end setOutputField
   
    public JButton getButton () {
        return jbDen;
    } // end getButton
   
    public String toString() {
        String str = new String (getNumerator() + " " + name + "(s)");
        str += " -- Fraction: " + super.toString() +
               " name: " + name + ", abbreviation: " + abbreviation;
        return  str;
    } // end toString method
   
} // end class Denomination


Date: Jun 20, 2017