学完类与对象的练习小作业,主要有三个类:
银行卡类包含银行卡的相关信息如卡号,密码,姓名,余额;
银行类中主要定义了一个银行卡数组,用来存储当前用户的银行卡信息,类方法包括用户操作界面,添加用户,用户检测,银行卡号/持卡人姓名存在检测并获取,存钱功能,取钱功能,转账功能等。
ATM类的作用就是调用银行类,并通过单例模式初始化一个对象,然后调用构造函数去实例化一个Bank对象,进行调用,功能包括用户欢迎界面,用户的登录,注册,注销,改密等功能。
1.银行卡类
package com.projectATM;
/**
* Description :
* Created by Resumebb
* Date :2020/10/12
*/
public class BankCard {
private int cardNum;
private String pwd;
private String userName;
private double money;
public BankCard(int cardNum, String pwd, String userName, double money) {
this.cardNum = cardNum;
this.pwd = pwd;
this.userName = userName;
this.money = money;
}
public int getCardNum() {
return cardNum;
}
public void setCardNum(int cardNum) {
this.cardNum = cardNum;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public void withDraw(double money){
this.money -= money;
}
public void saveMoney(double money){
this.money += money;
}
}
2.银行类
package com.projectATM;
import java.util.Scanner;
/**
* Description :
* Created by Resumebb
* Date :2020/10/11
*/
public class Bank {
private BankCard[] cards;
private int size = 0;
private static final int INITSIZE =10;
public Bank(){
this(INITSIZE);
}
public Bank(int initsize) {
cards = new BankCard[initsize];
BankCard bank1 = new BankCard(111, "123", "张三", 20000);
BankCard bank2 = new BankCard(222, "1231", "李四", 40000);
BankCard bank3 = new BankCard(333, "1232", "王五", 30000);
cards[0] = bank1;
cards[1] = bank2;
cards[2] = bank3;
size+=3;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public BankCard[] getBanks() {
return cards;
}
public void setCards(BankCard[] banks) {
this.cards = banks;
}
/**
* @description: 登录成功后进入界面
*/
public void start(){
String str="------------------------------------";
System.out.print(str+"\n");
System.out.print(
"1.取款"+" "+
"2.存款"+" "+
"3.转账"+" "+
"4.查询信息"+" "+
"5.修改密码"+" "+
"6.注销用户"+" "+
"0.返回上层"+" "
);
System.out.print("\n"+str+"\n");
}
/**
* @description: 用户注销
*/
public void deleteUser(Bank bank,int pos){
for(int i=0; i<size; i++){
if (cards[i] == cards[pos]) {
for(int j=i+1; j<size; j++) {
cards[j-1] = cards[j];
}
break;
}
}
size--;
}
public boolean addCard(){
Scanner in = new Scanner(System.in);
System.out.println("请输入注册卡号:");
int cardNum = Integer.valueOf(in.nextLine());
System.out.println("请输入注册密码:");
String pwd = in.nextLine();
System.out.println("请输入注册姓名:");
String userName = in.nextLine();
if(cardExist(cardNum) != -1) {
BankCard card = new BankCard(cardNum, pwd, userName, 100);
cards[size++] = card;
for (int i = 0; i < size; i++) {
System.out.println(i+" "+cards[i].getUserName()+" "+cards[i].getPwd());
}
return true;
}
else{
return false;
}
}
/**
* @description: 用户检测
*/
public int search(int cardNum, String pwd) {
for (int i = 0; i < size; i++) {
if(cards[i].getCardNum() == cardNum && cards[i].getPwd().equals(pwd)){
return i;
}
}
return -1;
}
/**
* @description: 卡号存在检测
*/
public int cardExist(int cardNum){
int i = 0;
for (i = 0; i < size; i++) {
if(cards[i].getCardNum() == cardNum){
return i;
}
}
return -1;
}
/**
* @description: 用户名存在检测
*/
public boolean nameExist(String name){
for (int i = 0; i < size; i++) {
if(cards[i].getUserName().equals(name)){
return false;
}
}
return true;
}
/**
* @description: 信息查询
*/
public void Inquire(Bank bank,int pos) throws Exception{
String str="-----------------------------";
System.out.print(str+"\n");
System.out.println("您的账户信息如下:");
System.out.print(
"账号:"+bank.cards[pos].getCardNum()+"\n"+
"姓名:"+bank.cards[pos].getUserName()+"\n"+
"余额:"+bank.cards[pos].getMoney()+"\n"+
str+"\n"
);
start();
}
/**
* @description: 存钱函数
*/
public void saveMoney(Bank bank,int pos) throws Exception{
int money = 0;
Scanner in = new Scanner(System.in);
int count = 0;
do {
System.out.println("请输入存款数目:");
money=in.nextInt();
if(money <= 0){
System.out.println("存款金额无效,请重新输入!");
continue;
}
bank.cards[pos].saveMoney(money);
System.out.println("存款成功,你的账户余额为:"+bank.cards[pos].getMoney());
start();
break;
} while (count<3);
}
/**
* @description: 取钱函数
*/
public void withDraw(Bank bank,int pos) throws Exception{
int money = 0;
Scanner in = new Scanner(System.in);
int count = 0;
do {
System.out.println("请输入取款数目:");
money=in.nextInt();
if(money > bank.cards[pos].getMoney()){
System.out.println("你只有"+bank.cards[pos].getMoney()+"元,请重新输入");
}
else {
bank.cards[pos].withDraw(money);
System.out.println("取款成功,账户可用余额:"+bank.cards[pos].getMoney());
start();
break;
}
} while (count<3);
}
/**
* @description: 转账函数
*/
public void transfer(Bank bank,int pos){
System.out.println("请输入要转账的账号:");
Scanner in = new Scanner(System.in);
int num = Integer.valueOf(in.nextLine());
int count = 0;
int pos1 = bank.cardExist(num);
if(pos1 != -1){
System.out.println("请输入要转账的金额:");
do {
double money = in.nextDouble();
if (money < 0 || money > bank.cards[pos].getMoney()) {
System.out.println("输入错误或余额不足!请重试");
count++;
continue;
}
else {
bank.cards[pos].withDraw(money);
bank.cards[pos1].saveMoney(money);
System.out.println("转账成功,当前余额:"+bank.cards[pos].getMoney());
start();
break;
}
}while (count < 3);
}
}
}
3.ATM机类
package com.projectATM;
import java.util.Scanner;
/**
* Description :
* Created by Resumebb
* Date :2020/10/11
*/
public class ATM {
private static ATM instance;
private Bank bank;
public ATM(){
bank = new Bank();
}
public static ATM getInstance(){
if(instance == null) {
instance = new ATM();
}
return instance;
}
/**
* @description: 用户操作选择界面
*/
public void welcome() throws Exception {
Scanner in = new Scanner(System.in);
int choice = 0;
do{
String str="------------------------------------";
System.out.print(str+"\n");
System.out.print(
"1.登录"+" "+
"2.注册"+" "+
"3.退出"+"\n"
);
System.out.print(str+"\n");
System.out.println("请选择选择:");
choice = in.nextInt();
switch (choice) {
case 1:
login();
break;
case 2:
register();
break;
case 3:
myExit();
break;
default:
break;
}
}while(true);
}
/**
* @description: 登录界面
*/
public void login() throws Exception{
int card;
String pwd;
int counter=0, pos = 0;
Scanner in = new Scanner(System.in);
do{
System.out.println("请输入你的卡号");
card = Integer.valueOf(in.nextLine());
System.out.println("请输入你的密码");
pwd = in.nextLine();
pos = bank.search(card,pwd);
if (pos == -1) {
System.out.println("你的卡号或密码有误");
counter++;
}
else {
bank.start();
Operate(bank,pos);
}
}while(counter < 3);
System.out.println("输入次数过多,请稍后重试或注册账户");
welcome();
}
/**
* @description: 注册界面
*/
public void register() throws Exception {
if(bank.addCard()) {
System.out.println("注册成功!送你100块");
welcome();
}
else{
System.out.println("账号已存在,请重新注册");
welcome();
}
}
/**
* @description: 修改密码界面
*/
public void modifyPwd(Bank bank,int pos) throws Exception {
Scanner in = new Scanner(System.in);
System.out.println("请输入新密码:");
String pwd = in.nextLine();
bank.getBanks()[pos].setPwd(pwd);
System.out.println("修改成功!,请重新登录");
login();
}
/**
* @description: 操作选择
*/
public void Operate(Bank bank,int pos) throws Exception {
int num;
Scanner in = new Scanner(System.in);
System.out.println("请选择选择:");
num=in.nextInt();
switch (num) {
case 1:
bank.withDraw( bank, pos);
Operate( bank, pos);
break;
case 2:
bank.saveMoney( bank, pos);
Operate( bank, pos);
break;
case 3:
bank.transfer( bank, pos);
Operate( bank, pos);
break;
case 4:
bank.Inquire( bank, pos);
Operate(bank , pos);
break;
case 5:
modifyPwd(bank, pos);
Operate( bank, pos);
break;
case 6:
bank.deleteUser( bank, pos);
Operate( bank, pos);
break;
case 0:
welcome();
default:
break;
}
in.close();
}
public void myExit() {
System.out.println("谢谢使用!");
System.exit(1);
}
}
4.测试用例
package com.projectATM;
/**
* Description :
* Created by Resumebb
* Date :2020/10/11
*/
public class testATM {
public static void main(String[] args) throws Exception {
ATM atm = ATM.getInstance();
atm.welcome();
}
}