import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;
import javax.swing.JOptionPane;
public class Atm {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null,"欢迎来到**银行!");
boolean isFind=login();
if(isFind==false){
JOptionPane.showMessageDialog(null, "非法用户!");
System.exit(0);
}
while(true){
String s=JOptionPane.showInputDialog(null,"1.存款\n2.取款\n3.查询\n4.改密\n5.退出");
switch(s){
case "1":
savaMoney();
break;
case "2":
getMoney();
break;
case "3":
showMoney();
break;
case "4":
revirse();
break;
case "5":
System.exit(0);
break;
}
}
}
/**
* 登陆
* @return
*/
public static boolean login(){
Properties pro=new Properties();
try{
pro.load(new FileReader("Pop.txt"));
}catch(Exception e){
System.out.println("文件没找到!");
}
String name=pro.getProperty("userName");
String pwd=pro.getProperty("password");
for(int i=0;i<3;i++){
String n=JOptionPane.showInputDialog(null,"请输入用户名:");
String p=JOptionPane.showInputDialog(null,"请输入密码:");
if(n.equals(name)&&p.equals(pwd)){
return true;
}else {JOptionPane.showMessageDialog(null, "密码或者用户名错误");}
}
return false;
}
/**
* 存款
*/
public static void savaMoney(){
String m=JOptionPane.showInputDialog(null,"请输入存款金额");
int num=Integer.parseInt(m);
if(num%100!=0||num>10000){
JOptionPane.showMessageDialog(null, "输入不符合规定!");
}else {
int item=liveMoney();
num+=item;
Properties pro=new Properties();
try{
pro.load(new FileReader("Pop.txt"));
}catch(Exception e){
System.out.println("文件没找到!");
}
pro.setProperty("money",""+num);
try{
pro.store(new FileWriter("Pop.txt"),null);
}catch (Exception e){
System.out.println("文件没找到!");
}
}
}
/**
* 取款
*/
public static void getMoney(){
String s=JOptionPane.showInputDialog(null,"请输入要取款的金额");
int n=Integer.parseInt(s);
int item=liveMoney();
if(n%100!=0||n>item||n>5000){
JOptionPane.showMessageDialog(null, "输入有误!");
}else {
item-=n;
Properties pro=new Properties();
try{
pro.load(new FileReader("Pop.txt"));
}catch(Exception e){
System.out.println("文件没找到!");
}
pro.setProperty("money",""+item);
try{
pro.store(new FileWriter("Pop.txt"),null);
}catch (Exception e){
System.out.println("文件没找到!");
}
}
}
/**
* 查询
*/
public static void showMoney(){
int item=liveMoney();
JOptionPane.showMessageDialog(null, "您的余额为:"+item);
}
/**
* 改密
*/
public static void revirse(){
Properties pro=new Properties();
try{
pro.load(new FileReader("Pop.txt"));
}catch(Exception e){
System.out.println("文件没找到!");
}String p=pro.getProperty("password");
String s=JOptionPane.showInputDialog(null,"请输入旧密码:");
if(s.equals(p)){
String s1=JOptionPane.showInputDialog(null,"请输入新密码:");
String item=JOptionPane.showInputDialog(null,"请确认新密码:");
if(s1.equals(item)){
try{
pro.load(new FileReader("Pop.txt"));
}catch(Exception e){
System.out.println("文件没找到!");
}
pro.setProperty("password",s1);
try{
pro.store(new FileWriter("Pop.txt"),null);
}catch (Exception e){
System.out.println("文件没找到!");
}
}else {
JOptionPane.showMessageDialog(null, "两次输入的密码不一致!");
}
}else {
JOptionPane.showMessageDialog(null, "旧密码输入不正确!");
}
}
/**
* 文件余额调用
* @return
*/
public static int liveMoney(){
Properties pro=new Properties();
try{
pro.load(new FileReader("Pop.txt"));
}catch(Exception e){
System.out.println("文件没找到!");
}String money=pro.getProperty("money");
int num=Integer.parseInt(money);
return num;
}
}