利用构造函数实现ATM模拟(精简版)
package day4;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;
import javax.swing.JOptionPane;
public class ATM {
public Properties pro = new Properties();
public String landcode;
public ATM() {//初始化ATM操作
try {
pro.load(new FileReader("src/day4/money.txt"));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "文件不存在");
}
boolean flag = login();
if (flag == false) {
JOptionPane.showMessageDialog(null, "非法用户");
return;
}
while (true) {
String s = JOptionPane.showInputDialog(null, "1、查询\n2、存款\n"
+ "3、取款\n4、修改密码\n5、添加用户\n6、删除用户\n7、轉賬\n8、退出");
int n = Integer.parseInt(s);
switch (n) {
case 1:
this.queryMoney();//查询余额
break;
case 2:
this.addMoney();//存款
break;
case 3:
this.getMoney();//取款
break;
case 4:
this.updatePwd();//改密
break;
case 5:
this.addUser();//增加用户
break;
case 6:
this.delUser();//删除用户
break;
case 7:
this.transfer();//转账
break;
case 8:
JOptionPane.showMessageDialog(null, "欢迎下次再来");
System.exit(0);
default:
JOptionPane.showMessageDialog(null, "输入有误");
break;
}
}
}
public void transfer() {// 转账
String str = JOptionPane.showInputDialog(null,"請輸入要转账的用户名");
if (str.equals(landcode)){
JOptionPane.showMessageDialog(null, "转账用户不能是自己");
return;
}
if (pro.getProperty(str+".pwd") == null){
JOptionPane.showMessageDialog(null,"该账号不存在");
return;
}
String s = JOptionPane.showInputDialog(null,"请输入转账金额");
int m = Integer.parseInt(s);
int num = Integer.parseInt(pro.getProperty(landcode+".money"));
if (num<=0 && m>num){
JOptionPane.showMessageDialog(null,"账户余额不足或者输入金额不对");
return;
}
if (m%100 == 0 && m>0){
String landStr = pro.getProperty(landcode+".money");
String targetStr = pro.getProperty(str+".money");
int landMoney = Integer.parseInt(landStr);
int targetMoney = Integer.parseInt(targetStr);
landMoney -= m;
targetMoney += m;
pro.setProperty(landcode+".money", landMoney+"");
pro.setProperty(str+".money", targetMoney+"");
this.writeFile();
}
else{
JOptionPane.showMessageDialog(null,"输入金额不对,请重新输入");
}
}
public void writeFile() {// 将文件写入money.txt
try {
pro.store(new FileWriter("src/day4/money.txt"), "ATM");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "文件不存在");
}
}
public void queryMoney() {// 查询余额
JOptionPane.showMessageDialog(null, "账户余额为:"+pro.getProperty(landcode+".money"));
}
public void addMoney() {// 存款
String s = JOptionPane.showInputDialog(null, "请输入存款的金额");
int m = Integer.parseInt(s);
if (m>0 && m%100==0){
int sum = m + Integer.parseInt(pro.getProperty(landcode+".money"));
pro.setProperty(landcode+".money", sum + "");
this.writeFile();
}
else{
JOptionPane.showMessageDialog(null,"请输入100的整数再来存款");
}
}
public void getMoney() {// 取款
String s = JOptionPane.showInputDialog(null, "请输入取款的金额");
int m = Integer.parseInt(s);
if (m>0 && m%100==0){
int sum = Integer.parseInt(pro.getProperty(landcode+".money")) - m;
pro.setProperty(landcode+".money", sum + "");
this.writeFile();
}
else{
JOptionPane.showMessageDialog(null,"请输入100的整数再来取款");
}
}
public void updatePwd() {// 修改密码
String p = JOptionPane.showInputDialog(null, "请输入原始密码");
if (p.equals(pro.getProperty(landcode+".pwd"))) {
String pwd = JOptionPane.showInputDialog(null, "请输入新密码");
String pwd1 = JOptionPane.showInputDialog(null, "请再次输入新密码");
if (pwd.equals(pwd1)) {
pro.setProperty(landcode+".pwd", pwd);
}
else
{
JOptionPane.showMessageDialog(null, "二次密码不一致");
}
}
else
{
JOptionPane.showMessageDialog(null, "密码输入有错");
}
this.writeFile();
}
public void addUser() {// 添加用户
String input = JOptionPane.showInputDialog("请输入注册用户名");
pro.setProperty(input+".pwd", JOptionPane.showInputDialog("请输入注册用户名的密码"));
pro.setProperty(input+".money", JOptionPane.showInputDialog("请输入注册用户名的余额"));
this.writeFile();
}
public void delUser() {// 删除用户
String input = JOptionPane.showInputDialog("请输入删除用户名");
if (pro.getProperty(input+".pwd") == null){
JOptionPane.showMessageDialog(null,"该用户不存在");
}
pro.remove(input+".pwd");
pro.remove(input+".money");
this.writeFile();
}
public boolean login() {// 登录
for (int i = 0; i < 3; i++) {
String inputName = JOptionPane.showInputDialog(null, "请输入用户名");
String inputPwd = JOptionPane.showInputDialog(null, "请输入用户名密码");
if (inputPwd.equals(pro.getProperty(inputName+".pwd"))) {
JOptionPane.showMessageDialog(null, "登陆成功");
this.landcode = inputName;
return true;
}
else {
JOptionPane.showMessageDialog(null, "用户名或者密码错误");
}
}
return false;
}
}

浙公网安备 33010602011771号