For the error and my question, skip to the bottom. I just posted all of the code in case it's needed.

/***********************************************************************************
 *
 * File:        Customer.java
 *
 * Author:      Austin Smith
 *
 * Date:        04/04/2011
 *
 * Description: The Customer class handles all client information
 *              such as name and address.
 *
 **********************************************************************************/

public class Customer
{
   /*** Defaults/Constants ***/

   public static final String DEFAULT_NAME = "ERROR: Invalid Name";
   public static final String DEFAULT_ADDRESS = "ERROR: Invalid Address";
   public static final String DEFAULT_CITY = "ERROR: Invalid City";
   public static final String DEFAULT_STATE = "ER";
   public static final String DEFAULT_ZIP_CODE = "ERROR";

   /*** Instance Variables ***/

   private String name;
   private String address;
   private String city;
   private String state;
   private String zipCode;

   /*** Constructors ***/

   public void Customer()
   {
   	  name    = DEFAULT_NAME;
   	  address = DEFAULT_ADDRESS;
   	  city    = DEFAULT_CITY;
   	  state   = DEFAULT_STATE;
   	  zipCode = DEFAULT_ZIP_CODE;
   }

   public void Customer( String name,
   	                     String address,
   	                     String city,
   	                     String state,
   	                     String zipCode )
   {
   	  this.name    = name;
   	  this.address = address;
   	  this.city    = city;
   	  this.state   = state;
   	  this.zipCode = zipCode;
   }
   /*** Accessors ***/

   public String getName()
   {
   	  return name;
   }

   public String getAddress()
   {
   	  return address;
   }

   public String getCity()
   {
   	  return city;
   }

   public String getState()
   {
   	  return state;
   }

   public String getZipCode()
   {
   	  return zipCode;
   }
   /*** Mutators ***/

   public void setName( String name )
   {
   	  if ( verifyOneChar( name ) )
   	  	 this.name = name;

   	  else
   	  	 this.name = DEFAULT_NAME;
   }

   public void setAddress( String address )
   {
   	  if ( verifyOneChar( address ) )
   	  	 this.address = address;

   	  else
   	  	 this.address = DEFAULT_ADDRESS;
   }

   public void setCity( String city )
   {
   	  if ( verifyOneChar( city ) )
   	  	 this.city = city;

   	  else
   	  	 this.city = DEFAULT_CITY;
   }

   public void setState( String state )
   {
   	  if ( verifyState( state ) )
   	     this.state = state;

   	  else
   	     this.state = DEFAULT_STATE;
   }

   public void setZipCode( String zipCode )
   {
   	  if ( verifyZipCode( zipCode ) )
   	     this.zipCode = zipCode;

   	  else
   	     this.zipCode = DEFAULT_ZIP_CODE;
   }
   /*** Helper Methods ***/

   public boolean verifyOneChar( String string )
   {
   	  boolean atLeastOneChar;

   	  if ( string.length() >= 1 )
   	     atLeastOneChar = true;

   	  else
   	  	 atLeastOneChar = false;

   	  return atLeastOneChar;
   }

   public boolean verifyState( String state )
   {
   	  boolean validState;

   	  if ( state.length() == 2 )
   	     validState = true;

   	  else
   	     validState = false;

   	  return validState;
   }

   public boolean verifyZipCode( String zipCode )
   {
      boolean validZipCode;

      if ( zipCode.length() == 5 )
         validZipCode = true;

      else if ( zipCode.length() == 10 )
         validZipCode = true;

      else
         validZipCode = false;

      return validZipCode;
   }

}
/***********************************************************************************
 *
 * File:        Order.java
 *
 * Author:      Austin Smith
 *
 * Date:        04/04/2011
 *
 * Description: The Order class keeps track of order information such as the
 *              number of bags ordered and the date on which it was ordered.
 *              It also calculates the number of boxes of each size needed
 *              to ship the order while incurring the least amount of cost.
 *
 **********************************************************************************/
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;

public class Order
{
   /*** Defaults/Constants ***/

   public static final int MINIMUM_BAGS_ORDERED = 1;

   public static final int DEFAULT_BAGS_ORDERED = 20;

   public static final int DEFAULT_MEDIUM_BOXES = 0;
   public static final int DEFAULT_SMALL_BOXES  = 0;

   public static final int MAXIMUM_MEDIUM_BOXES = 1;
   public static final int MAXIMUM_SMALL_BOXES  = 1;

   public static final int BAGS_PER_LARGE_BOX   = 20;
   public static final int BAGS_PER_MEDIUM_BOX  = 10;
   public static final int BAGS_PER_SMALL_BOX   = 5;

   /*** Instance Variables ***/

   private int bagsOrdered;
   private int largeBoxes;
   private int mediumBoxes;
   private int smallBoxes;
   private String dateOrdered;

   /*** Constructors ***/

   public Order( int bagsOrdered )
   {
      this.bagsOrdered = setBagsOrdered( bagsOrdered );

      largeBoxes = getLargeBoxes( order.bagsOrdered );
      mediumBoxes = getMediumBoxes( order.bagsOrdered );
      smallBoxes = getSmallBoxes( order.bagsOrdered );

      dateOrdered = getDate();
   }
   /*** Accessors ***/

   public int getBagsOrdered()
   {
   	  return order.bagsOrdered;
   }

   public int getLargeBoxes( int bagsOrdered )
   {
   	  int largeBoxes = ( bagsOrdered / BAGS_PER_LARGE_BOX );

      return largeBoxes;
   }

   public int getMediumBoxes( int bagsOrdered )
   {
   	  int mediumBoxes = DEFAULT_MEDIUM_BOXES;

   	  if ( ( bagsOrdered % BAGS_PER_LARGE_BOX ) > BAGS_PER_SMALL_BOX )
   	     mediumBoxes = MAXIMUM_MEDIUM_BOXES;

   	  return mediumBoxes;
   }

   public int getSmallBoxes( int bagsOrdered )
   {
   	  int smallBoxes = DEFAULT_SMALL_BOXES;

   	  if ( ( bagsOrdered % BAGS_PER_LARGE_BOX ) <= BAGS_PER_SMALL_BOX )
   	     smallBoxes = MAXIMUM_SMALL_BOXES;

   	  return smallBoxes;
   }
   /*** Mutators ***/

   public int setBagsOrdered( int bagsOrdered )
   {
   	  if ( verifyQuantity( bagsOrdered ) )
   	     this.bagsOrdered = bagsOrdered;

   	  else
   	     this.bagsOrdered = DEFAULT_BAGS_ORDERED;

   	  return this.bagsOrdered;
   }
   /*** Hepler Methods ***/

   private boolean verifyQuantity( int quantity )
   {
   	  boolean validQuantity;

   	  if ( quantity >= MINIMUM_BAGS_ORDERED )
   	     validQuantity = true;

   	  else
   	     validQuantity = false;

   	  return validQuantity;
   }

   public String getDate()
   {
   	  DateFormat dateFormat = new SimpleDateFormat("MMMMMMMMM d, yyyy");
      Calendar calendar = Calendar.getInstance();

      return dateFormat.format( calendar.getTime() );
   }
   /*** Instantiate order ***/

   Order order = new Order( 0 );
}
/***********************************************************************************
*
* File:        Invoice.java
*
* Author:      Austin Smith
*
* Date:        04/04/2011
*
* Description: The Invoice class calculates the cost of an order including
*              the cost of the coffee and the boxes used to ship the coffee.
*              It also calculates the expected date of arrival based on the
*              date the order was placed.
*
**********************************************************************************/
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;

public class Invoice
{
   /*** Defaults/Constants ***/

   public static final double COFFEE_PRICE_PER_BAG = 5.50;
   public static final double COST_PER_LARGE_BOX   = 1.80;
   public static final double COST_PER_MEDIUM_BOX  = 1.00;
   public static final double COST_PER_SMALL_BOX   = 0.60;

   /*** Instance Variables ***/

   private int bagsOrdered;
   private double coffeeCost;

   private int largeBoxes;
   private int mediumBoxes;
   private int smallBoxes;
   private double largeBoxCost;
   private double mediumBoxCost;
   private double smallBoxCost;

   private String dateOrdered;
   private String dateOfArrival;

   /*** Constructor ***/

   public Invoice( int bagsOrdered )
   {
   	  this.bagsOrdered = bagsOrdered;
   	  coffeeCost       = invoice.getCoffeeCost( order.getBagsOrdered() );

   	  largeBoxes    = order.getLargeBoxes(  order.getBagsOrdered() );
   	  mediumBoxes   = order.getMediumBoxes( order.getBagsOrdered() );
   	  smallBoxes    = order.getSmallBoxes(  order.getBagsOrdered() );
   	  largeBoxCost  = invoice.getLargeBoxCost();
   	  mediumBoxCost = invoice.getMediumBoxCost();
   	  smallBoxCost  = invoice.getSmallBoxCost();

   	  dateOrdered = order.getDate();
   	  dateOfArrival = getDateOfArrival();
   }

   /*** Instantiates invoice and order ***/

   Invoice invoice = new Invoice( 0 );

   Order order = new Order( 0 );

   /*** Accessors ***/

   public double getCoffeeCost( int bagsOrdered )
   {
   	  double coffeeCost = ( COFFEE_PRICE_PER_BAG * bagsOrdered );

   	  return coffeeCost;
   }

   public double getLargeBoxCost()
   {
   	  double largeBoxCost = ( COST_PER_LARGE_BOX *
   	  	                      order.getLargeBoxes( order.getBagsOrdered() ) );

   	  return largeBoxCost;
   }

   public double getMediumBoxCost()
   {
   	  double mediumBoxCost = ( COST_PER_MEDIUM_BOX *
   	  	                       order.getMediumBoxes( order.getBagsOrdered() ) );

   	  return mediumBoxCost;
   }

   public double getSmallBoxCost()
   {
   	  double smallBoxCost = ( COST_PER_SMALL_BOX *
   	  	                      order.getSmallBoxes( order.getBagsOrdered() ) );

   	  return smallBoxCost;
   }

   public double getTotalCost()
   {
   	  double totalCost = ( coffeeCost + largeBoxCost
   	  	                 + mediumBoxCost + smallBoxCost );

   	  return totalCost;
   }

   public String getDateOfArrival()
   {
   	  DateFormat dateFormat = new SimpleDateFormat("MMMMMMMMM d, yyyy");
      Calendar calendar = Calendar.getInstance();
      calendar.add( Calendar.DATE,14 );

      return dateFormat.format( calendar.getTime() );
   }
}
/***********************************************************************************
*
* File:        CoffeeGUI.java
*
* Author:      Austin Smith
*
* Date:        04/04/2011
*
* Description: The CoffeeGUI class is a GUI which uses Customer.java, Order.java,
*              and Invoice.java. It accepts input for customer and order
*              information and displays an invoice containing the information
*              necessary to bill the customer and ship the order.
*
**********************************************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CoffeeGUI implements ActionListener
{
   /*** Instance Variables ***/

   private JLabel lblName;
   private JLabel lblBags;
   private JLabel lblAddress;
   private JLabel lblCity;
   private JLabel lblState;
   private JLabel lblZipCode;

   private JTextField txfName;
   private JTextField txfBags;
   private JTextField txfAddress;
   private JTextField txfCity;
   private JTextField txfState;
   private JTextField txfZipCode;

   private JPanel inputPanel;
   private JPanel buttonPanel;
   private JPanel displayPanel;

   private JTextArea txaInvoice;

   private JButton btnClear;
   private JButton btnExit;
   private JButton btnCreate;
   private JButton btnPrint;

   private JFrame myGUI;

   /*** Constructor ***/

   public CoffeeGUI()
   {
      myGUI = createJFrame();

      Container leftSide = new Container();
      leftSide.setLayout( new BorderLayout() );

      inputPanel = createInputPanel();
      leftSide.add( "North", inputPanel );

      buttonPanel = createButtonPanel();
      leftSide.add( "South", buttonPanel );

      Container bothSides = myGUI.getContentPane();
      bothSides.setLayout( new BorderLayout() );

      bothSides.add( "West", leftSide );

      displayPanel = createDisplayPanel();
      bothSides.add( "East", displayPanel );

      myGUI = setJFrame();
   }
   /*** Instantiate Customer, Order, and Invoice ***/

   Customer customer = new Customer();

   Order order = new Order( 0 );

   Invoice invoice = new Invoice ( 0 );

   /*** Create GUI ***/

   private JFrame createJFrame()
   {
      JFrame myGUI = new JFrame();

      return myGUI;
   }

   private JFrame setJFrame()
   {
   	  myGUI.setSize( 1000, 500 );
      myGUI.setLocation( 10, 10 );
      myGUI.setTitle( "Place an order - MyJava Coffee Outlet" );
      myGUI.setVisible( true );

      return myGUI;
   }

   private JPanel createInputPanel()
   {
      JPanel inputPanel = new JPanel();

      lblName    = new JLabel( "           Name " );
      lblBags    = new JLabel( " Order Quantity " );
      lblAddress = new JLabel( "        Address " );
      lblCity    = new JLabel( "           City " );
      lblState   = new JLabel( "          State " );
      lblZipCode = new JLabel( "            ZIP " );

      txfName    = new JTextField( 20 );
      txfBags    = new JTextField( 20 );
      txfAddress = new JTextField( 20 );
      txfCity    = new JTextField( 15 );
      txfState   = new JTextField(  2 );
      txfZipCode = new JTextField( 10 );

      GridLayout inputGrid = new GridLayout(6,2,0,20);
      inputPanel = new JPanel( inputGrid );

      inputPanel.add( lblName    );
      inputPanel.add( txfName    );
      inputPanel.add( lblBags    );
      inputPanel.add( txfBags    );
      inputPanel.add( lblAddress );
      inputPanel.add( txfAddress );
      inputPanel.add( lblCity    );
      inputPanel.add( txfCity    );
      inputPanel.add( lblState   );
      inputPanel.add( txfState   );
      inputPanel.add( lblZipCode );
      inputPanel.add( txfZipCode );

      return inputPanel;
   }

   private JPanel createButtonPanel()
   {
      JPanel buttonPanel = new JPanel();

      btnClear  = new JButton( " Clear "          );
      btnExit   = new JButton( " Exit "           );
      btnCreate = new JButton( " Create Invoice " );
      btnPrint  = new JButton( " Print "          );

      btnClear.addActionListener(  this );
      btnExit.addActionListener(   this );
      btnCreate.addActionListener( this );
      btnPrint.addActionListener(  this );

      buttonPanel.add( btnClear  );
      buttonPanel.add( btnExit   );
      buttonPanel.add( btnCreate );
      buttonPanel.add( btnPrint  );

      return buttonPanel;
   }

   private JPanel createDisplayPanel()
   {
      JPanel displayPanel = new JPanel();

      txaInvoice = new JTextArea( 350, 350 );
      displayPanel.add( txaInvoice );

      return displayPanel;
	}

   public void actionPerformed( ActionEvent event )
   {
      if ( event.getSource() == btnClear )
      {
         processClearButton();
      }

      else if ( event.getSource() == btnExit )
      {
         processExitButton();
      }

      else if ( event.getSource() == btnCreate )
      {
         processCreateButton();
      }

      else if ( event.getSource() == btnPrint )
      {
         processPrintButton();
      }

      else
         txaInvoice.setText( "WARNING: Some unplanned action event happened" );
   }

   /*** Helper Methods ***/

   private void processClearButton()
   {
      txfName.setText(    "" );
      txfBags.setText(    "" );
      txfAddress.setText( "" );
      txfCity.setText(    "" );
      txfState.setText(   "" );
      txfZipCode.setText( "" );
      txaInvoice.setText( "" );
   }

   private void processExitButton()
   {
      System.exit( 0 );
   }

   private void processCreateButton()
   {
      createInvoice();
   }

   private void processPrintButton()
   {
      System.out.println( "" );
   }

   private void readText()
   {
   	  customer.setName(     txfName.getText() );
      order.setBagsOrdered( Integer.parseInt( txfBags.getText() ) );
      customer.setAddress(  txfAddress.getText() );
      customer.setCity(     txfCity.getText() );
      customer.setState(    txfState.getText() );
      customer.setZipCode(  txfZipCode.getText() );
   }

   private void createInvoice()
   {
      readText();

      txaInvoice.append("            Customer Name:  " + customer.getName() + "\n"
      	              + "   Number of Bags Ordered:  " + order.getBagsOrdered() + "\n"
      	              + "           Purchase Price:                $ " + invoice.getCoffeeCost( order.getBagsOrdered() ) + "\n\n"
      	              + "               Boxes Used: \n"
      	              + "                        Large:  " + order.getLargeBoxes( order.getBagsOrdered() ) + "   Cost: $   " + invoice.getLargeBoxCost() + "\n"
      	              + "                       Medium:  " + order.getMediumBoxes( order.getBagsOrdered() ) + "   Cost: $   " + invoice.getMediumBoxCost() + "\n"
      	              + "                        Small:  " + order.getSmallBoxes( order.getBagsOrdered() ) + "   Cost: $   " + invoice.getSmallBoxCost() + "\n\n\n"
      	              + "               Total Cost:                $ " + invoice.getTotalCost() + "\n\n"
      	              + "            Date of Order:  " + order.getDate() + "\n"
      	              + " Expected Date of Arrival:  " + invoice.getDateOfArrival() + "\n\n"
      	              + " Shipping Address:    " + customer.getAddress() + "\n"
      	              + "                      " + customer.getCity() + ", " + customer.getState() + "  " + customer.getZipCode() + "\n" );
   }

   /*** Main Method ***/

   public static void main( String arrayIdentifier[] )
   {
      CoffeeGUI coffeeGUI = new CoffeeGUI();
   }
}

(sorry that the createInvoice() part is messy... once I get the GUI working I'll make the adjustments to the invoice layout and then I'll be cleaning it up)

When I run the GUI:

Exception in thread "main" java.lang.StackOverflowError
	at Order.<init>(Order.java:49)
	at Order.<init>(Order.java:127)
	at Order.<init>(Order.java:127)
	at Order.<init>(Order.java:127)
	at Order.<init>(Order.java:127)
        ...
        ...

Why am I getting this error? I can't find any recursion that might be causing it. Thanks in advance for the help.

Recommended Answers

All 3 Replies

Why do you have this line in the Order class?

Order order = new Order( 0 );

Because without it I was getting some other error I think.

But I took it out and now it works. I should have known better than to do that. I did the same thing in Invoice. For some reason I thought I had to do that.

Now I have a new problem though and that is that my create button doesn't work and just turns the whole window white. When I move the cursor over the buttons they show up though. If you don't have any ideas about what may be causing that I'm going to start a new thread for that question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.