目录

  • ​​项目介绍​​
  • ​​系统准备,首页设计​​
  • ​​总结​​
  • ​​开户功能​​
  • ​​总结​​
  • ​​用户登录​​
  • ​​总结​​
  • ​​用户操作页设计、查询账户、退出账户功能​​
  • ​​用户存钱​​
  • ​​取款功能​​
  • ​​转账功能​​
  • ​​密码修改、销户​​
  • ​​源代码​​

项目介绍

Java之ATM系统_前端


Java之ATM系统_java_02

系统准备,首页设计

Java之ATM系统_前端_03

总结

1、用户的账户信息,系统如何表示的?
定义账户类Account,定义系统关心的属性信息。
2、系统采用什么来存储全部用户的账户对象信息?
ArrayList accounts = new ArrayList<>();

开户功能

Java之ATM系统_数据库_04

总结

1、开户功能的实现需要哪几步操作,需要注意什么问题?

  • 开户功能应该独立定义成方法,并传入当前集合对象给该方法。
  • 创建一个Account账户对象,封装账户信息
  • 录入开户信息(姓名、密码)
  • 卡号要自动生成且唯一

用户登录

Java之ATM系统_密码修改_05

总结

1、登录功能如何实现的?

  • 根据卡号去账户集合中查询对应的账户对象。
  • 如果找到了账户对象,说明卡号存在,继续输入密码。
  • 如果密码也正确,则登录成功。

用户操作页设计、查询账户、退出账户功能

Java之ATM系统_数据库_06

用户存钱

Java之ATM系统_System_07

取款功能

Java之ATM系统_密码修改_08

转账功能

Java之ATM系统_密码修改_09

密码修改、销户

Java之ATM系统_密码修改_10

源代码

package com.gushi;

public class Accout {
private String cardId;
private String userName;
private String passWord;
private double money;
private double quotaMoney;

public String getCardId() {
return cardId;
}

public void setCardId(String cardId) {
this.cardId = cardId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

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;
}
}
package com.gushi;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

import static com.sun.deploy.registration.RegisterDeploy.register;
import static sun.security.jgss.GSSUtil.login;

public class ATMSystem {
public static void main(String[] args) {
ArrayList <Accout> accouts = new ArrayList<>();

while (true) {
System.out.println("=====ATM系统=====");
System.out.println("1、账号登录");
System.out.println("2、账户开户");

Scanner sc = new Scanner(System.in);
System.out.println("请您选择操作:");
int command = sc.nextInt();

switch (command){
case 1:
login(accouts,sc);
break;
case 2:
register(accouts,sc);
break;
default:
System.out.println("您输入的操作不存在~~");
}
}
}

private static void login(ArrayList<Accout> accouts, Scanner sc) {
System.out.println("==============系统登录操作=============");
if(accouts.size()==0){
System.out.println("对不起,当前系统中,无任何账户,请先开户,再来登录~~");
return;
}

while(true){
System.out.println("请输入登录卡号:");
String cardId = sc.next();
Accout acc = getAccountByCardId(cardId,accouts);
if(acc!=null){
while(true){
System.out.println("请您输入登录密码:");
String passWord = sc.next();
if(acc.getPassWord().equals(passWord)){
System.out.println("恭喜您:"+acc.getUserName()+"先生/女士进入系统,您的卡号是:"+acc.getCardId());
showUserCommand(sc,acc,accouts);
return;
}else {
System.out.println("对不起,您输入的密码有误~~");
}
}
}else{
System.out.println("对不起,系统中不存在该账户卡号~~");
}
}
}



private static void showUserCommand(Scanner sc,Accout acc,ArrayList<Accout> accouts) {
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 command = sc.nextInt();
switch (command){
case 1:
showAccout(acc);
break;
case 2:
depositMoney(acc,sc);
break;
case 3:
drawMoney(acc,sc);
break;
case 4:
transferMoney(sc,acc,accouts);
break;
case 5:
updatePassWord(sc,acc);
return;
case 6:
System.out.println("退出成功,欢迎下次光临");
return;
case 7:
if (deleteAccout(acc,sc,accouts)){
return;
}else{
break;
}

default:
System.out.println("您输入的操作命令不正确~~");
}
}
}

private static boolean deleteAccout(Accout acc, Scanner sc,ArrayList<Accout> accouts) {
System.out.println("==========用户销户=========");
System.out.println("您真的要销户?y/n");
String rs =sc.next();
switch (rs){
case "y":
if(acc.getMoney()>0){
System.out.println("您账户中还有钱没有取完,不允许销户~");
}else {
accouts.remove(acc);
System.out.println("您的账户销户完成~~");
return true;
}

break;
default:
System.out.println("好的,当前账号继续保留~");
}
return false;
}

private static void updatePassWord(Scanner sc, Accout acc) {
System.out.println("========用户密码修改========");
System.out.println("请您输入当前密码");
String passWord = sc.next();

if(acc.getPassWord().equals(passWord)){
while(true){
System.out.println("请您输入新密码:");
String newPassWord = sc.next();

System.out.println("请您确认新密码:");
String okPassWord = sc.next();

if(newPassWord.equals(okPassWord)){
acc.setPassWord(newPassWord);
System.out.println("恭喜您,您密码修改成功了~");
return;
}else{
System.out.println("您输入的2次密码不正确");
}
}
}else{
System.out.println("您输入的密码不正确~~");
}
}

private static void transferMoney(Scanner sc, Accout acc, ArrayList<Accout> accouts) {
System.out.println("=========用户转账操作========");

if(accouts.size()<2){
System.out.println("当前系统中,不足2个账户,不能进行转帐,请去开户吧~");
return;
}
if(acc.getMoney()==0){
System.out.println("对不起,您自己都没钱,就别转了吧~~");
return;
}
while(true){
System.out.println("请您输入对方账户的卡号");
String cardId=sc.next();

if(cardId.equals(acc.getCardId())){
System.out.println("对不起,您不可以给自己转账");
continue;
}

Accout accout = getAccountByCardId(cardId,accouts);
if(accout == null){
System.out.println("对不起,您输入对方的这个账号不存在~~");
}else{
String userName = accout.getUserName();
String tip = "*"+userName.substring(1);
System.out.println("请您输入["+tip+"]的姓氏");
String preName =sc.next();

if(userName.startsWith(preName)){
while(true){
System.out.println("请您输入转账金额:");
double money = sc.nextDouble();
if(money>acc.getMoney()){
System.out.println("对不起,您余额不足,您最多可以转账:"+ acc.getMoney());
}else{
acc.setMoney(acc.getMoney()-money);
accout.setMoney(acc.getMoney()+money);
System.out.println("转账成功!您的账户还剩余:"+acc.getMoney());
return;
}
}
}else{
System.out.println("对不起,您输入的信息有误~~");
}
}


}
}

private static void drawMoney(Accout acc, Scanner sc) {
System.out.println("==========用户取钱操作===============");
if(acc.getMoney()<100){
System.out.println("对不起,当前账户中不够100元,不能取钱~");
return ;
}
while(true){
System.out.println("请您输入取款金额");
double money = sc.nextDouble();

if(money> acc.getQuotaMoney()){
System.out.println("对不起,您当前取款金额超过每次限额,每次最多可取:"+acc.getQuotaMoney());

}else{
if(money>acc.getMoney()){
System.out.println("余额不足,您账户余额是:"+acc.getMoney());

}else{
System.out.println("恭喜您,取钱"+money+"元,成功");
acc.setMoney(acc.getMoney()-money);
showAccout(acc);
return;
}
}
}
}

private static void depositMoney(Accout acc, Scanner sc) {
System.out.println("==========用户存钱操作=========");
System.out.println("请您输入存款金额:");
double money = sc.nextDouble();

acc.setMoney(acc.getMoney()+money);
System.out.println("恭喜您,存钱成功,当前账户信息如下:");
showAccout(acc);
}

private static void showAccout(Accout acc) {
System.out.println("===========当前账户信息如下=========");
System.out.println("卡号:"+acc.getCardId());
System.out.println("户主:"+acc.getUserName());
System.out.println("余额:"+acc.getMoney());
System.out.println("限额:"+acc.getQuotaMoney());
}

private static void register(ArrayList<Accout> accouts, Scanner sc) {
System.out.println("========系统开户操作========");
Accout accout = new Accout();

System.out.println("请您输入账户用户名:");
String userName = sc.next();
accout.setUserName(userName);

while (true) {
System.out.println("请您输入账户密码:");
String passWord = sc.next();
System.out.println("请您输入确认密码:");
String okPassWord = sc.next();
if(okPassWord.equals(passWord)){
accout.setPassWord(okPassWord);
break;
}else{
System.out.println("对不起,您输入的2次密码不一致,请重新确认");
}
}
System.out.println("请您输入账户当此限额:");
double quotaMoney = sc.nextDouble();
accout.setQuotaMoney(quotaMoney);

String cardId= getRandomCardId(accouts);
accout.setCardId(cardId);

accouts.add(accout);
System.out.println("恭喜您,"+userName+"先生/女生,您开号成功,您的卡号是:"+cardId+",请您妥善保管卡号");
}

private static String getRandomCardId(ArrayList<Accout> accouts){
Random r =new Random();
while (true) {
String cardId="";

for (int i = 0; i < 8; i++) {
cardId+=r.nextInt(10);
}
Accout acc = getAccountByCardId(cardId,accouts);
if(acc==null){
return cardId;
}
}
}

private static Accout getAccountByCardId(String cardId,ArrayList<Accout> accouts){

for (int i = 0; i < accouts.size(); i++) {
Accout acc = accouts.get(i);
if(acc.getCardId().equals(cardId)){
return acc;
}
}
return null;
}
}