⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atm.java

📁 一.TM柜员机模拟程序,其功能为: 当输入给定的卡号和密码(初始卡号和密码为123456)时
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Account
{ private String code    =null;   //信用卡号
  private String password=null;   //客户密码
  private double money   =1.00;   //卡里金额
 
 /********************/
 public Account(String code,String password,double money)
 {
  this.code=code;
  this.password=password;
  this.money=money;
 }
  public void setPassword(String p) {
  this.password=p;
 }
  public void setMoney(double m) {
  this.money=m;
 }
 protected String getCode() {
  return code;
 } 
 protected String getPassword() {
  return password;
 }
 public double getMoney() {
  return money;
 }
}

class  ATM  extends JFrame implements ActionListener
{   private Account A=new Account("123456","123456",10000);
	private JPanel contentpane;
    private JPanel Mpane;
	private JPanel Ml1p,Ml2p,Ml3p;
	JButton Jb1,Jb2,Jb3,Jb4,Jb5;
    JButton BOk,BEt;
    JButton Jbq,Jbg,Jbs,Jbr,JbcOk,JbcCa,JbcRe,BcOk;
    JTextField Ta,Tg,Ts;
	JPasswordField Tp,Tco,Tcn,Tcna;
ATM(){
	Mpane = new JPanel();
    contentpane = new JPanel();
	setLocation(100, 100);
	setTitle("ATM");
	this.setSize(300,300);
    login();
}

//登陆界面
public void login(){
   contentpane.remove(Mpane);
   Mpane = new JPanel();
   Ml1p = new JPanel();
   Ml2p = new JPanel();
   Ml3p = new JPanel();
   JPanel Ml0p = new JPanel();
   Ml1p.setLayout(new FlowLayout());
   Ml2p.setLayout(new FlowLayout());
   Ml3p.setLayout(new FlowLayout());
   Ta = new JTextField(15);
   Tp = new JPasswordField(15);
   Tp.setEchoChar('*');
   JLabel  Lt = new JLabel("ATM登录系统");
   JLabel  La = new JLabel("帐号");
   JLabel  Lp = new JLabel("密码");
   BOk = new JButton("确定");
   BEt= new JButton("退出");
   BOk.addActionListener(this);
   BEt.addActionListener(this);
   Ml0p.add(Lt);
   Ml1p.add(La);
   Ml1p.add(Ta);
   Ml2p.add(Lp);
   Ml2p.add(Tp);
   Ml3p.add(BOk);
   Ml3p.add(BEt);
   Mpane.setLayout(new GridLayout(7,1));
   Mpane.add(Ml0p);
   Mpane.add(Ml1p);
   Mpane.add(Ml2p);
   Mpane.add(Ml3p);
   setContentPane(contentpane);
   contentpane.setLayout(new BorderLayout());
   contentpane.add(Mpane,BorderLayout.CENTER);
   contentpane.updateUI(); 
}
//账号密码的检查
public void check(){
String pass=new String(Tp.getPassword()); 
String code=Ta.getText();
if (code.compareTo(A.getCode())==0&&pass.compareTo(A.getPassword())==0)
  init();
else {
	JLabel Lerr=new JLabel("账号密码错!");
    BcOk=new JButton("确定");
    Mpane.remove(Ml1p);
    Mpane.remove(Ml2p);
	Mpane.remove(Ml3p);
	Ml1p = new JPanel();
    Ml2p = new JPanel();
    Ml3p = new JPanel();
	BcOk.addActionListener(this);
	Ml2p.add(Lerr);
	Ml3p.add(BcOk);
	Mpane.add(Ml1p);
	Mpane.add(Ml2p);
	Mpane.add(Ml3p);
    contentpane.updateUI();
    }
}

//登录后的初始界面
public void init(){
   contentpane.remove(Mpane);
   Mpane = new JPanel();
   JPanel M0p = new JPanel();
   JPanel M1p = new JPanel();
   JPanel M2p = new JPanel();
   JPanel M3p = new JPanel();
   JPanel M4p = new JPanel();
   JPanel M5p = new JPanel();
   JLabel  Lt = new JLabel("欢迎使用ATM系统");
   Jb1 = new JButton("查询余额");
   Jb2 = new JButton("ATM取款");
   Jb3 = new JButton("ATM存款");
   Jb4 = new JButton("修改密码");
   Jb5= new JButton("退出系统");
   Jb1.addActionListener(this);
   Jb2.addActionListener(this);
   Jb3.addActionListener(this);
   Jb4.addActionListener(this);
   Jb5.addActionListener(this);
	Mpane.setLayout(new GridLayout(7,1));
    M0p.setLayout(new FlowLayout());
	M1p.setLayout(new FlowLayout());
    M2p.setLayout(new FlowLayout());
    M3p.setLayout(new FlowLayout());
    M4p.setLayout(new FlowLayout());
	M5p.setLayout(new FlowLayout());
    M0p.add(Lt);
    M1p.add(Jb1);
	M2p.add(Jb2);
	M3p.add(Jb3);
	M4p.add(Jb4);
	M5p.add(Jb5);
	Mpane.add(M0p);
    Mpane.add(M1p);
    Mpane.add(M2p);
    Mpane.add(M3p);
    Mpane.add(M4p);
	Mpane.add(M5p);
 	contentpane.add(Mpane,BorderLayout.CENTER);
	contentpane.updateUI();  
	}

	//查询余额的界面及方法
public void queryBalance(){
   contentpane.remove(Mpane);
   Mpane = new JPanel();
   JPanel M0p = new JPanel();
   JPanel M1p = new JPanel();
   JPanel M2p = new JPanel();
   JTextArea  M = new JTextArea(); 
   M.setText("账号:\t"+A.getCode()+"\n余额:\t"+A.getMoney()+"RMB");
   JLabel  Lt = new JLabel("查询余额");
   Jbq = new JButton("返回");
   Jbq.addActionListener(this);
   Mpane.setLayout(new GridLayout(3,1));
    M0p.setLayout(new FlowLayout());
	M1p.setLayout(new FlowLayout());
    M2p.setLayout(new FlowLayout());
	M0p.add(Lt);
    M1p.add(M);
	M2p.add(Jbq);
	Mpane.add(M0p);
    Mpane.add(M1p);
    Mpane.add(M2p);
 	contentpane.add(Mpane,BorderLayout.CENTER);
	contentpane.updateUI();  
}

//ATM取款的界面
public void gMoney(){
   contentpane.remove(Mpane);
   Mpane = new JPanel();
   JPanel M0p = new JPanel();
   JPanel M1p = new JPanel();
   JPanel M2p = new JPanel();
   JPanel M3p = new JPanel();
   JLabel  Ltg = new JLabel("ATM取款");
   JLabel  Lg = new JLabel("请输入取款金额(100的整数倍)");
   Tg = new JTextField(15);
   Jbg = new JButton("确定");
   Jbq = new JButton("返回");
   Jbq.addActionListener(this);
   Jbg.addActionListener(this);
   Mpane.setLayout(new GridLayout(6,1));
    M0p.setLayout(new FlowLayout());
	M1p.setLayout(new FlowLayout());
    M2p.setLayout(new FlowLayout());
	M3p.setLayout(new FlowLayout());
	M0p.add(Ltg);
    M1p.add(Lg);
	M2p.add(Tg);
	M3p.add(Jbg);
    M3p.add(Jbq);
	Mpane.add(M0p);
    Mpane.add(M1p);
    Mpane.add(M2p);
	Mpane.add(M3p);
 	contentpane.add(Mpane,BorderLayout.CENTER);
	contentpane.updateUI();  
}
//取款调用方法
public void getMoney(){
if (Tg.getText().length()!=0 ){
Double m=new Double(Tg.getText());
if(m.doubleValue()%100==0){
   if(m.doubleValue()<=5000){
	    if(m.doubleValue()<=A.getMoney()){
		  JOptionPane.showMessageDialog(this, "取款成功!请收好您的钱。","提示", JOptionPane.INFORMATION_MESSAGE);
          A.setMoney(A.getMoney()-m.doubleValue());
		  init();
		  }
	  else{
	      JOptionPane.showMessageDialog(this, "余额不足!","提示", JOptionPane.INFORMATION_MESSAGE);
		  init();
	      }
	   }
   else {
	JOptionPane.showMessageDialog(this, "单笔交易额不得超过5000元!","提示", JOptionPane.INFORMATION_MESSAGE);
	gMoney();
	}
   }
else {
	JOptionPane.showMessageDialog(this, "请输入100的整数倍!","提示", JOptionPane.INFORMATION_MESSAGE);
	gMoney();
	}
}
else {
	JOptionPane.showMessageDialog(this, "请输入取款金额!","提示", JOptionPane.INFORMATION_MESSAGE);
	gMoney();
	}
}

//ATM存款的界面
public void sMoney(){
   contentpane.remove(Mpane);
   Mpane = new JPanel();
   JPanel M0p = new JPanel();
   JPanel M1p = new JPanel();
   JPanel M2p = new JPanel();
   JPanel M3p = new JPanel();
   JLabel  Lts = new JLabel("ATM存款");
   JLabel  Ls = new JLabel("请输入存款金额");
   Ts = new JTextField(15);
   Jbs = new JButton("确定");
   Jbr = new JButton("返回");
   Jbs.addActionListener(this);
   Jbr.addActionListener(this);
   Mpane.setLayout(new GridLayout(6,1));
    M0p.setLayout(new FlowLayout());
	M1p.setLayout(new FlowLayout());
    M2p.setLayout(new FlowLayout());
	M3p.setLayout(new FlowLayout());
	M0p.add(Lts);
    M1p.add(Ls);
	M2p.add(Ts);
	M3p.add(Jbs);
    M3p.add(Jbr);
	Mpane.add(M0p);
    Mpane.add(M1p);
    Mpane.add(M2p);
	Mpane.add(M3p);
 	contentpane.add(Mpane,BorderLayout.CENTER);
	contentpane.updateUI();  
}

//存款调用方法
public void saveMoney(){
   if (Ts.getText().length()!=0 ){
	   Double s=new Double(Ts.getText());
	    if(s.doubleValue()>0){
		A.setMoney(A.getMoney()+s.doubleValue());
		JOptionPane.showMessageDialog(this, "存款成功!","提示", JOptionPane.INFORMATION_MESSAGE);
	    init();}}
	else{
		JOptionPane.showMessageDialog(this, "请输入正确的存款金额!","提示", JOptionPane.INFORMATION_MESSAGE);
		sMoney();
	}		  
}

//修改密码的界面
public void changePass(){
   contentpane.remove(Mpane);
   Mpane = new JPanel();
   JPanel M0p = new JPanel();
   JPanel M1p = new JPanel();
   JPanel M2p = new JPanel();
   JPanel M3p = new JPanel();
   JPanel M4p = new JPanel();
   JLabel  Lt = new JLabel("修改密码");
   JLabel  Lco = new JLabel("输入旧密码:\t");
   JLabel  Lcn = new JLabel("输入新密码:\t");
   JLabel  Lcna = new JLabel("请再次输入:\t");
   Tco =  new JPasswordField(10);
   Tcn=  new JPasswordField(10);
   Tcna=  new JPasswordField(10);
   Tco.setEchoChar('*');
   Tcn.setEchoChar('*');
   Tcna.setEchoChar('*');
   JbcOk = new JButton("确定");
   JbcCa = new JButton("重置");
   JbcRe = new JButton("返回");
   JbcOk.addActionListener(this);
   JbcCa.addActionListener(this);
   JbcRe.addActionListener(this);
    Mpane.setLayout(new GridLayout(6,1));
    M0p.setLayout(new FlowLayout());
	M1p.setLayout(new FlowLayout());
    M2p.setLayout(new FlowLayout());
	M3p.setLayout(new FlowLayout());
	M4p.setLayout(new FlowLayout());
    M0p.add(Lt);
	M1p.add(Lco);
	M1p.add(Tco);
    M2p.add(Lcn);
	M2p.add(Tcn);
	M3p.add(Lcna);
    M3p.add(Tcna);
    M4p.add(JbcOk);
	M4p.add(JbcCa);
    M4p.add(JbcRe);
	Mpane.add(M0p);
    Mpane.add(M1p);
    Mpane.add(M2p);
	Mpane.add(M3p);
	Mpane.add(M4p);
 	contentpane.add(Mpane,BorderLayout.CENTER);
	contentpane.updateUI();
}

//重置按钮调用的方法
public void reset(){
Tco.setText("");
Tcn.setText("");
Tcna.setText("");
}

//修改密码的调用方法
public void chPass(){
	int num=new String(Tcn.getPassword()).length();
	String oldPass=new String(Tco.getPassword());
    String newPass=new String(Tcn.getPassword());
	String newPassa=new String(Tcna.getPassword());
	if(oldPass.compareTo(A.getPassword())==0)	
	{if(num>=6)
          {if(newPass.compareTo(newPassa)==0)
	         {String s="1";
	          for(int j=1;j<newPass.length();j++)
			  s+="1";
		      if ((new Double(newPass).doubleValue())%(new Double(s).doubleValue())!=0)
            {
			JOptionPane.showMessageDialog(this, "修改成功!请使用新密码登录!","提示", JOptionPane.INFORMATION_MESSAGE); 
            A.setPassword(new String(Tcn.getPassword()));
			 login();
	            } 
		else JOptionPane.showMessageDialog(this, "密码位数不可完全相同!","提示", JOptionPane.INFORMATION_MESSAGE);
		 }
	        else JOptionPane.showMessageDialog(this, "两次输入的密码不一致!","提示", JOptionPane.INFORMATION_MESSAGE);
			}
		else JOptionPane.showMessageDialog(this, "新密码长度必须大于6!","提示", JOptionPane.INFORMATION_MESSAGE);} 
	else JOptionPane.showMessageDialog(this, "旧密码输入不正确!","提示", JOptionPane.INFORMATION_MESSAGE);
}

//监听事件
public void actionPerformed(ActionEvent e ) {
if(e.getSource() == BOk)
	{check();}
else if(e.getSource() == BEt)
	{System.exit(0);}
else if(e.getSource() == Jb5||e.getSource() == BcOk)
	{login();}
else if(e.getSource() == Jb1)
	{queryBalance();}
else if(e.getSource() == Jbq||e.getSource() == Jbr ||e.getSource() == JbcRe)
	{init();}
else if(e.getSource() == Jb2)
	{gMoney();}
else if(e.getSource() == Jb3)
	{sMoney();}
else if(e.getSource() == Jb4)
	{changePass();}
else if(e.getSource() == JbcOk)
	{chPass();}
else if(e.getSource() == Jbg)
	{getMoney();}
else if(e.getSource() == Jbs)
	{saveMoney();}
else if(e.getSource() == JbcCa)
	{reset();}

}


	public static void main(String[] args) 
	{
		 ATM t = new  ATM();
         t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         t.setVisible(true);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -