随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
package lovo;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;
import javax.swing.JOptionPane;
public class BankAtm {
public Properties pro = new Properties();
public BankAtm(){
try{
pro.load(new FileReader("BankTest.txt"));
}catch(Exception e){
System.out.println("没有找到文件");
}
boolean isLogin=login();
if(isLogin==false){
System.exit(0);
}
while(true){
String str = JOptionPane.showInputDialog(null,
"1、存款\n2、取款\n3、查询余额\n4、改密\n5、退出");
int item = Integer.parseInt(str);
switch(item){
case 1:
addMoney();
break;
case 2:
delMoney();
break;
case 3:
findMoney();
break;
case 4:
changePwd();
break;
case 5:
System.exit(0);
}
}
}
/*
* 改密
*/
private void changePwd() {
String str1=JOptionPane.showInputDialog(null,"请输入新密码");
String str2=JOptionPane.showInputDialog(null,"请确认新密码");
if(str1.equals(str2)){
this.pro.setProperty("pwd", str1);
this.save();
}
else{
JOptionPane.showMessageDialog(null, "两次输入的密码不一致");
}
}
/*
* 保存数据
*/
private void save() {
try{
this.pro.store(new FileWriter("BankTest.txt"), null);
}catch(Exception e){
System.out.println("文件不存在");
}
}
/*
* 查询余额
*/
private void findMoney() {
JOptionPane.showMessageDialog(null, "余额为:"+this.pro.getProperty("money"));
}
/*
* 取款
*/
private void delMoney() {
String str=JOptionPane.showInputDialog(null,"请输入取款金额");
int a=Integer.parseInt(str);
int y=Integer.parseInt(this.pro.getProperty("money"));
if(a>y){
JOptionPane.showMessageDialog(null, "卡上余额不足");
return;
}
y=y-a;
this.pro.setProperty("money", y+"");
this.save();
}
/*
* 存款
*/
private void addMoney() {
String str=JOptionPane.showInputDialog(null,"请输入存款金额");
int a=Integer.parseInt(str);
int b=Integer.parseInt(this.pro.getProperty("money"));
a=a+b;
this.pro.setProperty("money", a+"");
this.save();
}
/*
* 登陆
*/
public boolean login() {
for(int i=0;i<3;i++){
String str1=JOptionPane.showInputDialog(null,"请输入用户名");
String str2=JOptionPane.showInputDialog(null,"请输入密码");
if(str1.equals(this.pro.getProperty("userName"))
&&str2.equals(this.pro.getProperty("pwd")))
{
return true;
}else{
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
}return false;
}
public static void main(String[] args) {
BankAtm a = new BankAtm();
}
}