23 กันยายน 2554

[JAVA] การเขียนโปรแกรมจัดการกับเหตุการณ์ แบบฝึกหัดที่ 8.1

======= TellerGUI =======

import Banking.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TellerGUI implements ActionListener{
private Button bn1,bn2,bn3;
private TextField tf1,tf2,tf3;
private Label l1,l2,l3;
private TextField err;
private Panel p1,p2,p3;
private Frame fr;
private Customer cust;
public static void main(String den[]){
Customer cust = new Customer("Sommai","Sommut");
Account ac = new SavingAccount(5000);
cust.addAccount(ac);
TellerGUI tl = new TellerGUI(cust);
tl.init();
}
public void init(){
fr = new Frame("Button Sample");
fr.setLayout(new FlowLayout());

tf1 = new TextField();
tf2 = new TextField();
tf3 = new TextField();
err = new TextField();

l1 = new Label("Name:");
l2 = new Label("Balance:");
l3 = new Label("Amount");

bn1 = new Button("Deposit");
bn2 = new Button("Withdraw");
bn3 = new Button("Exit");

bn1.addActionListener(this);
bn2.addActionListener(this);
bn3.addActionListener(this);

p1 = new Panel();
p2 = new Panel();
p3 = new Panel();

p1.setLayout(new GridLayout(3,2));
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(l3);
p1.add(tf3);

p2.add(bn1);
p2.add(bn2);
p2.add(bn3);

p3.setLayout(new BorderLayout());
p3.add(p1,BorderLayout.CENTER);
p3.add(p2,BorderLayout.SOUTH);

fr = new Frame("Bank");
fr.add(p3,BorderLayout.CENTER);
fr.add(err,BorderLayout.SOUTH);


tf1.setEditable(false);
tf2.setEditable(false);
err.setEditable(false);

tf2.setText(cust.getAccount(0).getBalance()+"");
tf1.setText(cust.getFirstName()+" "+cust.getLastName());

fr.pack();
fr.show();
}
public TellerGUI(Customer c){
cust = c;
}

private void deposit()
{
double amt=Double.parseDouble(tf3.getText());
cust.getAccount(0).deposit(amt);
tf2.setText(cust.getAccount(0).getBalance()+ "" );
err.setText("Deposit "+amt+" baht");
}
public void withdraw()
{
double amt=Double.parseDouble(tf3.getText());
try
{
cust.getAccount(0).withdraw(amt);
err.setText("Withdraw "+amt+" baht");
} catch(WithdrawException ex) {
err.setText("Not Enought Monry");
}
tf2.setText(cust.getAccount(0).getBalance()+"");

}

@Override
public void actionPerformed(ActionEvent ev)
{
String cmd = ev.getActionCommand();
if ("Exit".equals(cmd)) System.exit(0);
else if ("Withdraw".equals(cmd)) withdraw();
else if ("Deposit".equals(cmd)) deposit();
}


}

======= Account =======
package Banking;
public class Account {
protected double balance;
public Account(double amount){
balance = amount;
}
public void deposit(double amount){
balance = balance + amount;
}
public void withdraw(double amount) throws WithdrawException
{
if(balance>=amount)
{
balance -= amount;
}
else
{
throw new WithdrawException();
}
}
public double getBalance(){
return balance;
}
public void showBalance(){
System.out.println(balance);
}
public Account(){

}
}
======= Customer =======
package Banking;
import java.util.*;
public class Customer {
private String firstName;
private String lastName;
private Account acct[];
private Vector v = new Vector();
private Enumeration e = v.elements();
private int NumAccount=0;
public Customer(String fName,String lName){
firstName = fName;
lastName = lName;
acct = new Account[20];
}
public void setFirstName(String fName){
firstName = fName;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String lName){
lastName = lName;
}
public String getLastName(){
return lastName;
}
public void setAccount(Account acct){
acct = acct;
}
public Account getAccount(int index){
return acct[index];
}
public void addAccount(Account acct){
this.acct[NumAccount]=acct;
NumAccount++;
}
public int getNumAccount(){
return NumAccount;
}
public void setVector(Account acct){
this.v.add(acct.getBalance());
}
public void printReport()
{
for(int i=0;i<getNumAccount();i++)
System.out.println("Account"+(i+1)+" Balance :"+getAccount(i).getBalance());
}
public void printReport2(){
int i=0;
while(e.hasMoreElements()){
System.out.println("Account"+(i + 1)+" Balance :"+e.nextElement());
i++;
}
}
}
======= SavingAccount =======
package Banking;
public class SavingAccount extends Account {
public SavingAccount(double amount) {
balance = amount;
}
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น