zoukankan      html  css  js  c++  java
  • JAVA 小程序之ATM

    一个JAVA的小程序,主要要求有模块化编程的思想,能够把ATM中各个功能独立成为一个一个的方法。

    ATM主要功能有:

    1. 查询余额;
    2. 取款;
    3. 存款;
    4. 修改密码;
    5. 退出。

    以上功能均由独立的方法给出,具体实现过程如下:

      1 import java.util.Scanner;
      2 
      3 /**
      4  * 
      5  * ATM机
      6  * */
      7 public class HomeWork0901 {
      8 
      9     
     10     static float money=100000f;
     11     
     12     public static void main(String[] args) {
     13         // TODO Auto-generated method stub
     14         String account="mmy123";
     15         String password="mmy12311";
     16         welcome();
     17         boolean check=LogCheck(account,password);
     18         if(check){
     19             do{
     20                 System.out.println("请选择你希望执行的操作:
    1:查询余额  2:存款  3:取款  4:修改密码  5:退出");    
     21                 Scanner sr=new Scanner(System.in);
     22                 int option=sr.nextInt();
     23                 switch(option){
     24                     case 1:check(money);break;
     25                     case 2:money+=deposit();break;
     26                     case 3:money-=MoneyOut();break;
     27                     case 4:password=changePassword(password);break;
     28                     case 5:System.out.println("成功退出!欢迎再次光临!");System.exit(0);
     29                 }
     30             }while(true);
     31         }        
     32     }
     33     
     34     
     35     /**
     36      * 欢迎界面
     37      * */
     38     public static void welcome(){
     39         System.out.println("--------------------------------------------------");
     40         System.out.println("--------------------欢迎光临---------------------");
     41         System.out.println("--------------------------------------------------");
     42         System.out.println("--------------------我的银行---------------------");
     43         System.out.println("--------------------------------------------------");
     44         System.out.println("------------------欢迎来存钱--------------------");
     45         System.out.println("--------------------------------------------------");
     46     }
     47     
     48     
     49     /**
     50      * 登录账号密码输入及检验
     51      * */
     52     public static boolean LogCheck(String account,String password){
     53         for(int i=3;i>0;i--){
     54             System.out.println("输入你的账号:");
     55             Scanner sr=new Scanner(System.in);
     56             String input_acc=sr.next();
     57             System.out.println("输入你的密码:");
     58             String input_password=sr.next();
     59 //            if(account!=input_acc||password!=input_password){   //匹配字符串不能用==!!!
     60             if(!account.equals(input_acc)||!password.equals(input_password)){
     61                 if(i==1){
     62                     System.out.println("机会用完,吞卡!");
     63                     return false;
     64                 }
     65                 System.out.println("账号密码错误!你还有"+(i-1)+"次输入机会!");
     66                 continue;
     67             }else{
     68                 System.out.println("登陆成功,欢迎!");
     69                 break;
     70             }
     71         }
     72         return true;
     73     }
     74     
     75     
     76     /**
     77      * 查询
     78      * */
     79     public static void check(float money){
     80         System.out.println("您的账户共有"+money+"元!");
     81     }
     82     
     83     
     84     /**
     85      * 存款
     86      * */
     87     public static int deposit(){
     88         System.out.println("请输入所存金额:");
     89         Scanner sr=new Scanner(System.in);
     90         int MoneyIn=sr.nextInt();
     91         if(MoneyIn%100!=0){
     92             System.out.println("只能存100的整数!");
     93             return 0;
     94         }
     95         if(MoneyIn>5000){
     96             System.out.println("单笔存款上限为5000!");
     97             return 0;
     98         }
     99         if(MoneyIn<0){
    100             System.out.println("输正数!输正数!输正数!");
    101             return 0;
    102         }
    103         System.out.println("存款成功!");
    104         return MoneyIn;
    105     }
    106     
    107     /**
    108      * 取款
    109      * */
    110     public static int MoneyOut(){
    111         System.out.println("请输入需要取款的金额:");
    112         Scanner sr=new Scanner(System.in);
    113         int MoneyOut=sr.nextInt();
    114         if(MoneyOut%100!=0){
    115             System.out.println("请输入整数!");
    116             return 0;
    117         }
    118         if(MoneyOut>money){
    119             System.out.println("所取金额超过该账户余额!");
    120             return 0;        
    121         }
    122         if(MoneyOut<0){
    123             System.out.println("输正数!输正数!输正数!");
    124             return 0;
    125         }
    126         System.out.println("取款成功!");
    127         return MoneyOut;
    128     }
    129     
    130     /**
    131      * 修改密码
    132      * */
    133     public static String changePassword(String pass){
    134         System.out.println("请输入旧密码:");
    135         Scanner sr=new Scanner(System.in);
    136         String password=sr.next();
    137         if(password.equals(pass)){
    138                 System.out.println("请输入新密码:");
    139                 String newpass1=sr.next();
    140                 System.out.println("请再次输入新密码:");
    141                 String newpass2=sr.next();
    142                 if(newpass1.equals(newpass2)){
    143                     System.out.println("密码修改成功!");
    144                     return newpass1;
    145                 }else{
    146                     System.out.println("两次密码不一致!");
    147                     return pass;
    148                 }
    149         }else{
    150             System.out.println("旧密码输入错误!");
    151             return pass;
    152         }
    153     }
    154 }


    JAVA真有意思啊!!!

  • 相关阅读:
    Conda 中安装 Keras
    Clean Docker :
    Conservation Vs Non-conservation Forms of conservation Equations
    C语言之预处理命令
    const指针用法总结
    C语言中的小启发(陆续更新。。。)
    左值与右值
    计算的次序
    C语言中的符号重载
    C语言之switch语句详解
  • 原文地址:https://www.cnblogs.com/orezero/p/5843900.html
Copyright © 2011-2022 走看看