ATM

package MyDtudent;

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

import javax.swing.JOptionPane;

public class Atm {
  public static void main(String[]aaa){
   JOptionPane.showMessageDialog(null, "欢迎光临");
   boolean s = login();
   if(s==false){
    JOptionPane.showMessageDialog(null, "非法用户");
   }
   while(s==true){
  String item = JOptionPane.showInputDialog
    (null,"1、存款\n2、取款\n3、查询\n4、改密\n5、退出");
  switch(item){
  case "1":
   deposit();
   break;
  case "2":
   fetch();
   break;
  case "3":
   query();
   break;
  case "4":
   alter();
   break;
  case "5":
   System.exit(0);
  }
   }
  }
//  登陆
  public static boolean login(){
   for (int i = 0; i < 3; i++) {
    Properties pro = new Properties();
    try{pro.load(new  FileReader("Properties"));
     }catch(Exception e){
      System.out.println("文件名不存在");
     }
   String  a =JOptionPane.showInputDialog(null,"请输入用户名");
   String  b =JOptionPane.showInputDialog(null,"请输入密码");
   if(a.equals(pro.getProperty("userName"))&&b.equals(pro.get("pwd"))){
    return true;
   }
   else{
    JOptionPane.showMessageDialog(null, "用户名或密码错误");
        }
       }
   return false;
  }
//  存款
  public static void deposit(){
   int a =Integer.parseInt(JOptionPane.showInputDialog
     (null,"请输入存款金额"));
   Properties pro = new Properties();
   try{pro.load(new  FileReader("Properties"));
    }catch(Exception e){
     System.out.println("文件名不存在");
    }
   int b = Integer.parseInt(pro.getProperty("money"));
   String s = a+b+"";
   pro.setProperty("money", s);
   try{
    pro.store(new FileWriter("Properties"),null);
   }catch (Exception e){
    System.out.println("文件不存在");
   }
  }
//  取款
  public static void fetch(){
   int a =Integer.parseInt(JOptionPane.showInputDialog
     (null,"请输入取款金额"));
   Properties pro = new Properties();
   try{pro.load(new  FileReader("Properties"));
    }catch(Exception e){
     System.out.println("文件名不存在");
    }
   int b = Integer.parseInt(pro.getProperty("money"));
   if(a>b){
    JOptionPane.showMessageDialog(null, "账户已超支");
   }
   else{
   String s = b-a+"";
   pro.setProperty("money", s);
   try{
    pro.store(new FileWriter("Properties"),null);
   }catch (Exception e){
    System.out.println("文件不存在");
    }
  }
  }
//  查询
  public static void query(){
   Properties pro = new Properties();
   try{pro.load(new  FileReader("Properties"));
    }catch(Exception e){
     System.out.println("文件名不存在");
    }
   String s = pro.getProperty("money");
   JOptionPane.showInputDialog(null,s);
  }
//  改密
  public static void alter(){
  String s = JOptionPane.showInputDialog(null,"请输入旧密码");
  Properties pro = new Properties();
   try{pro.load(new  FileReader("Properties"));
    }catch(Exception e){
     System.out.println("文件名不存在");
    }
  if(s.equals(pro.getProperty("pwd"))==false){
   JOptionPane.showMessageDialog(null, "旧密码不正确");
   return;
  }
  String a =JOptionPane.showInputDialog(null,"请输入新密码");
  String b =JOptionPane.showInputDialog(null,"请再次输入新密码");
  if(a.equals(b)==false){
   JOptionPane.showMessageDialog(null, "两次密码不一致");
      return;
  }
  pro.setProperty("pwd", a);
  try{
    pro.store(new FileWriter("Properties"),null);
   }catch (Exception e){
    System.out.println("文件不存在");
   }
  }
}

posted @ 2018-04-08 01:35  园全  阅读(108)  评论(0编辑  收藏  举报