一案例要求:
首先完成ATM的用户登录和用户开户两个大功能,用户开户有账户名,性别,账户密码,确认密码,每次取现额度,并且随机生成一个7位数的账号,用户登录功能有查询,存款,取款,转账,修改密码,退出,注销账号。
二案例收获:(也是细节要求)
三实现代码:
Ⅰ:(主函数)
package ATM;
public class test {
public static void main(String[] args) {
ATM atm=new ATM();
atm.start();
}
}
Ⅱ:(实体类)
package ATM;
public class account {
private String username;
private String sex;
private String idcard;
private String sueridcard;
private double limitmoney;
private double priceleft;
private String userid;
public account() {
}
public account(String username, String sex, String idcard, String sueridcard, double limitmoney, String userid,double priceleft) {
this.username = username;
this.sex = sex;
this.idcard = idcard;
this.sueridcard = sueridcard;
this.limitmoney = limitmoney;
this.userid = userid;
this.priceleft=priceleft;
}
public double getPrice() {
return priceleft;
}
public void setPrice(double priceleft) {
this.priceleft = priceleft;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
public String getSueridcard() {
return sueridcard;
}
public void setSueridcard(String sueridcard) {
this.sueridcard = sueridcard;
}
public double getLimitmoney() {
return limitmoney;
}
public void setLimitmoney(double limitmoney) {
this.limitmoney = limitmoney;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
}
Ⅲ:(操作类)
package ATM;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATM {
ArrayList<account> arrayList=new ArrayList<>();
Scanner sc=new Scanner(System.in);
Random sb=new Random();
public void start(){
int flag=0;
while (true) {
System.out.println("======欢迎进入ATM系统=======");
System.out.println("1用户登录");
System.out.println("2用户开户");
System.out.println("0退出");
System.out.println("请输入你的选择");
int x;
x=sc.nextInt();
switch (x){
case 0:return ;
case 1:
if (flag==1) land();
else System.out.println("该系统未开户,请先开户");break;
case 2:
visit();flag=1;break;
default:
System.out.println("输入错误,请重新输入");break;
}
}
}
private void visit() {
System.out.println("请输入用户名:");
String name= sc.next();
System.out.println("请输入性别(男|女)");
String sex;
while (true) {
sex= sc.next();
if (sex.equals("男")||sex.equals("女")){
break;
}else {
System.out.println("请重新输入性别");
}
}
String idcard;
String sureidcard;
while (true) {
System.out.println("请输入账户密码:");
idcard= sc.next();
System.out.println("请确认密码");
sureidcard=sc.next();
if(!idcard.equals(sureidcard)){
System.out.println("两次密码输入不一致,请重新输入:");
}else break;
}
System.out.println("请输入每次提现额度:");
double price= sc.nextDouble();
System.out.println("恭喜您,开户成功!");
String mima="";
for (int i = 0; i < 7; i++) {
mima+=sb.nextInt(10);
}
System.out.println("您的卡号为:"+mima);
account account=new account(name,sex,idcard,sureidcard,price,mima,0);
arrayList.add(account);
}
private void land() {
int flag=0,m=0;
System.out.println("请输入卡号:");
String landmark;
while (true) {
landmark=sc.next();
for (int i = 0; i < arrayList.size(); i++) {
if(landmark.equals(arrayList.get(i).getUserid())){
flag=1;m=i;break;
}
}
if (flag==0){
System.out.println("系统中无该账户,请重新输入:");
}else break;
}
System.out.println("请输入登录密码:");
String mi;
while (true) {
mi= sc.next();
if (mi.equals(arrayList.get(m).getIdcard())){
System.out.println("恭喜"+arrayList.get(m).getUsername()+"先生登录系统,您的卡号为"+landmark);break;
}
}
operate(m);
}
private void operate(int m) {
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("=============");
System.out.println("请输入你的选择:");
int x= sc.nextInt();
switch (x){
case 1:find(m);break;
case 2:System.out.println("请决定存取金额:");
double price= sc.nextDouble();ru(m,price);break;
case 3:System.out.println("请决定取款金额:");double price1= sc.nextDouble();chu(m,price1);break;
case 4:zhuan(m);break;
case 5:modify(m);break;
case 6:return;
case 7: zhuxiao(m);break;
default:
System.out.println("输入错误,请重新输入");
}
}
}
private void zhuxiao(int m) {
arrayList.remove(m);
System.out.println("注销成功");
}
private void modify(int m) {
System.out.println("请决定修改密码为(7位)");
String mia;
while (true) {
mia= sc.next();
if (mia.length()!=7){
System.out.println("密码位数不对,请重新输入");
}else break;
}
account a=arrayList.get(m);
account element=new account(a.getUsername(),a.getSex(),mia,a.getSueridcard(),a.getLimitmoney(),a.getUserid(),a.getPrice());
arrayList.set(m,element);
System.out.println("修改密码成功");
}
private void zhuan(int m) {
if (arrayList.size()<2){
System.out.println("当前系统中账户少于两个,请先创建账户");
return;
}
int n;
System.out.println("请输入转账卡号");
String id= sc.next();
for (int i = 0; i < arrayList.size(); i++) {
if (id.equals(arrayList.get(i).getUserid())){
n=i;
System.out.println("请决定转账金额:");
double price= sc.nextDouble();
chu(m,price);
ru(n,price);
System.out.println("转账成功!");
break;
}else System.out.println("该系统无转账账户,请新建一个账户");break;
}
}
private void chu(int m,double price) {
double price1;
account a= arrayList.get(m);
while (true) {
price1=a.getPrice();
price1-=price;
if (price1<=0){
System.out.println("金额不够"+price+"请重新输入取款金额");price= sc.nextDouble();
}else break;
}
account element=new account(a.getUsername(),a.getSex(),a.getIdcard(),a.getSueridcard(),a.getLimitmoney(),a.getUserid(),price1);
arrayList.set(m,element);
System.out.println("取款成功!");
}
private void ru(int m,double price) {
price+=arrayList.get(m).getPrice();
account a=arrayList.get(m);
account element=new account(a.getUsername(),a.getSex(),a.getIdcard(),a.getSueridcard(),a.getLimitmoney(),a.getUserid(),price);
arrayList.set(m,element);
System.out.println("存款成功");
}
private void find(int m){
System.out.println(arrayList.get(m).getUsername()+"先生的账户信息如下:");
System.out.println("性别"+arrayList.get(m).getSex());
System.out.println("账户卡号"+arrayList.get(m).getIdcard());
System.out.println("账户密码"+arrayList.get(m).getUserid());
System.out.println("账户余额"+arrayList.get(m).getPrice());
System.out.println("单次取现额度"+arrayList.get(m).getLimitmoney());
}
}