23 กันยายน 2554

[JAVA] การสร้างคลาสประเภท Exception แบบฝึกหัดที่ 9.2

======= 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;
}
}

[JAVA] การจัดการกับข้อผิดพลาด แบบฝึกหัดที่ 9.1

public class ExceptionDemo {
public static void main(String[] args){
try{
double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
double c = Double.parseDouble(args[2]);
double x = (-b + Math.sqrt(b*b-4*a*c))/(2*a);
System.out.print("x = "+x);
x = (-b - Math.sqrt(b*b-4*a*c)) / (2*a);
System.out.println(" or x = "+x);
}catch(ArithmeticException ex){
System.out.println(ex.toString());
}catch(NumberFormatException ex){
System.out.println("Invalid Numeric format");
}

}
}

[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;
}
}

09 กันยายน 2554

[JAVA]การใช้แพคเก็จ javax.swing แบบฝึกหัดที่ 7.2


import Banking.*;
import javax.swing.*;
import java.awt.*;
public class TellerGUISwing {
    private JButton bn1,bn2,bn3;
    private JTextField tf1,tf2,tf3;
    private JLabel l1,l2,l3;
    private JTextField err;
    private JPanel p1,p2,p3;
    private JFrame fr;
    private Customer cust;
    public static void main(String den[]){
        Customer cust = new Customer("Sommai","Sommut");
        Account ac = new SavingAccount(5000);
        cust.addAccount(ac);
        TellerGUISwing tl = new TellerGUISwing(cust);
        tl.init();
    }
    public void init(){
        fr = new JFrame("Button Sample");
        fr.setLayout(new FlowLayout());
        tf1 = new JTextField();
        tf2 = new JTextField();
        tf3 = new JTextField();
        err = new JTextField();
        l1 = new JLabel("Name:");
        l2 = new JLabel("Balance:");
        l3 = new JLabel("Amount");
        bn1 = new JButton("Deposit");
        bn2 = new JButton("Withdraw");
        bn3 = new JButton("Exit");
        p1 = new JPanel();
        p2 = new JPanel();
        p3 = new JPanel();
        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.getContentPane().add(p3,BorderLayout.CENTER);
        fr.getContentPane().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 TellerGUISwing(Customer c){
        cust = c;
    }
}

[JAVA]การสร้างโปรแกรม GUI โดยใช้แพคเก็จ java.awt แบบฝึกหัดที่ 7.1


import Banking.*;
import java.awt.*;
public class TellerGUI {
    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");
        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.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;
    }
}