Java 模拟ATM(修正)
ATM机的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,操作日期(Date),操作类型,账户密码(六位的数字,可以用0开头),当前的余额(可以为0)。 模拟ATM的功能设计,用户插卡后显示选择语言界面,输入密码界面,用户输入正确密码(用户输入错误密码,则提示该卡已被锁定,无法操作),则弹出选择界面:存款、取款、转账汇款、修改密码、查询余额。 选择“取款”,则显示100元、500元、1000元、1500元、2000元、5000元、其他金额、退卡、返回操作供用户选择; 选择“存款”,则提示用户输入存款金额,修改余额; 选择“转账”,则提示用户输入转账行号、转账金额,并提示转账成功。
要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。 自定义如下异常类,并在程序中验证: 取款时输入非整数时,抛出 “数字格式异常”,提示信息为“请输入正确的数字”; 如果输入数字非100的整倍数时,抛出“数字录入异常”,提示信息为“录入错误,请输出100的整数倍”; 三次密码输入错误,抛出“锁卡异常”,提示信息自定义。
import java.util.*;
public class ShowATM {
@SuppressWarnings({ "resource", "unused" })
public static void main(String[] args) throws NumberErrorException {
Scanner in=new Scanner(System.in);
PATM atm=new ATM();
NumberErrorException nee=new NumberErrorException("输入非法!");
NotHundredTimesException nhte=new NotHundredTimesException("不是一百的整数倍!");
PasswordErrorException pee=new PasswordErrorException("输入错误的密码超过三次!");
int passwordErrorTimes=0;
int choose=-1,number=0,x=0;
String num="";
String pw="";
next:while(true){
System.out.println("是否进入账户(0否1是):");
int kk=-1;
String c=in.next();
if(whetherNum(c))
kk=Integer.parseInt(c);
if(kk==0) break;
else if(kk!=1){
System.out.println("输入错误!");
continue;
}
System.out.println("输入账户密码:");
pw=in.next();
if(atm.ifPass(pw)){
while(true){
showFace();
String cc=in.next();
if(whetherNum(cc))
choose=Integer.parseInt(cc);
switch(choose){
case 1:
System.out.println("输入存款金额:");
while(true){
try{
num=in.next();
if(!whetherNum(num))
throw nee;
else if((x=Integer.parseInt(num)%100)!=0)
throw nhte;
else
break;
}
catch(NumberErrorException e){
System.out.println("输入正确的数字!");
}
catch(NotHundredTimesException e){
System.out.println("录入错误,请输入合法的整数(100的整数倍)!");
}
}
number=Integer.parseInt(num);
atm.save(number);
System.out.println("存款成功!");
System.out.println("当前余额:"+atm.getRest()+"元");
break;
case 2:
System.out.println("请选择:");
int a[]={100,500,1000,1500,2000,5000};
for(int i=0;i<a.length;i++)
System.out.println((i+1)+"."+a[i]+"元");
System.out.println("7.其他");
String bb="";
int ch=-1;
bb=in.next();
if(whetherNum(bb)){
ch=Integer.parseInt(bb);
}
if(ch>=1&&ch<=6){
if(atm.withdraw(a[ch-1]))
System.out.println("取款成功!");
else
System.out.println("余额不足!");
}
else if(ch==7){
System.out.println("请输入取款金额:");
while(true){
try{
num=in.next();
if(!whetherNum(num))
throw nee;
else if((x=Integer.parseInt(num)%100)!=0)
throw nhte;
else
break;
}
catch(NumberErrorException e){
System.out.println("输入正确的数字!");
}
catch(NotHundredTimesException e){
System.out.println("录入错误,请输入合法的整数(100的整数倍)!");
}
}
number=Integer.parseInt(num);
if(atm.withdraw(number))
System.out.println("取款成功!");
else
System.out.println("余额不足!");
}
else
System.out.println("输入有误!");
System.out.println("当前余额:"+atm.getRest()+"元");
break;
case 3:
System.out.println("账户号:");
String s=in.next();
System.out.println("转账金额:");
while(true){
try{
num=in.next();
if(!whetherNum(num))
throw nee;
else if((x=Integer.parseInt(num)%100)!=0)
throw nhte;
else
break;
}
catch(NumberErrorException e){
System.out.println("输入正确的数字!");
}
catch(NotHundredTimesException e){
System.out.println("录入错误,请输入合法的整数(100的整数倍)!");
}
}
number=Integer.parseInt(num);
if(atm.transfer(s, number))
System.out.println("转账成功!");
else
System.out.println("转账失败!");
System.out.println("当前余额:"+atm.getRest()+"元");
break;
case 4:
System.out.println("输入新的六位数密码:");
String p=in.next();
if(p.length()==6&&whetherNum(p))
atm.setPassword(p);
else{
System.out.println("不是六位或者不是数字!");
}
break;
case 5:
System.out.println("当前余额:"+atm.getRest()+"元");
break;
default:
continue next;
}
}
}
else{
System.out.println("密码错误!");
try{
passwordErrorTimes++;
if(passwordErrorTimes>3)
throw pee;
}
catch(PasswordErrorException e){
System.out.println(e.getMessage()+"账户暂时冻结!");
return ;
}
} }
}
//显示菜单方法
public static void showFace(){
System.out.println("********************");
System.out.println(" 1.存款:");
System.out.println(" 2.取款:");
System.out.println(" 3.转账汇款:");
System.out.println(" 4.修改密码:");
System.out.println(" 5.查询余额:");
System.out.println(" 6.退卡:");
System.out.println("********************");
System.out.println("请选择:");
}
//判断字符串是否为整数(0-999999)
public static boolean whetherNum(String s){
boolean flag=true;
if(s.length()>=7)
return false;
char arr[]=s.toCharArray();
for(int i=0;i<s.length();i++){
if(arr[i]<'0'||arr[i]>'9')
flag=false;
}
return flag;
}
} class PersonalAccount{
private String passWord="123456";//密码
@SuppressWarnings("unused")
private String number;//银行卡号
private int money=0;
public int getMoney(){return money;}//余额
public void setPw(String s){passWord=s;}//设置密码
public void addMoney(int x){money+=x;}//加钱
public void minusMoney(int x){money-=x;}//减钱
public boolean whetherPwTrue(String s){//密码是否正确
if(s.equals(passWord))
return true;
else return false;
}
}
abstract class PATM{
public abstract boolean withdraw(int x);//取款
public abstract void save(int x);//存款
public abstract boolean transfer(String s,int x);//转账
public abstract boolean ifPass(String s);//判断输入的密码是否正确
public abstract int getRest();//查询余额
public abstract void setPassword(String s);//设置密码
}
class ATM extends PATM{
private String numbers[]={"6227000000000000071","6227000000000000072",
"6227000000000000073","6227000000000000074"};//数据库中已有的账户卡号
private PersonalAccount account=new PersonalAccount();
public boolean withdraw(int x) {
if(x>account.getMoney())
return false;
else{
account.minusMoney(x);
return true;
}
}
public void save(int x) {
account.addMoney(x);
}
public boolean transfer(String s, int x) {
//转账
//先判断转到账户号是否存在
//再判断余额是否足够
boolean flag=false;
for(int i=0;i<numbers.length;i++)
if(s.equals(numbers[i])) flag=true;
if(x>account.getMoney()) flag=false;
if(x<=account.getMoney()&&flag) account.minusMoney(x);;
return flag;
}
public boolean ifPass(String s) {
return account.whetherPwTrue(s);
}
public int getRest() {
return account.getMoney();
}
public void setPassword(String s) {
account.setPw(s);
}
}
@SuppressWarnings("serial")
class NumberErrorException extends Exception{
public NumberErrorException(String msg){
super(msg);
}
}
@SuppressWarnings("serial")
class PasswordErrorException extends Exception{
public PasswordErrorException(String msg){
super(msg);
}
}
@SuppressWarnings("serial")
class NotHundredTimesException extends Exception{
public NotHundredTimesException(String msg){
super(msg);
}
}





Java 模拟ATM(修正)的更多相关文章
- 模拟ATM机银行系统
淄博汉企Java基础考核项目 模拟银行自助终端系统 一. 本系统模拟银行用户使用ATM机开户.查询.存款.取款功能,要求使用java语言编程实现. 说明: 1. 对于数据输入异常,可使用java异常处 ...
- java测试ATM自助操作系统
开学第一周系主任安排了一项测试,测试要求:模拟ATM自助取款机用文件进行存储账户信息,密码等,并进行存款取款,转账,查询记录等操作,而且要进行文件的读取与录入. 这是一个ATM自助取款的操作系统,进行 ...
- java模拟post请求发送json
java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main ...
- 语言模拟ATM自动取款机系统
C语言实验报告 题目名称:C语言模拟ATM自动取款机系统 C语言模拟实现ATM自动取款机功能:输入密码,余额查询,取款,存款,转账,修改密码,退出功能: 代码实现的功能: 账号及密码输入: ...
- java 模拟qq源码
java 模拟qq源码: http://files.cnblogs.com/files/hujunzheng/QQ--hjzgg.zip
- 从零开始学Python04作业源码:模拟ATM电子银行(仅供参考)
bin目录:程序启动入口 ATM_start.py: #!/usr/bin/python # -*- coding: utf-8 -*- # 模拟ATM电子银行+登录账户权限控制+管理员管理模块 # ...
- java模拟开锁
java模拟开锁 service qq:928900200 Introduction to Computer Science II: CSCI142Fall 2014Lab #1Instructor: ...
- Jsoup实现java模拟登陆
Jsoup实现java模拟登陆 2013-10-29 14:52:05| 分类: web开发|举报|字号 订阅 下载LOFTER我的照片书 | 1:如何获取cookies. 1.1 ...
- [Java] 模拟HTTP的Get和Post请求
在之前,写了篇Java模拟HTTP的Get和Post请求的文章,这篇文章起源与和一个朋友砍飞信诈骗网站的问题,于是动用了Apache的comments-net包,也实现了get和post的http请求 ...
随机推荐
- go语言之并发编程同步一
前面介绍了采用go语法的并行操作以及channel.既然是并行操作,那么就涉及到数据原子性以及同步的问题.所以在Go里面也需要采用同步的机制. 互斥锁: 由标准库代码包sync中的Mutex结构体类型 ...
- JavaScript setInterval()執行clearInterval() 再恢復setInterval()
clearInterval() 方法可取消由 setInterval() 设置的 timeout. clearInterval() 方法的参数必须是由 setInterval() 返回的 ID 值. ...
- ZFIR_001 ole下载
*&---------------------------------------------------------------------** Report ZFIR_001* Appli ...
- 剑指offer 面试15题
面试15题: 题目:二进制中1的个数 题:输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 解题思路一: 最佳方法:把一个整数减去1,再和原整数做“与运算”,会把该整数最右边的1变成0 ...
- django admin基础
通过onetoonefiled扩展得到的不会在添加user是自动添加原因是onetoonefiled只是一个model 可以they are just Django models that happe ...
- LeetCode:至少是其他数字两倍的最大数【747】
LeetCode:至少是其他数字两倍的最大数[747] 题目描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素 ...
- 安装mysql到ubuntu
Ubuntu 16.04上安装MySQL步骤: 如果你使用的是Ubuntu 16.04以前的版本,可以看这里:Ubuntu 14.04/15.10升级到Ubuntu 16.04 LTS.一. 安装My ...
- OpenGL学习进程(8)第六课:点、边和图形(三)绘制图形
本节是OpenGL学习的第六个课时,下面介绍OpenGL图形的相关知识: (1)多边形的概念: 多边形是由多条线段首尾相连而形成的闭合区域.OpenGL规定,一个多边形必须是一个“凸多边形”. ...
- PMON使用手册
转:http://www.docin.com/p-1949877603.html
- STP生成树协议原理与算法解析
转:https://wenku.baidu.com/view/2e52b91d866fb84ae45c8d34.html