Java面向对象_项目实战——ATM
1.首先创建账户类,保存账户相关数据
package com.itheima;
public class Account {
private String cardId;
private String userName;
private char sex;
private String passWord;
private double money;
private double limit;//限额
//实体类 方法内不定义构造器 即默认已有无参构造方法
//生成各成员变量的 get set 方法
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;
}
}
2.创建Test类启动方法(包含main()主函数)
package com.itheima;
public class Test {
public static void main(String[] args) {
ATM atm = new ATM();
atm.start();
}
}
3.编写ATM功能实现主体代码
package com.itheima;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATM {
//需要创建一个容器 保存一系列Account对象
private ArrayList<Account> accounts = new ArrayList<>();
//定义一个全类可用的扫描方法
private Scanner sc = new Scanner(System.in);
int command;//主界面命令选择
private Account loginUser;
/**定义一个启动页面 无返回值*/
public void start(){
while (true) {
System.out.println("==Welcome to the ATM System.==");
System.out.println("1.Login in.");
System.out.println("2.Account opening.");
System.out.println("Please enter the command:");
if(sc.hasNextInt()){
command = sc.nextInt();
}else{
System.out.println("*****************");
System.out.println("invalid character.");
System.out.println("*****************");
System.out.println(sc.nextLine());
//sc.nextLine();
continue;
}
System.out.println("-------------------------------");
//进入switch选择
switch(command){
case 1:
//登录账户
login();
break;
case 2:
//账户开户
createAccount();
break;
default:
System.out.println("*********************************");
System.out.println("Please enter the correct command.");
System.out.println("*********************************");
break;
}
}
}
/**用户开户*/
private void createAccount(){
//创建一个账户对象 存储账户信息
Account account = new Account();
Scanner sc = new Scanner(System.in);
System.out.println("****************");
System.out.println("Account opening.");
System.out.println("****************");
//输入账号名称
System.out.println("Please enter your account name:");
String name = sc.next();
account.setUserName(name);
//输入性别
while (true) {
System.out.println("Please enter account gender:");
char sex = sc.next().charAt(0);
if(sex == '男' || sex == '女'){
account.setSex(sex);
break;
}else{
System.out.println("Wrong gender!");
}
}
//输入密码
while (true) {
System.out.println("Please enter password:");
String passWord = sc.next();
System.out.println("Please enter the same password again:");
String okPassWord = sc.next();
if(okPassWord.equals(passWord)){
System.out.println("Password entered successfully!" );
account.setPassWord(passWord);
break;
}else{
System.out.println("The two passwords are different!");
}
}
//每次取现额度
while (true) {
System.out.println("Please enter your account withdrawal limit:");
double limit = sc.nextDouble();
if(limit < 0){
System.out.println("Wrong withdrawal limit!");
}else{
account.setLimit(limit);
break;
}
}
//调用方法 生成一个不重复的8位卡号
String cardId = createCardId();
account.setCardId(cardId);
//添加Account对象到ArrayList集合中去
accounts.add(account);
System.out.println("Account created successfully! " + account.getUserName() + ". CardId is: " + account.getCardId());
}
/**创建一个不重复的8位卡号 其中调用getAccountByCardId方法*/
private String createCardId(){
Random r = new Random();
while (true) {
String cardId = "";
for (int i = 0; i < 8; i++){
cardId += r.nextInt(10);
}
//调用getAccountByCardId方法判断是否重复
Account account = getAccountByCardId(cardId);
if(account == null){
return cardId;
}
}
}
/**根据卡号cardId查询 Account集合中是否有这个账号, 有则返回这个Account对象 无则返回null*/
private Account getAccountByCardId(String cardId){
//遍历集合accounts中所有的Account对象
for (int i = 0; i < accounts.size(); i++) {
Account account = accounts.get(i);
if(cardId.equals(account.getCardId())){
return account;
}
}
return null;
}
/**用户登录login()*/
private void login(){
System.out.println("***********");
System.out.println("==系统登录==");
System.out.println("***********");
//首先判断 Account集合中是否有已开户的账号 无则提示 应该先创建 然后直接return跳出登录方法
if(accounts.size() == 0){
System.out.println("No account has been opened!");
return;
}
while (true) {
//通过if判断 Account集合中有账号 判断卡号是否存在
System.out.println("Please enter your cardId:");
String cardId = sc.next();
Account account = getAccountByCardId(cardId);
//返回null 则代表输入的cardId在卡号库中没有匹配到
if(account == null){
System.out.println("cardId does not exist!");
}else{
//else返回这个Account对象
while (true) {
System.out.println("Please enter your password:");
String passWord = sc.next();
//passWord进行比对 account.getPassWord()
if(passWord.equals(account.getPassWord())){
loginUser = account;
//提示登录成功
System.out.println("Congratulations! Welcome " + account.getUserName() + ". Your cardId is " + account.getCardId());
//登录成功之后,应该进入用户界面
showUserInterface();
return;//用户界面在while(true)中循环 退出后再return退出登录页面
}else{
System.out.println("Wrong password! Please enter again!");
}
}
}
}
}
/**进入用户界面 进行功能选择*/
private void showUserInterface(){
System.out.println("== " + loginUser.getUserName() + ", Welcome to the User Interface!==");
while (true) {
System.out.println("--------------------------------------------------------------------");
System.out.println("1.Check account");
System.out.println("2.Deposit");
System.out.println("3.Withdrawal");
System.out.println("4.Transfer");
System.out.println("5.Password modification");
System.out.println("6.Exit");
System.out.println("7.Log out current account");
System.out.println("===Please select commands to enter different functional modules:===");
int command = sc.nextInt();
switch(command){
case 1:
//查询账户
showLoginAccount();
break;
case 2:
//存款
depositMoney();
break;
case 3:
//取款
withdrawMoney();
break;
case 4:
//转账
transferMoney();
break;
case 5:
//密码修改
updatePassWord();
return;
case 6:
//退出
System.out.println(loginUser.getUserName() + " Exit successfully!");
//return 退出当前用户界面
return;
case 7:
//注销当前账号
if(deleteAccount()){
return;
}
break;
default:
System.out.println("Wrong command! Please enter again!");
}
}
}
/**用户界面中的 1.查询账户*/
private void showLoginAccount(){
System.out.println("==============================================");
System.out.println("The current account information is as follows:");
System.out.println("CardId is " + loginUser.getCardId());
System.out.println("Username is " + loginUser.getUserName());
System.out.println("Sex is " + loginUser.getSex());
System.out.println("Balance is " + loginUser.getMoney());
System.out.println("Limit is " + loginUser.getLimit());
System.out.println("==============================================");
}
/**存款*/
private void depositMoney(){
System.out.println("==Deposit Money==");
System.out.println("Please enter the deposit amount:");
double money = sc.nextDouble();
if (money <= 0){
System.out.println("The deposit amount is invalid! Please enter a compliance amount!");
return;
}else{
loginUser.setMoney(loginUser.getMoney() + money);
System.out.println("Successfully deposit " + money + " . Current balance is " + loginUser.getMoney());
}
}
/**取款*/
private void withdrawMoney(){
System.out.println("==Withdraw Money==");
while (true) {
if(loginUser.getMoney() == 0){
System.out.println("Insufficient amount balance!");
}else{
System.out.println("Please enter the withdrawal amount:");
}
double money = sc.nextDouble();
if(money <= 0){
System.out.println("The withdrawal amount is invalid! Please enter a compliance amount!");
return;
}else{
//说明取款金额合规 下面需要判断是否超出限额 && 余额是否充足
if(money > loginUser.getLimit() || money > loginUser.getMoney()){
System.out.println("Invalid amount! " + "The remaining balance in your account is " + loginUser.getMoney() + " . And single withdrawal limit on your account is " + loginUser.getLimit());
}else{
loginUser.setMoney(loginUser.getMoney() - money);
System.out.println("Successfully withdraw " + money + " . Current balance is " + loginUser.getMoney());
break;
}
}
}
}
/**用户界面 4.转账功能*/
private void transferMoney(){
System.out.println("==Transfer Money==");
//排除只有一个账户 完成不了转账的情况
if(accounts.size() < 2){
System.out.println("The number of accounts contains only one!");
return;
}
//排除当前账户余额不足,完成不了转账的情况
if(loginUser.getMoney() == 0){
System.out.println("Insufficient amount balance!");
return;
}
//输入转账账户卡号
while (true) {
System.out.println("Please enter the transfer cardId:");
String cardId = sc.next();
Account transfer_account = getAccountByCardId(cardId);
//排除输入卡号不存在情况 此时返回输入对方卡号
if(transfer_account == null){
System.out.println("The account cardId does not exist!");
}else{
//此时卡号存在,要求判断对方姓氏
String name = transfer_account.getUserName();
System.out.println("Please enter the last name of the account: *" + name.substring(1));
String prename = sc.next();
if(name.startsWith(prename)){
//要求输入转账金额
while (true) {
System.out.println("Please enter the transfer amount:");
double money = sc.nextDouble();
//排除转账金额 大于 账户余额的情况
if(money > loginUser.getMoney()){
System.out.println("Insufficient amount balance! The amount balance is " + loginUser.getMoney());
}else if(money <= 0){
//排除错误金额情况
System.out.println("Invalid amount! Please enter the correct amount!");
}else{
//成功转账情况
//当前账户余额减少money
loginUser.setMoney(loginUser.getMoney() - money);
//转账账户余额增加money
transfer_account.setMoney(transfer_account.getMoney() + money);
//输出转账成功
System.out.println("Successfully transfer " + money + " to " + transfer_account.getUserName() +". Account balance is: " + loginUser.getMoney());
return;
}
}
}else{
System.out.println("Surname error!");
}
}
}
}
/**销户功能实现 deleteAccount方法*/
private boolean deleteAccount(){
System.out.println("==Account closing operation==");
System.out.println("Please confirm whether the account will be closed? y/n :");
String command = sc.next();
switch (command){
case "Y":
case "y":
if(loginUser.getMoney() == 0){
accounts.remove(loginUser);
System.out.println("The account is closed!");
return true;
}else{
System.out.println("The balance in the account is not zero!");
return false;
}
default:
System.out.println("Account retention!");
return false;
}
}
private void updatePassWord(){
System.out.println("==PassWord Modification==");
while (true) {
//检查输入当前密码是否正确
System.out.println("Please enter the password of your current account:");
String passWord = sc.next();
if(loginUser.getPassWord().equals(passWord)){
while (true) {
//输入新密码
System.out.println("Please enter your new password:");
String newPassWord = sc.next();
System.out.println("Please enter the same new password again:");
String okPassWord = sc.next();
//检查两次密码是否相同
if(newPassWord.equals(okPassWord)){
loginUser.setPassWord(okPassWord);
System.out.println("Password changed successfully!");
return;
}else{
System.out.println("The two passwords are different!");
}
}
}else{
System.out.println("Wrong password!");
System.out.println("Whether to exit password modification? y/n :");
String command = sc.next();
if(command.equals("y") || command.equals("Y")){
break;
}
}
}
}
}