/*
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.*;
{
//Declare output stream
//Construct a panel for each row
//Construct a panel for the fields and buttons
//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 wall1Label
= new JLabel("Wall 1: 10.2 8 = 81.6 sqft");
//Construct button
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"); }
{
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 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);
//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
//Add functionality to buttons
submitButton.addActionListener(this);
//get current date and open the file
String filename
= "rolls" + myFormat.
format(today
);
try
{
}
{
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);
}
addWindowListener(
{
{
int answer
= JOptionPane.
showConfirmDialog(null,
"Are you sure you want to exit and submit the file?",
"File Submission",
JOptionPane.
YES_NO_OPTION); }
}
);
}
{
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); }
{
}
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("");
}
}