模拟实现银行账户的注册,登录,查询,存款,取款,转账六种业务
1)银行账户Account类包括卡号(id),密码(password),余额(amount)三个属性
(2)银行业务包括注册,登录,查询,存款,取款,转账六种操作取款和转账操作中出现的错误提示信息都通过参数传入自定义异常类AccountException中
(3)主菜单—显示主菜单 【1—注册 2—登录 3—退出】
(4)注册—输入卡号和密码,余额初始化为0,查询卡号是否存在,如果不存在,则保存到账户集合中,并提示"注册成功",否则提示“卡号已存在”
(5)登录—输入卡号和密码,如果存在匹配的账户,则提示登录成功,并给出操作提示菜单 【1—查询 2—存款 3—取款 4—转账 5—返回主菜单】,否则提示“卡号不存在"或”密码错误“
(6)查询余额—显示当前账户的余额
(7)存款—提示用户输入存款金额,更新并显示账户的余额
(8)取款—提示用户输入取款金额,如果余额不足,则提示"余额不足,无法取款",否则更新并显示账户的余额
(9)转账—提示用户输入对方的卡号,如果卡号不存在,提示“目标账户卡号不存在”
提示用户输入转账金额,如果当前余额不足,提示"余额不足,无法转账"
否则更新并显示账户的余额
package Arraytsak;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;
public class BankBusiness {
static void registered(List<Account> list,String accountname,String accountpsd){
int amout = 0;
//获取账号
List<String> accountnames = new ArrayList<>();
ListIterator<Account> it = list.listIterator();
while (it.hasNext()){
String s = it.next().getId();
accountnames.add(s);
}
if (!accountnames.contains(accountname)) {
System.out.println("注册成功");
list.add(new Account(accountname, accountpsd, amout));
} else {
System.out.println("账户已存在");
}
}
static void login(List<Account> list,String accountname,String accountpsd){
int index = 0;
List<String> accountnames = new ArrayList<>();
ListIterator<Account> it = list.listIterator();
while (it.hasNext()){
String s1 = it.next().getId();
accountnames.add(s1);
}
if (!accountnames.contains(accountname)) {
System.out.println("账户不存在");
}
else {
int size = list.size();
for (int i = 0; i < size; i++) {
if(list.get(i).getId().equals(accountname)){
index = i;
break;
}
}
if(!(list.get(index).getPassword().equals(accountpsd))){
System.out.println("密码错误");
}
else {
System.out.println("登陆成功");
System.out.println("请选择你的业务");
System.out.println("提示信息:1——查询,——2——存款,3——取款,4——转账,5——返回主菜单");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
switch (x) {
case 1:
Inquire(list, index);
break;
case 2:
System.out.println("请输入您要存的金额:");
int addnum = sc.nextInt();
deposit(list, index, addnum);
break;
case 3:
System.out.println("请输入您的取款金额:");
int subnum = sc.nextInt();
try {
withdrawal(list, index, subnum);
}catch (AccountException e){
System.out.println(e.getMessage());
}
break;
case 4:
System.out.println("请输入转账账号:");
String id = sc.next();
System.out.println("输入转账金额:");
int num = sc.nextInt();
transfer(list, index,id,num);
break;
case 5:
return;
}
}
}
}
static void Inquire(List<Account> list,int index){
System.out.println("您的余额为:"+list.get(index).getAmount());
}
static void deposit(List<Account> list,int index,int addnum){
Account newaccount = new Account(list.get(index).getId(),list.get(index).getPassword(),list.get(index).getAmount()+addnum);
list.set(index,newaccount);
System.out.println("您现在的余额为:"+list.get(index).getAmount());
}
static void withdrawal(List<Account> list,int index,int subnum){
Account newaccount = new Account(list.get(index).getId(),list.get(index).getPassword(),list.get(index).getAmount()-subnum);
if(newaccount.getAmount()<0){
throw new AccountException("余额不足");
}
else {
list.set(index, newaccount);
System.out.println("您现在的余额为:" + list.get(index).getAmount());
}
}
static void transfer(List<Account> list,int index,String id,int num) {
List<String> accountnames = new ArrayList<>();
ListIterator<Account> it = list.listIterator();
while (it.hasNext()){
String s1 = it.next().getId();
accountnames.add(s1);
}
if (!accountnames.contains(id)) {
System.out.println("账户不存在");
}else {
System.out.println(id);
withdrawal(list, index, num);
int index1 = 0;
int size = list.size();
for (int i = 0; i < size; i++) {
if (list.get(i).getId().equals(id)) {
index1 = i;
deposit(list, index1, num);
break;
}
}
}
}
public static void main(String[] args) {
List<Account> list = new ArrayList<>();
Account account = new Account("1", "1", 0);
Account account1 = new Account("2", "2", 0);
list.add(account);
list.add(account1);
while (true) {
System.out.println("提示信息:1——注册,2——登录,3——退出");
System.out.println("请输入您所办理业务:");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
switch (x) {
case 1:
System.out.println("请输入账号:");
Scanner sc1 = new Scanner(System.in);
String accountname = sc1.nextLine();
System.out.println("请输入密码:");
String accountpsd = sc1.nextLine();
registered(list,accountname,accountpsd);
break;
case 2:
System.out.println("请输入账号");
Scanner sc2 = new Scanner(System.in);
String loginname = sc2.nextLine();
System.out.println("请输入密码");
String loginpsd = sc2.nextLine();
login(list, loginname, loginpsd);
break;
case 3:
System.out.println("已退出银行系统");
return;
}
}
}
}