要求编写程序模拟银行账户的存、取款操作。按要求完成以下步骤:
步骤1:编写程序Account.java,其中定义银行账户类Account。该类中有账号、姓名、存款余额等数据域,余额默认是0;有存款、取款、获取当前余额等方法。其中账号为长度为12位数字的字符串,姓名为字符串,存款余额为double。
步骤2:编写名为CreditAccount类的信用卡账户类。该类继承自Account类,增加一个透支限额(double)数据域,透支限额默认为1000。同时该类账户取款时允许透支,但不能超过透支限额。
步骤3:编写名为SavingAccount的储蓄账户类SavingAccount。该类继承自Account类。该类账户取款时不允许透支。
步骤4:编写名为Bank的模拟银行类,其中可以存储多个类型可能是信用卡账户或储蓄账户的对象(可以用数组或ArrayList实现)。该类包含以下方法:
<1>开户:即增加一个新的账户,注意:不允许两个账户的账号相同
<2>销户:即删除一个已有的账户
<3>查询账户:根据一个账号,查询有无该账号的账户
<4>统计目前银行的存款总余额的方法。
<5>统计目前银行的信用卡账户总透支金额的方法。
<6>统计目前总账户数。
<7>统计目前信用卡账户数
<8>统计目前储蓄卡账户数
步骤5:编写客户端类Client.java完成以下功能:
<1> 编写一个静态方法,创建一个银行对象,并随机生成10个账号从1000 0000 0000 0000到1000 0000 0000 0009,类型不同的账户。
<2> main方法中模拟几次开户操作。
<3> main方法中模拟几次销户操作。
<4> 模拟几个对指定账号的存款和取款操作。
<5> 输出银行的总账户数、总余额、总透支数、各类具体账户数。
package bank;
public class Account {
protected String AccountNum;
protected String name="null";
protected double balance=0;
public Account() {
}
public Account(String accountNum, String name, double balance) {
AccountNum = accountNum;
this.name = name;
this.balance = balance;
}
public String getAccountNum() {
return AccountNum;
}
public void setAccountNum(String accountNum) {
AccountNum = accountNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setBalance(double balance) {
this.balance = balance;
}
//存款
public void deposit(double money){
balance=balance+money;
}
//取款
public void withdrawal(double money){
balance=balance-money;
}
//获取当前余额
public double getBalance() {
return balance;
}
}
package bank;
//信用卡账户类(取款时允许透支,但不能超过透支限额)
public class CreditAccount extends Account{
//透支限额
protected double overDraftAmount=1000;
public double getOverDraftAmount() {
return overDraftAmount;
}
public void setOverDraftAmount(double overDraftAmount) {
this.overDraftAmount = overDraftAmount;
}
@Override
public void deposit(double money){
if(overDraftAmount!=1000){
if(money>=(1000-overDraftAmount)){
balance=money-(1000-overDraftAmount);
overDraftAmount=1000;
}else if(money<(1000-overDraftAmount)){
overDraftAmount=overDraftAmount+money;
}
}else{
balance=balance+money;
}
}
@Override
public void withdrawal(double money){
if(money>balance+overDraftAmount){
System.out.println("很抱歉!取款失败!您的取款金额大于账户余额加透支限额!");
}else{
System.out.print("取款成功!当前您的账户余额为:");
if(money<=balance){
balance=balance-money;
System.out.println(balance);
System.out.print("账户透支金额为:"+overDraftAmount);
}else{
overDraftAmount=overDraftAmount-(money-balance);
balance=0;
System.out.println(balance);
System.out.print("账户透支金额为:"+overDraftAmount);
}
}
}
}
package bank;
//储蓄账户类(取款时不允许透支)
public class SavingAccount extends Account{
@Override
public void withdrawal(double money){
if(money>balance){
System.out.println("很抱歉!取款失败!您的取款金额大于账户余额!");
}else{
System.out.print("取款成功!您的账户余额为:");
balance=balance-money;
System.out.println(balance);
}
}
}
package bank;
import java.util.ArrayList;
import java.util.Scanner;
//模拟银行类(可以存储多个类型可能是信用卡账户或储蓄账户的对象,用数组或ArrayList实现)
public class Bank {
Scanner sc=new Scanner(System.in);
//开户
public void openAccount(ArrayList<Account> list){
System.out.println("正在进行开户操作------");
System.out.println("感谢信任本银行!请输入您要开通的账户类型:1.储蓄账户类 2.信用卡账户类");
while (true){
int flag=sc.nextInt();
if(flag==1){ //储蓄账户类
SavingAccount sa=new SavingAccount();
System.out.println("请输入您的账号:(长度为12)");
while(true){
String str=sc.next();
if(str.length()!=12){
System.out.println("账号长度不符合要求!请重新输入!");
}else{
sa.setAccountNum(str);
break;
}
}
System.out.println("请输入您的姓名:");
sa.setName(sc.next());
System.out.println("开户成功!");
list.add(sa);
System.out.println("------------------");
System.out.println("已有账户数为:"+list.size());
Account a=list.get(list.size()-1);
System.out.println("新增加的账户信息为:");
if(a instanceof CreditAccount){
System.out.println("账户类型为:信用卡账户");
}else if(a instanceof SavingAccount){
System.out.println("账户类型为:储蓄账户");
}
System.out.println("账号:"+a.getAccountNum());
System.out.println("姓名:"+a.getName());
System.out.println("存款余额:"+a.getBalance());
if (a instanceof CreditAccount) {
System.out.println("透支限额:"+((CreditAccount) a).getOverDraftAmount());
}
System.out.println("------------------");
break;
}else if(flag==2){ //信用卡账户类
CreditAccount ca=new CreditAccount();
System.out.println("请输入您的账号:(长度为12)");
while(true){
String str=sc.next();
if(str.length()!=12){
System.out.println("账号长度不符合要求!请重新输入!");
}else{
ca.setAccountNum(str);
break;
}
}
System.out.println("请输入您的姓名:");
ca.setName(sc.next());
System.out.print("开户成功!");
list.add(ca);
System.out.println("------------------");
System.out.println("已有账户数为:"+list.size());
Account a=list.get(list.size()-1);
System.out.println("新增加的账户信息为:");
if(a instanceof CreditAccount){
System.out.println("账户类型为:信用卡账户");
}else if(a instanceof SavingAccount){
System.out.println("账户类型为:储蓄账户");
}
System.out.println("账号:"+a.getAccountNum());
System.out.println("姓名:"+a.getName());
System.out.println("存款余额:"+a.getBalance());
if (a instanceof CreditAccount) {
System.out.println("透支限额:"+((CreditAccount) a).getOverDraftAmount());
}
System.out.println("------------------");
break;
}else{
System.out.println("非法输入!请重新选择!");
}
}
}
//销户
public void closeAccount(ArrayList<Account> list){
System.out.println("正在进行销户操作------");
System.out.println("请输入销户账号:");
String str=sc.next();
int flag1=0;
for(Account i:list){
if(i.getAccountNum().equals(str)){
System.out.println("请输入该账户姓名:");
String str1=sc.next();
if(i.getName().equals(str1)){
flag1=1;
System.out.println("确认进行销户?1.确认 2.取消");
int flag2=sc.nextInt();
if(flag2==1){
list.remove(i);
System.out.println("销户成功!");
break;
} else if (flag2==2) {
System.out.println("销户操作终止!");
break;
}
}
}
}
if(flag1==0){
System.out.println("查无此账号!");
}
}
//统计目前银行的存款总余额
public void totalBalance(ArrayList<Account> list){
double totalBalance=0;
for(Account i:list){
totalBalance=totalBalance+i.getBalance();
}
System.out.println("目前银行的存款总余额为:"+totalBalance);
}
//统计目前银行的信用卡账户总透支金额
public void totaloverDraftAmount(ArrayList<Account> list){
double totaloverDraftAmount=0;
for(Account i:list){
if(i instanceof CreditAccount){
totaloverDraftAmount=totaloverDraftAmount+(1000-((CreditAccount) i).getOverDraftAmount());
}
}
System.out.println("目前银行的存款总透支金额为:"+totaloverDraftAmount);
}
//统计目前总账户数
public void totalAccount(ArrayList<Account> list){
System.out.println("目前总账户数为"+list.size());
}
//统计目前信用卡账户数
public void totalCreditAccount(ArrayList<Account> list){
int count=0;
for(Account i:list){
if(i instanceof CreditAccount){
count++;
}
}
System.out.println("目前信用卡账户数为"+count);
}
//统计目前储蓄卡账户数
public void totalSavingAccount(ArrayList<Account> list){
int count=0;
for(Account i:list){
if(i instanceof SavingAccount){
count++;
}
}
System.out.println("目前储蓄卡账户数为"+count);
}
}
package bank;
import java.util.*;
public class Client {
public static void main(String[] args) {
ArrayList<Account> list = new ArrayList<>();
Bank b = new Bank();
creatBank(list); //创建十个随机账户
Scanner sc=new Scanner(System.in);
while (true){
System.out.println("----------欢迎来到XX银行,所有操作如下:-----------");
System.out.println("1.开户");
System.out.println("2.销户");
System.out.println("3.对指定账号的存款和取款");
System.out.println("4.输出银行的总账户数、总余额、总透支数、各类具体账户数");
System.out.println("5.退出");
System.out.println("----------------------------------------------");
System.out.println("请选择操作:");
int flag=sc.nextInt();
if(flag==5){
break;
}
switch (flag){
case 1:
//多次开户操作
openOperate(b,list);
break;
case 2:
//多次销户
closeOperate(b,list);
break;
case 3:
//对指定账号的存款和取款操作
operate(list);
break;
case 4:
//输出银行的总账户数、总余额、总透支数、各类具体账户数
b.totalAccount(list);
b.totalBalance(list);
b.totaloverDraftAmount(list);
b.totalCreditAccount(list);
b.totalSavingAccount(list);
break;
default:
break;
}
}
}
//创建十个随机账户
public static void creatBank(ArrayList<Account> list) {
Random r = new Random();
Set<Integer> set = new HashSet<>();
while (set.size() < 10) {
int num = r.nextInt(10);
if (set.add(num)) {
String accountNum = "10000000000" + num;
int num1 = r.nextInt(2);
if (num1 == 0) {
SavingAccount sa = new SavingAccount();
sa.setAccountNum(accountNum);
list.add(sa);
} else if (num1 == 1) {
CreditAccount ca = new CreditAccount();
ca.setAccountNum(accountNum);
list.add(ca);
}
}
}
System.out.println("已有的随机账户数为:"+list.size());
for (Account i : list) {
if(i instanceof CreditAccount){
System.out.println("账户类型为:信用卡账户");
}else if(i instanceof SavingAccount){
System.out.println("账户类型为:储蓄账户");
}
System.out.println("账号:"+i.getAccountNum());
System.out.println("姓名:"+i.getName());
System.out.println("存款余额:"+i.getBalance());
if (i instanceof CreditAccount) {
System.out.println("透支限额:"+((CreditAccount) i).getOverDraftAmount());
}
System.out.println("------------------");
}
}
//存取款
public static void operate(ArrayList<Account> list){
Scanner sc=new Scanner(System.in);
System.out.println("正在进行存款和取款操作-----");
while(true){
int flag1=0;
System.out.println("请输入您的账号:");
String str=sc.next();
for(Account i:list){
if(i.getAccountNum().equals(str)){
flag1=1;
System.out.println("------------------");
System.out.println("您的账户信息为:");
if(i instanceof CreditAccount){
System.out.println("账户类型为:信用卡账户");
}else if(i instanceof SavingAccount){
System.out.println("账户类型为:储蓄账户");
}
System.out.println("账号:"+i.getAccountNum());
System.out.println("姓名:"+i.getName());
System.out.println("存款余额:"+i.getBalance());
if (i instanceof CreditAccount) {
System.out.println("透支限额:"+((CreditAccount) i).getOverDraftAmount());
}
System.out.println("------------------");
System.out.println("请输入您的操作:1.存款 2.取款");
int flag2=sc.nextInt();
if(flag2==1){
System.out.println("请输入存款金额:");
double d=sc.nextDouble();
i.deposit(d);
System.out.println("------------------");
System.out.println("更新后的账户信息为:");
if(i instanceof CreditAccount){
System.out.println("账户类型为:信用卡账户");
}else if(i instanceof SavingAccount){
System.out.println("账户类型为:储蓄账户");
}
System.out.println("账号:"+i.getAccountNum());
System.out.println("姓名:"+i.getName());
System.out.println("存款余额:"+i.getBalance());
if (i instanceof CreditAccount) {
System.out.println("透支限额:"+((CreditAccount) i).getOverDraftAmount());
}
System.out.println("------------------");
}else if(flag2==2){
System.out.println("请输入取款金额:");
double d=sc.nextDouble();
i.withdrawal(d);
System.out.println("------------------");
System.out.println("更新后的账户信息为:");
if(i instanceof CreditAccount){
System.out.println("账户类型为:信用卡账户");
}else if(i instanceof SavingAccount){
System.out.println("账户类型为:储蓄账户");
}
System.out.println("账号:"+i.getAccountNum());
System.out.println("姓名:"+i.getName());
System.out.println("存款余额:"+i.getBalance());
if (i instanceof CreditAccount) {
System.out.println("透支限额:"+((CreditAccount) i).getOverDraftAmount());
}
System.out.println("------------------");
}
break;
}
}
if(flag1==0){
System.out.println("查无此账户!");
}
System.out.println("是否继续账号的存款和取款操作:1.是 0.否");
int num=sc.nextInt();
if(num==0){
break;
}
}
}
//多次开户
public static void openOperate(Bank b,ArrayList<Account> list){
Scanner sc=new Scanner(System.in);
while (true){
b.openAccount(list);
System.out.println("是否继续开户?1.是 0.否");
int flag=sc.nextInt();
if(flag==0){
break;
}
}
}
//多次销户
public static void closeOperate(Bank b,ArrayList<Account> list){
Scanner sc=new Scanner(System.in);
while(true){
b.closeAccount(list);
System.out.println("当前账户数为:"+list.size());
System.out.println("是否继续销户?1.是 0.否");
int flag=sc.nextInt();
if(flag==0){
break;
}
}
}
}