Java——demo之仿ATM操作

java.util.Scanner类,这是一个用于扫描输入文本的新的实用程序。其中nextInt()获取String型,而next()获取int、double型。

这是一个仿ATM的小程序。

实现条件 1.登陆界面,2.三次登陆机会,登陆成功进入登陆菜单,3,进入菜单进行选择,实现 1,查询,2,存款,3,取款,4,修改密码,5退出,再位选择退出时可一直重复操作。

import java.util.Scanner;
public class TestATM{
	public static void main(String[] args) {
		String user = "admin";
		String password ="123456";
		double  money = 10000;
		Scanner sc = new Scanner(System.in);
		welcome();
		boolean b = login(user,password);

		/*login successful come to the menu*/
		
		while(b==true){/
			System.out.println("login successful !");
			System.out.println("Now You In The Menu And Select Any Operation.");
			System.out.println("***1.Check the Balance***"+"\n"+ 
								"***2.Withdrawing Money***"+"\n"+ 
								"***3.Deposit Money***"+"\n"+ 
								"***4.Change Password***"+"\n"+ 
								"***5.exit***");
			while(true){
				int opt = sc.nextInt();
				switch (opt){
					case 1:
						checkMoney(money);
						retreate();
						break;
					case 2:
						money = withdrawMoney(money);
						System.out.println("operate successfully,you remaining sum is "+money);
						retreate();
						break;
					case 3:
						money = depositMoney(money);
						System.out.println("operate successfully,you remaining sum is "+money);
						retreate();
						break;
					case 4:
						password = changePassword(password);
						retreate();
						break;
					case 5:
						retreate();
						System.exit(0);
					default:
					break;
				}
			}
		}
	}
	public static  void welcome(){
		System.out.println("***********************************************");
		System.out.println("--------------------------Welcoming------------");
		System.out.println("--------------------------Version 1 ------------");
		System.out.println("-----------------------------------------------");
	}
	/*Three times to verify login*/
	public static boolean login(String us,String pw){
		System.out.println("***Only Thirds Tiems Please Carefully***");
		int k = 2;
		for(int i = 1; i<4;i++){	
			Scanner  sc = new Scanner(System.in);
			System.out.println("please input your username:");
			 String user_name = sc.next();
			 System.out.println("please input your password:");
			 String user_pw = sc.next();
			if (us.equals(user_name)&&pw.equals(user_pw))
				return true;
			else{ 
				if(k!=0)
				{
					System.out.println("***Please Again Input Your Name And Password"+" "+"Only"+" "+k-- +" "+"Times***");
				}else
					System.out.println("You Have No Time,Please Contact Our Staff Now.");
				continue;
			}
		}
		return false;
	}
	/*check money*/
	public static void checkMoney(double money)
	{
		System.out.println("Your Balance is"+" "+money+" RMB ");
	}

	public static double withdrawMoney(double money1){
		Scanner sc =new Scanner(System.in);
		System.out.println("please input you want to withdraw money ");
		double wm  = sc.nextInt();
		money1 = money1 - wm;
		return money1;
	}
	public static double depositMoney(double money2){
		Scanner sc =new Scanner(System.in);
		System.out.println("please input you want to set money ");
		double sm = sc.nextInt();
		money2 = money2 + sm;
		return money2;
	}
	public static String changePassword(String old_password){
		System.out.println("please input your old ps");
		Scanner sc = new Scanner(System.in);
		String pd = sc.next();
		if(pd.equals(old_password)){	
			System.out.println("please input your new pw ");
			String pw1 = sc.next();
			System.out.println("please again input your new pw");
			String pw2 = sc.next();
			if (pw1.equals(pw2)) {
				System.out.println("chage password is successful.");
				return pw1;
			}else{
				System.out.println("The two passwords differ.");
				return pd;
			}
		}else{
			System.out.println("input old password is failed try again.");
			return old_password;
		}
	}
	public static void retreate(){
		System.out.println("Continue  OR Not Operation"+"(y/n)");
		Scanner sc = new Scanner(System.in);
		String cn = sc.next();
		if (cn.equals("y")) {
			return ;
		}else if (cn.equals("n")) {
			System.out.println("Exit  successful!1");
			System.exit(0);
		}else {
			 while (true) {
                System.out.println("input is failed again?(y/n))");
                String temp = sc.next();
                if (temp.equals("y")) {
                    return;
                } else if (temp.equals("n")) {
                    System.out.println("Exit  successful!2");
                    System.exit(0);
                }
            }
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值