文章目录
- 完整程序
- 1. 课程设计目的
- 2. 课程设计任务与要求
- 3. 课程设计说明书
- 3.1 需求分析
- 3.1.1 功能分析
- 3.1.2 性能要求分析
- 3.2 概要设计
- 3.2.1 功能模块图,如图1。
- 3.3 详细设计
- 3.3.1 实体类的设计
- 3.3.2 实现数据库处理
- 3.4 主要程序功能流程图
- 4. 课程设计成果
- 4.1 主要代码
- 4.1.1 测试类
- 4.1.2 初始化界面类
- 4.1.3 登录界面类
- 4.1.4 注册界面类
- 4.1.5 登录成功后,主界面类
- 4.1.6 查询存款信息类
- 4.1.7 存款类
- 4.1.8 取款类
- 4.1.9 转账类
- 4.1.10 修改密码类
- 4.2 TXT文件内容
- 4.3 运行结果
- 5. 课程设计心得
完整程序
1. 课程设计目的
《面向对象程序设计》是一门实践性很强的计算机专业基础课程,课程设计是学习完该课程后进行的一次较全面的综合练习。其目的在于通过实践加深学生对面向对象程序设计的理论、方法和基础知识的理解,掌握使用Java语言进行面向对象设计的基本方法,提高运用面向对象知识分析实际问题、解决实际问题的能力。
2. 课程设计任务与要求
课程设计可选用NetBeans、Eclipse、JBuilder等作为开发平台以提高开发效率,尽可能熟练掌握其中一种集成开发环境。
通过这次设计,要求掌握以下内容:
1)面向对象技术中的继承与多态(重载和覆盖)机制、各种修饰符的使用
2)类、包、接口的定义与使用
3)常用工具类与算法的实现(数组、向量、字符串、链表)
4)Java常用标准GUI组件及其事件处理
5)Java的异常处理机制
6)Java的数据库连接技术
7)Java的多线程技术
8)Java的网络编程
3. 课程设计说明书
3.1 需求分析
3.1.1 功能分析
1、ATM机个人用户的信息的显示和删除,个人用户信息主要包括:账号,密码,存款的多少。
2、存款信息的显示,更改,操作,存款的存储,提取,转账,金额。
3、个人用户密码的更改。满足用户的需求。
4、设置一个登陆界面,保护用户的个人账户的安全。
3.1.2 性能要求分析
1、系统易操作性:
所开发的系统就做到操作简单,尽量使系统操作不受用户对电脑知识水平的限制。
2、系统具有可维护性:
由于系统涉及的信息比较广,TXT中的数据需要定期修改,系统可利用的空间及性能也随之下降,为了使系统更好地运转。
3、系统具有开放性:
该系统能够在开放的硬件系结构中运行,并且能与其他系统顺利连接,不会因外部系统的不同面要做在量的修改工作。
3.2 概要设计
3.2.1 功能模块图,如图1。
3.3 详细设计
3.3.1 实体类的设计
存款信息实体类:属性包括存款余额(deposit),取款数额(withdraw),转账数额(transfer accounts)。
账户信息实体类:属性包括账户密码(Password)
3.3.2 实现数据库处理
本程序采用txt文件对数据进行存储,在java实际中添加一个data类来进行对txt文件中的东西进行操作。
3.4 主要程序功能流程图
1、查询存款信息,如图2。存储存款信息,如图3。取出存款信息,如图4。转出存款信息,如图5。
2、修改账户信息,如图6。
4. 课程设计成果
4.1 主要代码
(加注释共1200+代码)
4.1.1 测试类
package com.wangjunwei.system;
public class A_Text {
public static void main(String[] args) {
//主函数
new B_Init();
}
}
4.1.2 初始化界面类
package com.wangjunwei.system;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class B_Init extends JFrame {
//初始化界面
public B_Init() {
this.setTitle("ATM系统");
this.setSize(1800, 1000);
this.setLayout(null);
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setResizable(false);
//创建一个容器
JPanel initPanel = new JPanel();
setContentPane(initPanel);
initPanel.setLayout(null);
//"欢迎光临"标签
JLabel hello1 = new JLabel("欢迎光临");
hello1.setFont(new Font("黑体", 3, 150));
hello1.setForeground(Color.WHITE);
//System.out.println(hello1.getPreferredSize());
hello1.setBounds(420, 250, 790, 172);
hello1.setOpaque(false);
initPanel.add(hello1);
//"ATM系统"标签
JLabel hello2 = new JLabel("ATM系统");
hello2.setFont(new Font("黑体", 3, 150));
hello2.setForeground(Color.WHITE);
//System.out.println(hello2.getPreferredSize());
hello2.setBounds(700, 450, 790, 172);
hello2.setOpaque(false);
initPanel.add(hello2);
//登录按钮
JButton loginIn = new JButton("登录");
String user = null;
loginIn.setForeground(Color.DARK_GRAY);
loginIn.setFont(new Font("黑体", 1, 40));
loginIn.setBackground(Color.WHITE);
loginIn.setBounds(600, 700, 200, 66);
initPanel.add(loginIn);
//登录事件
loginIn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new B_Login(user);
}
});
//注册按钮
JButton registerIn = new JButton("注册");
registerIn.setForeground(Color.DARK_GRAY);
registerIn.setFont(new Font("黑体", 1, 40));
registerIn.setBackground(Color.WHITE);
registerIn.setBounds(1000, 700, 200, 66);
//注册事件
registerIn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new B_Register();
}
});
initPanel.add(registerIn);
//背景图
ImageIcon picture = new ImageIcon(new ImageIcon("src\\com\\wangjunwei\\system\\P1.jpg").getImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_DEFAULT));
JLabel imageLabel = new JLabel(picture);
imageLabel.setBounds(0, 0, getWidth(), getHeight());
initPanel.add(imageLabel);
this.setVisible(true);
}
}
4.1.3 登录界面类
package com.wangjunwei.system;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Image;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class B_Login extends JFrame {
//创建一个字典,用于存放账号密码
Map<String, String> users = new HashMap<>();
public B_Login(String User) {
//初始化界面
this.setTitle("Login");
this.setSize(800, 400);
this.setLocationRelativeTo(null);
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(2);
this.setResizable(false);
init();
this.setVisible(true);
}
public void init() {
//字体
Font faceOne = new Font("黑体", 3, 40);
Font faceTwo = new Font("Comic Sans MS", 3, 25);
//创建一个容器
JPanel loginPanel = new JPanel();
loginPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
setContentPane(loginPanel);
loginPanel.setLayout(null);
//"登录"标签
JLabel loginTitle = new JLabel("登 录");
loginTitle.setFont(new Font("黑体", 1, 50));
loginTitle.setForeground(Color.WHITE);
//System.out.println(ID.getPreferredSize());
loginTitle.setBounds(125, 50, 148, 47);
loginTitle.setOpaque(false);
loginPanel.add(loginTitle);
//"账号"标签
JLabel ID = new JLabel("账号:");
ID.setFont(faceOne);
ID.setForeground(Color.WHITE);
//System.out.println(ID.getPreferredSize());
ID.setBounds(30, 130, 148, 47);
ID.setOpaque(false);
loginPanel.add(ID);
//"密码"标签
JLabel passWord = new JLabel("密码:");
passWord.setFont(faceOne);
passWord.setForeground(Color.WHITE);
passWord.setBounds(30, 180, 148, 47);
passWord.setOpaque(false);
loginPanel.add(passWord);
//账号输入框
JFormattedTextField idText = new JFormattedTextField("请输入您的账号");
idText.setForeground(Color.WHITE);
idText.setToolTipText("");
idText.setOpaque(false);
idText.setBounds(150, 135, 200, 40);
MouseListener idAdd = new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if (idText.getText().equals("请输入您的账号")) {
idText.setText(null);
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
};
idText.addMouseListener(idAdd);
loginPanel.add(idText);
//密码输入框
JPasswordField passText = new JPasswordField();
passText.setForeground(Color.WHITE);
passText.setOpaque(false);
passText.setBounds(150, 185, 200, 40);
loginPanel.add(passText);
//登录按钮
JButton loginTo = new JButton("Login");
loginTo.setForeground(Color.DARK_GRAY);
loginTo.setFont(faceTwo);
loginTo.setBackground(Color.WHITE);
loginTo.setBounds(120, 270, 150, 50);
//登录事件
loginTo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String idTextUse = idText.getText();
String passTextUse = new String(passText.getPassword());
String usersPath = "src\\com\\wangjunwei\\system\\Tusers.txt";
try {
File file = new File(usersPath);
if (!file.exists()) {
file.createNewFile();
}
FileReader fileReader = new FileReader(usersPath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(":");
if (parts.length >= 2) {
String key = parts[0].trim();
String value = parts[1].trim();
users.put(key, value);
}
}
bufferedReader.close();
fileReader.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
if ((idTextUse.equals("请输入您的账号")) || (idTextUse.length() == 0) || (!users.containsKey(idTextUse))) {
JOptionPane.showMessageDialog(loginPanel, "账号不存在,请注册!");
} else {
if (passTextUse.equals(users.get(idTextUse))) {
dispose();
JOptionPane.showMessageDialog(loginPanel, "登录成功!");
new C_Deposit(idTextUse);
} else {
JOptionPane.showMessageDialog(loginPanel, "账号或密码错误,请重新输入!");
}
}
}
});
loginPanel.add(loginTo);
//背景图
ImageIcon picture = new ImageIcon(new ImageIcon("src\\com\\wangjunwei\\system\\P2.jpg").getImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_DEFAULT));
JLabel imageLabel = new JLabel(picture);
imageLabel.setBounds(0, 0, getWidth(), getHeight());
loginPanel.add(imageLabel);
}
}
4.1.4 注册界面类
package com.wangjunwei.system;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class B_Register extends JFrame {
//创建两个字典,分别保存账号密码和账号金额
Map<String, String> users = new HashMap<>();
Map<String, String> deposits = new HashMap<>();
public B_Register() {
//初始化界面
this.setTitle("注册");
this.setSize(400, 280);
this.setLocationRelativeTo(null);
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(2);
this.setResizable(false);
init();
this.setVisible(true);
}
public void init() {
//字体
Font faceOne = new Font("黑体", 3, 25);
Font faceTwo = new Font("Comic Sans MS", 3, 25);
//创建一个容器
JPanel registerPanel = new JPanel();
registerPanel.setBorder(BorderFactory.createLineBorder(Color.WHITE));
setContentPane(registerPanel);
registerPanel.setLayout(null);
//"注册账号"标签
JLabel ID = new JLabel("注册账号:");
ID.setFont(faceOne);
ID.setForeground(Color.WHITE);
//System.out.println(ID.getPreferredSize());
ID.setBounds(20, 20, 148, 47);
ID.setOpaque(false);
registerPanel.add(ID);
//"注册密码"标签
JLabel passWord = new JLabel("注册密码:");
passWord.setFont(faceOne);
passWord.setForeground(Color.WHITE);
passWord.setBounds(20, 70, 148, 47);
passWord.setOpaque(false);
registerPanel.add(passWord);
//"确认密码"标签
JLabel passWordAgain = new JLabel("确认密码:");
passWordAgain.setFont(faceOne);
passWordAgain.setForeground(Color.WHITE);
passWordAgain.setBounds(20, 120, 148, 47);
passWordAgain.setOpaque(false);
registerPanel.add(passWordAgain);
//"注册账号"输入框
JFormattedTextField idText = new JFormattedTextField("请输入您的账号");
idText.setForeground(Color.WHITE);
idText.setFont(new Font("宋体", Font.BOLD, 20));
idText.setToolTipText("");
idText.setOpaque(false);
idText.setBounds(150, 25, 200, 40);
//点击"注册账号"输入框触发的事件
MouseListener idAdd = new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if (idText.getText().equals("请输入您的账号")) {
idText.setText(null);
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
};
idText.addMouseListener(idAdd);
registerPanel.add(idText);
//"注册密码"输入框
JPasswordField passText = new JPasswordField();
passText.setForeground(Color.WHITE);
passText.setOpaque(false);
passText.setBounds(150, 75, 200, 40);
registerPanel.add(passText);
//"确认密码"输入框
JPasswordField passAgainText = new JPasswordField();
passAgainText.setForeground(Color.WHITE);
passAgainText.setOpaque(false);
passAgainText.setBounds(150, 125, 200, 40);
registerPanel.add(passAgainText);
//"Register"按钮
JButton loginTo = new JButton("Register");
loginTo.setForeground(Color.WHITE);
loginTo.setFont(faceTwo);
loginTo.setBackground(Color.BLACK);
loginTo.setBounds(100, 180, 200, 40);
//"Register"事件
loginTo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String idTextUse = idText.getText();
String passTextUse = new String(passText.getPassword());
String passAgainTextUse = new String(passAgainText.getPassword());
String filePath = "src\\com\\wangjunwei\\system\\Tusers.txt";
try {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(":");
if (parts.length >= 2) {
String key = parts[0].trim();
String value = parts[1].trim();
users.put(key, value);
}
}
bufferedReader.close();
fileReader.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
if ((idTextUse.equals("请输入您的账号")) || (idTextUse.length() == 0)) {
JOptionPane.showMessageDialog(registerPanel, "请输入你要注册的账号!");
} else if (passTextUse.length() == 0) {
JOptionPane.showMessageDialog(registerPanel, "请输入你要注册的密码!");
} else if (users.containsKey(idTextUse)) {
JOptionPane.showMessageDialog(registerPanel, "该账号已存在,请重新输入!");
} else if (!(passTextUse.equals(passAgainTextUse))) {
JOptionPane.showMessageDialog(registerPanel, "两次密码不一致,请重新输入!");
} else {
try {
File file = new File(filePath);
users.put(idTextUse, passTextUse);
FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (Map.Entry<String, String> entry : users.entrySet()) {
bufferedWriter.write(entry.getKey() + " : " + entry.getValue());
bufferedWriter.newLine();
}
bufferedWriter.close();
fileWriter.close();
JOptionPane.showMessageDialog(registerPanel, "注册成功!");
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
//第一次注册时需要同时创建保存存款信息的文件
String storePath = new String("src\\com\\wangjunwei\\system\\Tdeposits.txt");
try {
File file = new File(storePath);
if (!file.exists()) {
file.createNewFile();
}
FileReader fileReader = new FileReader(storePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(":");
if (parts.length >= 2) {
String key = parts[0].trim();
String value = parts[1].trim();
deposits.put(key, value);
}
}
bufferedReader.close();
fileReader.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
try {
File file = new File(storePath);
deposits.put(idTextUse, "0");
FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (Map.Entry<String, String> entry : deposits.entrySet()) {
bufferedWriter.write(entry.getKey() + " : " + entry.getValue());
bufferedWriter.newLine();
}
bufferedWriter.close();
fileWriter.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
});
registerPanel.add(loginTo);
//背景图
ImageIcon picture = new ImageIcon(new ImageIcon("src\\com\\wangjunwei\\system\\P3.jpg").getImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_DEFAULT));
JLabel imageLabel = new JLabel(picture);
imageLabel.setBounds(0, 0, getWidth(), getHeight());
registerPanel.add(imageLabel);
}
}
4.1.5 登录成功后,主界面类
package com.wangjunwei.system;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class C_Deposit extends JFrame {
public C_Deposit(String User) {
//初始化界面
this.setTitle("存款信息");
this.setSize(800, 500);
this.setLayout(null);
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(2);
this.setResizable(false);
//创建一个容器
JPanel initPanel = new JPanel();
initPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
setContentPane(initPanel);
initPanel.setLayout(null);
//"欢迎"标签
JLabel hello = new JLabel("欢迎 " + User + "!");
hello.setFont(new Font("黑体", 3, 50));
hello.setForeground(Color.WHITE);
//System.out.println(hello.getPreferredSize());
hello.setBounds(150, 50, 1000, 60);
initPanel.add(hello);
//"查询"标签
JButton showMoney = new JButton("查询");
showMoney.setForeground(Color.WHITE);
showMoney.setBackground(Color.BLACK);
showMoney.setOpaque(false);
showMoney.setFont(new Font("黑体", 1, 50));
//System.out.println(showInformation.getPreferredSize());
showMoney.setBounds(150, 180, 200, 50);
showMoney.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new C_DepositShow(User);
}
});
initPanel.add(showMoney);
//"存款"标签
JButton putMoney = new JButton("存款");
putMoney.setForeground(Color.WHITE);
putMoney.setBackground(Color.WHITE);
putMoney.setOpaque(false);
putMoney.setFont(new Font("黑体", 1, 50));
//System.out.println(showInformation.getPreferredSize());
putMoney.setBounds(430, 180, 200, 50);
putMoney.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new C_DepositPut(User);
}
});
initPanel.add(putMoney);
//"取款"标签
JButton getMoney = new JButton("取款");
getMoney.setForeground(Color.WHITE);
getMoney.setBackground(Color.WHITE);
getMoney.setOpaque(false);
getMoney.setFont(new Font("黑体", 1, 50));
//System.out.println(showInformation.getPreferredSize());
getMoney.setBounds(150, 250, 200, 50);
getMoney.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new C_DepositGet(User);
}
});
initPanel.add(getMoney);
//"转账"标签
JButton pushMoney = new JButton("转账");
pushMoney.setForeground(Color.WHITE);
pushMoney.setBackground(Color.WHITE);
pushMoney.setOpaque(false);
pushMoney.setFont(new Font("黑体", 1, 50));
//System.out.println(showInformation.getPreferredSize());
pushMoney.setBounds(430, 250, 200, 50);
pushMoney.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new C_DepositPush(User);
}
});
initPanel.add(pushMoney);
//"修改密码"标签
JButton changePassword = new JButton("修改密码");
changePassword.setForeground(Color.BLACK);
changePassword.setBackground(Color.WHITE);
//changePassword.setOpaque(false);
changePassword.setFont(new Font("黑体", Font.BOLD, 40));
//System.out.println(showInformation.getPreferredSize());
changePassword.setBounds(270, 350, 240, 60);
changePassword.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new C_ChangePassword(User);
}
});
initPanel.add(changePassword);
//背景图
ImageIcon picture = new ImageIcon(new ImageIcon("src\\com\\wangjunwei\\system\\P4.jpg").getImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_DEFAULT));
JLabel imageLabel = new JLabel(picture);
imageLabel.setBounds(0, 0, getWidth(), getHeight());
initPanel.add(imageLabel);
this.setVisible(true);
}
}
4.1.6 查询存款信息类
package com.wangjunwei.system;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class C_DepositShow extends JFrame {
public C_DepositShow(String User) {
//创建一个字典,用于存放存款信息
Map<String, String> deposit = new HashMap<>();
//初始化界面
this.setTitle("查询");
this.setSize(500, 400);
this.setLayout(null);
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(2);
this.setResizable(false);
//创建一个容器
JPanel initPanel = new JPanel();
setContentPane(initPanel);
initPanel.setLayout(null);
//打开文件
String depositsPath = "src\\com\\wangjunwei\\system\\Tdeposits.txt";
try {
FileReader fileReader = new FileReader(depositsPath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(":");
if (parts.length >= 2) {
String key = parts[0].trim();
String value = parts[1].trim();
deposit.put(key, value);
}
}
//"您的余额如下"标签
JLabel myMoneyTitle = new JLabel("您的余额如下");
myMoneyTitle.setFont(new Font("黑体", Font.BOLD, 50));
//System.out.println(fixedMoney.getPreferredSize());
myMoneyTitle.setForeground(Color.WHITE);
myMoneyTitle.setBackground(Color.WHITE);
myMoneyTitle.setBounds(85, 100, 377, 58);
initPanel.add(myMoneyTitle);
//"余额"标签
JLabel myMoney = new JLabel(deposit.get(User) + "元");
myMoney.setFont(new Font("黑体", Font.BOLD, 50));
//System.out.println(fixedMoney.getPreferredSize());
myMoney.setForeground(Color.WHITE);
myMoney.setBackground(Color.WHITE);
myMoney.setBounds(150, 200, 1000, 58);
initPanel.add(myMoney);
bufferedReader.close();
fileReader.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
//背景图
ImageIcon picture = new ImageIcon(new ImageIcon("src\\com\\wangjunwei\\system\\P4.jpg").getImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_DEFAULT));
JLabel imageLabel = new JLabel(picture);
imageLabel.setBounds(0, 0, getWidth(), getHeight());
initPanel.add(imageLabel);
this.setVisible(true);
}
}
4.1.7 存款类
package com.wangjunwei.system;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class C_DepositPut extends JFrame {
public C_DepositPut(String User) {
//创建一个字典,保存存款信息
Map<String, String> deposit = new HashMap<>();
//初始化界面
this.setTitle("存款");
this.setSize(500, 400);
this.setLayout(null);
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(2);
this.setResizable(false);
//创建一个容器
JPanel initPanel = new JPanel();
setContentPane(initPanel);
initPanel.setLayout(null);
//打开文件
String depositsPath = "src\\com\\wangjunwei\\system\\Tdeposits.txt";
try {
FileReader fileReader = new FileReader(depositsPath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(":");
if (parts.length >= 2) {
String key = parts[0].trim();
String value = parts[1].trim();
deposit.put(key, value);
}
}
//"存款"标签
JLabel myMoney = new JLabel("存 款");
myMoney.setFont(new Font("宋体", Font.BOLD, 50));
myMoney.setForeground(Color.WHITE);
myMoney.setBounds(180, 50, 377, 58);
initPanel.add(myMoney);
//"存款"文本框
JFormattedTextField moneyText = new JFormattedTextField("请输入您要存入的金额");
moneyText.setForeground(Color.WHITE);
moneyText.setFont(new Font("宋体", Font.BOLD, 15));
moneyText.setToolTipText("");
moneyText.setOpaque(false);
moneyText.setBounds(150, 150, 200, 40);
MouseListener moneyAdd = new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if (moneyText.getText().equals("请输入您要存入的金额")) {
moneyText.setFont(new Font("宋体", Font.BOLD, 30));
moneyText.setText(null);
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
};
moneyText.addMouseListener(moneyAdd);
initPanel.add(moneyText);
//"Put"按钮
JButton put = new JButton("Put");
put.setForeground(Color.WHITE);
put.setFont(new Font("Comic Sans MS", 3, 50));
put.setBackground(Color.BLACK);
put.setOpaque(false);
put.setBounds(175, 250, 150, 50);
//"Put"事件
put.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int addMoney = Integer.parseInt(moneyText.getText());
int oldMoney = Integer.parseInt(deposit.get(User));
if ((addMoney % 100) == 0 && addMoney != 0) {
String newMoney = Integer.toString(addMoney + oldMoney);
deposit.put(User, newMoney);
String depositsPath = "src\\com\\wangjunwei\\system\\Tdeposits.txt";
try {
File file = new File(depositsPath);
FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (Map.Entry<String, String> entry : deposit.entrySet()) {
bufferedWriter.write(entry.getKey() + " : " + entry.getValue());
bufferedWriter.newLine();
}
bufferedWriter.close();
fileWriter.close();
dispose();
JOptionPane.showMessageDialog(initPanel, "存入成功!");
} catch (IOException ioException) {
ioException.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(initPanel, "请输入100的倍数!(非0)");
}
} catch (NumberFormatException numberFormatException) {
JOptionPane.showMessageDialog(initPanel, "非法输入!");
}
}
});
initPanel.add(put);
bufferedReader.close();
fileReader.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
//背景图
ImageIcon picture = new ImageIcon(new ImageIcon("src\\com\\wangjunwei\\system\\P4.jpg").getImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_DEFAULT));
JLabel imageLabel = new JLabel(picture);
imageLabel.setBounds(0, 0, getWidth(), getHeight());
initPanel.add(imageLabel);
this.setVisible(true);
}
}
4.1.8 取款类
package com.wangjunwei.system;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class C_DepositGet extends JFrame {
public C_DepositGet(String User) {
//创建一个字典,用于保存存款信息
Map<String, String> deposit = new HashMap<>();
//初始化界面
this.setTitle("取款");
this.setSize(500, 400);
this.setLayout(null);
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(2);
this.setResizable(false);
//创建一个容器
JPanel initPanel = new JPanel();
setContentPane(initPanel);
initPanel.setLayout(null);
//打开文件
String depositsPath = "src\\com\\wangjunwei\\system\\Tdeposits.txt";
try {
FileReader fileReader = new FileReader(depositsPath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(":");
if (parts.length >= 2) {
String key = parts[0].trim();
String value = parts[1].trim();
deposit.put(key, value);
}
}
//"取款"标签
JLabel myMoney = new JLabel("取 款");
myMoney.setFont(new Font("宋体", Font.BOLD, 50));
myMoney.setForeground(Color.WHITE);
myMoney.setBounds(180, 50, 377, 58);
initPanel.add(myMoney);
//"取款"文本框
JFormattedTextField moneyText = new JFormattedTextField("请输入您要取出的金额");
moneyText.setForeground(Color.WHITE);
moneyText.setFont(new Font("宋体", Font.BOLD, 15));
moneyText.setToolTipText("");
moneyText.setOpaque(false);
moneyText.setBounds(145, 150, 200, 40);
MouseListener moneyAdd = new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if (moneyText.getText().equals("请输入您要取出的金额")) {
moneyText.setFont(new Font("宋体", Font.BOLD, 30));
moneyText.setText(null);
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
};
moneyText.addMouseListener(moneyAdd);
initPanel.add(moneyText);
//"Get"按钮
JButton get = new JButton("Get");
get.setForeground(Color.WHITE);
get.setFont(new Font("Comic Sans MS", 3, 50));
get.setBackground(Color.BLACK);
get.setOpaque(false);
get.setBounds(170, 250, 150, 50);
//"Get"事件
get.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int deleteMoney = Integer.parseInt(moneyText.getText());
int oldMoney = Integer.parseInt(deposit.get(User));
if ((oldMoney - deleteMoney) < 0) {
JOptionPane.showMessageDialog(initPanel, "余额不足!请重新输入!");
} else if (deleteMoney % 100 != 0 || deleteMoney == 0) {
JOptionPane.showMessageDialog(initPanel, "请输入100的倍数!(非0)");
} else {
String newMoney = Integer.toString(oldMoney - deleteMoney);
deposit.put(User, newMoney);
String depositsPath = "src\\com\\wangjunwei\\system\\Tdeposits.txt";
try {
File file = new File(depositsPath);
FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (Map.Entry<String, String> entry : deposit.entrySet()) {
bufferedWriter.write(entry.getKey() + " : " + entry.getValue());
bufferedWriter.newLine();
}
bufferedWriter.close();
fileWriter.close();
dispose();
JOptionPane.showMessageDialog(initPanel, "取出成功!");
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
} catch (NumberFormatException numberFormatException) {
JOptionPane.showMessageDialog(initPanel, "非法输入!");
}
}
});
initPanel.add(get);
bufferedReader.close();
fileReader.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
//背景图
ImageIcon picture = new ImageIcon(new ImageIcon("src\\com\\wangjunwei\\system\\P4.jpg").getImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_DEFAULT));
JLabel imageLabel = new JLabel(picture);
imageLabel.setBounds(0, 0, getWidth(), getHeight());
initPanel.add(imageLabel);
this.setVisible(true);
}
}
4.1.9 转账类
package com.wangjunwei.system;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class C_DepositPush extends JFrame {
public C_DepositPush(String User) {
//创建一个字典,保存存款信息
Map<String, String> deposit = new HashMap<>();
//初始化界面
this.setTitle("转 账");
this.setSize(500, 400);
this.setLayout(null);
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(2);
this.setResizable(false);
//创建一个容器
JPanel initPanel = new JPanel();
setContentPane(initPanel);
initPanel.setLayout(null);
//打开文件
String depositsPath = "src\\com\\wangjunwei\\system\\Tdeposits.txt";
try {
FileReader fileReader = new FileReader(depositsPath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(":");
if (parts.length >= 2) {
String key = parts[0].trim();
String value = parts[1].trim();
deposit.put(key, value);
}
}
//"转账"标签
JLabel pushTitle = new JLabel("转 账");
pushTitle.setFont(new Font("宋体", Font.BOLD, 50));
pushTitle.setForeground(Color.WHITE);
pushTitle.setBounds(180, 30, 377, 58);
initPanel.add(pushTitle);
//"账号"标签
JLabel pushUser = new JLabel("账号:");
pushUser.setFont(new Font("宋体", Font.BOLD, 40));
pushUser.setForeground(Color.WHITE);
pushUser.setBounds(50, 110, 377, 58);
initPanel.add(pushUser);
//"金额"标签
JLabel pushMoney = new JLabel("金额:");
pushMoney.setFont(new Font("宋体", Font.BOLD, 40));
pushMoney.setForeground(Color.WHITE);
pushMoney.setBounds(50, 180, 377, 58);
initPanel.add(pushMoney);
//"转账账号"文本框
JFormattedTextField userText = new JFormattedTextField("请输入您要转给的账号");
userText.setForeground(Color.WHITE);
userText.setFont(new Font("宋体", Font.BOLD, 15));
userText.setToolTipText("");
userText.setOpaque(false);
userText.setBounds(180, 120, 200, 40);
MouseListener userAdd = new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if (userText.getText().equals("请输入您要转给的账号")) {
userText.setFont(new Font("宋体", Font.BOLD, 30));
userText.setText(null);
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
};
userText.addMouseListener(userAdd);
initPanel.add(userText);
//"转账金额"文本框
JFormattedTextField moneyText = new JFormattedTextField("请输入您要转账的金额");
moneyText.setForeground(Color.WHITE);
moneyText.setFont(new Font("宋体", Font.BOLD, 15));
moneyText.setToolTipText("");
moneyText.setOpaque(false);
moneyText.setBounds(180, 190, 200, 40);
MouseListener moneyAdd = new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if (moneyText.getText().equals("请输入您要转账的金额")) {
moneyText.setFont(new Font("宋体", Font.BOLD, 30));
moneyText.setText(null);
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
};
moneyText.addMouseListener(moneyAdd);
initPanel.add(moneyText);
//"Push"按钮
JButton push = new JButton("Push");
push.setForeground(Color.WHITE);
push.setFont(new Font("Comic Sans MS", 3, 50));
push.setBackground(Color.BLACK);
push.setOpaque(false);
push.setBounds(175, 260, 150, 50);
//"Push"事件
push.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String user = userText.getText();
int pushMoney = Integer.parseInt(moneyText.getText());
int myMoney = Integer.parseInt(deposit.get(User));
int userMoney = Integer.parseInt(deposit.get(user));
System.out.println(userText.getText());
System.out.println(moneyText.getText());
myMoney = myMoney - pushMoney;
if (user.equals(User)) {
JOptionPane.showMessageDialog(initPanel, "不能转给自己!");
} else if ((user.equals("请输入您要转给的账号")) || (moneyText.getText().equals(""))) {
JOptionPane.showMessageDialog(initPanel, "请输入你要转给的账号和金额!");
} else if (myMoney < 0) {
JOptionPane.showMessageDialog(initPanel, "余额不足!请重新输入!");
} else if (pushMoney % 100 != 0 || pushMoney == 0) {
JOptionPane.showMessageDialog(initPanel, "请输入100的倍数!(非0)");
} else if (deposit.containsKey(user)) {
userMoney = userMoney + pushMoney;
String newMyMoney = Integer.toString(myMoney);
String newUserMoney = Integer.toString(userMoney);
deposit.put(User, newMyMoney);
deposit.put(user, newUserMoney);
String depositsPath = "src\\com\\wangjunwei\\system\\Tdeposits.txt";
try {
File file = new File(depositsPath);
FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (Map.Entry<String, String> entry : deposit.entrySet()) {
bufferedWriter.write(entry.getKey() + " : " + entry.getValue());
bufferedWriter.newLine();
}
bufferedWriter.close();
fileWriter.close();
dispose();
JOptionPane.showMessageDialog(initPanel, "转账成功!");
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
} catch (NumberFormatException numberFormatException) {
JOptionPane.showMessageDialog(initPanel, "非法输入!");
}
}
});
initPanel.add(push);
bufferedReader.close();
fileReader.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
//背景图
ImageIcon picture = new ImageIcon(new ImageIcon("src\\com\\wangjunwei\\system\\P4.jpg").getImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_DEFAULT));
JLabel imageLabel = new JLabel(picture);
imageLabel.setBounds(0, 0, getWidth(), getHeight());
initPanel.add(imageLabel);
this.setVisible(true);
}
}
4.1.10 修改密码类
package com.wangjunwei.system;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class C_ChangePassword extends JFrame {
public C_ChangePassword(String User) {
//创建一个字典,用于保存账号和密码
Map<String, String> users = new HashMap<>();
//初始化界面
this.setTitle("修改密码");
this.setSize(500, 400);
this.setLayout(null);
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(2);
this.setResizable(false);
//字体
Font faceOne = new Font("宋体", Font.BOLD, 25);
//创建一个容器
JPanel initPanel = new JPanel();
setContentPane(initPanel);
initPanel.setLayout(null);
//"修改密码"标签
JLabel changeTitle = new JLabel("修改密码");
changeTitle.setFont(new Font("黑体", 3, 40));
changeTitle.setForeground(Color.WHITE);
changeTitle.setBounds(150, 30, 200, 47);
changeTitle.setOpaque(false);
initPanel.add(changeTitle);
//"原始密码"标签
JLabel oldPassword = new JLabel("原始密码:");
oldPassword.setFont(faceOne);
oldPassword.setForeground(Color.WHITE);
oldPassword.setBounds(50, 100, 148, 47);
oldPassword.setOpaque(false);
initPanel.add(oldPassword);
//"新的密码"标签
JLabel newPassword = new JLabel("新的密码:");
newPassword.setFont(faceOne);
newPassword.setForeground(Color.WHITE);
newPassword.setBounds(50, 150, 148, 47);
newPassword.setOpaque(false);
initPanel.add(newPassword);
//"确认密码"标签
JLabel passWordAgain = new JLabel("确认密码:");
passWordAgain.setFont(faceOne);
passWordAgain.setForeground(Color.WHITE);
passWordAgain.setBounds(50, 200, 148, 47);
passWordAgain.setOpaque(false);
initPanel.add(passWordAgain);
//"原始密码"输入框
JPasswordField oldPassText = new JPasswordField();
oldPassText.setForeground(Color.WHITE);
oldPassText.setOpaque(false);
oldPassText.setBounds(200, 100, 200, 40);
initPanel.add(oldPassText);
//"新的密码"输入框
JPasswordField newPassText = new JPasswordField();
newPassText.setForeground(Color.WHITE);
newPassText.setOpaque(false);
newPassText.setBounds(200, 150, 200, 40);
initPanel.add(newPassText);
//"确认密码"输入框
JPasswordField passAgainText = new JPasswordField();
passAgainText.setForeground(Color.WHITE);
passAgainText.setOpaque(false);
passAgainText.setBounds(200, 200, 200, 40);
initPanel.add(passAgainText);
//"Change"按钮
JButton changePassword = new JButton("Change");
changePassword.setBackground(Color.BLACK);
changePassword.setFont(new Font("Comic Sans MS", 3, 30));
changePassword.setForeground(Color.WHITE);
changePassword.setOpaque(false);
changePassword.setBounds(150, 270, 200, 50);
//"Change"事件
changePassword.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String oldPassword = new String(oldPassText.getPassword());
String newPassword = new String(newPassText.getPassword());
String againPassword = new String(passAgainText.getPassword());
String filePath = "src\\com\\wangjunwei\\system\\Tusers.txt";
try {
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(":");
if (parts.length >= 2) {
String key = parts[0].trim();
String value = parts[1].trim();
users.put(key, value);
}
}
if (oldPassword.equals(users.get(User))) {
if (newPassword.equals(againPassword)) {
users.put(User, newPassword);
try {
File file = new File(filePath);
FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (Map.Entry<String, String> entry : users.entrySet()) {
bufferedWriter.write(entry.getKey() + " : " + entry.getValue());
bufferedWriter.newLine();
}
bufferedWriter.close();
fileWriter.close();
dispose();
JOptionPane.showMessageDialog(initPanel, "修改成功!");
} catch (IOException ioException) {
ioException.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(initPanel, "两次密码不一致!请重新输入!");
}
} else {
JOptionPane.showMessageDialog(initPanel, "原始密码错误!请重新输入!");
}
bufferedReader.close();
fileReader.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
});
initPanel.add(changePassword);
//背景图
ImageIcon picture = new ImageIcon(new ImageIcon("src\\com\\wangjunwei\\system\\P4.jpg").getImage().getScaledInstance(getWidth(), getHeight(), Image.SCALE_DEFAULT));
JLabel imageLabel = new JLabel(picture);
imageLabel.setBounds(0, 0, getWidth(), getHeight());
initPanel.add(imageLabel);
this.setVisible(true);
}
}
4.2 TXT文件内容
① Tusers.txt
(账号:密码)
1 : 1
2 : 2
wjw : 1
lsr:1
② Tdeposits.txt
(账号:存款金额)
1 : 3100
2 : 3100
wjw : 1000
lsr : 10000000
4.3 运行结果
①初始化界面
②登录入口
③注册入口
④登录后进入的主界面
⑤查询界面
⑥存款界面
⑦取款界面
⑧转账界面
⑨修改密码界面
5. 课程设计心得
在Java课程的大作业中,我完成了一个ATM存款机的设计和实现。完成这个任务,不仅让我深刻认识到了Java编程的强大,同时也让我明白了ATM机器背后那个看不见的程序员的努力与艰辛。
我在Java课程设计中学到了很多知识和技能,并且对编程有了更深入的理解。首先,在这个项目中,我了解了Java的面向对象开发,掌握了基础语法和常用类的使用。其次,我学习了如何设计和实现一个简单的ATM自助取款系统,包括需求分析、设计方案、编码和测试,在这个过程中,我学会了如何使用各种编程工具来提高效率和减少错误。
首先,我了解了ATM存款机的基本功能和要求,比如身份验证、存款等。经过学习和思考,我一步步地实现了这些功能。注册登录模块,输入密码进行登陆,正确的密码才能登陆成功,同时页面的跳转有了清晰的设计,让整个系统变得更加人性化。然后我设计了查询余额,存款,取款,转账等功能,简单的实现了一个ATM自助取款系统的主要功能,最后我还设置了修改密码的功能,丰富了整个系统。
在整个项目中,我也遇到了很多挑战和难题。例如,在各种类的设计和使用中,我遇到了很多困难,但通过不断的尝试和学习,我最终解决了这些问题。此外,在实现各种功能时,我也经常遇到错误和bug,但我学会了如何通过调试和排查问题来解决这些问题。
总体而言,Java课程设计是一次非常有收获的学习经历。它不仅提高了我的编程能力,还使我更加了解软件开发的整个过程。我相信这些经验和技能将对我的未来学习和职业发展有很大帮助,在这个项目中,我收获了很多,它让我对Java编程语言的实际应用更加深入地了解。在独自完成这个项目的一瞬间,我充满了成就感,那一刻,我感觉自己的付出都是值得的,我会继续努力下去,争取完成更加出色的项目!