/*
Chapter 8:      Midterms
Programmer:		Ryan Barker
Date:				3-12-2013
Program name:	Midterm2.java
*/

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JTextField.*;
import java.text.*;
import java.util.*;
import java.lang.Integer.*;

public class Midterm2 extends JFrame implements ActionListener
{
//Declare output stream
DataOutputStream output;
    
        //Construct a panel for each row
        JPanel firstRow = new JPanel();
        JPanel secondRow = new JPanel();
        JPanel thirdRow = new JPanel();
        JPanel fourthRow = new JPanel();
        JPanel fifthRow = new JPanel();
        JPanel sixthRow = new JPanel();
        JPanel seventhRow = new JPanel();
        JPanel eighthRow = new JPanel();
 
        //Construct a panel for the fields and buttons
        JPanel fieldPanel = new JPanel();
        JPanel buttonPanel = new JPanel();
        
        //Variable declaration per wall
        //int wall1Area = 81.6;
        int widthWall2, heighthWall2, areaWall2;
        int widthWall3, heighthWall3, areaWall3;
        int widthWall4, heighthWall4, areaWall4;
        int totalArea, totalRolls;

        //Construct labels and text boxes
        JLabel totalRollsLabel = new JLabel("Total # of rolls needed: ");
        JLabel areaLabel = new JLabel("Width x Heighth = Area");
        JLabel wall1Label = new JLabel("Wall 1: 10.2              8                = 81.6 sqft");
        JLabel wall2Label = new JLabel("Wall 2");
            JTextField wall2Width = new JTextField(5);
            JTextField wall2Heighth = new JTextField(5);
				JTextField wall2Area = new JTextField(5);
        JLabel wall3Label = new JLabel("Wall 3");
            JTextField wall3Width = new JTextField(5);
				JTextField wall3Heighth = new JTextField(5);
				JTextField wall3Area = new JTextField(5);
        JLabel wall4Label = new JLabel("Wall 4");
            JTextField wall4Width = new JTextField(5);
				JTextField wall4Heighth = new JTextField(5);
				JTextField wall4Area = new JTextField(5);
        JLabel totalAreaLabel = new JLabel("Total Area + 10% = ");
         
		//Construct button
		JButton submitButton = new JButton("Calculate");
 
		public static void main(String[] args) //main method
		{
			//set the look and feel of the interface
			try
			{
				UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
			}
			catch(Exception e)
			{
			    JOptionPane.showMessageDialog(null, "The UIManager could not set the Look and Feel.","error",JOptionPane.INFORMATION_MESSAGE);
			}
			
			//main method executes at run time
			Midterm2 f = new Midterm2();
			f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			f.setSize(450,300);
			f.setTitle("Crandall Power and Light Customer Payments");
			f.setResizable(false);
			f.setLocation(200,200);
			f.setVisible(true);
		}
         
		public Midterm2()
		{
			Container c = getContentPane();//content pane assigned
				c.setLayout((new BorderLayout()));//layout methods
					fieldPanel.setLayout(new GridLayout(8,1));
					FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
						firstRow.setLayout(rowSetup);//layout methods
						secondRow.setLayout(rowSetup);
						thirdRow.setLayout(rowSetup);
						fourthRow.setLayout(rowSetup);
						fifthRow.setLayout(rowSetup);
						sixthRow.setLayout(rowSetup);
						seventhRow.setLayout(rowSetup);
						eighthRow.setLayout(rowSetup);
				buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));//layout methods
         
        //Add fields to rows         
            firstRow.add(areaLabel);
         
            secondRow.add(wall1Label);         
         
            thirdRow.add(wall2Label);
            thirdRow.add(wall2Width);
            thirdRow.add(wall2Heighth);
				thirdRow.add(wall2Area);
         
            fourthRow.add(wall3Label);
				fourthRow.add(wall3Width);
				fourthRow.add(wall3Heighth);
				fourthRow.add(wall3Area);
         
            fifthRow.add(wall4Label);
				fifthRow.add(wall4Width);
				fifthRow.add(wall4Heighth);
				fifthRow.add(wall4Area);
         
            sixthRow.add(totalAreaLabel);
            seventhRow.add(totalRollsLabel);
         
            eighthRow.add(submitButton);
         
            //Add rows to panel
            fieldPanel.add(firstRow);
            fieldPanel.add(secondRow);
            fieldPanel.add(thirdRow);
            fieldPanel.add(fourthRow);
            fieldPanel.add(fifthRow);
            fieldPanel.add(sixthRow);
            fieldPanel.add(seventhRow);
            fieldPanel.add(eighthRow);
         
         
            //Add button to panel
            buttonPanel.add(submitButton);
         
            //Add panels to frame
            c.add(fieldPanel, BorderLayout.CENTER);
            c.add(buttonPanel, BorderLayout.SOUTH);
         
            //Add functionality to buttons
            submitButton.addActionListener(this);
            
            //get current date and open the file
            Date today = new Date();
            SimpleDateFormat myFormat = new SimpleDateFormat("MMddyy");
            String filename = "rolls" + myFormat.format(today);
         
        try
        {
            output = new DataOutputStream(new FileOutputStream(filename));
        }
        catch(IOException io)
        {
            JOptionPane.showMessageDialog(null, "The program could not create a storage location.  Please check the disk drive and the run the program again.","Error",JOptionPane.INFORMATION_MESSAGE);
         
            System.exit(1);
        }
            addWindowListener(
                new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit and submit the file?", "File Submission",JOptionPane.YES_NO_OPTION);
                        if (answer == JOptionPane.YES_OPTION)
                            System.exit(0);
                        }
                    }
                    );
         
        }
        public void actionPerformed(ActionEvent e)
        {
            String arg = e.getActionCommand();
         
            if (checkFields())
            {
                try
                {
                    output.writeUTF(wall2Width.getText());
                    output.writeUTF(wall2Heighth.getText());
                    output.writeUTF(wall3Width.getText());
                    output.writeUTF(wall3Heighth.getText());
                    output.writeUTF(wall4Width.getText());
                    output.writeUTF(wall4Heighth.getText());     
                    JOptionPane.showMessageDialog(null,"The wall dimensions have been saved.", "Submission Successful", JOptionPane.INFORMATION_MESSAGE);
            }
            catch(IOException c)
            {
                System.exit(1);
            }
            clearFields();
        }
        }
         
        public boolean checkFields()
        {
            if ((wall2Width.getText().compareTo("")<1)      ||
                (wall2Heighth.getText().compareTo("")<1)    ||
                (wall3Width.getText().compareTo("")<1)      ||
                (wall3Heighth.getText().compareTo("")<1)    ||
                (wall4Width.getText().compareTo("")<1)      ||
                (wall4Heighth.getText().compareTo("")<1))
                {
                    JOptionPane.showMessageDialog(null, "You must complete all fields", "Data Entry Error", JOptionPane.WARNING_MESSAGE);
                    return false;
                }
                else
                {
		    widthWall2 = Integer.parseInteger(wall2Width.getText());
                    heighthWall2 = Integer.parseInteger(wall2Heighth.getText());
                    //wall2Area = widthWall2*heighthWall2;
		    wall2Area = Integer.valueOf(wall2Width.getText()) * Integer.valueOf(wall2Heighth.getText());
                    /*
                    widthWall3 = Integer.parseDouble(wall3Width.getText());
                    heighthWall3 = Integer.parseDouble(wall3Heighth.getText());
		    wall3Area = widthWall3 * heighthWall3;
                    
                    widthWall4 = Integer.parseDouble(wall4Width.getText());
                    heighthWall4 = IntegerDouble.parseDouble(wall4Heighth.getText());
                    wall4Area = widthWall4 * heighthWall4;
                    
                    totalArea = wall1Area + wall2Area + wall3Area + wall4Area;
                    totalArea = totalArea + (totalArea*.10);
                    
                    totalRolls = totalArea/35;*/
                    //return true;
                }
            }
         
            public void clearFields()
            {
                //clear fields and reset the focus
                wall2Width.setText("");
                wall2Heighth.setText("");
                wall3Width.setText("");
                wall3Heighth.setText("");
                wall4Width.setText("");
                wall4Heighth.setText("");
		wall2Area.setText("");
		wall3Area.setText("");
		wall4Area.setText("");
            }
        }
 
 