【Java课设】--ATM取款机(Gui界面)

【Java课设】–ATM取款机(Gui界面)

🎈🎈 作者 : @whispar
🎈🎈专栏 : C语言从无到有

✨放低姿态,空杯心态✨
在这里插入图片描述

一、实现要求

要求使用图形用户界面:
1、通过主界面,可以进入管理员界面、用户界面、系统设置界面、退出;
2、启动软件,可以进入用户模式,也可以进入系统管理模式;
3、进入系统管理模式,需要输入管理员账号和密码,可以查看这台ATM机近期资金出入明细,可以查看这台AT机上面操作的所有账户的历史记录和明细;
4、进入用户账号和密码的登录界面,当输入给定的卡号和密码(初始卡号16位和密码6位)时,对比系统存储的账号和密码正确,能登录ATM柜员机系统,当日出错次数操作3次,当日锁定账户不能继续操作,累计三日被锁定,需要管理员账号才能够完成解锁操作。
用户可以按照以下规则进行操作:
1、查询余额:初始余额为100000元;
2、ATM取款:每次取款金额为100的倍数,总额不超过5000元,支取金额不允许透支;
3、ATM存款:不能出现负存款,要求存钱为100的整数倍;
4、ATM转账:通过登录的用户向指定的银行账号(在系统中已经保存)转账,若银行卡不存在,则提
示银行卡号输入错误,转账成功则提示“转账成功”,并且工具日期能够实现当日转账金额的限制;
5、历史交易记录查询,要求能够保存20条以上的交易记录;
6、修改密码:新密码长度不小于6位,不允许出现6位完全相同的情况,只有旧密码正确,新密码符合要求,且两次输入相同的情况下才可以成功修改密码;
7、退卡:点击退卡,返回登录界面。

二、实现过程

1.Account.java

package atm;
//import com.sun.deploy.util.SyncFileAccess;  
//import com.sun.org.apache.regexp.internal.RE;  
  
import javax.swing.*;  
import  java.io.*;  
import java.text.SimpleDateFormat;  
import  java.util.*;
public class Account {
	  int money;  
	    String id;//账号名  
	  
	    String password;  
	    Date now=new Date();  
	    Date currentTime;  
	    SimpleDateFormat formatter;  
	    Reader fr;  
	    ;  
	    public Account(String id, String password, String money) {//构造方法  
	        this.id = id;  
	  
	        this.password = password;  
	        this.money=Integer.parseInt(money);  
	    }  
	  
	  
	  
	  
	  
	  
	  
	    public void outMoney (int money)throws Exception {//抛出异常,由相关的界面类弹窗处理异常,下面几个方法同理  
	        //如在取钱界面取钱,则会调用此函数,进行try/catch处理,获得这个函数的异常,弹窗说明异常  
	        if (money > this.money) {  
	            throw new Exception("余额不足");  
	        }
	        if(money<0)  
	        {  
	            throw new Exception("不能取出负数");  
	        }  
	        formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");//时间格式  
	        currentTime = new Date();//当前时间  
	        String dateString = formatter.format(currentTime);//处理当前时间格式  
	        Writer fw = new FileWriter(Test.file);  
	        fw.write(Test.recordString.append(dateString + "\t" + Test.currentAccount.id + "\t取出" + money + "元\r\n").toString());//将这次的取钱行为添加到记录文件中  
	        fw.flush();//写进文件  
	        fw.close();  
	        this.money -= money;  
	        Test.usersListUpdate();//更新用户文档(信息)  
	    }  
	  
	    public void inMoney(int money)throws Exception  
	    {  
	        try {  
	            Writer fw = new FileWriter(Test.file);  
	           // System.out.println(Test.file);  
	            formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");  
	            currentTime=new Date();  
	            String dateString=formatter.format(currentTime);  
	            fw.write(Test.recordString.append(dateString+"\t"+Test.currentAccount.id+"\t存入" + money + "元\r\n").toString());  
	            fw.flush();//写进文件  
	            fw.close();  
	  
	            this.money+=money;  
	  
	            Test.usersListUpdate();//更新当前用户信息  
	  
	        }  
	        catch (Exception e1)  
	        {  
	            throw new Exception("写入记录失败");  
	        }  
	  
	    }  
	  
	    public void transfer(int money,String id)throws Exception//转账  
	    {  
	        if(id.equals(Test.currentAccount.id))  
	        {  
	            throw new Exception("不能转给自己");  
	        }  
	        if(money>this.money)  
	        {  
	            throw new Exception("余额不足");  
	        }  
	        if(money<0) {  
	            throw new Exception("不能转入负数");  
	        }  
	  
	  
	        for(int i=0;i<Test.usersList.size();i++)  
	        {  
	            if(Test.usersList.get(i).id.equals(id))//找到要转帐的用户  
	            {  
	                Test.usersList.get(i).money+=money;//转入  
	                this.money-=money;//扣钱  
	  
	                FileWriter fw=new FileWriter(Test.file);  
	                formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");//声明时间格式  
	                currentTime=new Date();//获取当前时间  
	                String dateString=formatter.format(currentTime);//转换时间格式  
	                fw.write(Test.recordString.append(dateString+"\t向"+id+"\t转出"+money+"元\r\n").toString());//Test类中的静态字符串拼接上这个字符串覆盖写入当前用户文档  
	                fw.close();  
	  
	                /********************向转入目标写入转账信息*************************/  
	                try {  
	                fr = new FileReader(id+".txt");//字符流  
	                 }  
	                 catch (Exception e)  
	                 {  
	                System.out.println("字符流创建失败");  
	                }  
	  
	                BufferedReader bfr = new BufferedReader(fr);  
	  
	                String temp="";  
	                String temp1;  
	  
	                while ((temp1 = bfr.readLine()) != null)  
	                {  
	                    temp+=temp1;  
	                }  
	                temp=temp.replace("元","元\n\r")+dateString+"\t由"+Test.currentAccount.id+"\t转进"+money+"元\r\n";  
	                System.out.println(temp);  
	                fw=new FileWriter(id+".txt");  
	                fw.write(temp);  
	                fw.close();  
	  
	  
	                JOptionPane.showMessageDialog(null,"转账成功");  
	                Test.usersListUpdate();//更新用户文档  
	  
	                return;  
	            }  
	        }  
	        throw new Exception("目标用户不存在");  
	    }  
	  
	    public void ChangePassword(String newPassword)throws Exception  
	    {  
	     if(newPassword.equals(this.password))  
	     {  
	         throw new Exception("原密码和新密码不能一样");  
	     }  
	  
	     else  
	     {  
	         if(newPassword.equals(""))  
	         {  
	             throw new Exception("密码不能为空");  
	         }  
	  
	     }  
	     password=newPassword;  
	        Test.usersListUpdate();  
	  
	  
	    }  
	  	  	  
}

ChangePassWord.java

package atm;
import javax.swing.*;  
import java.awt.*;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener; 
public class ChangePassword implements ActionListener{
	public JPasswordField oldPassword,newPassword,checkPassword;  
    public JFrame iframe;  
    public JPanel ip0,ip1,ip2,ip3,ip4;  
    public JButton confirm,cancel,exit;  
    public ChangePassword() {  
        iframe=new JFrame("更改密码");  
        iframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  
  
        ip2=new JPanel();  
        ip2.add(new JLabel("原密码:"));  
        oldPassword=new JPasswordField(20);  
        ip2.add(new JLabel("<html><br/><html>"));//换行  
        ip2.add(oldPassword);  
  
        ip0=new JPanel();  
        ip0.add(new JLabel("新密码:"));  
        newPassword=new JPasswordField(20);  
        ip0.add(new JLabel("<html><br/><html>"));//换行  
        ip0.add(newPassword);  
  
        ip4=new JPanel();  
        ip4.add(new JLabel("再次输入新密码:"));  
        checkPassword=new JPasswordField(20);  
        ip4.add(new JLabel("<html><br/><html>"));//换行  
        ip4.add(checkPassword);  
  
        ip3=new JPanel();  
        confirm=new JButton("确定");  
        ip3.add(confirm);  
        cancel=new JButton("返回");  
        ip3.add(cancel);  
  
        iframe.add(ip2);  
        iframe.add(ip0);  
        iframe.add(ip4);  
        iframe.add(ip3);  
        iframe.add(confirm);  
        iframe.add(cancel);  
        iframe.setLayout(new FlowLayout());  
        iframe.setVisible(true);  
        iframe.setTitle("密码更改");//窗体标签  
        iframe.setSize(400, 200);//窗体大小  
        iframe.setLocationRelativeTo(null);//在屏幕中间显示(居中显示)  
        confirm.addActionListener(this);  
  
        cancel.addActionListener(this);  
  
    } 
    public void  pw_clean() {
    	newPassword.setText("");
		oldPassword.setText("");
		checkPassword.setText("");
    }
  
    @Override  
    public void actionPerformed(ActionEvent e) {  
        if(e.getActionCommand().equals("确定")) {  
            if (Test.currentAccount.password.equals(oldPassword.getText())) {  
                try {  
                    if(newPassword.getText().equals(checkPassword.getText())) { 
                    	if(newPassword.getText().length()>=6) {
                    		Test.currentAccount.ChangePassword(newPassword.getText());  
                    		JOptionPane.showMessageDialog(null, "更改密码成功");  
                    		iframe.setVisible(false);  
                            Test.menu.mframe.setVisible(false);//关闭菜单界面  
                            new LoginGui();  
                    	}else {
                    		JOptionPane.showMessageDialog(null,"密码不能少于6位!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
                    		pw_clean();
                    	}
                    }  
                    else  
                    {  
                        JOptionPane.showMessageDialog(null, "两次输入的密码不一致");  
                        pw_clean();
                    }  
                }  
             catch (Exception e1) {//捕获账户类中更改密码函数的异常并弹窗显示  
                    JOptionPane.showMessageDialog(null, e1.getMessage()); 
                    pw_clean();
                }  
            } else {  
  
                JOptionPane.showMessageDialog(null, "原密码错误");  
                pw_clean();
            }  
        }  
        else//如果点击cancel  
        {  
            iframe.setVisible(false);  
        }  
    }  
}

InMoney.java

package atm;
import javax.swing.*;  
import java.awt.*;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.io.FileWriter;  
public class InMoney implements ActionListener{
	public JTextField money;  
    public JFrame iframe;  
    public JPanel ip0,ip1,ip2,ip3;  
    public JButton confirm,cancel,exit;  
    public JLabel yue; 
    public InMoney() {  
        iframe=new JFrame("存款");  
        ip0=new JPanel();  
        ip0.add(new JLabel("账户id:"+Test.currentAccount.id));  
        
        ip1=new JPanel();  
        yue=new JLabel("账户余额:"+Test.currentAccount.money);  
        ip1.add(yue);  
  
        ip2=new JPanel();  
        ip2.add(new JLabel("存款金额:"));  
        money=new JTextField(20);  
        ip2.add(money);  
  
        ip3=new JPanel();  
        confirm=new JButton("确定");  
        ip3.add(confirm);  
        cancel=new JButton("返回");  
        ip3.add(cancel);  
        
        
        
        iframe.add(ip0);  
        iframe.add(ip1);  
        iframe.add(ip2);  
        iframe.add(confirm);  
        iframe.add(cancel);  
        iframe.setLayout(new FlowLayout());  
        iframe.setVisible(true);  
        iframe.setSize(350, 300);//窗体大小 
        iframe.setLocationRelativeTo(null);//在屏幕中间显示(居中显示)    
        confirm.addActionListener(this);//绑定监听器  
  
        cancel.addActionListener(this);  
  
    }  
  
    @Override  
    public void actionPerformed(ActionEvent e) {  
        if(e.getActionCommand().equals("确定"))//按下确定按钮  
        {  
            try { 
            	int money1=Integer.parseInt(money.getText());
            	if(money1%100==0) {
            		Test.currentAccount.inMoney(Integer.parseInt(money.getText()));//调用当前登陆账户的存钱函数          
                    JOptionPane.showMessageDialog(null, "存款成功");//弹窗  
                    yue.setText("账户余额:"+Test.currentAccount.money);
            	}else {
            		JOptionPane.showMessageDialog(null, "系统不支持非100元整钞存款\n 请重新输入存款款金额 ! ");//弹窗 
            		money.setText("");
            	}
                  
            }  
            catch (ClassCastException e1)//捕获当前登录账户中inmoney函数中的异常。类型转换异常  
            {  
  
                JOptionPane.showMessageDialog(null, "输入数据类型错误,请输入整数");  
  
            }  
            catch (Exception e1)//  
            {  
                JOptionPane.showMessageDialog(null, e1.getMessage());  
            }  
        }  
        else  
        {  
        iframe.setVisible(false);//隐藏  
  
        }  
    }  
}

三、完整代码

该课程设计评级为优秀,需要完整代码详见

【Java课设】–ATM取款机(Gui界面)

  • 8
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

署前街的少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值