ATM 自动取款机

来源:互联网 发布:owncloud源码分析 编辑:程序博客网 时间:2024/05/16 18:04
/************************** Account类 ************************************/
public class Account {
private String code = null; // 信用卡号
private String uname = null; // 客户姓名
private String pword = null; // 客户密码
private double money = 0.0; // 卡里金额
/****************** set,get方法 *******************/
public Account(String code, String name, String i, double money) {
  this.code = code;
  this.uname = name;
  this.pword = i;
  this.money = money;
}
protected String get_Code() {
  return code;
}
protected String get_Name() {
  return uname;
}
protected void set_Password(String str) {
  pword = str;
}
protected String get_Password() {
  return pword;
}
public double get_Money() {
  return money;
}
/* 得到剩余的钱的数目 */
public void get_Balance(double mon) {
  money = money - mon;
}
public void give_Balance(double mon) {
  money = money + mon;
}
}


import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class logintest1 {
  public static Account user;
  /******************************** 查询类 **********************************/
  static class re_info implements ActionListener {
   private static final long serialVersionUID = 1L;
   JLabel show = new JLabel("");
   JLabel info = new JLabel("您当前的可用余额为:");
   JFrame jf = new JFrame("" + user.get_Name() + " 您好,欢迎来到查询界面");
   private JButton back = new JButton("返回");
   public re_info() {
    jf.setLayout(null);
    jf.add(back);
    jf.add(info);
    back.addActionListener(this);
    back.setBounds(200, 300, 100, 20);
    show.setBounds(100, 200, 100, 20);
    info.setBounds(50, 50, 200, 20);
    show.setText(user.get_Money() + "");
    jf.setVisible(true);
    jf.add(show);
    jf.setBounds(200, 200, 300, 400);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
   public void actionPerformed(ActionEvent e) {
    if (e.getSource() == back) {
     show.setText("退出");
     jf.dispose();
    }
   }
  }
 
 
  /***************************** 修改密码类 *********************************/
  static class set_password implements ActionListener {
   String money;
   String newpassword[] = new String[2];
   static int count = 0;
   private JFrame jf = new JFrame("" + user.get_Name() + " 您好,欢迎来到ATM修改密码界面");
   JLabel lb1 = new JLabel("请输入您的新密码");
   JLabel lb2 = new JLabel("");
   JLabel show = new JLabel("");
   static JButton jb1 = new JButton("确定"); // 输完金额后确定
   static JButton jb2=new JButton("取消");// 退出放入的钱,终止存款操作
            static JButton jb3=new JButton("返回");//退出存款界面
   private JLabel[] jlarray = { lb1, lb2, show };
   private JButton[] jbarray1 = { jb1, jb2, jb3 };
   JTextField input1 = new JTextField(6); // 用来输入密码
   JTextField input2 = new JTextField(6);
   public set_password()
   {
    jf.setLayout(null);
    jf.setBounds(200, 200, 300, 400);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    for (int i = 0; i < 3; i++)
    {
        jbarray1[i].addActionListener(this);
        jf.add(jbarray1[i]);
        jf.add(jlarray[i]);
    }
    lb1.setBounds(100, 100, 200, 20);
    lb2.setBounds(100, 180, 200, 20);
    jb1.setBounds(70, 260, 80, 20);
    jb2.setBounds(160, 260, 80, 20);
    jb3.setBounds(200, 350, 80, 20);
    input1.setBounds(100, 150, 100, 20);
    input2.setBounds(100, 210, 100, 20);
    show.setBounds(50, 235, 330, 20);
    jf.add(input1);
    jf.add(input2);
    }
    public void actionPerformed(ActionEvent e) {
     if (e.getSource() == input1) {
      newpassword[0] = input1.getText();
      jb1.requestFocus();
      show.setText(null);
     } else if (e.getSource() == input2) {
      newpassword[1] = input1.getText();
      jb1.requestFocus();
      show.setText(null);
     } else if (e.getSource() == jb1) {
      count++;
      if (count == 2) {
       int k = 0;
       for (int j = 0; j < input1.getText().length(); j++)
       {
        if (input1.getText().charAt(j) == input2
          .getText().charAt(j))
         k++;
       }
       if (k == 6) {
        show.setText("恭喜您修改成功,您的新密码为"
          + input2.getText());
        count = 0;
        user.set_Password(input1.getText());
        input1.setText(null);
        input2.setText(null);
       } else {
        input1.setText(null);
        input2.setText(null);
        show.setText("修改失败,两次输入不一致");
        count = 0;
        lb2.setText(null);
       }
      } else if (count == 1)
      {
       int i = 0;
       for (int j = 0; j < input1.getText().length() - 1; j++)
       {
        if (input1.getText().charAt(j) == input1
          .getText().charAt(j + 1))
         i++;
       }
       if (input1.getText().length() < 6 || i == input1.getText().length() - 1) {
        show.setText("密码过于简单,请重新设密码");
        count = 0;
        input1.setText(null);
        lb2.setText(null);
       } else {
        lb2.setText("请再次输入密码");
        show.setText(null);
       }
      }
     } else if (e.getSource() == jb2) {
      input1.setText(null);
      input2.setText(null);
      count = 0;
      lb2.setText(null);
     } else if (e.getSource() == jb3) {
      jf.dispose();
     } 
    }
  }
 
  /***************************** 主菜单类 ***********************************/
  static class mainmenu extends Frame implements ActionListener {
    private static final long serialVersionUID = 2L;
    private JPanel jp = new JPanel();
    JLabel show = new JLabel("");
    JButton quit = new JButton("退出");
    JButton inqu = new JButton("查询");
    JButton set_password = new JButton("修改密码");
    JButton get_balance = new JButton("取款");
    JButton give_balance = new JButton("存款");
    JFrame jf = new JFrame("" + user.get_Name() + " 您好,欢迎进入主界面");
    JLabel jl1 = new JLabel("请选择你想进行的操作");
private JButton[] jb = { inqu, set_password, get_balance,give_balance, quit };
    public mainmenu() {
     jf.setLayout(null);
     jp.setBounds(100, 100, 100, 500);
     for (int i = 0; i < 5; i++) {
      jf.add(jb[i]);
      jb[i].addActionListener(this);
      if (i != 4)
       jb[i].setBounds(30, 100 + 40 * i, 100, 20);
      else
       jb[i].setBounds(200, 330, 100, 20);
     }
     show.setBounds(30, 300, 180, 20);
     jl1.setBounds(30, 50, 200, 20);
     jf.setVisible(true);
     jf.add(show);
     jf.add(jl1);
     jf.setBounds(200, 200, 300, 400);
     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e) 
    { 
     if(e.getSource()==jb[0]) 
     {
      show.setText("查询");  
      new re_info(); 
      this.dispose();
     }  else if(e.getSource()==jb[1])
     {  
      show.setText("重置密码");  
      this.dispose();
      new set_password(); 
     }  else if(e.getSource()==jb[2]) 
     {  
      show.setText("取款"); 
      this.dispose();
      new get_balance();
     } else if(e.getSource()==jb[3]) 
     {  
      show.setText("存款"); 
      this.dispose();
      new give_balance();
     }else if(e.getSource()==jb[4])
     {
      show.setText("退出");
      jf.dispose();
     }
    }
  }
 
  /**********************************取款类*******************************/
  static class get_balance implements ActionListener {
   private static final long serialVersionUID = 1L;
   double count=0;
   double a;
   private JFrame jf=new JFrame(""+user.get_Name()+" 您好,欢迎来到ATM模拟取款界面");
   JButton jb1=new JButton("100");
   JButton jb2=new JButton("300"); 
   JButton jb3=new JButton("500");
   JButton jb4=new JButton("1000");
   JButton jb5=new JButton("2000");
   JButton jb6=new JButton("输入");
   JLabel show=new JLabel("");
   JButton jb7=new JButton("确定");            //输完金额后确定  
   JButton jb8=new JButton("取消");            //重新输入金额  
   JButton jb9=new JButton("返回");
   private JButton[] jbarray1={jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9};
   JTextField input=new JTextField(5);
   public get_balance()
   { 
    jf.setLayout(null); 
    jf.setBounds(200,200,300,400);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    for(int i=0;i<9;i++) 
    {  
     jbarray1[i].addActionListener(this);  
     jf.add(jbarray1[i]);
     if(i<5)   
      jbarray1[i].setBounds(30,50+40*i,80,20);
     else if(i==5) 
      jbarray1[i].setBounds(200,213,80,20); 
     else if(i==6) 
      jbarray1[i].setBounds(100,280,80,20);  
     else if(i==7)   
      jbarray1[i].setBounds(200,280,80,20);   
     else        
      jbarray1[i].setBounds(200,350,80,20); 
     } 
    input.setBounds(200,180,80,20); 
    show.setBounds(0,250,320,20); 
    jf.add(input); 
    jf.add(show);
    }
   public void actionPerformed(ActionEvent e)
   { 
    if(e.getSource()==jb6) 
    {    
     a=Double.parseDouble(input.getText());  
     if((a%100==0) &&((a/100)<=50) && a>0)   
     {      
      if(a<user.get_Money()) 
      {     
       show.setText("恭喜您取款"+a+"成功,请在30秒内取走您的钱");
       user.get_Balance(Double.parseDouble(input.getText()));
       input.setText(null);  
      }    
      else show.setText("余额不足"); 
     }  
     else if(a%100!=0)  
     {        
      show.setText("请输入整百的数");     
      input.setText(""); 
     }    
     else if(a/100>50)   
     {   
      show.setText("每次取款金额不能超过5000元");
      input.setText(""); 
     }
     else if(a<0)    
     {    
      show.setText("取款不能为负"); 
      input.setText("");
     } 
    }   
    else if(e.getSource()==jb8) 
    {  
     input.setText("");
    }  
    else if(e.getSource()==jb9)
    { 
     jf.dispose();
    } 
    else if(e.getSource()==jb1)
    {   
     jb7.requestFocus(); 
     count=100; 
    }  
    else if(e.getSource()==jb2)
    {  
     jb7.requestFocus(); 
     count=300;
    }  
    else if(e.getSource()==jb3) 
    {  
     jb7.requestFocus(); 
     count=500;
    } 
    else if(e.getSource()==jb4) 
    {  
     jb7.requestFocus();
     count=1000;
    }  
    else if(e.getSource()==jb5)
    {  
     jb7.requestFocus(); 
     count=2000; 
    } 
    else 
    {  
     if(count<=user.get_Money())
     {  
      show.setText("恭喜您成功取款"+count+"元,请在30秒内取走您的钱");
      user.get_Balance(count); 
     }  
     else   
      show.setText("余额不足"); 
     }  
    }
   }
 
  /********************************存款类**********************************/
  static class give_balance  implements ActionListener {
   private static final long serialVersionUID = 1L;
   int count=0;
   private JFrame jf=new JFrame(""+user.get_Name()+"您好,欢迎来到ATM模拟存款界面");
   JLabel lb1=new JLabel("请放入您将要存的钱");
   JLabel show=new JLabel("");
   JButton jb1=new JButton("确定");            //输完金额后确定   
   JButton jb2=new JButton("取消");            //退出放入的钱,终止存款操作 
   JButton jb3=new JButton("返回");            //退出存款界面   
   private JLabel[]jlarray={lb1,show};
   private JButton[] jbarray1={jb1,jb2,jb3}; 
   JTextField input=new JTextField();   //用输入来模拟放钱的过程 
   public give_balance() 
   {  
    jf.setLayout(null); 
    jf.setBounds(200,200,300,450); 
    jf.setVisible(true); 
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    for(int i=0;i<2;i++) 
    {   
     jf.add(jlarray[i]);
     } 
    for(int i=0;i<3;i++)
    {   
     jbarray1[i].addActionListener(this); 
     jf.add(jbarray1[i]);
     }  
    lb1.setBounds(100,100,200,20); 
    jb1.setBounds(70,230,80,20);  
    jb2.setBounds(160,230,80,20); 
    jb3.setBounds(200,350,80,20);  
    input.setBounds(100,150,100,20); 
    show.setBounds(90,190,200,20); 
    jf.add(input);
    }
    
    public void actionPerformed(ActionEvent e)
    { 
     double a; 
     if(e.getSource()==input)
     {  
        show.setText(input.getText());
        jb1.requestFocus();    //requestFocus请求此 Component 获得输入焦点,并且此 Component 的顶层祖先成为获得焦点的 Window
        jb2.requestFocus();
     }
    
     if(e.getSource()==jb1) 
     {  
         count++;   
         if(count==1) 
         {   
             a=Double.parseDouble(input.getText());
             if(Double.parseDouble(input.getText())>0&&a%100==0)
             {
                 show.setText("您是否要存入"+Double.parseDouble(input.getText())+"元");
                 if(e.getSource()==jb2)
                 {
                  count = 0;
                  show.setText("请重新输入");
                  input.setText(null);
                
                 }
             }
             else  if(Double.parseDouble(input.getText())<0) 
             {   
                 show.setText("存款不能为负");  
                 count=0;   
                 input.setText(null);  
             }
             else if(e.getSource()==jb2){
                 count=0;   
                 input.setText(null);
            }
            else 
            {   
                show.setText("您只能存入整百金额的钱数");  
                count=0; 
                input.setText(null); 
            }   
      }  
      else if(count==2) 
      {   
          show.setText("恭喜您,成功存款"+Double.parseDouble(input.getText())+"元"); 
          user.give_Balance(Double.parseDouble(input.getText())); 
          input.setText(null); 
      }    
      else if(count==0) 
      {    
          count=0;
          input.setText(null); 
          show.setText(null);
      } 
    
    }
    if(e.getSource()==jb2)
    {
      count = 0;
      input.setText(null);
    }
    if(e.getSource()==jb3){
     jf.dispose();
    }
   
   }
  }
 
/*********************************登录类*********************************/
    static class login implements ActionListener {
   private static final long serialVersionUID = 1L;
   int count=0;
   private JFrame jf=new JFrame();    
   //创建JPanel对象 
   JLabel usernum = new JLabel("请插入银行卡");     
   //创建标签
   JLabel password = new JLabel("请输入密码");                 //创建标签
   JLabel show = new JLabel("");                              // 创建标签 
   private  JLabel[]j1 ={usernum,password,show};
   JButton ok=new JButton("确定");                            //输完密码后确定
   JButton cancel=new JButton("取消");
   JButton quit=new JButton("退出");                          //重新输入密码
   private JButton[] jbarray2={ok,cancel,quit};
   private JTextField jnum = new JTextField(); 
   private JPasswordField jpassword = new JPasswordField(6); //密码只能为六位数字   
   Account user1=new Account("000000","Devil","123456",50000);
   Account user2=new Account("123456","Demo","213452",10000); 
   Account user3=new Account("111222","Jane","112233",500); 
   Account user4=new Account("222222","Lili","123123",5000);
   Boolean is_right(String unum,String upword)
   { 
    if(unum.equals(user1.get_Code())) 
    {  
     if(upword.equals(user1.get_Password())) 
     {    
      user=user1; 
      return true; 
     }
    }   else if(unum.equals(user2.get_Code())) 
    { 
     if(upword.equals(user2.get_Password()))  
     {   
      user=user2; 
      return true; 
     } 
    }   else if(unum.equals(user3.get_Code())) 
    { 
     if(upword.equals(user3.get_Password()))
     {  
      user=user3;  
      return true;  
     } 
    }   else if(unum.equals(user4.get_Code())) 
    {  
     if(upword.equals(user4.get_Password()))  
     {   
      user=user4;  
      return true;  
     }
    } 
    return false;
   } 
   public login() 
   {
    jf.setLayout(null); 
    for(int i=0;i<2;i++)
    {   
     j1[i].setBounds(30,50+40*i,180,20); 
     jbarray2[i].setBounds(30+110*i,220,80,20); 
     jf.add(j1[i]);  
     jf.add(jbarray2[i]);   
     jbarray2[i].addActionListener(this);
    }
    jf.add(quit);
    quit.addActionListener(this);
    quit.setBounds(200,300,80,20); 
    jf.setBounds(200,340,80,20); 
    jnum.setBounds(130,50,100,20); 
    jf.add(jnum); 
    jnum.addActionListener(this);
    jpassword.setBounds(130,90,100,20); 
    jf.add(jpassword);
    jpassword.setEchoChar('*');
    jpassword.addActionListener(this); 
    j1[2].setBounds(10,180,270,20);
    jf.add(j1[2]);  
    jf.setTitle("欢迎使用ATM模拟系统"); 
    jf.setBounds(200,200,300,400);  
    jf.setVisible(true);  
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } 
    @SuppressWarnings("deprecation") 
    public void actionPerformed(ActionEvent e) 
    {  
     if(e.getSource()==jnum) 
     {   
      password.requestFocus(); 
     } 
     else if(e.getSource()==jpassword) 
     {  
      jnum.requestFocus();
     }  
     else if(e.getSource()==quit) 
     {  
      jnum.setText(null);   
      jpassword.setText(null); 
      System.exit(0);
     }   else if(e.getSource()==jbarray2[1])
     {   
      j1[2].setText(""); 
      jnum.setText(""); 
      jpassword.setText(""); 
      jnum.requestFocus();  
     }  
     else      
     {        
      if(jnum.getText().equals("")) 
      {    
       j1[2].setText("请插入银行卡");  
      }  
      else if(jpassword.getText().equals(""))
      {    
       j1[2].setText("密码不能为空,请输入密码");
      }   
      else if((is_right(jnum.getText(),jpassword.getText()))) 
      {   
       j1[2].setText("密码正确,欢迎使用!");  
       new mainmenu(); 
       jf.dispose(); 
      } 
      else 
      {   
       j1[2].setText("密码错误,请重新输入"); 
       password.setText("");  
       count++;    
       if(count==3)  
       {   
        j1[2].setText("对不起,您的操作有误,银行卡已被吞入");   
        jnum.setText(null);   
        jpassword.setText(null);     
       }  
      }
     }
    }
    }
   
    /*********************************主函数********************************/   
    public static void main(String[] args) { 
     new login();  
     }
}
  
0 0