Account.java
package ATMSystem;
public class Account {
private String userName;
private String cardId;
private char sex;
private String passWord;
private double money;
private double limit;
public Account(String userName, String cardId, char sex, String passWord, double money, double limit) {
this.userName = userName;
this.cardId = cardId;
this.sex = sex;
this.passWord = passWord;
this.money = money;
this.limit = limit;
}
public Account() {
}
public String getUserName() {
return userName;
}
public String getCardId() {
return cardId;
}
public char getSex() {
return sex;
}
public String getPassWord() {
return passWord;
}
public double getMoney() {
return money;
}
public double getLimit() {
return limit;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public void setSex(char sex) {
this.sex = sex;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public void setMoney(double money) {
this.money = money;
}
public void setLimit(double limit) {
this.limit = limit;
}
}
ATM.java
package ATMSystem;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATM {
private ArrayList<Account> accounts=new ArrayList<>();
private ArrayList<Account>err_accounts=new ArrayList<>();
Scanner sc=new Scanner(System.in);
public void start(){
while (true){
System.out.println("~~~~~欢迎进入中国建设银行ATM系统~~~~~");
System.out.println("1.用户登录");
System.out.println("2.用户开户");
System.out.println("请选择:");
int command=sc.nextInt();
switch (command){
case 1:
Login();
break;
case 2:
creatAccount();
break;
default:
System.out.println("没有该操作~~");
}
}
}
/* 用户开户*/
private void creatAccount(){
Account acc=new Account();
System.out.println("请输入用户姓名:");
String name=sc.next();
acc.setUserName(name);
while (true) {
System.out.println("请输入性别:");
char sex=sc.next().charAt(0);
if (sex == '男') {
acc.setSex(sex);
break;
} else if (sex=='女') {
acc.setSex(sex);
break;
}else {
System.out.println("输入性别有误,请重新输入:");
}
}
// cardid
Random r=new Random();
String cardid="";
for (int i = 0; i < 8; i++) {
cardid+=Integer.toString(r.nextInt(10));
}
acc.setCardId(cardid);
// password
while (true){
System.out.println("请输入密码:");
String pwd1=sc.next();
System.out.println("请再次输入密码:");
String pwd2=sc.next();
if(pwd1.equals(pwd2)){
System.out.println("密码设置成功!");
acc.setPassWord(pwd1);
break;
}else{
System.out.println("两次密码设置不一致,请重新设置密码~");
}
}
acc.setLimit(5000);
acc.setMoney(0.0);
accounts.add(acc);
System.out.println("恭喜您!开户成功~~");
showAccount(acc);
}
private void showAccount(Account acc){
System.out.println("用户名:"+acc.getUserName());
System.out.println("卡号:"+acc.getCardId());
System.out.println("性别:"+acc.getSex());
System.out.println("余额:"+acc.getMoney());
System.out.println("限额:"+acc.getLimit());
}
/*登录操作*/
private void Login(){
// Scanner sc=new Scanner(System.in);
while (true){
System.out.println("请输入卡号:");
String carid=sc.next();
boolean flag=false;
for (int i = 0; i < accounts.size(); i++) {
if(carid.equals(accounts.get(i).getCardId())){
flag=true;
boolean flag2=true;
if(err_accounts.size()>0){
for (int j=0;j<err_accounts.size();j++){
if(carid.equals(err_accounts.get(j).getCardId())){
flag2=false;
}
}
}
if(flag2){
// 验证密码
int index=3;
while(index>0){
System.out.println("请输入密码:");
String pwd=sc.next();
if(pwd.equals(accounts.get(i).getPassWord())){
System.out.println("登陆成功!");
opMoney(accounts.get(i));
break;
}else {
System.out.println("密码错误,请再次输入:");
index--;
if(index==0){
err_accounts.add(accounts.get(i));
System.out.println("密码错误次数超过三次,您的账户被冻结,请联系工作人员~");
}
}
}
}else {
System.out.println("您的账户已被冻结,请联系工作人员解冻!");
break;
}
}
}
if(flag==false){
System.out.println("用户不存在,请重新输入登录!");
break;
}else {
break;
}
}
}
/*存款取款*/
private void opMoney(Account acc){
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.退出");
Scanner sc=new Scanner(System.in);
int op=sc.nextInt();
if (op == 1) {
System.out.println("请输入要存的金额:");
double in_money=sc.nextInt();
if(in_money>acc.getLimit()){
System.out.println("超出限额~请重新选择业务");
} else if (0<in_money&&in_money<=acc.getLimit()) {
acc.setMoney(acc.getMoney()+in_money);
System.out.println("余额:"+acc.getMoney());
// break;
}
} else if (op==2) {
System.out.println("请输入要取的金额:");
double out_money=sc.nextInt();
double final_money=acc.getMoney()-out_money;
if (final_money>=0){
acc.setMoney(final_money);
System.out.println("余额:"+acc.getMoney());
// break;
}else {
System.out.println("余额不足~~");
}
} else if (op==3) {
// 转账业务
Transfer(acc);
} else if (op==4) {
// 注销账户
CancelAccount(acc);
}else if(op==5){
// 修改密码
ChangePassword(acc);
break;
}else if (op==6){
// 退出
break;
}
else {
System.out.println("没有该选项!");
}
}
}
/*转账*/
private void Transfer(Account acc){
System.out.println("请输入对方卡号:");
String in_cardID=sc.next();
boolean flag=true;
// boolean flag2=false;
int index=-1;
for (int i = 0; i < accounts.size(); i++) {
if(in_cardID.equals(accounts.get(i).getCardId())){
// flag2=true;
index=i;
for (int j = 0; j < err_accounts.size(); j++) {
if (in_cardID.equals(err_accounts.get(j).getCardId())){
flag=false;
}
}
}
}
if (index==-1){
System.out.println("您输入的卡号不存在!");
}else {
if (flag==false){
System.out.println("您输入的卡号已被冻结,无法进行转账业务!");
}else {
System.out.println("请输入您要转账的金额:");
double tranfer_money=sc.nextInt();
System.out.println("请输入您的密码:");
String pwd=sc.next();
if (pwd.equals(acc.getPassWord())){
if(tranfer_money>acc.getLimit()){
System.out.println("转账金额超过限额!");
} else if (tranfer_money>acc.getMoney()) {
System.out.println("余额不足!");
}else {
acc.setMoney(acc.getMoney()-tranfer_money);
accounts.get(index).setMoney(accounts.get(index).getMoney()+tranfer_money);
System.out.println("转账成功!");
System.out.println("账户余额:"+acc.getMoney());
}
}else {
System.out.println("密码错误!");
}
}
}
}
/*账户注销*/
private void CancelAccount(Account acc){
System.out.println("请确认您是否要注销该账户:");
System.out.println("1.是");
System.out.println("2.否");
showAccount(acc);
int option=sc.nextInt();
if (option==1){
System.out.println("请再次输入您的密码:");
String pwd=sc.next();
if (pwd.equals(acc.getPassWord())){
for (int i = 0; i < accounts.size(); i++) {
if (acc.getCardId().equals(accounts.get(i).getCardId())){
if (acc.getMoney()>0){
System.out.println("账户还有余额,无法注销!");
}else {
accounts.remove(i);
System.out.println("账户已注销!");
break;
}
}
}
}else {
System.out.println("密码错误!请重试~");
}
}
}
/*修改密码*/
private void ChangePassword(Account acc){
while(true){
System.out.println("请输入新密码:");
String newpwd1=sc.next();
System.out.println("请再次输入:");
String newpwd2=sc.next();
if (!newpwd1.equals(newpwd2)){
System.out.println("两次输入密码不一致,请重新输入!");
}else {
acc.setPassWord(newpwd1);
System.out.println("修改密码成功!");
break;
}
}
}
}
TEST.java
package ATMSystem;
public class TEST {
public static void main(String[] args) {
ATM atm=new ATM();
atm.start();
}
}
标签:acc,Java,String,ATM,系统,System,else,println,out
From: https://www.cnblogs.com/Weber-security/p/17623788.html