当前位置:首页 > 开发教程 > java教程 >

Java实现银行账户管理子系统

时间:2022-05-27 16:29 来源:未知 作者:大吃饭最大 收藏

这篇文章主要为大家详细介绍了Java实现银行账户管理子系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java实现银行账户管理子系统的具体代码,供大家参考,具体内容如下

所用到的知识点:面向对象基础语法,封装,方法覆盖(重写)、继承、多态

话不多说,直接上代码

Account.java

package com.task1;

import java.util.Scanner;

public class Account {
 //规定账户类型:
 //0 – 储蓄账户 1 – 信用账户 2 – 可贷款储蓄账户 3– 可贷款信用账户
 private Long id ;//账户号码
 private String password;//账户密码
 private String name;//真实姓名
 private String personId;//身份证号码
 private String email;//客户的电子邮箱
 private double balance;//账户余额
 private int type;//账户类型
 
 //无参的构造方法
 public Account(){
  
 }
 
 //有参的构造方法
 public Account(long id, String password, String name, String personId, String email, double balance, int type) {
   super();
   this.id = id;
   this.password = password;
   this.name = name;
   this.personId = personId;
   this.email = email;
   this.balance = balance;
   this.type = type;
  }
 
 //get、set方法
 public Long getId() {
  return id;
 }

 public void setId(long id) {
  this.id = id;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getPersonId() {
  return personId;
 }

 public void setPersonId(String personId) {
  this.personId = personId;
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 public double getBalance() {
  return balance;
 }

 public void setBalance(double balance) {
  this.balance = balance;
 }

 public int getType() {
  return type;
 }

 public void setType(int type) {
  this.type = type;
 }

 //存款
 public Account deposit(double money ) {
  this.balance+= money;  //余额增加
  return this;
 }
 
 //取款
 public Account withdraw(double money) {
  if(balance - money >= 0) { //判断余额是否足够
   balance -= money; //余额减少
//   System.out.println("存款成功");
  }
  else {
   System.out.println("您的余额不足!");
  }
  return this;
 }

 //重写toString方法
 @Override
 public String toString() {
  return "Account [id=" + id + ", password=" + password + ", name=" + name + ", personId=" + personId + ", email="
    + email + ", balance=" + balance + ", type=" + type + "]";
 } 
 
}

SavingAccount.java

package com.task1;

public class SavingAccount extends Account {

 public SavingAccount() {
  super();
  // TODO Auto-generated constructor stub
 }

 public SavingAccount(long id, String password, String name, String personId, String email, double balance,
   int type) {
  super(id, password, name, personId, email, balance, type);
  // TODO Auto-generated constructor stub
 }

 @Override
 public String toString() {
  return "SavingAccount [getId()=" + getId() + ", getPassword()=" + getPassword() + ", getName()=" + getName()
    + ", getPersonId()=" + getPersonId() + ", getEmail()=" + getEmail() + ", getBalance()=" + getBalance()
    + ", getType()=" + getType() + "]";
 }


}

CreditAccpunt.java

package com.task1;

public class CreditAccount extends Account{
 private double ceiling;


 public CreditAccount() {
  super();
 }

 public CreditAccount(long id, String password, String name, String personId, String email, double balance,
   int type,double ceiling) {
  super(id, password, name, personId, email, balance, type);
  this.ceiling = ceiling;

 }

 public double getCeiling() {
  return ceiling;
 }

 public void setCeiling(double ceiling) {
  this.ceiling = ceiling;
 }

 @Override
 public String toString() {
  return "CreditAccount [getCeiling()=" + getCeiling() + ", getId()=" + getId() + ", getPassword()="
    + getPassword() + ", getName()=" + getName() + ", getPersonId()=" + getPersonId() + ", getEmail()="
    + getEmail() + ", getBalance()=" + getBalance() + ", getType()=" + getType() + "]";
 }

 @Override
 public Account withdraw(double money) {
  if(getBalance() >= money) {
   setBalance(getBalance() - money);
  }else if(getBalance() + getCeiling() >= money) {
   setCeiling(getCeiling()-(money-getBalance()));
   setBalance(0);
  }else {
   System.out.println("对不起,您的余额不足");
  }
  return this;
 }

 
}

Bank.java

package com.task1;

import java.util.Scanner;

public class Bank {
 int index = 0; //当前用户数量
 Account [] accounts = new Account [100];//数据库
 Account account = null;
 /*
  * 用户开户(register)
   参数列表: Long 账号, String密码, String确认密码,String 姓名,String身份证号码,String邮箱,int 账户类型;

(Long id, String password, String repassword, String name, String personID, String email, int type)

   返回类型:Account

   规定账户类型:0 – 储蓄账户 1 – 信用账户 2 – 可贷款储蓄账户 3– 可贷款信用账户
  */
 //用户开户
 public Account register(Long id, String password, String repassword, String name, String personId, String email, int type) {

  if(password.equals(repassword)) { // 判断两次输入密码是否一致
   switch (type) {     //根据用户类型创建不同账户
   case 0:
    account = new SavingAccount(id, repassword, name, personId, email, 0, type);  //创建储蓄账户
    
    break;
   case 1:
    account = new CreditAccount(id, repassword, name, personId, email, 0, type,1000);  //创建信用账户
   }
   accounts[index++] = account;  //账户信息存到数据库,用户数量+1
  }
//  else {
//   System.out.println("两次输入的密码不一致");
//  }
  return account;
 }
 
 //用户登录
 public Account login(Long id, String password) {
  int find = searchIdPassword(index, id, password); //当前用户数组下标
  if(find >= 0) {      //判断账户密码是否正确
//   System.out.println("登录成功");
   return accounts[find];     //返回当前对象
  }
//  else {
//   System.out.println("用户名密码错误");
//  }
  
  return null;//如果账户密码错误返回空
 }
 
 //用户存款
 public Account deposit(Long id, double money) {
  int find = searchId(index, id);当前用户数组下标
  if(find >= 0) {       //判断账户是否存在
   accounts[find].deposit(money);   //调用Account类的存款方法
//   System.out.println("存款成功");
   return accounts[find];       //返回当前对象
//   accounts[find].setBalance(accounts[find].getBalance()+money);
  }
//  else {
//   System.out.println("用户不存在");
//  }
  

  return null;//如果账户不存在返回空
 }
 
 //用户取款
 public Account withdraw(Long id, String password, double money) {
  int find = searchIdPassword(index, id, password);//当前用户数组下标
  if(find >= 0) {  //判断账户密码是否正确
   
   accounts[find].withdraw(money); //调用当前对象的取款方法 
//   System.out.println("取款成功");
   return accounts[find]; //返回当前对象

  }
  return null;
 }
 //设置透支额度
 public Account updateCeiling(Long id, String password, double money) {
  int find = searchIdPassword(index, id, password);//获取当前用户数组下标
  if((find >= 0) && (accounts[find].getType()) == 1){ //判断账户号码和密码是否正确,账户类型是否为信用账户
   ((CreditAccount)accounts[find]).setCeiling(((CreditAccount)accounts[find]).getCeiling() + money); //调用set方法设置透支额度
   return accounts[find];
  }
  return null;
 }
 
 
 // 转账功能
 // 参数:from转出账户,passwordFrom 转出账号的密码,to转入账户,money转账的金额
 public boolean transfer(Long from, String passwordFrom, Long to, double money) {
  int find = searchIdPassword(index, from, passwordFrom); //转账账户数组下标
  int find2 = searchId(index, to);       //收款账户数组下标
  if(find >= 0 && find2 >= 0 && accounts[find].getBalance() >= money) { //判断转账账户密码、收款账户是否匹配,判断转账用户余额是否足够
    accounts[find].withdraw(money);//转账用户转账操作==取款
    accounts[find2].deposit(money);//收款用户 == 存款
    return true;

  }

  return false;
 }
 
 //统计银行所有账户余额总数
 public double balanceSum() {
  double sum = 0;   //初始化所有账户余额
  for(int i = 0; i < index; i++) { //遍历数组
   sum += accounts[i].getBalance();//求和(账户余额)
  }
  return sum;
 }
 
 //统计所有信用账户透支额度总数
 public double ceilingSum() {
  double sum = 0; //初始化所有透支额度和
  for(int i = 0; i < index; i++) { //遍历
   if(accounts[i].getType() == 1) { //判断账户类型是否为信用账户
    sum += ((CreditAccount)accounts[i]).getCeiling(); //求和
   }
  }
  return sum;
 }
 //搜索Id与密码返回数组下标位置
 public int searchIdPassword(int index, Long id, String password) {
  for(int i = 0; i < index; i++) {
   if(id.equals(accounts[i].getId()) && password.equals(accounts[i].getPassword())){ //比较账户和密码是否匹配
    return i ; //匹配则返回账户数组下标
   }
   
  }
  return -1; //不匹配则返回-1
 }
 //搜索Id
 public int searchId(int index, Long id) {
  for(int i = 0; i < index; i++) {
   if(id.equals(accounts[i].getId())){  //比较账户是否匹配
    return i ;      //匹配则返回账户数组下标
   }
   
  }
  return -1; //不匹配则返回-1
 }
}

TestAccount.java

package com.task1;

public class TestAccount {

 public static void main(String[] args) {

  Account a =new Account(123456L, "123456","张三", "421356", "tt@tt.com", 100.0, 0);
  System.out.println(a);  
 }

}

TestBank.java

package com.task1;

import java.util.Scanner;

public class TestBank {

 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  Bank bank = new Bank();
  String action =null;
  do {
   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("请选择服务");
   int choice =sc.nextInt();
   
   switch(choice) {
   
    case 1:   //开户

     System.out.println("请输入账户号码:");
     Long id = sc.nextLong();
     System.out.println("请输入账户密码:");
     String password = sc.next();
     System.out.println("请确认账户密码:");
     String repassword = sc.next();
     System.out.println("请输入真实姓名:");
     String name = sc.next();
     System.out.println("请输入身份证号:");
     String personId = sc.next();
     System.out.println("请输入电子邮箱:");
     String email = sc.next();
     System.out.println("请输入账户类型:0 – 储蓄账户 1 – 信用账户 2 – 可贷款储蓄账户 3– 可贷款信用账户");
     int type = sc.nextInt();
     
     Account a1 = bank.register(id, password, repassword, name, personId, email, type);
     if(a1 != null) {
      System.out.println(a1);
      System.out.println("开户成功");
     }else {
      System.out.println("两次输入密码不一致");
     }
     
     break;
    case 2:   //登录
     System.out.println("请输入账户号码:");
     id = sc.nextLong();
     System.out.println("请输入账户密码:");
     password = sc.next();
     Account a2 = bank.login(id, password);
     if(a2 != null) {
      System.out.println(a2);
      System.out.println("登录成功");
     }else {
      System.out.println("账户号码密码错误");
     }
     
     break;
     
    case 3:   //存款
     
     System.out.println("请输入账户号码:");
     id = sc.nextLong();
     System.out.println("请输入存款金额:");
     double money = sc.nextDouble();
     Account a3 = bank.deposit(id, money);
     if(a3 != null) {
      System.out.println(a3);
       System.out.println("存款成功");
     }else {
      System.out.println("账户不存在");
     }
     break;
     
    case 4:   //取款
     System.out.println("请输入账户号码:");
     id = sc.nextLong();
     System.out.println("请输入账户密码:");
     password = sc.next();
     System.out.println("请输入取款金额:");
     money = sc.nextDouble();
     Account a4 = bank.withdraw(id, password, money);
     if(a4 != null ) {
      System.out.println(a4);
      System.out.println("取款成功");
     }else {
      System.out.println("账户号码密码错误");
     }
     
     break;
     
    case 5://设置透支额度
     System.out.println("请输入账户号码:");
     id = sc.nextLong();
     System.out.println("请输入账户密码:");
     password = sc.next();
     System.out.println("请输入透支金额:");
     money = sc.nextDouble();
     Account a5 = bank.updateCeiling(id, password, money);
     if(a5 != null ) {
      System.out.println(a5);
      System.out.println("设置透支额度成功");
     }else {
      System.out.println("账户号码密码错误或账户类型错误");
     }
     
     break;
     
    case 6:// 转账功能
     System.out.println("请输入转账账户号码:");
     Long from = sc.nextLong();
     System.out.println("请输入转账账户密码:");
     String passwordFrom = sc.next();
     System.out.println("请输入收款账户号码:");
     Long to = sc.nextLong();
     System.out.println("请输入转账金额:");
     money = sc.nextDouble();
     boolean flag = bank.transfer(from, passwordFrom, to, money);
     if(flag) {
      System.out.println("转账成功");
     }else {
      System.out.println("转账失败");
     }
     
     break;
    default:
     System.out.println("服务选择错误");
   }
   System.out.println("是否继续(y/n)");
   action = sc.next();
  
  }while("y".equals(action));
  
  double balanceSum = bank.balanceSum();
  double ceilingSum = bank.ceilingSum();
  System.out.println("银行所有账户余额总数:"+balanceSum);
  System.out.println("所有信用账户透支额度总数"+ceilingSum);
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持源码搜藏网。


下一篇:没有了

java教程阅读排行

最新文章