package xxxxxx;
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 static void main(String[] args) {
ATM a = new ATM();
}
public ATM() {
try {
pro.load(new FileReader("atm.txt"));
} catch (Exception e) {
System.out.println("文件没找到");
}
JOptionPane.showMessageDialog(null, "欢迎使用XXX银行");
boolean island = denglu();
if (island == false) {
JOptionPane.showMessageDialog(null, "非法用户");
System.exit(0);
}
while (true) {
String a = JOptionPane.showInputDialog(null, "1.存款\n2.取款\n3.查询余额\n4.修改密码\n5.退出");
int str = Integer.parseInt(a);
switch (str) {
case 1:
saveMoney();
break;
case 2:
getMoney();
break;
case 3:
shouMoney();
break;
case 4:
update();
break;
case 5:
JOptionPane.showMessageDialog(null, "请收好您的卡片");
System.exit(0);
break;
}
}
}
/** 修改密码 */
public void update() {
String s = JOptionPane.showInputDialog(null, "请输入旧密码");
if (s.equals(pro.getProperty("pwd"))) {
String a = JOptionPane.showInputDialog(null, "请输入新密码");
String b = JOptionPane.showInputDialog(null, "请再次输入新密码");
if (a.equals(b)) {
pro.setProperty("pwd", b + "");
set();
JOptionPane.showMessageDialog(null, "密码修改成功");
return;
}
JOptionPane.showMessageDialog(null, "两次密码不正确");
return;
}
JOptionPane.showMessageDialog(null, "旧密码错误");
return;
}
/** 余额查询 */
public void shouMoney() {
JOptionPane.showMessageDialog(null, pro.getProperty("money"));
}
/** 取款 */
public void getMoney() {
String a = JOptionPane.showInputDialog(null, "请输入要取款的金额");
int s = Integer.parseInt(a);
int money = Integer.parseInt(pro.getProperty("money"));
if (s > money) {
JOptionPane.showMessageDialog(null, "余额不足");
return;
}
money -= s;
pro.setProperty("money", money + "");
set();
JOptionPane.showMessageDialog(null, "存款成功,现在余额为" + pro.getProperty("money"));
}
/** 存款 */
public void saveMoney() {
String a = JOptionPane.showInputDialog(null, "请输入存款金额");
// 存款金额
int s = Integer.parseInt(a);
// 卡里面的钱
int money = Integer.parseInt(pro.getProperty("money"));
// 存款以后的钱
money += s;
pro.setProperty("money", money + "");
set();
JOptionPane.showMessageDialog(null, "取款成功,现在余额为" + pro.getProperty("money"));
}
// 替换
public void set() {
try {
pro.store(new FileWriter("atm.txt"), null);
} catch (Exception e) {
System.out.println("文件没找到");
}
}
/** 登录 */
public boolean denglu() {
for (int i = 0; i < 3; i++) {
String s = JOptionPane.showInputDialog(null, "请输入账号");
String m = JOptionPane.showInputDialog(null, "请输入密码");
if (s.equals(pro.getProperty("name")) && m.equals(pro.getProperty("pwd"))) {
return true;
}
JOptionPane.showMessageDialog(null, "账号或密码错误,请重新输入");
}
return false;
}
}