目录
他人之得,不必视为自己之失
—— 24.7.15
① 定义一个账户类Account,需要包含(卡号、姓名、性别、密码、余额、取现额度)
② 定义一个ATM类,用来代替ATM系统,负责提供所有的业务需求,比如:展示ATM的系统欢迎页、开通账户、转账等
③ 定义一个测试类Test,负责对我们开发的ATM系统进行测试
- package ATM_Project;
-
- public class Account {
- private String cardID;
- private String userName;
- 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 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 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 getMoney() {
- return money;
- }
-
- public void setMoney(double money) {
- this.money = money;
- }
-
- public double getLimit() {
- return limit;
- }
-
- public void setLimit(double limit) {
- this.limit = limit;
- }
- }
在ATM中设计一个方法start(),方法中负责展示欢迎页面
- package ATM_Project;
-
- import java.util.ArrayList;
- import java.util.Scanner;
-
- public class ATM {
- private ArrayList
accounts = new ArrayList<>(); - private Scanner sc = new Scanner(System.in);
- // 启动ATM系统,展示欢迎界面
-
- public void start(){
- while(true){
- System.out.println("————————————————————————————————");
- System.out.println("——————————欢迎您进入ATM系统————————");
- System.out.println("———————————— 1.用户登录 ———————————");
- System.out.println("———————————— 2.用户开户 ———————————");
- System.out.println("——————————————— 请选择 —————————————");
-
- int choice = sc.nextInt();
- switch(choice){
- case 1:
- break;
- case 2:
- break;
- default:
- System.out.println("没有该操作,请重新输入");
- }
- }
- }
- }
就是新增一个账户,也就是往系统的账户集合中添加一个账户对象。
账户要求
① 用户信息包含:姓名、性别、密码、每次提现额度、卡号
② 注意:卡号由系统生成,要求是8位的数字组成的(且卡号不能重复)
- // 用户开户操作
- private void createAccount(){
- System.out.println("——————————系统开户操作——————————");
- Account account = new Account();
- System.out.println("请你输入您的姓名:");
- String name = sc.next();
- account.setUserName(name);
- while(true){
- System.out.println("请您输入您的性别:");
- char sex = sc.next().charAt(0);
- if (sex == '男'||sex == '女'){
- account.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)){
- System.out.println("密码输入成功");
- account.setPassWord(password);
- break;
- }else{
- System.out.println("密码不一致重新确认");
- }
- }
-
- System.out.println("请您输入您的取现额度:");
- double limit = sc.nextDouble();
- account.setLimit(limit);
-
- String newCardId = createCardId();
- account.setCardID(newCardId);
- accounts.add(account);
- System.out.println("恭喜您"+account.getUserName()+"开户成功,您的卡号是:"+account.getCardID());
- }
为新开户的账户生成一个新卡号:
新卡号要求是一个8位的数字,且不能与其他账户对象的卡号重复。
新卡号得到后,需要赋值给当前账户对象。
- // 返回一个八位数字的卡号,且这个卡号不呢与其他账户的卡号重复
- private String createCardId(){
- while (true) {
- String cardId = "";
- Random r = new Random();
- for (int i = 0; i < 8; i++) {
- int data = r.nextInt(10);
- cardId = cardId + data;
- }
- Account acc = getAccByCardId(cardId);
- if(acc == null){
- return cardId;
- }else{
- System.out.println("该卡号已被注册");
- }
- }
- }
- // 根据卡号查找账户对象
- private Account getAccByCardId(String cardId){
- for (int i = 0; i < accounts.size(); i++) {
- Account acc = accounts.get(i);
- if(acc.getCardID().equals(cardId)){
- return acc;
- }
- }
- return null;
- }
如果系统没有任何账户对象,则不允许登录。
让用户输入登录的卡号,先判断卡号是否正确,如果不正确要给出提示如果卡号正确,再让用户输入账户密码,如果密码不正确要给出提示如果密码也正确,则给出登录成功的提示。
- // 用户登录功能
- private void login() {
- System.out.println("——————————用户登录——————————");
- if (accounts.size()==0){
- System.out.println("系统中无账户,请先注册");
- return;
- }
- while (true) {
- System.out.println("请您选择要登录:1 还是退出:0");
- int choice = sc.nextInt();
- if (choice==1) {
- System.out.println("请输入你登陆的卡号:");
- String cardId = sc.next();
- Account acc = getAccByCardId(cardId);
- if(acc == null){
- System.out.println("卡号不存在,请重新输入:1 或 选择退出:0");
- int exit = sc.nextInt();
- if (exit==1){
- System.out.println("重新输入");
- }else{
- System.out.println("退出系统");
- return;
- }
- }else{
- while (true) {
- System.out.println("请您输入密码");
- String password = sc.next();
- if (acc.getPassWord().equals(password)){
- loginAccount=acc;
- System.out.println("恭喜您,登陆成功");
- userManage();
- return;
- }else{
- System.out.println("密码错误,请重新输入:1 或 选择退出:0");
- int exit = sc.nextInt();
- if (exit==1){
- System.out.println("重新输入");
- }else{
- System.out.println("退出系统");
- return;
- }
- }
- }
- }
- }else{
- System.out.println("您已退出");
- return;
- }
- }
- }
用户操作页设计、查询账户、退出账户功能分析
用户登录成功后,需要进入用户操作页。
查询就是直接展示当前登录成功的用户的账户信息,退出账户就是回到欢迎页面
- // 展示用户登陆后的操作页面
- private void userManage() {
- 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 choice = sc.nextInt();
- switch (choice){
- case 1:
- showLoginAcc();
- break;
- case 2:
- SaveMoney();
- break;
- case 3:
- GetMoney();
- break;
- case 4:
- transforMoney();
- break;
- case 5:
- updatePassword();
- return;
- case 6:
- System.out.println(loginAccount.getUserName()+"已退出");
- return;
- case 7:
- if (deleteAccount()) {
- return;
- }
- break;
- default:
- System.out.println("选择错误");
- }
- }
用户登录成功后,需要进入用户操作页。
查询就是直接展示当前登录成功的用户的账户信息。
退出账户就是回到欢迎页面,
- // 查询
- private void showLoginAcc() {
- System.out.println("您的帐户信息如下:");
- System.out.println("卡号:"+loginAccount.getCardID());
- System.out.println("户主:"+loginAccount.getUserName());
- System.out.println("性别:"+loginAccount.getSex());
- System.out.println("余额:"+loginAccount.getMoney());
- System.out.println("每次提现额度:"+loginAccount.getLimit());
- }
- }
就是用户为自己的账户存钱,存钱后更新账户的余额即可。
- // 存钱
- private void SaveMoney() {
- System.out.println("—————————存钱操作———————");
- System.out.println("请您输入存款金额");
- double money = sc.nextDouble();
- if (money<0){
- System.out.println("您输入有误");
- return;
- }
- loginAccount.setMoney(loginAccount.getMoney()+money);
- System.out.println("恭喜您存钱成功,存入:"+money+"余额为:"+loginAccount.getMoney());
- }
就是从自己的账户中取钱,取钱的要求
需要先判断账户的余额是否大于>=100元,如果够,让用户输入取款金额。
需要判断取款金额是否超过了当次限额,以及余额是否足够。
- // 取款
- private void GetMoney() {
- System.out.println("—————————取钱操作———————");
- if (loginAccount.getMoney()<100){
- System.out.println("卡上余额不足,无法从ATM系统取钱,请前往营业厅办理");
- return;
- }
- System.out.println("请您输入您的取款金额:");
- double money = sc.nextDouble();
- if (money<=loginAccount.getMoney()&&money<=loginAccount.getLimit()) {
- loginAccount.setMoney(loginAccount.getMoney()-money);
- System.out.println("恭喜您取款成功,取出:"+money+"余额为:"+loginAccount.getMoney());
- }else{
- System.out.println("账户余额不足或取钱超过当日限额,您的余额是:"+loginAccount.getMoney()+"您的限额是:"+loginAccount.getLimit());
- }
- }
把钱转给别人,转账前需要判断:
1.自己账户是否有钱,系统中是否有其他账户
2.接下来让用户输入对方卡号,判断对方账户是否存在,账户如果存在,还需要认证对方账户的户主姓氏。
- // 转账
- private void transforMoney() {
- System.out.println("———————————————用户转账——————————————");
- if (accounts.size()<2){
- System.out.println("当前系统无其他账户,无法转账");
- return;
- }
- if (loginAccount.getMoney()==0){
- System.out.println("当前账户余额不足");
- return;
- }
- while (true) {
- System.out.println("请您输入对方账户号码:");
- String other = sc.next();
- Account acc = getAccByCardId(other);
- if(acc == null){
- System.out.println("卡号输入错误,请重新输入:1 或退出:0");
- int exit = sc.nextInt();
- if (exit==0){
- break;
- }
- }else {
- System.out.println("请您输入对方账户姓氏:");
- String X = sc.next();
- if (acc.getUserName().startsWith(X)){
- while (true) {
- System.out.println("请您输入转账金额:");
- double Omoney = sc.nextDouble();
- if(Omoney>loginAccount.getMoney()){
- System.out.println("当前账户余额不足"+",当前账户余额:"+loginAccount.getMoney());
- }else{
- loginAccount.setMoney(loginAccount.getMoney()-Omoney);
- acc.setMoney(acc.getMoney()+Omoney);
- System.out.println("转账成功,您现在账户余额为:"+loginAccount.getMoney());
- return;
- }
- }
- }else{
- System.out.println("核验失败,请再次核验");
- }
- }
- }
- }
销户就是从系统中删除当前账户,销户的要求:
1.首先要询问用户是否确定要销户,如果不确定,则回到操作界面,
2.如果确定,要判断用户的账户中是否有钱,有则不允许销户,并回到操作界面
3.如果没钱,则完成销户,并回到欢迎页,
- // 销户
- private boolean deleteAccount() {
- System.out.println("—————————————销户操作—————————————");
- while (true) {
- System.out.println("您确认要销户吗?确认:1 不确认:0");
- int choice = sc.nextInt();
- if (choice==1){
- if (loginAccount.getMoney()!=0){
- System.out.println("您的账户中有钱,请先取出再销户");
- return false;
- }else{
- accounts.remove(loginAccount);
- System.out.println("您好,您的帐户已经成功销户");
- return true;
- }
- } else if (choice == 0) {
- System.out.println("退出销户");
- return false;
- }else{
- System.out.println("您点击错误,请重新选择");
- }
- }
- }
就是更改账户的密码,修改密码的要求:
1.需要先认证用户当前的密码
2.认证通过后,需要让用户输入两次新密码,
两次密码一样,则更新账户密码,并回到欢迎界面
- // 密码修改
- private void updatePassword() {
- System.out.println("——————————————账户密码修改操作————————————");
- System.out.println("请输入当前账户密码:");
- String password = sc.next();
- if (loginAccount.getPassWord().equals(password)){
- while (true) {
- System.out.println("请您输入新密码");
- String newPassword1 = sc.next();
-
- System.out.println("请您输入确认密码");
- String newPassword2 = sc.next();
-
- if (newPassword1.equals(newPassword2)){
- loginAccount.setPassWord(newPassword1);
- System.out.println("密码修改成功,新密码为:");
- return;
- }else {
- System.out.println("两次密码输入不一致,请重新输入");
- }
- }
- }else{
- System.out.println("两次输入密码不正确");
- }
- }
- package ATM_Project;
-
- public class Account {
- private String cardID;
- private String userName;
- 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 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 getMoney() {
- return money;
- }
-
- public void setMoney(double money) {
- this.money = money;
- }
-
- public double getLimit() {
- return limit;
- }
-
- public void setLimit(double limit) {
- this.limit = limit;
- }
- }
- package ATM_Project;
-
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.Scanner;
-
-
- public class ATM {
- private ArrayList
accounts = new ArrayList<>(); - private Scanner sc = new Scanner(System.in);
- public Account loginAccount;
-
- // 启动ATM系统,展示欢迎界面
- public void start(){
- while(true){
- System.out.println("————————————————————————————————");
- System.out.println("——————————欢迎您进入ATM系统————————");
- System.out.println("———————————— 1.用户登录 ———————————");
- System.out.println("———————————— 2.用户开户 ———————————");
- System.out.println("———————————— 3.退出系统 ———————————");
- System.out.println("——————————————— 请选择 —————————————");
-
- int choice = sc.nextInt();
- switch(choice){
- case 1:
- login();
- break;
- case 2:
- createAccount();
- break;
- case 3:
- System.out.println("您已退出系统");
- return;
- default:
- System.out.println("没有该操作,请重新输入");
- return;
- }
- }
- }
-
-
-
- // 用户开户操作
- private void createAccount(){
- System.out.println("——————————系统开户操作——————————");
- Account account = new Account();
- System.out.println("请你输入您的姓名:");
- String name = sc.next();
- account.setUserName(name);
- while(true){
- System.out.println("请您输入您的性别:");
- char sex = sc.next().charAt(0);
- if (sex == '男'||sex == '女'){
- account.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)){
- System.out.println("密码输入成功");
- account.setPassWord(password);
- break;
- }else{
- System.out.println("密码不一致重新确认");
- }
- }
-
- System.out.println("请您输入您的取现额度:");
- double limit = sc.nextDouble();
- account.setLimit(limit);
-
- String newCardId = createCardId();
- account.setCardID(newCardId);
- accounts.add(account);
- System.out.println("恭喜您"+account.getUserName()+"开户成功,您的卡号是:"+account.getCardID());
- }
-
-
- // 返回一个八位数字的卡号,且这个卡号不呢与其他账户的卡号重复
- private String createCardId(){
- while (true) {
- String cardId = "";
- Random r = new Random();
- for (int i = 0; i < 8; i++) {
- int data = r.nextInt(10);
- cardId = cardId + data;
- }
- Account acc = getAccByCardId(cardId);
- if(acc == null){
- return cardId;
- }else{
- System.out.println("该卡号已被注册");
- }
- }
- }
-
- // 根据卡号查找账户对象
- private Account getAccByCardId(String cardId){
- for (int i = 0; i < accounts.size(); i++) {
- Account acc = accounts.get(i);
- if(acc.getCardID().equals(cardId)){
- return acc;
- }
- }
- return null;
- }
-
-
-
- // 用户登录功能
- private void login() {
- System.out.println("——————————用户登录——————————");
- if (accounts.size()==0){
- System.out.println("系统中无账户,请先注册");
- return;
- }
- while (true) {
- System.out.println("请您选择要登录:1 还是退出:0");
- int choice = sc.nextInt();
- if (choice==1) {
- System.out.println("请输入你登陆的卡号:");
- String cardId = sc.next();
- Account acc = getAccByCardId(cardId);
- if(acc == null){
- System.out.println("卡号不存在,请重新输入:1 或 选择退出:0");
- int exit = sc.nextInt();
- if (exit==1){
- System.out.println("重新输入");
- }else{
- System.out.println("退出系统");
- return;
- }
- }else{
- while (true) {
- System.out.println("请您输入密码");
- String password = sc.next();
- if (acc.getPassWord().equals(password)){
- loginAccount=acc;
- System.out.println("恭喜您,登陆成功");
- userManage();
- return;
- }else{
- System.out.println("密码错误,请重新输入:1 或 选择退出:0");
- int exit = sc.nextInt();
- if (exit==1){
- System.out.println("重新输入");
- }else{
- System.out.println("退出系统");
- return;
- }
- }
- }
- }
- }else{
- System.out.println("您已退出");
- return;
- }
- }
- }
-
-
-
- // 展示用户登陆后的操作页面
- private void userManage() {
- 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 choice = sc.nextInt();
- switch (choice){
- case 1:
- showLoginAcc();
- break;
- case 2:
- SaveMoney();
- break;
- case 3:
- GetMoney();
- break;
- case 4:
- transforMoney();
- break;
- case 5:
- updatePassword();
- return;
- case 6:
- System.out.println(loginAccount.getUserName()+"已退出");
- return;
- case 7:
- if (deleteAccount()) {
- return;
- }
- break;
- default:
- System.out.println("选择错误");
- }
- }
-
-
-
- // 密码修改
- private void updatePassword() {
- System.out.println("——————————————账户密码修改操作————————————");
- System.out.println("请输入当前账户密码:");
- String password = sc.next();
- if (loginAccount.getPassWord().equals(password)){
- while (true) {
- System.out.println("请您输入新密码");
- String newPassword1 = sc.next();
-
- System.out.println("请您输入确认密码");
- String newPassword2 = sc.next();
-
- if (newPassword1.equals(newPassword2)){
- loginAccount.setPassWord(newPassword1);
- System.out.println("密码修改成功,新密码为:");
- return;
- }else {
- System.out.println("两次密码输入不一致,请重新输入");
- }
- }
- }else{
- System.out.println("两次输入密码不正确");
- }
- }
-
-
-
- // 销户
- private boolean deleteAccount() {
- System.out.println("—————————————销户操作—————————————");
- while (true) {
- System.out.println("您确认要销户吗?确认:1 不确认:0");
- int choice = sc.nextInt();
- if (choice==1){
- if (loginAccount.getMoney()!=0){
- System.out.println("您的账户中有钱,请先取出再销户");
- return false;
- }else{
- accounts.remove(loginAccount);
- System.out.println("您好,您的帐户已经成功销户");
- return true;
- }
- } else if (choice == 0) {
- System.out.println("退出销户");
- return false;
- }else{
- System.out.println("您点击错误,请重新选择");
- }
- }
- }
-
-
-
- // 转账
- private void transforMoney() {
- System.out.println("———————————————用户转账——————————————");
- if (accounts.size()<2){
- System.out.println("当前系统无其他账户,无法转账");
- return;
- }
- if (loginAccount.getMoney()==0){
- System.out.println("当前账户余额不足");
- return;
- }
- while (true) {
- System.out.println("请您输入对方账户号码:");
- String other = sc.next();
- Account acc = getAccByCardId(other);
- if(acc == null){
- System.out.println("卡号输入错误,请重新输入:1 或退出:0");
- int exit = sc.nextInt();
- if (exit==0){
- break;
- }
- }else {
- System.out.println("请您输入对方账户姓氏:");
- String X = sc.next();
- if (acc.getUserName().startsWith(X)){
- while (true) {
- System.out.println("请您输入转账金额:");
- double Omoney = sc.nextDouble();
- if(Omoney>loginAccount.getMoney()){
- System.out.println("当前账户余额不足"+",当前账户余额:"+loginAccount.getMoney());
- }else{
- loginAccount.setMoney(loginAccount.getMoney()-Omoney);
- acc.setMoney(acc.getMoney()+Omoney);
- System.out.println("转账成功,您现在账户余额为:"+loginAccount.getMoney());
- return;
- }
- }
- }else{
- System.out.println("核验失败,请再次核验");
- }
- }
- }
- }
-
-
- // 取款
- private void GetMoney() {
- System.out.println("—————————取钱操作———————");
- if (loginAccount.getMoney()<100){
- System.out.println("卡上余额不足,无法从ATM系统取钱,请前往营业厅办理");
- return;
- }
- System.out.println("请您输入您的取款金额:");
- double money = sc.nextDouble();
- if (money<=loginAccount.getMoney()&&money<=loginAccount.getLimit()) {
- loginAccount.setMoney(loginAccount.getMoney()-money);
- System.out.println("恭喜您取款成功,取出:"+money+"余额为:"+loginAccount.getMoney());
- }else{
- System.out.println("账户余额不足或取钱超过当日限额,您的余额是:"+loginAccount.getMoney()+"您的限额是:"+loginAccount.getLimit());
- }
- }
-
-
- // 存钱
- private void SaveMoney() {
- System.out.println("—————————存钱操作———————");
- System.out.println("请您输入存款金额");
- double money = sc.nextDouble();
- if (money<0){
- System.out.println("您输入有误");
- return;
- }
- loginAccount.setMoney(loginAccount.getMoney()+money);
- System.out.println("恭喜您存钱成功,存入:"+money+"余额为:"+loginAccount.getMoney());
- }
-
-
- // 查询
- private void showLoginAcc() {
- System.out.println("您的帐户信息如下:");
- System.out.println("卡号:"+loginAccount.getCardID());
- System.out.println("户主:"+loginAccount.getUserName());
- System.out.println("性别:"+loginAccount.getSex());
- System.out.println("余额:"+loginAccount.getMoney());
- System.out.println("每次提现额度:"+loginAccount.getLimit());
- }
- }
- public class Test {
- public static void main(String[] args) {
- ATM atm = new ATM();
- atm.start();
- }
- }
注:转账金额不受取现额度控制



