// Author : George Coxhead
// This program is to be used as the basis for Assignment 1 (COOP)
import java.awt.*;
import java.awt.event.*;
import Account;
class Application extends Frame
{
private static final int H = 400; //Height of frame
private static final int W = 300; //Width of frame
private TextField theinput; //Textfield for input
private TextArea theoutput; //Textarea to display output
private Transaction thetrans = new Transaction();
private Account theaccount = new Account();
public Application() //constructor for Application
{
setLayout(null); //nullify the default layout manager so we can place things ourself
setSize(W, H);
setTitle("Iternational Bank of George");
Font font = new Font("Monospaced",Font.PLAIN,12); //create a font for input and output
// build the input field and add to Frame
theinput = new TextField(); // create an input field
theinput.setBounds(10,H-50,W-20,40); //set its co-ordinates within the frame
theinput.setFont(font); //set the font
add(theinput); //add theinput field to the frame
theinput.addActionListener(thetrans);//add a listener to listen for a transaction event
// build the output area and add to the frame
theoutput = new TextArea(10,40); //create a text area with dimensions
theoutput.setBounds(10,30,W-20,H-100);//set its co-ordinates within the frame
theoutput.setFont(font); //set the font
add(theoutput); //add the output area to the frame
} // end of constructor for Application
// Now create a transaction class as an inner class to Application in order to process
// the different transactions deposit, withdraw etc
class Transaction implements ActionListener
{
public void actionPerformed(ActionEvent e) // handle the transaction
{
String userinput = theinput.getText() + " "; //get some input and make safe
theinput.setText(""); //clear the input field
char transtype = userinput.charAt(0); //get the first character it denotes transaction type
String tranamount = userinput.substring(1); //Get the rest of the input
//We now need to parse tranamount into a double value
double value = 0.00; //declare and initialise a variable to hold the parsed value
// string to number parsing throws a NumberFormatException so we have to handle it
try
{
value = Double.parseDouble(tranamount);
}
catch(NumberFormatException ex)
{
//minimal job for the time being
}
// We now need to setup the different cases for a transaction (transtype)
// D Deposit, W Withdraw, C clear theoutput
double res = 0.0; //Variable to hold the value returned by theaccount withdraw method
switch(transtype) //setup a switch to handle each transaction type
{
//*************** deposit ****************
case 'D':
case 'd':
theoutput.append( "Not yet implemented\n");
break;
//************* Withdraw ***************
case 'W':
case 'w':
theoutput.append( "Not yet implemented\n");
break;
//*************** Get the Balance ************
case 'B':
case 'b':
res = 0.00; //need to call an account method for this
theoutput.append("Current Balance : " + res + "\n");
break;
//************** Clear output area ************
case 'C':
case 'c':
break;
default :
theoutput.append(transtype + "not valid\n");
} //end case
} //end actionPerformed
} //end Transaction(inner class)
} //end Application
class Main
{
public static void main(String args[])
{
(new Application()).show();
}
}
|