基于控制台的ATM
实体类
Student类
public class User {
private int UserId; //用户id
private String name;
private String password;
private double money; //账户余额
private double quotaMoney; //每次转账的限制金额
public User(int userId, String name, String password, double money, double quotaMoney) {
UserId = userId;
this.name = name;
this.password = password;
this.money = money;
this.quotaMoney = quotaMoney;
}
public int getUserId() {
return UserId;
}
public void setUserId(int userId) {
UserId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public double getQuotaMoney() {
return quotaMoney;
}
public void setQuotaMoney(double quotaMoney) {
this.quotaMoney = quotaMoney;
}
@Override
public String toString() {
return "用户Id"+"\t\t"+"用户名"+"\t\t"+"用户密码"+"\t\t"+"余额"+"\t\t\t"+"提现额度"+"\n"+
UserId+"\t\t\t" + name+"\t\t\t" + password+"\t\t\t" + money +"\t\t\t"+ quotaMoney ;
}
}
逻辑类
public class ATMService {
int IdCard = 1;
User[] users; //定义用户数组保存用户所有信息
int num = 1; //保存数组当前存储的数量
public ATMService(int size) {
users = new User[size];
users[0] = new User(1, "张三", "123", 4000, 2000);
}
//开户
public Boolean register(User user) {
if (num == users.length) {
System.out.println("存储已满请停止添加");
return false;
}
users[num] = user;
num++;
user.setUserId(IdCard += 1);
System.out.println("账户创建成功" + " " + "用户ID是" + IdCard);
return true;
}
//登录
public User login(String name, String psw) {
for (int i = 0; i < num; i++) {
if (name.equals(users[i].getName()) && psw.equals(users[i].getPassword())) {
return users[i];
}
if (name.equals(users[i].getName()) && !psw.equals(users[i].getPassword())) {
System.out.println("密码错误");
return null;
}
}
System.out.println("用户不存在");
return null;
}
//登录1
public User login(int idCard, String password) {
for (int i = 0; i < num; i++) {
if (idCard == users[i].getUserId() && password.equals(users[i].getPassword())) {
return users[i];
}
if (idCard == users[i].getUserId() && !password.equals(users[i].getPassword())) {
System.out.println("密码错误");
return null;
}
}
System.out.println("用户不存在");
return null;
}
主菜单视图类
public class ATMView {
ATMService atmService = new ATMService(10);
boolean loop = true;
boolean loop1 = true;
public void MainEu() {
do {
System.out.println("==============ATM系统==============");
System.out.println("\t\t\t1、登录账户");
System.out.println("\t\t\t2、注册开户");
System.out.println("\t\t\t3、结束服务");
System.out.println("请输入1、2、3命令来执行对应的操作");
int key = Utility.readInt();
switch (key) {
case 1:
login();
Main();
break;
case 2:
register();
break;
case 3:
outAll();
break;
}
} while (loop);
System.out.println("您退出了整个系统");
}
//用户注册
public void register() {
System.out.println("==============开户==============");
System.out.println("请输入用户名");
String name = Utility.readString(4);
boolean flag = true;
String psw;
do {
System.out.println("请确认密码");
psw = Utility.readString(4);
System.out.println("请确认密码");
String psw1 = Utility.readString(4);
if (!psw.equals(psw1)) {
System.out.println("两次密码输入不一致,请重新输入");
} else {
flag = false;
}
} while (flag);
System.out.println("请设置提现额度");
int QMoney = Utility.readInt();
User user = new User(0, name, psw, 0, QMoney);
atmService.register(user);
}
//用户登录
int user1;
String name;
String psw;
public void login() {
boolean flag = true;
do {
System.out.println("==============用户登录==============");
System.out.println("请输入用户名");
name = Utility.readString(4);
System.out.println("请输入密码");
psw = Utility.readString(4);
if (atmService.login(name, psw) != null) {
System.out.println("==============登录成功=============");
// User user = atmService.login(name, psw);
user1 = atmService.login(name, psw).getUserId();
flag = false;
} else {
System.out.println("登录失败,请重新登录");
}
} while (flag);
}
//用户登录1
int findId;
public void login1() {
boolean flag = true;
do {
System.out.println("请输入卡号");
int idCard = Utility.readInt();
System.out.println("请输入密码");
String password = Utility.readString(6);
User user = atmService.login(idCard, password);
if (user != null) {
System.out.println("登录成功");
findId = atmService.login(idCard, password).getUserId();
flag = false;
} else {
System.out.println("登录失败");
}
} while (flag);
}
//登录后的操作
//登录后查询用户信息
public void find() {
atmService.find(user1);
}
//存款
public void setMoney() {
User user = atmService.add(user1);
double money1 = atmService.add(user1).getMoney();
System.out.println("请输入你要存款的金额");
double money = Utility.readInt();
money = money1 + money;
user.setMoney(money);
System.out.println("=============存款成功=============");
}
//取款
public void getMoney() {
User user = atmService.add(user1);
boolean loop = true;
do {
System.out.println("请输入你要取款的金额");
double money = Utility.readInt();
double money1 = atmService.add(user1).getMoney();
if (money > money1) {
System.out.println("余额不足,请重新输入");
} else {
money1 = money1 - money;
user.setMoney(money1);
loop = false;
}
} while (loop);
System.out.println("=============取款成功=============");
}
//转账
public void outMoney() {
System.out.println("============转账页面=============");
System.out.println("请输入你要进行转账的id");
int id = Utility.readInt();
if (atmService.outM(id) != null) {
//获取到要转账的用户信息
User newUser = atmService.outM(id);
//得到自己的余额
User user = atmService.add(user1);
double money1 = atmService.add(user1).getMoney();
boolean flag = true;
do {
System.out.println("请输入你要转账的金额");
double money = Utility.readInt();
if (money <= user.getQuotaMoney()) {
if (money > money1) {
System.out.println("余额不足");
} else {
//得到要转账用户的余额
double money2 = newUser.getMoney();
//将转过来的钱加进去
money2 = money + money2;
newUser.setMoney(money2);
//在自己账户上实现减的操作
money1 = money1 - money;
user.setMoney(money1);
System.out.println("转账成功");
flag = false;
}
} else {
System.out.println("超过提现额度");
}
} while (flag);
} else {
System.out.println("没有找到该用户");
}
}
//修改密码
public void update() {
User user = atmService.add(user1);
boolean flag = true;
do {
System.out.println("请输入你的新密码" + "(" + user.getPassword() + ")");
String psw = Utility.readString(4);
System.out.println("请确认你的新密码");
String psw1 = Utility.readString(4);
if (psw.equals(psw1)) {
user.setPassword(psw);
flag = false;
} else {
System.out.println("两次密码输入不一致,请重新输入");
}
} while (flag);
}
//退出
public void exit() {
System.out.println("退出登录页面(请输入1-9的数字,-1退出)");
int a = Utility.readInt();
if (a == -1) {
System.out.println("您放弃了该操作");
return;
}
char choice = Utility.readConfirmSelection();
if (choice == 'Y') {
loop1 = false;
} else {
System.out.println("放弃该操作");
}
}
//注销账户
public void deleteUser() {
System.out.println("注销页面(-1退出),请输入您的选择");
int choice = Utility.readInt();
if (choice == -1) {
return;
}
System.out.println("真的确定要注销吗?(Y/N");
char choice1 = Utility.readConfirmSelection();
if (choice1 == 'Y') {
if (atmService.delete(user1)) {
System.out.println("注销成功");
loop1 = false;
}
} else {
System.out.println("===========放弃删除商品===========");
}
}
//退出整个系统
public void outAll() {
System.out.println("退出登录页面(请输入1-9的数字,-1退出)");
int a = Utility.readInt();
if (a == -1) {
System.out.println("您放弃了该操作");
return;
}
char choice = Utility.readConfirmSelection();
if (choice == 'Y') {
loop = false;
} else {
System.out.println("放弃该操作");
}
}
public void Main() {
do {
System.out.println("\t\t\t1、查询账户");
System.out.println("\t\t\t2、存款");
System.out.println("\t\t\t3、取款");
System.out.println("\t\t\t4、转账");
System.out.println("\t\t\t5、修改密码");
System.out.println("\t\t\t6、退出");
System.out.println("\t\t\t7、注销");
System.out.println("请选择");
int choice = Utility.readInt();
switch (choice) {
case 1:
find();
Main();
break;
case 2:
setMoney();
break;
case 3:
getMoney();
break;
case 4:
outMoney();
break;
case 5:
update();
break;
case 6:
exit();
break;
case 7:
deleteUser();
break;
default:
System.out.println("输入有误请重新输入");
break;
}
} while (loop1);
System.out.println("退出了233登录");
}
}
键盘输入类
package ATM.util;
/**
* @author hyhWTX
* @version 1.0
* @date 2022年05月23日 13:29
*/
import java.util.Scanner;
/**
工具类的作用:
处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。
*/
public class Utility {
//静态属性
private static Scanner scanner = new Scanner(System.in);
/**
* 功能:读取键盘输入的一个菜单选项,值:1——5的范围
* @return 1——5
*/
public static char readMenuSelection(){
char c ;
for (; ;){
String str = readKeyBoard(1,false);//包含一个字符的字符串
c = str.charAt(0);
if (c!='1'&&c!='2'&&c!='3'&c!='4'&c!='5'){
System.out.println("选择错误,请重新输入:");
}
else break;
}
return c;
}
/**
* 功能:读取键盘输入的一个字符
* @return 一个字符
*/
public static char readChar(){
String str = readKeyBoard(1,false);//读取字符
return str.charAt(0);
}
/**
* 功能:读取键盘输入的一个字符,如果直接按回车,则返回指定的默认值;否则返回输入的那个字符
* @param defaultValue 指定的默认值
* @return 默认值或输入的字符
*/
public static char readChar(char defaultValue){
String str = readKeyBoard(1,true);//读取结果不是字符就是一个空的字符串
return (str.length()==0)?defaultValue:str.charAt(0);
}
/**
* 功能:读取键盘输入的整型,长度小于2位
* @return 整数
*/
public static int readInt(){
int n;
for(; ;){
String str = readKeyBoard(10,true);//读取一个整数,长度<=10位
try {
n=Integer.parseInt(str);//进行转型,字符转换成整数
break;
}catch (NumberFormatException e){
System.out.println("数字输入的有错误,请重新输入!");
}
}return n;
}
/**
* 功能:读取键盘输入的 整数或默认值,如果直接回车,则返回默认值,否则返回输入的整数
*
* @param defaultValue 指定的默认值
* @return 整数或默认值
*
*/
public static int readInt(int defaultValue){
int n;
for(; ;){
String str = readKeyBoard(10,true);
if (str.equals("")){
return defaultValue;
}
//异常处理
try {
n = Integer.parseInt(str);
break;
}catch (NumberFormatException e){
System.out.println("数字输入的有错误,请重新输入!");
}
}
return n;
}
/**
* 功能:读取键盘输入的指定长度的字符串
* @param limit 限制的长度
* @return 指定长度的字符串
*/
public static String readString(int limit){
return readKeyBoard(limit,false);
}
/**
* 功能:读取键盘输入的指定长度的字符串或默认值,如果直接回车,返回默认值,否则返回字符串
* @param limit 限制的长度
* @param defaultValue 指定的默认值
* @return 指定长度的字符串
*/
public static String readString(int limit,String defaultValue){
String str = readKeyBoard(limit, true);
return str.equals("")? defaultValue : str;
}
/**
* 功能:读取键盘输入的确认选项,Y或N
* 将小的功能,封装到一个方法中.
* @return Y或N
*/
public static char readConfirmSelection(){
System.out.println("请输入你的选择(Y/N)");
char c ;
for(; ; ){
//将接受的字符转换成大写字母
String str = readKeyBoard(1,false).toUpperCase();
c = str.charAt(0);
if (c=='Y'||c=='N'){
break;
}else {
System.out.println("选择有错误,请重新输入!");
}
}
return c;
}
/**
* 功能: 读取一个字符串
* @param limit 读取的长度
* @param blankReturn 如果为true ,表示 可以读空字符串。
* 如果为false表示 不能读空字符串。
*
* 如果输入为空,或者输入大于limit的长度,就会提示重新输入。
* @return
*/
public static String readKeyBoard(int limit,boolean blankReturn){
//定义一个字符串
String line = "";
while (scanner.hasNextLine()){//判断是否有下一行
line = scanner.nextLine();
//如果line.length=0, 即用户没有输入任何内容,直接回车
if (line.length()==0){
if (blankReturn)return line;//如果blankReturn=true,可以返回空串
else continue; //如果blankReturn=false,不接受空串,必须输入内容
}
//如果用户输入的内容大于了 limit,就提示重写输入
//如果用户如的内容 >0 <= limit ,我就接受
if (line.length()<1||line.length()>limit){
System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}
测试类
public class Test {
public static void main(String[] args) {
ATMView atmView = new ATMView();
atmView.MainEu();
}
}
运行截图