| Name: _____________________ | Class: CIS 355 |
| SSN/ID: _____________________ | Section & Group: ____________ |
Lab
Overview: General SpecsTCO:
| 07 | Given a business systems application requiring detail reporting and summarization of sorted user data, such as creating a sales report from a sorted customer file, involving control break logic: design and code a program that produces a report that includes both detail lines and control totals in the appropriate format. |
Desired (Lab) Learning Outcome: The objective of this lab is to use an IDE to write a Swing GUI program that uses Polymorphism, Inheritance and Composition along with files.
Dependencies: Reading assignments, on-site and on-line topic/lectures.
Lab Assignment: Using an IDE write a Swing GUI program that uses a Polymorphism, Inheritance, and Composition Architecture along with file usage.
Narrative/Case Study:
The objective of this lab is to use an IDE to write a Swing GUI program that uses Polymorphism, Inheritance, and Composition along with files. Since this is Java, we concentrate on the GUI, files and a PICA (Polymorphic, Inheritance, Composition Architecture). The Check Acct button gives us a Checking Account GUI, the Mortgage button gives us a Mortgage GUI.
You can write to and read from a file that works with the CheckAcct Gui — which you need to appear in order to do file operations. The Mortgage GUI file operations do not work. That is left as an exercise for you — if you want to and/or have the time.
The polymorphic operations are kept to a bare minimum. A few of my students have trouble understanding polymorphism and some more think they understand it, but really don't. And then there are those few who get it the first time — maybe. Take a look at the Visio picture/file — 9.jpg and Lab6.vsd.
For files, I use sequential output and input files for CheckAcct, while you should set up the files for the Mortgage class. Also notice that I have 3 identifiers (variables) for Account, CheckAcct and Mortgage. This is Composition. As before, this Lab mimics the previous Labs.
Environment: Once again, you should have Sun's JDK installed, as well as, if you like, an IDE like JCreator LE.
Remarks: Here is the code for the top of the class is:
/**
* Lab 7 for CIS 355 with Prof. Ricky J. Sethi
* GUI Lab Description
*
* @author Your Name Here
* @version Version 1.0 as of 2006-05-05
* For: CIS 355 with Professor Sethi
*
* Specification:
* input(keyboard):
* output(screen):
******************************************************************/
package cis355Package; // Need to place .class files in cis355Package dir
// Use java cis355Package/Lab6Cis355 to run
// Make sure gifs directory is at same level as cis355Package directory
import javax.swing.*; // Need for Swing
import java.awt.*; // Need for awt
import java.awt.event.*; // Need for events
import java.io.*; // Need for files
public class Lab7Cis355 extends JFrame { // Start of class Lab7
.
.
.
} // End of class
The attributes are:
// attributes private JButton jbutClear; private JButton jbutEnd; private JPanel jpanTop; //top jpanel private JPanel jpanMiddle; //middle jpanel private JPanel jpanBottom; //bottom panel //top panel private JTextArea jtxaTop; //jtextarea for top jpanel private JButton jbutRead; //read the data from a file private JButton jbutWrite; //write the data to a file private FlowLayout flLt1; //flow layout for top jpanel private JFileChooser jfcWrite; //JFilechooser to write data private JFileChooser jfcRead; //JFilechooser to read data private JButton jbutCheckAcct; //button for checking account private JButton jbutMortgage; //button for mortgage private boolean isCheckAcct = false; // is it a checking acct??? private boolean isMortgage = false; //is it a mortgage??? //middle panel private JButton jbutName; //button for name - Account class private JTextField jtxfName; //text field for first name - Account class private JButton jbutAddress; //button for address - Account class private JTextField jtxfAddress; //text field for address - Account class private JButton jbutCSZ; //button for city, state, zip - Account class private JTextField jtxfCSZ; //text field for city, stat, zip - Account class private JButton jbutCheckAcctNum; //button for number - CheckAcct class private JTextField jtxfCheckAcctNum; //text field for number - CheckAcct class private JButton jbutCheckAcctType; //button for type - CheckAcct class private JTextField jtxfCheckAcctType; //text field for type - CheckAcct class private JButton jbutCheckAcctDep; //button for deposit - CheckAcct class private JTextField jtxfCheckAcctDep; //text field for deposit - CheckAcct class private JButton jbutCheckAcctBal; //button for balance - CheckAcct class private JTextField jtxfCheckAcctBal; //text field for balance - CheckAcct class private JButton jbutMortNum; //button for number - Mortgage class private JTextField jtxfMortNum; //text field for number - Mortgage class private JButton jbutMortAmt; //button for amount - Mortgage class private JTextField jtxfMortAmt; //text field for amount - Mortgage class private JButton jbutMortTerm; //button for term - Mortgage class private JTextField jtxfMortTerm; //text field for term - Mortgage class private JButton jbutMortIntRate; //button for int rate - Mortgage class private JTextField jtxfMortIntRate; //text field - int rate - Mortgage class private GridLayout grLt1 ; //grid layout for middle jpanel private File fileNameOfWrite; //jfc write selected file private File fileNameOfRead; //jfc read selected file //bottom panel private JTextArea jtxaBottom; //output area for file private String str1 = " Lab7CIS355, Sun June 5, 2005, Soln by "; private String str2 = " Devry University "; private String str3 = " \temail = email@devry.edu "; private String str4 = str1 + "\n" + str2 + "\n" + str3; private String str5 = "You hit the clear button"; //my other classes Account acctMy; CheckAcct caMy; Mortgage mortMy;
Part of the constructor is:
public Lab7Cis355(){ //start of constructor
super("Lab7 CIS 355"); //title of the JFrame
final Container theWindow = getContentPane(); //get ref to the JFrame
jbutClear = new JButton("Clear Everything");
jbutEnd = new JButton("End the Program");
jbutEnd.setToolTipText("This button ends the program");
//add the clear button to the west and the end button to the east
theWindow.add(jbutClear, BorderLayout.WEST);
theWindow.add(jbutEnd, BorderLayout.EAST);
//create a new jtextarea for the top jpanel
jtxaTop = new JTextArea(2, 30);
jtxaTop.setEditable(false);
jtxaTop.append(str4);
//do the top jpanel
jpanTop = new JPanel();
jbutRead = new JButton("Read Data from File");
jbutWrite = new JButton("Write Data to file");
jbutCheckAcct = new JButton("CK Acct");
jbutMortgage = new JButton("Mortgage");
flLt1 = new FlowLayout();
jpanTop.setLayout(flLt1);
jpanTop.add(jbutCheckAcct);
jpanTop.add(jbutMortgage);
jpanTop.add(jtxaTop);
jpanTop.add(jbutRead);
jpanTop.add(jbutWrite);
theWindow.add(jpanTop, BorderLayout.NORTH);
//do the middle jpanel
jpanMiddle = new JPanel(); //create middle jpanel
jbutName = new JButton(" First, Last Name");
jtxfName = new JTextField(10);
jbutAddress = new JButton("Address");
jtxfAddress = new JTextField(10);
jbutCSZ = new JButton("City, State, Zip");
jtxfCSZ = new JTextField(10);
jbutCheckAcctNum = new JButton("Check Acct Num");
jtxfCheckAcctNum = new JTextField(10);
jbutCheckAcctType = new JButton("Checking Account Type");
jtxfCheckAcctType= new JTextField(10);
jbutCheckAcctDep = new JButton("Checking Account Deposit ");
jtxfCheckAcctDep = new JTextField(10);
jbutCheckAcctBal = new JButton("Checking Account Balance");
jtxfCheckAcctBal = new JTextField(10);
jbutMortNum = new JButton("Mortgage Number");
jtxfMortNum = new JTextField(15);
jbutMortAmt = new JButton("Mortgage Amt");
jtxfMortAmt = new JTextField(15);
jbutMortTerm = new JButton("Mortgage Term");
jtxfMortTerm = new JTextField(15);
jbutMortIntRate = new JButton("Mortgage Interest Rate");
jtxfMortIntRate = new JTextField(15);
grLt1 = new GridLayout(10, 2);
jpanMiddle.setLayout(grLt1);
theWindow.add(jpanMiddle, BorderLayout.CENTER);
//theWindow.add(jpanMiddle, BorderLayout.CENTER);
//do the bottom panel
jpanBottom = new JPanel();
jtxaBottom = new JTextArea(20, 60);
jpanBottom.add(jtxaBottom);
theWindow.add(jpanBottom, BorderLayout.SOUTH);
.
.
.
}// End of constructor
The code in jbutCheckAcct.addActionListener() is:
isCheckAcct = true; // this is a check acct isMortgage = false ; // not a mortgage jpanMiddle.removeAll(); jpanMiddle.repaint(); jpanMiddle.setLayout(grLt1); jpanMiddle.add(jbutName); jpanMiddle.add(jtxfName); jpanMiddle.add(jbutAddress); jpanMiddle.add(jtxfAddress); jpanMiddle.add(jbutCSZ); jpanMiddle.add(jtxfCSZ); jpanMiddle.add(jbutCheckAcctNum); jpanMiddle.add(jtxfCheckAcctNum); jpanMiddle.add(jbutCheckAcctType); jpanMiddle.add(jtxfCheckAcctType); jpanMiddle.add(jbutCheckAcctDep); jpanMiddle.add(jtxfCheckAcctDep); jpanMiddle.add(jbutCheckAcctBal); jpanMiddle.add(jtxfCheckAcctBal); theWindow.add(jpanMiddle, BorderLayout.CENTER); jpanMiddle.validate(); jpanMiddle.repaint(); jtxfName.grabFocus();
The code in jbutMortgage.addActionListener() is:
isCheckAcct = false; isMortgage = true; jpanMiddle.removeAll(); jpanMiddle.repaint(); jpanMiddle.setLayout(grLt1); jpanMiddle.add(jbutName); jpanMiddle.add(jtxfName); jpanMiddle.add(jbutAddress); jpanMiddle.add(jtxfAddress); jpanMiddle.add(jbutCSZ); jpanMiddle.add(jtxfCSZ); jpanMiddle.add(jbutMortNum); jpanMiddle.add(jtxfMortNum); jpanMiddle.add(jbutMortAmt); jpanMiddle.add(jtxfMortAmt); jpanMiddle.add(jbutMortTerm); jpanMiddle.add(jtxfMortTerm); jpanMiddle.add(jbutMortIntRate); jpanMiddle.add(jtxfMortIntRate); theWindow.add(jpanMiddle, BorderLayout.CENTER); jpanMiddle.validate(); jpanMiddle.repaint(); jtxfName.grabFocus();
The code in jbutClear.addActionListener() is:
jtxfName.setText("");
jtxfAddress.setText("");
jtxfCSZ.setText("");
jtxfCheckAcctNum.setText("");
jtxfCheckAcctType.setText("");
jtxfCheckAcctDep.setText("");
jtxfCheckAcctBal.setText("");
jtxfMortNum.setText("");
jtxfMortAmt.setText("");
jtxfMortTerm.setText("");
jtxfMortIntRate.setText("");
jtxaBottom.setText("");
jtxfName.grabFocus();
the code for jbutEnd.addActionListener is:
JOptionPane.showMessageDialog(null, str4); System.exit(0);
The code in jbutRead.addActionListener() is:
if (isCheckAcct) { //start if
readFromFile();
} //end if
if (isMortgage) { //start if
} //end if
You would have to write the code for if (isMortgage)
The code in jbutWrite.addActionListener() is
if (isCheckAcct) { //start if
writeToFile();
} //end if
if (isMortgage) { //start if
} //end if
As you see, you have to write the code for if (isMortgage) and please don't forget to write the code for the WindowAdapter(), as well. Finally, be sure to add in:
setSize(1100, 800); setVisible(true);
The code for the public methods called when the two major buttons are clicked is:
public void readFromFile() { //start readFromFile()
jfcRead = new JFileChooser();
jfcRead.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int intReturnFromJFCRead = jfcRead.showOpenDialog(null);
if(intReturnFromJFCRead == JFileChooser.APPROVE_OPTION)
try { //start approve try
{ //start if
fileNameOfRead = jfcRead.getSelectedFile();
jtxaBottom.setText("You chose " + fileNameOfRead);
//we are going to read from a text file
//we will use a File, FileReader,
//and a BufferedReader
String strBufRIn;
FileReader filerRead = new FileReader(fileNameOfRead);
BufferedReader bufrRead = new BufferedReader(filerRead);
jtxaBottom.append("\nYour file has the following data::");
while ((strBufRIn = bufrRead.readLine()) != null) { //start while
jtxaBottom.append( "\n" + strBufRIn);
} //end while
} //end if
} //end approve try
catch (IOException ioEx) { //start approve catch
JOptionPane.showMessageDialog(null, "Approve JFC is Bad");
} //end approve catch
if(intReturnFromJFCRead == JFileChooser.CANCEL_OPTION || intReturnFromJFCRead == JFileChooser.ERROR_OPTION) { //start if
jtxaBottom.setText("You either cancelled "
+ "or had an error, "
+ "\nwith an Open JFileChooser");
} //end if
} //end readFromFile()
public void writeToFile() { //start writeToFile()
jfcWrite = new JFileChooser();
jfcWrite.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int intReturnFromJFCWrite = jfcWrite.showSaveDialog(null);
if(intReturnFromJFCWrite == JFileChooser.APPROVE_OPTION)
try { //start approve try
{ //start if
fileNameOfWrite = jfcWrite.getSelectedFile();
jtxaBottom.setText("You chose " + fileNameOfWrite);
//we are going to write to a text file
//we will use a File, FileOutputStream
//and a PrintWriter
//set up polymorphic behavior
caMy = new CheckAcct();
acctMy = caMy;
//true means append to the FileOutputStream
FileOutputStream fosOut = new FileOutputStream(fileNameOfWrite, true);
PrintWriter pwOut = new PrintWriter(fosOut);
pwOut.print(jtxfName.getText());
pwOut.print(jtxfAddress.getText());
pwOut.print(jtxfCSZ.getText());
pwOut.print(jtxfCheckAcctNum.getText());
pwOut.print(jtxfCheckAcctType.getText());
pwOut.print(jtxfCheckAcctDep.getText());
pwOut.println(jtxfCheckAcctBal.getText());
jtxaBottom.append("\nYou are saving the following fields:" +
"\n" + jtxfName.getText() +
"\n" + jtxfAddress.getText() +
"\n" + jtxfCSZ.getText() +
"\n" + jtxfCheckAcctNum.getText() +
"\n" + jtxfCheckAcctType.getText() +
"\n" + jtxfCheckAcctDep.getText() +
"\n" + jtxfCheckAcctBal.getText() +
"\n" + "Here is the polymorphic message" +
"\n" + acctMy.openCost());
pwOut.close();
} //end if
} //end approve try
catch (IOException ioEx) { //start approve catch
JOptionPane.showMessageDialog(null, "Approve JFC is Bad");
} //end approve catch
if(intReturnFromJFCWrite == JFileChooser.CANCEL_OPTION || intReturnFromJFCWrite == JFileChooser.ERROR_OPTION) { //start if
jtxaBottom.setText("You either cancelled "
+ "or had an error, "
+ "\nwith a Save JFileChooser");
} //end if
} //end writeToFile()
Please Note: it's okay not to use the package directives in the lab; you should read Appendix F and utilize them but it's not required for the lab, per se.
You can see a demo of the shell of the application here: Applet Demo Shell.
The nine (9) output files for this lab are : 1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg, 6.jpg, 7.jpg, 8.jpg, 9.jpg, Lab7.vsd.
Student Name:
Date:
Read the Lab Presentation and respond to the following items prior to beginning the lab exercise.
Prepare this Lab Report when you have completed and lab assignment. Include any charts, graphs required to complete the lab. Refer to your initial lab plan to respond to the first questions.