这是零钱系统的第二个版本,第一个版本进行充值和提现的操作都是基于控制台完成的,而该版本是在窗口中运行。
同样的,在做项目前,先画出流程图,整理好一个整体框架,如下:
有了框架,再逐一按照框架去设计编程思路,,图中主界面有三个按钮,于是第一就可以设计三个不同类,分别为TopUp(充值)类,Withdraw(提现)类,CheckBalance(余额查询)类,然后创建PayTest类去实现这三个类中的方法。然后这次的每次充值和提现我希望是可以保存的,设计了一个Update类去更新和保存每次充值和提现后的余额。总计5个类。
上源码:
TopUp(充值)类:
import javax.swing.*;
import java.awt.*;
public class TopUp {
String str1;
Update balance = new Update();
JPasswordField f2 = new JPasswordField();//创建成员对象方便内部类使用
public void Top_Up() {
class Operate {//局部内部类
final JFrame b = new JFrame();
public boolean show() {
try {
if (0 < Double.parseDouble(str1)) {
return true;
} else if (str1.equals("")) {
JOptionPane.showMessageDialog(b, "输入内容为空");
return false;
} else {
JOptionPane.showMessageDialog(b, "输入正数!");
return false;
}
} catch (NumberFormatException E) {
JOptionPane.showMessageDialog(b, "输入数字啊喂(#`O′)");
return false;
}
}
public boolean Password(){
String password = new String(f2.getPassword());
if (password.equals("123")) {
return true;
}else if (password.equals("")){
JOptionPane.showMessageDialog(b,"请输入密码!");
return false;
}else {
JOptionPane.showMessageDialog(b,"密码错误!请重试","错误",JOptionPane.ERROR_MESSAGE);
return false;
}
}
}
JFrame A = new JFrame("充值");
A.setSize(300, 300);
JLabel l1 = new JLabel("请输入你要充值的金额:");
JLabel l2 = new JLabel("请输入支付密码:");
Font font = new Font("黑体", Font.PLAIN, 20);
l1.setFont(font);
l2.setFont(font);
JButton b1 = new JButton("确认");
JTextField f1 = new JTextField();
b1.setBounds(90, 200, 100, 30);
l1.setBounds(30, 10, 300, 20);
l2.setBounds(30,100,300,20);
f1.setBounds(30, 50, 100, 20);
f2.setBounds(30,150,100,20);
A.add(b1);
A.add(l1);
A.add(f1);
A.add(l2);
A.add(f2);
b1.addActionListener(e -> {
str1 = f1.getText();
Operate O = new Operate();
if (O.show()&& O.Password()) {
topUp();
}
});
A.setLayout(null);
A.setLocationRelativeTo(null);
A.setVisible(true);
}
public void topUp(){
balance.readBalance();
double add = Double.parseDouble(str1);
balance.balance += add;
JOptionPane.showMessageDialog(null, "充值成功!");
balance.writeBalance();
}
}
Withdraw(提现)类:
import javax.swing.*;
import java.awt.*;
public class Withdraw {
String str2;
Update balance = new Update();
JPasswordField f2 = new JPasswordField();//创建成员对象方便后内部类使用
public void withdraw() {
class Operate {//局部内部类
final JFrame b = new JFrame();
public boolean show() {
try {
if (0 < Double.parseDouble(str2)) {
return true;
}else if (str2.equals("")) {
JOptionPane.showMessageDialog(b, "输入内容为空");
return false;
} else {
JOptionPane.showMessageDialog(b, "输入正数");
return false;
}
} catch (NumberFormatException E) {
JOptionPane.showMessageDialog(b, "输入整数啊喂(#`O′)");
return false;
}
}
public boolean Password() {
String password = new String(f2.getPassword());
if (password.equals("123")) {
return true;
} else if(password.equals("")){
JOptionPane.showMessageDialog(b,"请输入密码!");
return false;
}else {
JOptionPane.showMessageDialog(b, "密码错误!请重试", "错误", JOptionPane.ERROR_MESSAGE);
return false;
}
}
}
JFrame B = new JFrame("提现");
B.setSize(300, 300);
JLabel l1 = new JLabel("请输入你要提现的金额:");
JLabel l2 = new JLabel("请输入支付密码:");
Font font = new Font("黑体", Font.PLAIN, 20);//设置字体和字体大小
l2.setFont(font);
l1.setFont(font);
JButton b2 = new JButton("确认");
JTextField f1 = new JTextField();
b2.setBounds(90, 200, 100, 30);
l1.setBounds(30, 10, 300, 20);
l2.setBounds(30,100,300,20);
f1.setBounds(30, 50, 100, 20);
f2.setBounds(30,150,100,20);
B.add(l1);
B.add(f1);
B.add(b2);
B.add(l2);
B.add(f2);
b2.addActionListener(e -> {
str2 = f1.getText();
Operate O = new Operate();
if (O.show()&&O.Password()){
withdrawOperation();
}
});
B.setLayout(null);
B.setLocationRelativeTo(null);
B.setVisible(true);
}
public void withdrawOperation(){
balance.readBalance();
double delete = Double.parseDouble(str2);
if (balance.balance - delete < 0) {
JOptionPane.showMessageDialog(null, "余额不足", "错误", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "提现成功!");
balance.balance -= delete;
balance.writeBalance();
}
}
}
这俩个类的代码比较相似,主要一个是加钱一个是提钱,然后提钱多了一个余额不足时的报错。
CheckBalance(余额查询)类
import javax.swing.*;
import java.awt.*;
public class CheckBalance {
Update balance = new Update();
public void checkBalance(){
JFrame C = new JFrame("余额查询");
C.setSize(300,100);
balance.readBalance();
JLabel l3 = new JLabel("当前余额为:"+balance.balance+"¥");
Font font = new Font("黑体",Font.PLAIN,20);//设置字体和字体大小
l3.setFont(font);
l3.setBounds(50,50,300,200);
C.add(l3);
C.setLocationRelativeTo(null);
C.setVisible(true);
}
}
Update类
import java.io.*;
public class Update {
double balance;
// 读取文件中的余额信息
public void readBalance() {
try {
File file = new File("D:\\桌面\\balance.txt");
if (!file.exists()) {
file.createNewFile();
} else {
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String line = br.readLine();
if (line != null && !line.equals("")) {
balance = Double.parseDouble(line);
writeBalance();
}
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 保存余额信息到文件
public void writeBalance() {
try {
File file = new File("D:\\桌面\\balance.txt");
FileOutputStream fos = new FileOutputStream(file);//字节流
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(Double.toString(balance));//转换为字符串形式写到文件里
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
该类中设计了两个方法,分别为readBalance(读取余额),writeBalance(写入余额)。每次充值和体现都会先读取文件里的余额,然后充值就会在读取到的余额上加上往里面充的钱,提现就会在读取到的余额上减掉相应的钱,余额不足会报错。每次充值和提现后就会更新余额,保存到文件里。
PayTest测试类:
import javax.swing.*;
import java.awt.*;
public class PayTest {
public static void main(String[] args) {
JFrame J = new JFrame("微信支付");
J.setSize(400,400);
J.setLayout(new BorderLayout());
JPanel P = new JPanel();//创建面板
JButton a = new JButton("充值");
JButton b = new JButton("提现");
JButton c = new JButton("余额查询");
a.setBounds(90,50,200,50);
b.setBounds(90,150,200,50);
c.setBounds(90,250,200,50);
P.add(a);
P.add(b);
P.add(c);
P.setLayout(new BorderLayout());
a.addActionListener(e -> {
TopUp topUp = new TopUp();
topUp.Top_Up();
});
b.addActionListener(e -> {
Withdraw withdraw = new Withdraw();
withdraw.withdraw();
});
c.addActionListener(e -> {
CheckBalance checkBalance = new CheckBalance();
checkBalance.checkBalance();
});
J.add(P);
J.setResizable(false);
J.setLocationRelativeTo(null);
J.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
J.setVisible(true);
}
}
我将主界面设置在测试类中了,所以测试类看起来会有点繁琐。
下面是我截取的几张效果图;