"修仙版"ATM指南
一、问题描述
烛九幽穿越到了九州修仙界,加入了人族第一大势力:仙盟,他发现宗门的任务系统有点眼熟,就是披着仙侠皮的ATM系统。下面我们就来实现仙侠版的ATM系统。
二、基本流程
定义property类
注册账户
账户登录
三、具体步骤
1.定义属性类
/**
* 定义属性类
*/
class property{
private String name;//姓名
private String ID;//身份号
private String password;//密码
private int age;//年龄
private String race;//种族
private String quality;//修为
private int contribution;//贡献点
public property(String name, String ID, String password, int age, String race, String quality, int contribution) {
this.name = name;
this.ID = ID;
this.password = password;
this.age = age;
this.race = race;
this.quality = quality;
this.contribution = contribution;
}
public property() {
}
public String getQuality() {
return quality;
}
public void setQuality(String quality) {
this.quality = quality;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getRace() {
return race;
}
public void setRace(String race) {
this.race = race;
}
public int getContribution() {
return contribution;
}
public void setContribution(int contribution) {
this.contribution = contribution;
}
}
2.菜单栏
private static void menu() {
System.out.println("-----------------欢迎道友来到仙盟---------------------");
System.out.println("请您选择操作:");
System.out.println("1.注册账户");
System.out.println("2.登录系统");
System.out.println("3.退出");
}
3.账户注册
(1)根据ID获取账户
/**
* 通过ID获得账户信息
*
* @param proArrayList 集合
* @param ID
* @return 找到的账户信息(如果未找到,返回null)
*/
private static property getproByID(ArrayList<property> proArrayList, String ID) {
for (int i = 0; i < proArrayList.size(); i++) {
if(ID.equals(proArrayList.get(i).getID()))
return proArrayList.get(i);
}
return null;
}
(2)获取注册ID
/**
* 获取不重复的ID
* @return ID
*/
private static String getPropertyID(ArrayList<property> proArrayList) {
Random ran = new Random();
while(true){
StringBuilder sb=new StringBuilder();
for (int i = 0; i < 9; i++) {
sb.append(ran.nextInt(10));
}
String ID = sb.toString();
property pro = getproByID(proArrayList,ID);
if(pro==null)
return ID;
}
}
(3)注册账户
/**
* 进入注册/登录界面
* @param sca 扫描器
* @param proArrayList 集合
*/
private static void register(Scanner sca, ArrayList<property> proArrayList) {
System.out.println("-----------------注册---------------------");
property pro = new property();
System.out.println("请您输入您的姓名:");
String name = sca.next();
pro.setName(name);
System.out.println("请输入您的年龄:");
int age = sca.nextInt();
pro.setAge(age);
System.out.println("请您输入您的种族:");
String race = sca.next();
pro.setRace(race);
System.out.println("请您输入您的修为:");
String quality = sca.next();
pro.setQuality(quality);
String ID;
ID = getPropertyID(proArrayList);
pro.setID(ID);
System.out.println("请您输入登录密码:");
String password = sca.next();
pro.setPassword(password);
System.out.println("尊敬的【"+name+"】道友,您的登录ID是:"+ID);
proArrayList.add(pro);//添加到集合中
}
4.账户登录
/**
* 登录系统
* @param sca 扫描器
* @param proArrayList 集合
*/
private static void longin(Scanner sca, ArrayList<property> proArrayList) {
while (true){
System.out.println("请您输入您的登录ID:");
String ID = sca.next();
property pro = getproByID(proArrayList,ID);
if(pro == null)
{
System.out.println("系统中未查询到该用户,请您检查ID输入是否正确,请重新输入!");
}
else{
while (true){
System.out.println("请您输入登录密码:");
String password = sca.next();
if(pro.getPassword().equals(password)){
System.out.println("密码输入正确!");
System.out.println("登录成功!");
users_function(sca,pro,proArrayList);
break;//登录成功,退出内循环
}
else {
System.out.println("密码输入错误,请您重新输入!");
}
}
}
break;
}
}
5.登录界面
/**
* 用户操作具体实现
* @param sca 扫描器
* @param pro 登录的账户
* @param proArrayList 集合
*/
private static void users_function(Scanner sca, property pro, ArrayList<property> proArrayList) {
boolean flag = false;
boolean flag2 = false;
while (true) {
function();
System.out.println("请您输入选择的操作命令:");
int input = sca.nextInt();
switch (input) {
case 1:
//查询
check_property(pro);
break;
case 2:
//交易
deal_property(sca,pro,proArrayList);
break;
case 3:
//退出
flag = quit_porperty();
break;
case 4:
//注销
flag2 = logout_porperty(sca,pro,proArrayList);
break;
}
if (flag||flag2)
break;
}
}
6.操作界面
/**
* 用户操作界面
*/
private static void function() {
System.out.println("------------欢迎来到用户操作界面------------");
System.out.println("请您输入选择:");
System.out.println("1.查询");
System.out.println("2.交易");
System.out.println("3.退出");
System.out.println("4.注销");
}
(1)查询
/**
* 展示属性
* @param pro 登录的该账户
*/
private static void check_property(property pro) {
System.out.println("----------------------属性------------------------");
System.out.print("【");
System.out.println("姓名:"+pro.getName());
System.out.println("ID:"+pro.getID());
System.out.println("年龄:"+pro.getAge());
System.out.println("修为:"+pro.getQuality());
System.out.println("种族:"+pro.getRace());
System.out.print("贡献点:"+pro.getContribution());
System.out.println("】");
}
(2)交易界面
/**
* 交易系统
* @param sca 扫描器
* @param pro 登录的账户
* @param proArrayList 集合
*/
private static void deal_property(Scanner sca, property pro, ArrayList<property> proArrayList) {
boolean flag = false;
while(true){
System.out.println("-------------------交易系统---------------------");
System.out.println("1.存款");
System.out.println("2.取款");
System.out.println("3.转账");
System.out.println("4.退出");
System.out.println("请您输入您的选择:");
int input = sca.nextInt();
switch (input){
case 1:
//存款
add_contribution(sca,pro);
break;
case 2:
//取款
sub_contribution(sca,pro);
break;
case 3:
//转账
transfer_contribution(sca,pro,proArrayList);
break;
case 4:
//退出
flag = quit_deal();
break;
}
if(flag)
break;
}
}
(1)存款
/**
* 存款
* @param sca 扫描器
* @param pro 登录的账户
*/
private static void add_contribution(Scanner sca, property pro) {
System.out.println("-----------------存款----------------------");
System.out.println("请您输入待存的贡献点数:");
int contr = sca.nextInt();
pro.setContribution(pro.getContribution()+contr);
System.out.println("您已成功存取 "+contr+" 点贡献点!");
System.out.println("您当前共有贡献点:"+pro.getContribution());
}
(2)取款
/**
* 取出贡献点
* @param sca 扫描器
* @param pro 登录的账户
*/
private static void sub_contribution(Scanner sca, property pro) {
System.out.println("---------------------取款----------------------");
while (true){
System.out.println("请您输入待取点数:");
int contr = sca.nextInt();
if(contr>=0){
if(contr<=pro.getContribution()) {
pro.setContribution(pro.getContribution()-contr);
System.out.println("您已成功取出"+contr+"点!");
System.out.println("您当前共有贡献点:"+pro.getContribution());
break;
}
System.out.println("您的余额不足!");
System.out.println("当前余额为:"+pro.getContribution());
System.out.println("请您重新输入!");
}
else
{
System.out.println("您输入的点数非法,请您重新输入!");
}
}
}
(3)转账
(a)得到屏蔽姓名
/**
* 得到屏蔽姓名
* @param other_pro 对方账户
* @return 屏蔽姓名
*/
private static String getotherName(property other_pro) {
String name = other_pro.getName();
int flag = name.length();
String other_name ="";
if(flag==2)
return other_name = name.replace(name.substring(1),"*");
if(flag==3)
return other_name=name.replace(name.substring(1),"**");
else return other_name=name.replace(name.substring(2),"**");
}
(b)转账
/**
* 转账
* @param sca 扫描器
* @param pro 登录的账户
* @param proArrayList 集合
*/
private static void transfer_contribution(Scanner sca, property pro, ArrayList<property> proArrayList) {
System.out.println("--------------------转账-----------------------");
if(proArrayList.size()<2){
System.out.println("当前系统中只有一个账户,无法转账!");
return;
}
while (true){
System.out.println("请您输入对方账户的ID:");
String other_ID = sca.next();
if(other_ID.equals(pro.getID()))
System.out.println("您输入的是自己的ID,无法转账,请您重新输入!");
property other_pro = new property();
other_pro = getproByID(proArrayList,other_ID);
if(other_pro==null){
System.out.println("系统中查无此人,请您重新输入!");
}
else{
while (true){
System.out.println("请您输入要转账的贡献点数:");
int contr = sca.nextInt();
if(contr>=0){
if(contr<=pro.getContribution()){
while(true){
String other_name = getotherName(other_pro);
System.out.println("请您输入【"+other_name+"】的完整姓名以验证身份!");
String input_name = sca.next();
if(other_pro.getName().equals(input_name)){
other_pro.setContribution(other_pro.getContribution()+contr);
pro.setContribution(pro.getContribution()-contr);
System.out.println("您已成功转账 "+contr+" 点贡献点!");
System.out.println("您当前还有 "+pro.getContribution()+" 点贡献点!");
break;
}
else{
System.out.println("您输入的姓名有误,请您重新输入!");
}
}
break;
}
else{
System.out.println("您当前贡献点不足!");
System.out.println("当前您的贡献点为:"+pro.getContribution());
System.out.println("请您重新输入!");
}
}
else {
System.out.println("您输入的点数非法,请您重新输入!");
}
}
}
break;
}
}
(4)退出交易
/**
* 退出交易
* @return true
*/
private static boolean quit_deal() {
System.out.println("------------------退出交易------------------");
System.out.println("您已退出交易系统!");
return true;
}
(3)退出登录
/**
* 退出登录
*/
private static boolean quit_porperty() {
System.out.println("-------------------退出登录--------------------");
System.out.println("您已退出登录!");
return true;
}
(4)注销账户
/**
* 注销账户
* @param sca 扫描器
* @param pro 登录的账户
* @param proArrayList 集合
* @return true
*/
private static boolean logout_porperty(Scanner sca, property pro, ArrayList<property> proArrayList) {
System.out.println("----------------注销账户-------------------");
System.out.println("请您确认是否要注销账户? 是/否");
System.out.println("请你输入选择:");
String input = sca.next();
if(input.equals("是")){
proArrayList.remove(pro);
System.out.println("注销账户成功!");
return true;
}
else
return false;
}
四、代码实现
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class tianwang {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
ArrayList<property> proArrayList = new ArrayList<>();
while (true){
menu();
while(true){
System.out.println("请您输入选择命令:");
int input = sca.nextInt();
switch(input){
case 1:
register(sca,proArrayList);
break;
case 2:
longin(sca,proArrayList);
break;
case 3:
System.out.println("仙盟感谢道友的光临!");
return;
default:
System.out.println("输入错误,请您重新输入!");
}
break;
}
}
}
/**
* 登录系统
* @param sca 扫描器
* @param proArrayList 集合
*/
private static void longin(Scanner sca, ArrayList<property> proArrayList) {
while (true){
System.out.println("请您输入您的登录ID:");
String ID = sca.next();
property pro = getproByID(proArrayList,ID);
if(pro == null)
{
System.out.println("系统中未查询到该用户,请您检查ID输入是否正确,请重新输入!");
}
else{
while (true){
System.out.println("请您输入登录密码:");
String password = sca.next();
if(pro.getPassword().equals(password)){
System.out.println("密码输入正确!");
System.out.println("登录成功!");
users_function(sca,pro,proArrayList);
break;//登录成功,退出内循环
}
else {
System.out.println("密码输入错误,请您重新输入!");
}
}
}
break;
}
}
/**
* 用户操作具体实现
* @param sca 扫描器
* @param pro 登录的账户
* @param proArrayList 集合
*/
private static void users_function(Scanner sca, property pro, ArrayList<property> proArrayList) {
boolean flag = false;
boolean flag2 = false;
while (true) {
function();
System.out.println("请您输入选择的操作命令:");
int input = sca.nextInt();
switch (input) {
case 1:
//查询
check_property(pro);
break;
case 2:
//交易
deal_property(sca,pro,proArrayList);
break;
case 3:
//退出
flag = quit_porperty();
break;
case 4:
//注销
flag2 = logout_porperty(sca,pro,proArrayList);
break;
}
if (flag||flag2)
break;
}
}
/**
* 注销账户
* @param sca 扫描器
* @param pro 登录的账户
* @param proArrayList 集合
* @return true
*/
private static boolean logout_porperty(Scanner sca, property pro, ArrayList<property> proArrayList) {
System.out.println("----------------注销账户-------------------");
System.out.println("请您确认是否要注销账户? 是/否");
System.out.println("请你输入选择:");
String input = sca.next();
if(input.equals("是")){
proArrayList.remove(pro);
System.out.println("注销账户成功!");
return true;
}
else
return false;
}
/**
* 交易系统
* @param sca 扫描器
* @param pro 登录的账户
* @param proArrayList 集合
*/
private static void deal_property(Scanner sca, property pro, ArrayList<property> proArrayList) {
boolean flag = false;
while(true){
System.out.println("-------------------交易系统---------------------");
System.out.println("1.存款");
System.out.println("2.取款");
System.out.println("3.转账");
System.out.println("4.退出");
System.out.println("请您输入您的选择:");
int input = sca.nextInt();
switch (input){
case 1:
//存款
add_contribution(sca,pro);
break;
case 2:
//取款
sub_contribution(sca,pro);
break;
case 3:
//转账
transfer_contribution(sca,pro,proArrayList);
break;
case 4:
//退出
flag = quit_deal();
break;
}
if(flag)
break;
}
}
/**
* 退出交易
* @return true
*/
private static boolean quit_deal() {
System.out.println("------------------退出交易------------------");
System.out.println("您已退出交易系统!");
return true;
}
/**
* 转账
* @param sca 扫描器
* @param pro 登录的账户
* @param proArrayList 集合
*/
private static void transfer_contribution(Scanner sca, property pro, ArrayList<property> proArrayList) {
System.out.println("--------------------转账-----------------------");
if(proArrayList.size()<2){
System.out.println("当前系统中只有一个账户,无法转账!");
return;
}
while (true){
System.out.println("请您输入对方账户的ID:");
String other_ID = sca.next();
if(other_ID.equals(pro.getID()))
System.out.println("您输入的是自己的ID,无法转账,请您重新输入!");
property other_pro = new property();
other_pro = getproByID(proArrayList,other_ID);
if(other_pro==null){
System.out.println("系统中查无此人,请您重新输入!");
}
else{
while (true){
System.out.println("请您输入要转账的贡献点数:");
int contr = sca.nextInt();
if(contr>=0){
if(contr<=pro.getContribution()){
while(true){
String other_name = getotherName(other_pro);
System.out.println("请您输入【"+other_name+"】的完整姓名以验证身份!");
String input_name = sca.next();
if(other_pro.getName().equals(input_name)){
other_pro.setContribution(other_pro.getContribution()+contr);
pro.setContribution(pro.getContribution()-contr);
System.out.println("您已成功转账 "+contr+" 点贡献点!");
System.out.println("您当前还有 "+pro.getContribution()+" 点贡献点!");
break;
}
else{
System.out.println("您输入的姓名有误,请您重新输入!");
}
}
break;
}
else{
System.out.println("您当前贡献点不足!");
System.out.println("当前您的贡献点为:"+pro.getContribution());
System.out.println("请您重新输入!");
}
}
else {
System.out.println("您输入的点数非法,请您重新输入!");
}
}
}
break;
}
}
/**
* 得到屏蔽姓名
* @param other_pro 对方账户
* @return 屏蔽姓名
*/
private static String getotherName(property other_pro) {
String name = other_pro.getName();
int flag = name.length();
String other_name ="";
if(flag==2)
return other_name = name.replace(name.substring(1),"*");
if(flag==3)
return other_name=name.replace(name.substring(1),"**");
else return other_name=name.replace(name.substring(2),"**");
}
/**
* 取出贡献点
* @param sca 扫描器
* @param pro 登录的账户
*/
private static void sub_contribution(Scanner sca, property pro) {
System.out.println("---------------------取款----------------------");
while (true){
System.out.println("请您输入待取点数:");
int contr = sca.nextInt();
if(contr>=0){
if(contr<=pro.getContribution()) {
pro.setContribution(pro.getContribution()-contr);
System.out.println("您已成功取出"+contr+"点!");
System.out.println("您当前共有贡献点:"+pro.getContribution());
break;
}
System.out.println("您的余额不足!");
System.out.println("当前余额为:"+pro.getContribution());
System.out.println("请您重新输入!");
}
else
{
System.out.println("您输入的点数非法,请您重新输入!");
}
}
}
/**
* 存款
* @param sca 扫描器
* @param pro 登录的账户
*/
private static void add_contribution(Scanner sca, property pro) {
System.out.println("-----------------存款----------------------");
System.out.println("请您输入待存的贡献点数:");
int contr = sca.nextInt();
pro.setContribution(pro.getContribution()+contr);
System.out.println("您已成功存取 "+contr+" 点贡献点!");
System.out.println("您当前共有贡献点:"+pro.getContribution());
}
/**
* 退出登录
*/
private static boolean quit_porperty() {
System.out.println("-------------------退出登录--------------------");
System.out.println("您已退出登录!");
return true;
}
/**
* 展示属性
* @param pro 登录的该账户
*/
private static void check_property(property pro) {
System.out.println("----------------------属性------------------------");
System.out.print("【");
System.out.println("姓名:"+pro.getName());
System.out.println("ID:"+pro.getID());
System.out.println("年龄:"+pro.getAge());
System.out.println("修为:"+pro.getQuality());
System.out.println("种族:"+pro.getRace());
System.out.print("贡献点:"+pro.getContribution());
System.out.println("】");
}
/**
* 用户操作界面
*/
private static void function() {
System.out.println("------------欢迎来到用户操作界面------------");
System.out.println("请您输入选择:");
System.out.println("1.查询");
System.out.println("2.交易");
System.out.println("3.退出");
System.out.println("4.注销");
}
/**
* 进入注册/登录界面
* @param sca 扫描器
* @param proArrayList 集合
*/
private static void register(Scanner sca, ArrayList<property> proArrayList) {
System.out.println("-----------------注册---------------------");
property pro = new property();
System.out.println("请您输入您的姓名:");
String name = sca.next();
pro.setName(name);
System.out.println("请输入您的年龄:");
int age = sca.nextInt();
pro.setAge(age);
System.out.println("请您输入您的种族:");
String race = sca.next();
pro.setRace(race);
System.out.println("请您输入您的修为:");
String quality = sca.next();
pro.setQuality(quality);
String ID;
ID = getPropertyID(proArrayList);
pro.setID(ID);
System.out.println("请您输入登录密码:");
String password = sca.next();
pro.setPassword(password);
System.out.println("尊敬的【"+name+"】道友,您的登录ID是:"+ID);
proArrayList.add(pro);//添加到集合中
}
/**
* 获取不重复的ID
* @return ID
*/
private static String getPropertyID(ArrayList<property> proArrayList) {
Random ran = new Random();
while(true){
StringBuilder sb=new StringBuilder();
for (int i = 0; i < 9; i++) {
sb.append(ran.nextInt(10));
}
String ID = sb.toString();
property pro = getproByID(proArrayList,ID);
if(pro==null)
return ID;
}
}
/**
* 通过ID获得账户信息
*
* @param proArrayList 集合
* @param ID
* @return 找到的账户信息(如果未找到,返回null)
*/
private static property getproByID(ArrayList<property> proArrayList, String ID) {
for (int i = 0; i < proArrayList.size(); i++) {
if(ID.equals(proArrayList.get(i).getID()))
return proArrayList.get(i);
}
return null;
}
private static void menu() {
System.out.println("-----------------欢迎道友来到仙盟---------------------");
System.out.println("请您选择操作:");
System.out.println("1.注册账户");
System.out.println("2.登录系统");
System.out.println("3.退出");
}
}
/**
* 定义属性类
*/
class property{
private String name;//姓名
private String ID;//身份号
private String password;//密码
private int age;//年龄
private String race;//种族
private String quality;//修为
private int contribution;//贡献点
public property(String name, String ID, String password, int age, String race, String quality, int contribution) {
this.name = name;
this.ID = ID;
this.password = password;
this.age = age;
this.race = race;
this.quality = quality;
this.contribution = contribution;
}
public property() {
}
public String getQuality() {
return quality;
}
public void setQuality(String quality) {
this.quality = quality;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getRace() {
return race;
}
public void setRace(String race) {
this.race = race;
}
public int getContribution() {
return contribution;
}
public void setContribution(int contribution) {
this.contribution = contribution;
}
}
五、效果展示
1.账户注册
2.账户登录
(1)查询属性
(2)交易界面
(1)存款
(2)取款
(3)转账
(4)退出交易系统
(3)退出登录
(4)注销账户
此时我们再登录韩立的账户,发现已经不能登录了!
(5)登录【萧炎】的账户
刚才转账的1000贡献点已到账!