亲宝软件园·资讯

展开

java ATM项目 java实现简单的ATM项目

wymolin 人气:0
想了解java实现简单的ATM项目的相关内容吗,wymolin在本文为您仔细讲解java ATM项目的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,ATM项目,下面大家一起来学习吧。

首先要了解的是,这个ATM项目本身是一个轻量级的项目,只为了完成一些ATM具备的一些方法,并非是真正完成一个ATM的全部功能和需求

那么在这个轻量级的ATM项目中,我将完成添加储蓄账号,添加信用账户,提款,取款等基本功能。

适合新手查看,需要掌握java的继承,多态,封装等基本技术能力

那么,首先创建如下的对象类:Account(账户类),Bank(银行类),CreditAccount(信用账户),SavingAccount(储蓄账户类);

大家首先应该搞清楚,这些类文件中之间的关系,每个类之间需要用到什么样的方法;

那么我们先填写Account类

package com.atm.entity;
 
/**
 * 银行账户类
 */
public abstract class Account {
 /**
 * 账户帐号
 */
 private String accountId;
 /**
 * 账户姓名
 */
 private String accountName;
 /**
 * 账户密码
 */
 private String accountPwd;
 /**
 * 账户余额
 */
 private double accountBalance;
 /**
 * 账户SFZ号
 */
 private String accountPersonId;
 /**
 * 账户邮箱
 */
 private String accountEmail;
 /**
 * 账户联系电话
 */
 private long accountTelno;
 
 public Account() {
 }
 
 public Account(String accountName, String accountPwd, String accountPersonId, long accountTelno,
 String accountEmail) {
 this.accountName = accountName;
 this.accountPwd = accountPwd;
 this.accountPersonId = accountPersonId;
 this.accountTelno = accountTelno;
 this.accountEmail = accountEmail;
 
 }
 
 public String getAccountId() {
 return accountId;
 }
 
 public void setAccountId(String accountId) {
 this.accountId = accountId;
 }
 
 public String getAccountName() {
 return accountName;
 }
 
 public void setAccountName(String accountName) {
 this.accountName = accountName;
 }
 
 public String getAccountPwd() {
 return accountPwd;
 }
 
 public void setAccountPwd(String accountPwd) {
 this.accountPwd = accountPwd;
 }
 
 public double getAccountBalance() {
 return accountBalance;
 }
 
 public void setAccountBalance(double accountBalance) {
 this.accountBalance = accountBalance;
 }
 
 public String getAccountPersonId() {
 return accountPersonId;
 }
 
 public void setAccountPersonId(String accountPersonId) {
 this.accountPersonId = accountPersonId;
 }
 
 public String getAccountEmail() {
 return accountEmail;
 }
 
 public void setAccountEmail(String accountEmail) {
 this.accountEmail = accountEmail;
 }
 
 public long getAccountTelno() {
 return accountTelno;
 }
 
 public void setAccountTelno(long accountTelno) {
 this.accountTelno = accountTelno;
 }
 
 /**
 * 存款
 * 
 * @param money
 *  存款金额
 * @return 返回账户余额
 */
 public double depoist(double money) {// money 形式参数
 if (money > 0)
 this.accountBalance += money;
 return this.accountBalance;
 }
 
 /**
 * 取款
 * 
 * @param money
 *  取款金额
 * @return 返回账户余额
 */
 public abstract double withdraw(double money);
 
 /**
 * 转账
 * 
 * @param anotherAccount
 *  转账的对方账户
 * @param money
 *  转账金额
 * @return 返回当前账户的余额
 */
 public double tranferAccount(Account anotherAccount, double money) {// 形参
 anotherAccount.accountBalance += money;
 this.accountBalance -= money;
 
 return this.accountBalance;
 }
 
}

之后填写信用账户类CreditAccount;我们应该明白,他是继承Account类的,但是,他又需要拥有自身独立的属性,我们可以添加一个最高透支额度的属性
这样来实现代码

/**
 * 信用账户
 * 
 *
 */
public class CreditAccount extends Account {
 //成员变量
 private double maxOverdraw;//最高透支额度
 
 //构造函数
 public CreditAccount(String accountName,String accountPwd,String accountPersonId,long accountTelno,String accountEmail,double maxOverdraw){
 super( accountName, accountPwd, accountPersonId, accountTelno, accountEmail);
 this.maxOverdraw = maxOverdraw;
 }
 
 
 //set,get
 public void setMaxOverdraw(double maxOverdraw ){
 this.maxOverdraw = maxOverdraw;
 }
 
 public double getMaxOverdraw(){
 return this.maxOverdraw;
 }
 
 
 @Override
 public double withdraw(double money) {
 // TODO Auto-generated method stub
 return 0;
 }
 
 
 
}

同理  填写储蓄账户(SavingAccount)类文件

package com.atm.entity;
/**
 * 储蓄账户
 * 
 *
 */
public class SavingAccount extends Account {
 
 public SavingAccount(String accountName,String accountPwd,String accountPersonId,long accountTelno,String accountEmail){
 super( accountName, accountPwd, accountPersonId, accountTelno, accountEmail);
 }
 
 @Override
 public double withdraw(double money) {
 // TODO Auto-generated method stub
 if(money <= getAccountBalance()){
 
 }
 
 else
 System.out.println("账户余额不足");
 return getAccountBalance();
 
 }
 
 
}

最重要的是填写Bank类的内容,在这个类中,我们要完成注册,产生银行账户,统计所有信用账户的最高透支额度的总和,统计所有账户的总余额, 查询出所有信用账户中透支额度最高的账户, 查询出所有储蓄账户中余额最高的账户等功能

我们这样填写

package com.atm.entity;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * 银行类
 * 
 * @author qianghj
 * 
 *  银行开户 ----> 银行账户 Account account = bank.开户(...)
 *
 */
public class Bank {
 public Account[] accArray = new Account[2000];
 
 public int count = 0;// 表示银行账户的个数
 
 /**
 * 银行账户开户
 * 
 * @param accName
 *  用户名称
 * @param accPwd
 *  用户密码
 * @param accPersonId
 *  用户SFZ
 * @param accTelno
 *  用户手机号码
 * @param accEmail
 *  用户邮箱
 * @param accountType
 *  账户类型 0: 储蓄账户 1 : 信用账户
 * @param maxOverdraw
 *  信用账户的最高透支额度
 * @return 返回有效的银行账户
 */
 public Account registAccount(String accName, String accPwd, String accPersonId, long accTelno, String accEmail,
 int accountType, double maxOverdraw) {
 Account account = null;
 if (accountType == 0)
 account = new SavingAccount(accName, accPwd, accPersonId, accTelno, accEmail);
 else
 account = new CreditAccount(accName, accPwd, accPersonId, accTelno, accEmail, maxOverdraw);
 
 account.setAccountId(generateNextAccountId());
 accArray[count++] = account;
 return account;
 }
 /**
 * 产生银行账户帐号
 * 
 * @return 返回下一个账户的帐号 1,2,3,,4
 */
 public String generateNextAccountId() {
 
 return "62223421" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
 
 }
 
 // 统计所有信用账户的最高透支额度的总和 (1050 ) 2000 , 1050
 public double statisticsCreditAccountMaxoverdrawSum() {
 double sum = 0;
 for (int i = 0; i < count; i++) {
 // 判断账户 是不是 CreditAccount类型
 if (accArray[i] instanceof CreditAccount) {
 CreditAccount creditAcc = (CreditAccount) accArray[i];
 sum += creditAcc.getMaxOverdraw();
 }
 }
 
 return sum;
 }
 
 // 统计所有账户的总余额
 public double aggregateAamount() {
 double sum = 0;
 for (int i = 0; i < count; i++) {
 if (accArray[i] instanceof SavingAccount) {
 SavingAccount savingAccount = (SavingAccount) accArray[i];
 sum += savingAccount.getAccountBalance();
 
 }
 }
 return sum;
 
 }
 
 // 查询出所有信用账户中透支额度最高的账户
 public double maxLimit() {
 
 double tem = 0;
 for (int i = 0; i < count; i++) {
 if (accArray[i] instanceof CreditAccount) {
 CreditAccount creditAccount = (CreditAccount) accArray[i];
 
 if (creditAccount.getMaxOverdraw() > tem) {
 tem = creditAccount.getMaxOverdraw();
 }
 
 }
 }
 return tem;
 
 }
 
 // 查询出所有储蓄账户中余额最高的账户
 public double maxBalance() {
 
 double tem = 0;
 for (int i = 0; i < count; i++) {
 if (accArray[i] instanceof SavingAccount) {
 SavingAccount savingAccount = (SavingAccount) accArray[i];
 
 if (savingAccount.getAccountBalance() > tem) {
 tem = savingAccount.getAccountBalance();
 }
 
 }
 }
 return tem;
 
 }
 
}

最后测试类

package test;
 
import org.junit.Test;
 
import com.atm.entity.Account;
import com.atm.entity.Bank;
import com.atm.entity.CreditAccount;
 
public class TestAccount {
 
 @Test
 public void testRegist() {
 Bank bank = new Bank();
 
 for (int i = 0; i < 1000; i++) {
 // 0: 储蓄账户 1 : 信用账户
 Account acc = bank.registAccount("tom" + i, "abc123", "2729382932", 183923302L, "tom" + i + "@163.com",
 i % 2, (i % 2 == 0) ? 0 : 3000);
 if (i % 2 != 0) {
 CreditAccount creditAcc = (CreditAccount) acc;
 System.out.println("所有信用账户的名字:" + creditAcc.getAccountName() + "和透支额度:" + creditAcc.getMaxOverdraw());
 }
 
 }
 
 // 1000个银行账户开户,500是信用账户,最高透支额度随机数赋值,再测试
 // double sum = bank.统计所有信用账户的最高透支额度的总和 ();
 double sum = bank.statisticsCreditAccountMaxoverdrawSum();
 System.out.println("所有信用账户的最高透支额度的总和 :" + sum);
 double sum1 = bank.aggregateAamount();
 System.out.println("总余额为" + sum1);
 }
 
}

测试类的内容不多写,大家有兴趣可以自行测试。这样,我们就完成了一个比较简单的ATM项目。希望对新学者有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论