前言介绍:
最近很多同学找我帮忙做一些课程设计或Web前端大作业、其中控制台项目应该是初学者必须经历的一个过程、作为java初学者这个控制台版本的模拟ATM银行管理系统十分合适、用到的技术也是最简单的JavaList集合、变量的声明、对象的创建、一些基本的while语句、switch语句、循环遍历等基础语法。一个最基础版本的控制台模拟ATM银行管理系统也就是CRUD功能、 直接上完整代码****
创建抽象类Card类
abstract class Card { int cardId; public Card() { super(); } public Card(int cardId) { super(); this.cardId = cardId; } public abstract String getCardId(); public abstract void setCardId(int cardId); }
创建普通银行类继承Card
public class CommonCard extends Card{ public CommonCard() { super(); } public CommonCard(int cardId) { super(); this.cardId = cardId; } @Override public String getCardId() { return "普通银行卡卡号为:" cardId; } @Override public void setCardId(int cardId) { this.cardId = cardId; } }
创建普通信用卡继承Card
package com.railway.modules.base.test;public class CreditCard extends Card{ public CreditCard() { super(); } public CreditCard(int cardId) { super(cardId); } @Override public String getCardId() { return "信用卡卡号为:" cardId; } @Override public void setCardId(int cardId) { this.cardId = cardId; }}
创建普通医保卡继承Card
package com.railway.modules.base.test;public class MedicalInsuranceCard extends Card{ public MedicalInsuranceCard() { super(); } public MedicalInsuranceCard(int cardId) { super(cardId); } @Override public String getCardId() { return "医保卡卡号为:" cardId; } @Override public void setCardId(int cardId) { this.cardId = cardId; } }
ATM主要方法业务类逻辑实现
package com.railway.modules.base.test;import java.util.Scanner;public class ATM { Scanner input = new Scanner(System.in); private User[] user=new User[100]; User atm = new User(); private User usr; private int accountId = 1; private Card card; public void Select() { while(true) { System.out.println("=======ATM管理系统======="); System.out.print("1.开户n2.登录n3.取款n4.存款n5.余额查询n6.修改密码n7.查看用户信息n8.退出登录n9.退出系统n请选择需要选择的项目:"); switch(input.nextInt()) { case 1: setAccount(); //开户 break; case 2: usr=userLogin(); //登录 break; case 3: drawMoney(); //取款 break; case 4: depositMoney(); //存款 break; case 5: showBalance(); //余额查询 break; case 6: revisePassword(); //修改密码 break; case 7: lookAccount(); //查看用户信息 break; case 8: cancel();//退出登录 break; case 9: System.out.println("======================="); System.out.println("欢迎下次登录该系统!"); //退出系统 System.exit(0); break; default: System.out.println("======================="); System.out.println("输入错误!请重新输入!"); break; } } } public void setAccount(){ //开户 if (usr!=null) { System.out.println("======================="); System.out.println("请先退出上一个账户!"); } else{ boolean x = true; while(x) { System.out.println("======================="); System.out.print("1.普通银行卡n2.信用卡n3.医保卡n0.返回n请选择开户类型:"); switch(input.nextInt()) { case 1: card = new CommonCard(); card.setCardId(accountId); x=false; break; case 2: card = new CreditCard(); card.setCardId(accountId); x=false; break; case 3: card = new MedicalInsuranceCard(); card.setCardId(accountId); x=false; break; case 0: Select(); default: System.out.println("======================="); System.out.println("输入错误!请重新输入!"); break; } } System.out.print("请输入姓名:"); String name=input.next(); System.out.print("请输入性别:"); String sex=input.next(); System.out.print("请输入年龄:"); String age=input.next(); System.out.print("请输入身份证号:"); String ID=input.next(); System.out.print("请输入银行卡要设置的密码:"); String password1=input.next(); System.out.print("请再次输入密码:"); String password2=input.next(); if(password1.equals(password2)){ user[accountId]=new User(name,sex,age,password1,ID,0,card); System.out.println("======================="); System.out.println("开户成功!你的卡号为:" accountId); accountId ; } else { System.out.println("======================="); System.out.println("两次密码不同!请重新开始!"); } } } public User userLogin(){ //登录 if (usr!=null) { System.out.println("======================="); System.out.println("请先退出上一个账户!"); return usr; } else{ System.out.print("请输入卡号:"); int aId=input.nextInt(); System.out.print("请输入密码:"); String password=input.next(); for(int i=0;i<user.length;i ){ if (user[i]!=null) { if (aId==user[i].getCard()&&password.equals(user[i].getPassword())) { System.out.println("======================="); System.out.println(user[i].getName() ",欢迎登录!"); return user[i]; } } } System.out.println("======================="); System.out.println("卡号或密码有误"); return null; } } public void drawMoney(){ //取款 if(usr==null){ System.out.println("======================="); System.out.println("请先登录账户!"); } else{ System.out.print("请输入取款金额:"); double blc=input.nextDouble(); while(blc>usr.getBalance()) { System.out.println("======================="); System.out.print("取款金额大于余额,请重新输入取款金额:"); blc=input.nextDouble(); } while(blc<=0){ System.out.println("======================="); System.out.print("取款金额错误,请重新输入取款金额:"); blc=input.nextDouble(); } usr.setBalance(usr.getBalance()-blc); System.out.println("======================="); System.out.println("取款成功!"); System.out.println("账户余额:" usr.getBalance()); } } public void depositMoney(){ //存款 if(usr==null){ System.out.println("======================="); System.out.println("请先登录账户!"); } else{ System.out.print("请输入存款金额:"); double dsm=input.nextDouble(); while(dsm<=0){ System.out.println("======================="); System.out.print("输入金额错误,请重新输入:"); dsm=input.nextDouble(); } usr.setBalance(usr.getBalance() dsm); System.out.println("======================="); System.out.println("存款成功!"); System.out.println("账户余额:" usr.getBalance()); } } public void showBalance(){ //余额查询 if(usr==null){ System.out.println("======================="); System.out.println("请先登录账户!"); } else{ System.out.println("======================="); System.out.println("账户余额:" usr.getBalance()); } } public void revisePassword(){ //修改密码 if(usr==null){ System.out.println("======================="); System.out.println("请先登录账户!"); } else{ System.out.print("请输入原密码:"); String password=input.next(); for(int i=2;i>=0;i--) { if(password.equals(usr.getPassword())) break; if(i==0) { System.out.println("三次密码输入错误!即将退出账户!"); cancel(); Select(); } System.out.println("密码错误!你还有" i "次机会!"); System.out.print("请输入原密码:"); password=input.next(); } System.out.print("验证成功,请输入新密码:"); String password1=input.next(); System.out.print("请再次输入密码:"); String password2=input.next(); while(!password1.equals(password2)) { System.out.println("======================="); System.out.print("两次密码不同,请重新输入新密码:"); password1=input.next(); System.out.print("请再次输入密码:"); password2=input.next(); } usr.setPassword(password1); System.out.println("======================="); System.out.println("修改成功!"); } } public void lookAccount() { //查看用户信息 if(usr==null){ System.out.println("======================="); System.out.println("请先登录账户!"); } else{ System.out.println("======================="); System.out.println("姓名:" usr.getName()); System.out.println("性别:" usr.getSex()); System.out.println(usr.getCardId()); System.out.println("身份证号:" usr.getID()); System.out.println("余额:" usr.getBalance()); } } public void cancel() { if(usr==null){ System.out.println("======================="); System.out.println("请先登录账户!"); } else{ usr=null; System.out.println("======================="); System.out.println("账户退出成功!"); } }}
main方法测试启动类
public class TestATM { public static void main(String[] args) { ATM atm = new ATM(); atm.Select(); }}
控制台打印:
=======ATM管理系统=======1.开户2.登录3.取款4.存款5.余额查询6.修改密码7.查看用户信息8.退出登录9.退出系统请选择需要选择的项目:1=======================1.普通银行卡2.信用卡3.医保卡0.返回请选择开户类型:1请输入姓名:admin请输入性别:男请输入年龄:22请输入身份证号:513922199555555555请输入银行卡要设置的密码:123456请再次输入密码:123456=======================开户成功!你的卡号为:1=======ATM管理系统=======1.开户2.登录3.取款4.存款5.余额查询6.修改密码7.查看用户信息8.退出登录9.退出系统请选择需要选择的项目:2请输入卡号:1请输入密码:123456=======================admin,欢迎登录!=======ATM管理系统=======1.开户2.登录3.取款4.存款5.余额查询6.修改密码7.查看用户信息8.退出登录9.退出系统请选择需要选择的项目:
很基础、 适合java初学者来进行练习以及当做课程作业来使用
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。