?? atm.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class AKey extends JPanel implements ActionListener
{
private final int bn =12;
private ATM atm;
private JButton but[]=new JButton [bn];
private String[] labels={
"1","2","3",
"4","5","6",
"7","8","9",
"Del","0","#"};
public AKey(ATM atm)
{
this.atm = atm;
this.setLayout(new GridLayout(4,3,1,1));
for(int i=0;i<but.length;i++)
{
but[i]=new JButton(labels[i]);
add(but[i]);
but[i].addActionListener(this);
}
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
atm.keyPress(s);
}
}
import javax.swing.*;
import java.io.*;
public class UserInfo
{
User user[]=new User[4];
public void outputSerialize() throws Exception
{
FileInputStream fis=new FileInputStream("UserInformation.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
for(int i=0;i<user.length;i++)
{
user[i]=(User)ois.readObject();
}
ois.close();
}
public void inputSerialize() throws Exception
{
User user1=new User("李鞠佑","20065607",1000000);
User user2=new User("南曉光","20065606",100000);
User user3=new User("劉進位","20065608",10000);
User user4=new User("花志強","20065605",1000);
FileOutputStream fos=new FileOutputStream("UserInformation.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(user1);
oos.writeObject(user2);
oos.writeObject(user3);
oos.writeObject(user4);
oos.close();
}
}
class User implements Serializable
{
protected String name;
protected String password;
protected double balance;
public User(String n,String p,float b)
{
name=n;
password=p;
balance=b;
}
}
import java.awt.*;
import javax.swing.*;
public class Display extends JPanel
{
protected CardLayout cl=new CardLayout();
private final int n=4;
private JLabel labels[]=new JLabel[n];
private ImageIcon ima[]=new ImageIcon[n];
protected Password pc2=new Password();
protected Welcome pc3=new Welcome();
private Balance pc3_1=new Balance();
protected Change pc3_2=new Change();
protected TakeMoney pc3_3=new TakeMoney();
protected OutMoney pc4=new OutMoney();
private ContinueOrNot pc6=new ContinueOrNot();
public Display()
{
ima[0]=new ImageIcon("5.png"); //wel
ima[1]=new ImageIcon("3.jpg"); //guitar
ima[2]=new ImageIcon("1.jpg"); //hua
ima[3]=new ImageIcon("4.jpg"); //me
this.setLayout(cl);
for(int i=0;i<labels.length;i++)
labels[i]=new JLabel(ima[i]);
add(labels[0],"1");
add(pc2,"2");
add(pc3,"3");
add(labels[2],"7");//pc3_3.TakeMoney(50.2);
}
public void choice(int c)
{
pc3.choice=c;
if(pc3.choice==1)
{
add(pc3_1,"4");
add(pc6,"5");
}
if(pc3.choice==2) //用一個 來判斷ADD哪一個PC
{
add(pc3_2,"4");
add(pc6,"5");
}
if(pc3.choice==3)
{
add(pc3_3,"4");
add(pc6,"5");
add(pc4,"6");
}
}
}
class Password extends JPanel
{
private JLabel label=new JLabel(" Please input your Passcode:");
public JPasswordField ps=new JPasswordField();
protected String password;
public Password()
{
setLayout(new BorderLayout(5,5));
ps.setBackground(Color.YELLOW);
ps.setEchoChar('*');
add(label,"North");
add(ps);
}
}
class Welcome extends JPanel
{
private JLabel lab0=new JLabel(" Make your choice ,press the OK");
private JLabel lab1=new JLabel(" 1. See about the balance ");
private JLabel lab2=new JLabel(" 2. Change the password");
private JLabel lab3=new JLabel(" 3. Take money");
protected JTextField in=new JTextField();
protected int choice=1; //記錄用戶的選擇
public Welcome()
{
setLayout(new GridLayout(5,1));
setBackground(Color.blue);
in.setVisible(false);
add(lab0);
add(lab1);
add(lab2);
add(lab3);
add(in);
}
}
class Balance extends JPanel
{
private double balance;
private JLabel lab=new JLabel("Your balance are "+balance); //加上一個余額的變量
public Balance()
{
setLayout(new BorderLayout());
add(lab);
}
}
class Change extends JPanel
{
protected JPasswordField newPass=new JPasswordField();
private JLabel lab =new JLabel(" Must remenber your new password ");
protected String newPassword;
public Change()
{
setLayout(new GridLayout(2,1));
newPass.setEchoChar('*');
add(lab);
add(newPass); //傳遞一個新密碼 建立一個新方法
}
}
class TakeMoney extends JPanel
{
private JLabel lab=new JLabel(" please input the accout you want: ");
protected static JTextArea in=new JTextArea();
public TakeMoney()
{
setLayout(new GridLayout(2,1));
in.setText("¥");
add(lab);
add(in);
}
}
class OutMoney extends JPanel
{
protected double money;
protected JLabel lab=new JLabel(" The money is output ");
private JLabel lab1=new JLabel(" press OK to return");
public OutMoney()
{
setLayout(new BorderLayout());
setBackground(Color.BLUE);
add(lab);
add(lab1,"South");
}
/* public void addLab()
{
String str=
TakeMoney.in.getText().substring(1,TakeMoney.in.getText().length());
money=Double.parseDouble(str);
lab.setText(" The money "+str+" is output ");
add(lab);
}*/
}
class ContinueOrNot extends JPanel
{
private JLabel lab1=new JLabel(" 1. Continue ");
private JLabel lab2=new JLabel(" Any key to Exit");
public ContinueOrNot()
{
this.setLayout(new GridLayout(2,1));
add(lab1);
add(lab2);
}
}
public class Judge
{
UserInfo info=new UserInfo();
User user[];
boolean jugde=false;
public boolean judgePassword(String s) throws Exception
{
info.outputSerialize() ;
for(int i=0;i<info.user.length;i++)
{
if(s.equals(info.user[i].password))
jugde=true;
}
return jugde;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ATM extends JFrame
{
private JButton b1 = new JButton("Insert Card");
private JButton b2 = new JButton("OK");
private JButton b3 = new JButton("Cancel");
private JButton b4 = new JButton("Over");
private AKey key = new AKey(this);
private Display dis = new Display();
private ImageIcon ima=new ImageIcon("2.jpg");
private JLabel lab = new JLabel(ima);
private Judge judge=new Judge();
protected static int temp=-1;
private boolean bool=false; // 判斷用戶是否已經插入卡
public ATM()
{
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(4,1,2,2));
jp.add(b1);
jp.add(b2);
jp.add(b3);
jp.add(b4);
JPanel jp2=new JPanel();
jp2.add(dis);
jp2.add(lab);
getContentPane().setLayout(new BorderLayout(2,2));
getContentPane().add(key, "Center");
getContentPane().add(jp2, "North");
getContentPane().add(jp, "East");
MyActionListener event=new MyActionListener();
b1.addActionListener(event);
b2.addActionListener(event);
b3.addActionListener(event);
b4.addActionListener(event);
}
class MyActionListener implements ActionListener //響應四個鍵
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Insert Card"))
{
dis.cl.show(dis, "2");
temp=0;
dis.pc2.ps.setText("");
bool=true;
}
else if(bool!=true) //bool 是總開關
{ return; }
else if(e.getActionCommand().equals("OK"))
{
if(temp==1) //show choice
{
dis.cl.show(dis,"3");temp=2;
}
if(temp==3) // three show
{
dis.cl.show(dis,"4");
dis.pc2.ps.setText("");
temp=4;return;
}
if(temp==4) //show after three show
{
dis.cl.show(dis, "5");
dis.pc3_2.newPass.setText("");
temp=7; //continue or not
}
if(temp==5) //給 money 賦值 money is out
{
// dis.pc4.addLab(); ????????
dis.cl.show(dis, "6"); // "6" money is out
dis.pc3.choice=0;temp=4;
}
}
else if(e.getActionCommand().equals("Cancel"))
{
dis.cl.previous(dis);
if(temp!=-1)
temp -=temp;
}
else if(e.getActionCommand().equals("Over"))
{
dis.cl.show(dis, "7");bool=false;
}
}
}
public void keyPress(String s) throws Exception
{
if(temp==0 &&dis.pc2.ps.getPassword().length<8)
{
String str=new String();
if(s.equals("Del"))
{
str=new String(dis.pc2.ps.getPassword());
str=str.substring(0,str.length()-1);
dis.pc2.ps.setText(str);
}
else
{
if(dis.pc2.ps.getPassword().equals(""))
{
dis.pc2.ps.setText(s);
}
else
{
str=new String(dis.pc2.ps.getPassword());
str=str+s;
dis.pc2.ps.setText(str);
}
}
if(str.length()==8)
{
if(judge.judgePassword(str))
{
dis.pc2.password=str;temp=1;
}
}
}
if(temp==2)
{
int choice=0;
if(s.equals("1"))
choice=1;
else if(s.equals("2"))
choice=2;
else if(s.equals("3"))
choice=3;
if(choice!=0)
temp=3;
dis.choice(choice);
return;
}
if(dis.pc3.choice==2&&dis.pc3_2.newPass.getPassword().length<8)
{
String str=new String();
if(s.equals("Del"))
{
str=new String(dis.pc3_2.newPass.getPassword());
str=str.substring(0,str.length()-1);
dis.pc3_2.newPass.setText(str);
}
if(dis.pc3_2.newPass.getPassword().equals(""))
{
dis.pc3_2.newPass.setText(s);
}
else
{
str=new String(dis.pc3_2.newPass.getPassword());
str=str+s;
dis.pc3_2.newPass.setText(str);
}
if(str.length()==8)
{
dis.pc3_2.newPassword=str;
temp=4;
dis.pc3.choice=0; //JOptionPane.showMessageDialog(null,str);
}
}
if(dis.pc3.choice==3)
{
String str=new String();
if(s.equals("Del")&&TakeMoney.in.getText().length()>1) //¥ 不能刪
{
str=new String(TakeMoney.in.getText());
str=str.substring(0,str.length()-1);
TakeMoney.in.setText(str);
}
else
{
str=new String(TakeMoney.in.getText());
str=str+s;
TakeMoney.in.setText(str);
}
//dis.pc3.choice=0;
temp=5; // 輸入完畢,按 OK 執行
}
if(temp==7)
{
if(s.equals("1"))
{
dis.cl.show(dis,"3");temp=2; //show three choice
}
else
{
dis.cl.show(dis, "7");bool=false; // to say goodbye
}
}
}
public static void main(String args[])
{
ATM atm = new ATM();
atm.setSize(370, 310);
atm.setVisible(true);
atm.setTitle("ATM");
atm.setDefaultCloseOperation(3);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -