ATM取款代码

#Sun Apr 01 19:09:52 CST 2018
money=99855000
userName=java
pwd=123

import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;
import javax.swing.JOptionPane;
public class atmSystem {
//定义全局变量
public static Properties user=new Properties();
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "欢迎光临");
try{
user.load(new FileReader("ATM.txt"));
}catch(Exception e){
JOptionPane.showMessageDialog(null, "文件夹不存在");
}
boolean a=register();
if (a==false){
JOptionPane.showMessageDialog(null, "非法用户");
System.exit(0);
}
while(true){
int select=Integer.parseInt(JOptionPane.showInputDialog(null, "1、存款"+"\n"+"2、取款"+"\n"+"3、查询"+"\n"+"4、改密"+"\n"+"5、退出"));
switch(select){
case 1:
deposit();
break;
case 2:
withdrawal();
break;
case 3:
show();
break;
case 4:
amend();
break;
case 5:
System.exit(0);
}
}
}
//登录方法调用
public static boolean register(){
for(int i=0;i<3;i++){
String name=JOptionPane.showInputDialog(null, "请输入用户名");
String pwd=JOptionPane.showInputDialog(null, "请输入密码");
if (name.equals(user.getProperty("userName"))&&pwd.equals(user.getProperty("pwd"))){
return true;
}
else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
}
return false;
}
//写入文件
public static void writer(){
try{
user.store(new FileWriter("ATM.txt"), null);
}catch (Exception e){
JOptionPane.showMessageDialog(null, "文件夹不存在");
}
}
//存款
public static void deposit(){
int money=Integer.parseInt(JOptionPane.showInputDialog(null, "请输入存款金额"));
user.setProperty("money", (Integer.parseInt(user.getProperty("money"))+money)+"");
writer();

}
//取款
public static void withdrawal(){
while(true){
int money=Integer.parseInt(JOptionPane.showInputDialog(null, "请输入取款金额"));
if (money>Integer.parseInt(user.getProperty("money"))){
JOptionPane.showMessageDialog(null, "账号以超支");
}
else{
user.setProperty("money", (Integer.parseInt(user.getProperty("money"))-money)+"");
writer();
break;
}
}
}
//显示
public static void show(){
JOptionPane.showMessageDialog(null, "余额 为:"+user.getProperty("money"));
}
//改密
public static void amend(){
String oldPwd=JOptionPane.showInputDialog(null, "请输入旧密码");
if(oldPwd.equals(user.getProperty("pwd"))==false){
JOptionPane.showMessageDialog(null, "旧密码不正确");
return;
}
String newPwd=JOptionPane.showInputDialog(null, "请输入新密码");
String newPwdAgain=JOptionPane.showInputDialog(null, "请再次输入新密码");
if(newPwd.equals(newPwdAgain)==false){
JOptionPane.showMessageDialog(null, "二次密码不相同");
return;
}
JOptionPane.showMessageDialog(null, "修改密码成功");
user.setProperty("pwd", newPwd);
writer();
}
}

posted on 2018-04-01 20:54  好浪号  阅读(259)  评论(0编辑  收藏  举报

导航