public class Account {
private String cardID;
private String username;
private char sex;
private String password;
private double restmoney;
private double limit;
public Account() {
}
public Account(String cardID, String username, char sex, String password, double restmoney, double limit) {
this.cardID = cardID;
this.username = username;
this.sex = sex;
this.password = password;
this.restmoney = restmoney;
this.limit = limit;
}
public String getCardID() {
return cardID;
}
public void setCardID(String cardID) {
this.cardID = cardID;
}
public String getUsername() {
return username + (sex == '男'?"先生":"女士");
}
public void setUsername(String username) {
this.username = username;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getRestmoney() {
return restmoney;
}
public void setRestmoney(double restmoney) {
this.restmoney = restmoney;
}
public double getLimit() {
return limit;
}
public void setLimit(double limit) {
this.limit = limit;
}
}
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATM {
private ArrayList<Account> accounts = new ArrayList();
private Account loginacc;
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 login(){
System.out.println("===系统登录===");
if (accounts.size() == 0){
System.out.println("目前系统中无账户请先创建账户后再登录");
return;
}
Account acc = null;
while (true) {
System.out.println("请输入你的登录卡号");
String cardId = sc.next();
acc = getbycreatId(cardId);
if (acc == null){
System.out.println("你输入的卡号不存在请重新确认");
}else {
while (true) {
System.out.println("请输入你的登录密码");
String loginword = sc.next();
if (acc.getPassword().equals(loginword)){
loginacc = acc;
System.out.println("恭喜" + acc.getUsername() +"您成功登录系统 您的卡号是:" + acc.getCardID());
showusercommand();
return;
}else {
System.out.println("你输入的密码不正确请重新输入");
}
}
}
}
}
private void showusercommand(){
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.注销账户");
int command = sc.nextInt();
switch (command){
case 1:
checkaccount();
break;
case 2:
resortmoney();
break;
case 3:
withdrawmoney();
break;
case 4:
transfermoney();
break;
case 5:
updatpassword();
return;
case 6:
System.out.println(loginacc.getUsername()+"您退出账户成功");
return;
case 7:
if (deleteAccount()){
return;
}
break;
default:
System.out.println("无该操作请重新选择!");
}
}
}
private void updatpassword() {
System.out.println("===密码修改页面===");
while (true) {
System.out.println("请先输入原密码");
String code = sc.next();
if (loginacc.getPassword().equals(code)){
while (true) {
System.out.println("请输入新密码");
String newcode = sc.next();
System.out.println("请再次输入新密码");
String newcode1 = sc.next();
if (newcode1.equals(newcode)){
loginacc.setPassword(newcode1);
System.out.println("密码重置成功");
return;
}else {
System.out.println("前后两次密码不相同请再次设置");
}
}
}else {
System.out.println("你输入的原密码不正确");
}
}
}
private boolean deleteAccount() {
System.out.println("请确认是否要销户?y/n");
String command = sc.next();
switch (command){
case "y":
if (loginacc.getRestmoney() == 0){
accounts.remove(loginacc);
System.out.println("已经成功为您完成销户!");
return true;
}else {
System.out.println("您的账户余额不为0无法为您完成销户!");
return false;
}
default:
System.out.println("好的 不进行账户的注销");
}
return false;
}
private void transfermoney() {
System.out.println("===用户转账===");
if (accounts.size() < 2){
System.out.println("对不起当前系统中只有您一个账户无法为您转账!");
}
if (loginacc.getRestmoney() == 0 ){
System.out.println("抱歉当前余额为0无法为他人转账!!!");
}
while (true) {
System.out.println("请输入对方卡号");
String cardID = sc.next();
Account acc = getbycreatId(cardID);
if (acc == null ){
System.out.println("您输入的对方的卡号不存在");
}else {
String name = "*"+acc.getUsername().substring(1);
System.out.println("请输入对方【"+name+"】的姓氏");
String prename = sc.next();
if (acc.getUsername().startsWith(prename)){
System.out.println("请输入要给对方的转账金额");
double money = sc.nextDouble();
if (loginacc.getRestmoney() >= money){
loginacc.setRestmoney(loginacc.getRestmoney() - money);
acc.setRestmoney(acc.getRestmoney() + money);
System.out.println("转账成功!");
return;
}else {
System.out.println("您的余额不够无法完成转账 您当前能转账的金额为:" + loginacc.getRestmoney());
}
}else {
System.out.println("抱歉您输入的信息错误!请再次输入");
}
}
}
}
private void withdrawmoney() {
System.out.println("===请输入取款金额===");
double money = sc.nextDouble();
if (loginacc.getRestmoney() <= 100){
System.out.println("您当前账户余额小于100元 无法为您取款~~");
return;
}
if ( loginacc.getRestmoney() >= money){
if (money > loginacc.getLimit()){
System.out.println("您取款金额大于您的取款限度无法取款~您的取款限度为" + loginacc.getLimit()+"元");
}else {
loginacc.setRestmoney(loginacc.getRestmoney() - money);
System.out.println("您取款成功取款:"+money+"元目前账户余额为:"+loginacc.getRestmoney()+"元");
}
}else {
System.out.println("你取款金额小于最低限度100元,无法为您进行取款哦~");
}
}
private void resortmoney() {
System.out.println("===请输入存款金额===");
double money = sc.nextDouble();
loginacc.setRestmoney(loginacc.getRestmoney() + money);
System.out.println("恭喜您存款"+money+"元成功"+"目前账户余额为:"+loginacc.getRestmoney()+"元3");
}
private void checkaccount(){
System.out.println("====尊敬的"+loginacc.getUsername()+"你的账户信息如下====");
System.out.println("卡号:"+loginacc.getCardID());
System.out.println("性别:"+loginacc.getSex());
System.out.println("余额:"+loginacc.getRestmoney());
System.out.println("限额:"+loginacc.getLimit());
}
private void creataccount(){
Account acc = new Account();
System.out.println("=====系统开户操作=====");
System.out.println("请输入你的账户用户名");
acc.setUsername(sc.next());
while (true) {
System.out.println("请输入你的性别");
char sex = sc.next().charAt(0);
if (sex =='男' || sex =='女'){
acc.setSex(sex);
break;
}else {
System.out.println("你输入的性别有问题~只能是男或女!");
}
}
while (true) {
System.out.println("请输入你的密码");
String password = sc.next();
System.out.println("请再次确认你的密码");
String okpassword = sc.next();
if (okpassword.equals(password)){
acc.setPassword(okpassword);
break;
}else {
System.out.println("前后两次输入的密码不一样请您重新输入!");
}
}
System.out.println("请您输入你的每次取款限额");
acc.setLimit(sc.nextDouble());
acc.setCardID(creatcardID());
accounts.add(acc);
System.out.println("恭喜您"+acc.getUsername()+"开户成功!"+"你的卡号是:"+acc.getCardID());
}
private String creatcardID(){
while (true) {
Random b =new Random();
String cardID = "";
for (int i = 0; i < 8; i++) {
int date = b.nextInt(10);
cardID += date;
}
Account acc = getbycreatId(cardID);
if (acc == null){
return cardID;
}
}
}
private Account getbycreatId(String cardID){
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (acc.getCardID().equals(cardID)){
return acc;
}
}
return null;
}
}
public class Test {
public static void main(String[] args) {
ATM operator = new ATM();
operator.start();
}
}