Java基于控制台界面实现ATM系统
本文实例为大家分享了Java实现ATM系统的具体代码,供大家参考,具体内容如下
这应该算最基础的Javase项目了,但其中逻辑还是得想想的。
功能还算完善,只是对输入数据的校验没做全,之后做WEB时再加上。
没有数据库。
完整代码在最后。
流程图
登录
想模拟提款机插卡登录的,因此没做注册账号的功能,手动先塞了三个账号。
有三次输入密码机会。
查询
取款
存款
转账
修改密码
退出
代码
文件结构
下面两个类复制过去放到ATM文件夹下就能运行。
package ATM;
import java.text.SimpleDateFORMat;
import java.util.Calendar;
import java.util.Date;
public class Account {
private String cardID;
private String username;
private String passWord;
private double balance;
private boolean status;//卡 锁定状态
public Account() {
}
public Account(String cardID, String username, String password, double balance, boolean status) {
this.cardID = cardID;
this.username = username;
this.password = password;
this.balance = balance;
this.status = status;
}
public Account(String cardID, String username, String password, double balance) {
this.cardID = cardID;
this.username = username;
this.password = password;
this.balance = balance;
}
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 getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
@Override
public String toString(){
System.currentTimeMillis();
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatDate = sdf.format(date);
return "银行:新航路银行\n" +
"银行账号:"+cardID+"\n"
+"用户名:"+username+"\n"
+"余额:"+balance+"\n"
+formatDate;
}
}
package ATM;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class ATM {
static Scanner sc = new Scanner(System.in);
static ArrayList<Account> accounts;
public static void main(String[] args) {
//模仿插卡ATM,因此没做注册用户功能,这里自己添加了几个用户做测试。
accounts = new ArrayList<>();
accounts.add(new Account("10001", "路飞", "ONEPIECE", 100,true));
accounts.add(new Account("10002", "索隆", "123456", 10000,false));
accounts.add(new Account("10003", "娜美", "123456", 1000000d,true));
//登录
loginVerify();
}
//登录验证
public static void loginVerify() {
System.out.println("提示:有这些账户");
for (Account account : accounts) {
System.out.println("卡号:"+account.getCardID() +" 用户名:" + account.getUsername()+" 密码:"+account.getPassword()+" 余额:"+account.getBalance());
}
System.out.println("---------------------------------------------");
//模拟插卡,手动输入银行卡号
System.out.print("输入银行卡号:");
String cardID = sc.next();
//根据卡号,判断此账号是否存在
for (int i = 0; i < accounts.size(); i++) {
Account account = accounts.get(i);
if (cardID.equals(account.getCardID())) { //判断卡号是否存在
if(account.isStatus()) { //看该卡是否被锁
//账号存在,输入密码
System.out.print("请输入密码:");
int count = 3;//可输入3次密码
while (count > 0) {
sc.useDelimiter("\n");
String pwd = sc.next();
if (pwd.equals(accounts.get(i).getPassword())) {
//登录成功,转到主界面
mainInterface(accounts.get(i));
} else {
if(--count == 0){
System.out.println("此卡已锁,请到人工处咨询办理。");
accounts.get(i).setStatus(false);
}else {
System.out.println("还有" + count + "次输入机会");
}
}
}
}else{
System.out.println("此卡已锁,请到人工处咨询办理。");
}
break;
}
if(i == accounts.size()-1){
System.out.println("此卡号不存在!!");
}
}
}
public static void mainInterface(Account account) {
System.out.println("=====================================");
System.out.println("|| 1.查询余额 5.修改密码||");
System.out.println("|| 2.取款 6.退卡 ||");
System.out.println("|| 3.存款 ||");
System.out.println("|| 4.转账 ||");
System.out.println("=====================================");
System.out.print("请输入要操作的号码:");
int i = sc.nextInt();
switch (i) {
case 1:
queryBalance(account);
break;
case 2:
withdrawal(account);
break;
case 3:
deposit(account);
break;
case 4:
transfer(account);
break;
case 5:
updatePassword(account);
break;
case 6:
exitSystem();
break;
default:
System.out.println("输入号码不对!");
break;
}
}
//查询余额
public static void queryBalance(Account account) {
System.out.println("=====================================");
System.out.println("|| "+account.getUsername() + "的余额: " + account.getBalance());
System.out.println("=====================================");
mainInterface(account);
}
//取款
public static void withdrawal(Account account) {
System.out.print("请输入取款金额:");
double money = sc.nextInt();
if (money <= account.getBalance()) {
account.setBalance(account.getBalance() - money);
System.out.println("取款成功!!");
System.out.println("=====================================");
System.out.println("|| 1.返回主界面 2.打印发票||");
System.out.println("=====================================");
int i = sc.nextInt();
switch (i) {
case 1:
mainInterface(account);
break;
case 2:
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatDate = sdf.format(date);
System.out.println("银行:新航路银行\n"+
"银行卡:"+account.getCardID()+"\n"+
"用户名:"+account.getUsername()+"\n"+
"本次取款:"+money+"\n"+
formatDate);
mainInterface(account);
break;
}
} else {
System.out.println("oh,我可怜的孩子!余额不足捏。");
System.out.println("=====================================");
System.out.println("|| 1.返回 enter ||");
System.out.println("=====================================");
sc.next();
mainInterface(account);
}
}
//存款
public static void deposit(Account account) {
System.out.print("请输入要存款金额:");
double tempMoney = sc.nextDouble();
account.setBalance(account.getBalance() + tempMoney);
System.out.println("哔~哔~哔 点钞中...");
System.out.println("存款成功");
mainInterface(account);
}
//转账
public static void transfer(Account account) {
System.out.println("请输入对方卡号(提示账号有:10001 /10002 /10003)");
String cardID = sc.next();
//根据卡号,判断此账号是否存在
for (int i = 0; i < accounts.size(); i++) {
if (cardID.equals(accounts.get(i).getCardID())) {
System.out.print("请输入对象用户名验证:");
sc.nextLine();
String next = sc.nextLine();
if(next.equals(accounts.get(i).getUsername())) {
System.out.print("请输入要转的金额:");
double m = sc.nextDouble();
if (m > account.getBalance()) {
System.out.println("你没这么多钱,转账失败!");
break;
} else {
account.setBalance(account.getBalance() - m);
accounts.get(i).setBalance(m + accounts.get(i).getBalance());
System.out.println("转账成功");
break;
}
}else{
System.out.println("账号与用户名不匹配,转账失败");
break;
}
}
if(i == accounts.size()-1) {
System.out.println("此卡号不存在!!");
}
}
mainInterface(account);
}
//5. 修改密码
public static void updatePassword(Account account){
System.out.print("请输入新密码:");
String s1 = sc.next();
System.out.print("请再次输入新密码:");
String s2 = sc.next();
if(s1.equals(s2)){
account.setPassword(s1);
System.out.println("密码修改成功,请重新登录!");
loginVerify();
}else{
System.out.println("两次密码不一致");
mainInterface(account);
}
}
//6. 退出
public static void exitSystem() {
System.exit(0);
//loginVerify();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
相关文章