要求:使用Java语言,编写ATM系统运行逻辑,内容包括登录系统界面、登录、注册账户、退出系统以及登录后的查询、存款、取款、转账、修改密码、注销等功能。
该代码逻辑严谨,功能较齐全,适合初学者学习。
package ATM;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMsystem {
public static void main(String[] args) {
ArrayList<Account> Accounts = new ArrayList<>();//定义一个账户的集合
Scanner S = new Scanner(System.in);//创建键盘扫描器
while (true) {
System.out.println("-----------------ATM登陆系统-----------------");
System.out.println("-----------------1.登录账户------------------");
System.out.println("-----------------2.注册账户------------------");
System.out.println("-----------------3.退出系统------------------");
System.out.println("请选择操作:");
int Select = S.nextInt();
switch (Select) {
case 1:
Login(Accounts, S);
break;
case 2:
Register(Accounts, S);
break;
case 3:
System.out.println("退出成功!");
return;
default:
System.out.println("输入有误!");
break;
}
}
}
private static void Login(ArrayList<Account> accounts, Scanner S) {//登录系统函数
if (accounts.size() == 0) {//判断账户集合中元素个数
System.out.println("您还没有账户,先注册一个吧!");
} else {
System.out.println("-----------------系统登录操作-----------------");
System.out.println("请输入卡号:");
String CardId = S.next();
Account acc = getAccountByCardId(accounts, CardId);//通过卡号获取账户信息的函数
if (acc != null) {//查询到账户
while (true) {
System.out.println("请输入密码:");
String Password = S.next();
for (int j = 0; j < accounts.size(); j++) {//遍历集合中的每一个元素
if (Password.equals(acc.getPassword())) {//对应内容一样即可
System.out.println("卡号:" + acc.getIdcard() + "登录成功!");
showmenu(accounts, acc, S);//操作界面函数
return;
}
}
System.out.println("密码错误,请重新输入!");
}
} else {
System.out.println("查无此账户,请注册!");
}
}
}
private static void showmenu(ArrayList<Account> accounts,Account acc,Scanner S) {//操作界面函数
while (true) {
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("7.注销账户;");
System.out.println("请选择操作:");
int Select=S.nextInt();
switch (Select) {
case 1://查询
showaccount(acc);
break;
case 2://存款
depositmoney(acc, S);
break;
case 3://取款
drawmoney(acc, S);
break;
case 4://转账
transfermoney(accounts, acc, S);
break;
case 5://修改密码
changepassword(acc, S);
break;
case 6://退出
System.out.println("欢迎下次使用!");
return;
case 7://注销账户
if (cancellationaccount(accounts, acc, S))
return;//退出到初始界面
else
break;//显示登陆后的操作界面
default:
System.out.println("输入错误!");
break;
}
}
}
private static boolean cancellationaccount(ArrayList<Account> accounts, Account acc, Scanner S) {
//注销账户函数,用布尔类型返回真假值
while (true) {
System.out.println("请确认操作:");
System.out.println("确认");
System.out.println("取消");
String select=S.next();
switch (select)
{
case "确认":
accounts.remove(acc);
System.out.println("注销成功!");
return true;
case "取消":
System.out.println("取消注销成功!");
return false;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
private static void changepassword(Account acc, Scanner S) {//修改密码函数
while (true) {
System.out.println("请输入当前密码:");
String password = S.next();
if (password.equals(acc.getPassword())) {//判断两密码内容是否相同
System.out.println("请输入修改后的密码:");
String newpassword = S.next();
if (newpassword.equals(password)) {
System.out.println("新密码不能与旧密码一致!");
} else {
System.out.println("请再次输入以确认:");
String new2pasword = S.next();
if (new2pasword.equals(newpassword)) {
System.out.print("修改成功!");
acc.setPassword(new2pasword);
System.out.println("新密码为:" + acc.getPassword());
return;
} else {
System.out.println("两次密码输入不一致,请重新输入!");
}
}
} else {
System.out.println("输入有误,请重新输入!");
}
}
}
private static void transfermoney(ArrayList<Account> accounts, Account acc, Scanner S) {//转账函数
if (accounts.size() >= 2) {//查询集合中元素个数,两个以上进行操作
while (true) {
System.out.println("请输入转账账户卡号:");
String cardid=S.next();
Account acc2=getAccountByCardId(accounts,cardid);
if(acc2!=null)//查询是否存在转账账户
{
while (true) {
System.out.println("请输入转账金额:");
double money=S.nextDouble();
if(money>acc.getMoney())//与账户现有金额做对比
{
System.out.println("余额不足,转账失败!(余额:"+acc.getMoney()+")");
}
else
{
System.out.print("转账成功!");
acc.setMoney(acc.getMoney()-money);//本账户金额减少
acc2.setMoney(acc2.getMoney()+money);//转账账户金额增多
System.out.println("(余额:"+acc.getMoney()+")");//显示本账户余额
return;
}
}
}
else
{
System.out.println("查无此账户!");
}
}
} else {
System.out.println("当前系统无其他账户!");
return;
}
}
private static void drawmoney(Account acc, Scanner S) {//取款函数
while (true) {
System.out.println("请输入取款数额:");
double money=S.nextDouble();
if(acc.getMoney()>acc.getQuota())//判断余额是否大于限额
{
if(money> acc.getQuota())//判断取款数目是否大于限额
{
System.out.println("超出额度上限,取款失败!(限额:"+acc.getQuota()+")");
}
else
{
System.out.println("取款成功!");
acc.setMoney(acc.getMoney()-money);
return;
}
}
else
{
if(money>acc.getMoney())//判断取款数目是否大于余额
{
System.out.println("余额不足,取款失败!(余额:"+acc.getMoney()+")");
}
else
{
System.out.println("取款成功!");
acc.setMoney(acc.getMoney()-money);//本账户余额减少
return;
}
}
}
}
private static void depositmoney(Account acc,Scanner S) {//存款函数
System.out.println("请输入存款数额:");
double money=S.nextDouble();
acc.setMoney(acc.getMoney()+money);//本账户余额增多
System.out.println("存款成功!");
showaccount(acc);
}
private static void showaccount(Account acc) {//显示账户信息函数
System.out.println("当前账户信息如下:");
System.out.println("卡号:"+acc.getIdcard());
System.out.println("用户名:"+acc.getName());
System.out.println("余额:"+acc.getMoney());
System.out.println("单次交易额度:"+acc.getQuota());
}
private static void Register(ArrayList<Account> accounts, Scanner S) {//注册账户函数
System.out.println("-----------------系统开户操作-----------------");
Account account = new Account();
System.out.println("请输入用户名:");
String Name = S.next();
account.setName(Name);//将用户名存入该账户
System.out.println("请输入密码:");
String Password = S.next();
while (true) {
System.out.println("请您确认密码:");
String Confirmpassword = S.next();
if (Password.equals(Confirmpassword)) {//判断两次输入的密码内容是否相同
account.setPassword(Password);//将密码存入该账户
break;
} else {
System.out.println("输入错误,请重新输入!");
}
}
System.out.println("请您输入单次交易限额:");
double Quota = S.nextDouble();
account.setQuota(Quota);//将限额存入该账户
String Idcard = getRandomCardId(accounts);//通过随机卡号函数生成一个随机卡号
account.setIdcard(Idcard);//将卡号存入该账户
accounts.add(account);//将账户存入账户集合中
System.out.println("注册成功!您的卡号是:" + Idcard);
}
private static String getRandomCardId(ArrayList<Account> accounts) {//生成随机卡号函数
Random R = new Random();
while (true) {
String id = "";
for (int i = 0; i < 8; i++) {//循环八次来生成一个八位的卡号
id += R.nextInt(10);
}
Account acc = getAccountByCardId(accounts, id);//将卡号传给该函数判断是否存在相同卡号
if (acc == null) {//不存在则创建成功/存在则重新随机生成
return id;
}
}
}
private static Account getAccountByCardId(ArrayList<Account> accounts, String id) {//通过账户获取卡号函数
for (int i = 0; i < accounts.size(); i++) {//遍历账户集合中的所有账户
Account acc = accounts.get(i);
if (id.equals(acc.getIdcard())) {//判断传入的卡号与账户中原有卡号内容是否相同
return acc;//相同则返回该账户
}
}
return null;//不同则返回空值
}
}
package ATM;
//账户类的内部成员定义
public class Account {
private String Idcard;//卡号
private String Name;//用户名
private String Password;//密码
private double Money;//余额
private double Quota;//额度
public String getIdcard() {//获取卡号
return Idcard;
}
public void setIdcard(String idcard) {//修改卡号
Idcard = idcard;
}
public String getName() {//获取用户名
return Name;
}
public void setName(String name) {//修改用户名
Name = name;
}
public String getPassword() {//获取密码
return Password;
}
public void setPassword(String password) {//修改密码
Password = password;
}
public double getMoney() {//获取余额
return Money;
}
public void setMoney(double money) {//修改余额
Money = money;
}
public double getQuota() {//获取限额
return Quota;
}
public void setQuota(double quota) {//修改限额
Quota = quota;
}
}