package HomeWork0421;
import java.io.FileReader; import java.io.FileWriter; import java.util.Properties;
import javax.swing.JOptionPane;
public class MyWork {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "欢迎光临");
boolean a = login();
if (a == false) {
JOptionPane.showMessageDialog(null, "非法用户");
System.exit(0); }
while (true) {
String s = JOptionPane.showInputDialog(null, "请输入你选择的操作\n1、存款\n2、取款\n3、查询\n4、改密\n5、退出");
switch (s) {
case "1":
deposit();
break;
case "2":
draw();
break;
case "3":
query();
break;
case "4":
passwordChange();
break;
case "5":
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "输入错误");
break;
}
}
}
// 改密码
public static void passwordChange() {
Properties a = new Properties();
try {
a.load(new FileReader("pwd.txt"));
} catch (Exception e) {
System.out.println("没有找到该文件");
}
String n = a.getProperty("pwd");
String s=JOptionPane.showInputDialog(null,"请输入旧密码");
if(s.equals(n)){
String s1=JOptionPane.showInputDialog(null,"请输入新密码");
String s2=JOptionPane.showInputDialog(null,"请确认新密码");
if(s1.equals(s2)){
a.setProperty("pwd", s1);
try {
a.store(new FileWriter("pwd.txt"), null);
} catch (Exception e) {
System.out.println("没有找到该文件");
}
}else{
JOptionPane.showMessageDialog(null, "两次输入的密码不一致");
return;
}
} else{
JOptionPane.showMessageDialog(null, "旧密码不正确");
}
}
// 查询
public static void query() {
Properties a = new Properties();
try {
a.load(new FileReader("pwd.txt"));
} catch (Exception e) {
System.out.println("没有找到该文件");
}
JOptionPane.showMessageDialog(null, a.getProperty("money"));
}
// 取款
public static void draw() {
Properties a = new Properties();
try {
a.load(new FileReader("pwd.txt"));
} catch (Exception e) {
System.out.println("没有找到该文件");
}
int n = Integer.parseInt(a.getProperty("money"));
int b = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入你的存款金额"));
if (b > n) {
JOptionPane.showMessageDialog(null, "账号已超支");
}
a.setProperty("money", (n - b) + "");
try {
a.store(new FileWriter("pwd.txt"), null);
} catch (Exception e) {
System.out.println("没有找到该文件"); } }
// 存款
public static void deposit() {
Properties a = new Properties();
try { a.load(new FileReader("pwd.txt"));
} catch (Exception e) {
System.out.println("没有找到该文件");
}
int n = Integer.parseInt(a.getProperty("money"));
int b = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入你的存款金额"));
a.setProperty("money", (n + b) + "");
try { a.store(new FileWriter("pwd.txt"), null);
} catch (Exception e) {
System.out.println("没有找到该文件");
}
}
public static boolean login() {
Properties a = new Properties();
try { a.load(new FileReader("pwd.txt"));
} catch (Exception e) {
System.out.println("没有找到该文件");
}
String s = a.getProperty("userName");
String n = a.getProperty("pwd");
for (int i = 3; i > 0; i--) {
String nam = JOptionPane.showInputDialog(null, "请输入帐号");
String num = JOptionPane.showInputDialog(null, "请输入密码");
if (s.equals(nam) && n.equals(num)) {
return true;
} else
JOptionPane.showMessageDialog(null, "输入的账号或密码错误"); } return false; }
}