码迷,mamicode.com
首页 > 其他好文 > 详细

ATM取款基础

时间:2018-04-01 14:59:54      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:账号   set   exit   put   close   login   show   fun   vax   

atm.txt

money=3000
userName=java
pwd=123

import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;

import javax.swing.JOptionPane;

public class ATMus {

public static void main(String[] sad) {
JOptionPane.showMessageDialog(null, "欢迎光临");

boolean login = lander();

while (true) {
if (login == false) {
JOptionPane.showMessageDialog(null, "非法用户");
System.exit(0);
}
String xmb = JOptionPane.showInputDialog(null, "1、存款 \n2、取款 \n3、查询 \n4、改密 \n 5、退出");
switch (xmb) {
case "1":
fund();
break;
case "2":
wtwa();
break;
case "3":
refer();
break;
case "4":
close();
break;
case "5":
System.exit(0);

}
}
}

// 登陆
public static boolean lander() {
Properties pro = new Properties();
try {
pro.load(new FileReader("atm.txt"));
} catch (Exception e) {
System.out.println("文件不存在");
}
for (int i = 0; i < 3; i++) {
String name = JOptionPane.showInputDialog(null, "请输入用户名");
String prpwd = JOptionPane.showInputDialog(null, "请输入密码");
if (pro.getProperty("userName").equals(name) && pro.getProperty("pwd").equals(prpwd)) {
return true;
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
}
return false;
}

// 存款
public static void fund() {
Properties pro = new Properties();
try {
pro.load(new FileReader("atm.txt"));
} catch (Exception e) {
System.out.println("文件不存在");
}
int sum = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入存款金额"));
pro.setProperty("money", sum + Integer.parseInt(pro.getProperty("money")) + "");
try {
pro.store(new FileWriter("atm.txt"), null);
} catch (Exception e) {
System.out.println("文件不存在");
}
}

// 取款
public static void wtwa() {
Properties pro = new Properties();
try {
pro.load(new FileReader("atm.txt"));
} catch (Exception e) {
System.out.println("文件不存在");
}
int sum = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入取款金额"));
if (sum > Integer.parseInt(pro.getProperty("money"))) {
JOptionPane.showMessageDialog(null, "账号已超支");
} else {
pro.setProperty("money", Integer.parseInt(pro.getProperty("money")) - sum + "");
try {
pro.store(new FileWriter("atm.txt"), null);
} catch (Exception e) {
System.out.println("文件不存在");
}
}

}

// 查询
public static void refer() {
Properties pro = new Properties();
try {
pro.load(new FileReader("atm.txt"));
} catch (Exception e) {
System.out.println("文件不存在");
}
JOptionPane.showMessageDialog(null, "账号余额为:" + pro.getProperty("money"));
}

// 改密
public static void close() {
Properties pro = new Properties();
try {
pro.load(new FileReader("atm.txt"));
} catch (Exception e) {
System.out.println("文件不存在");
}
String oldpwd = JOptionPane.showInputDialog(null, "请输入旧密码");
if (pro.getProperty("pwd").equals(oldpwd) == false) {
JOptionPane.showMessageDialog(null, "旧密码不正确");
return;
}
String newpwd = JOptionPane.showInputDialog(null, "请输入新密码");
String veypwd = JOptionPane.showInputDialog(null, "请确认新密码");
if (newpwd.equals(veypwd) == false) {
JOptionPane.showMessageDialog(null, "两次密码不一致");
return;
}
pro.setProperty("pwd", newpwd);
try {
pro.store(new FileWriter("atm.txt"), null);
} catch (Exception e) {
System.out.println("文件不存在");
}
}
}

ATM取款基础

标签:账号   set   exit   put   close   login   show   fun   vax   

原文地址:https://www.cnblogs.com/cannnian/p/8686556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!