设计一个简单的金融系统,用于存储用户的账户信息和交易记录。
交易类:Transaction
属性:
private LocalDateTime time; // 交易时间
private String type; // 交易类型
private BigDecimal amount; // 交易金额
private BigDecimal balance; // 交易后余额
银行账户类:BankAccount
属性:
private String accountNumber; // 银行账号
private String accountName; // 银行账户名称
private BigDecimal balance; // 账户余额
private ArrayList<Transaction> transactionHistory; // 交易历史记录
方法:
存款 public void deposit(金额)
取款public void withdraw(金额)
查询余额public void queryBalance()
转账public void transfer(银行账户, 金额)
查询交易记录public void printTransactionHistory()
在测试类中:
先创建第一个账户对象,并初始化,
在查询余额,显示余额为XX。
在存款,显示存款成功,当前余额为XX
在取款,注意如果取款金额大于余额,则余额不足,取款失败。
否则显示取款成功,当前余额为XXX。
最后在打印交易记录,显示时间、类型、金额、余额。
创建第二个账户对象,并初始化,让第一个账户对象给第二个账户对象转账。
注意如果转账金额大于余额,则显示余额不足,转账失败
否则转账成功,当前余额为XX。
在打印交易记录。
效果图如图所示:仅供参考
import java.math.BigDecimal;
import java.time.LocalDateTime;
public class Transaction {
private LocalDateTime time; // 交易时间
private String type; // 交易类型
private BigDecimal amount; // 交易金额
private BigDecimal balance; // 交易后余额
public Transaction() {
}
public Transaction(LocalDateTime time, String type, BigDecimal amount, BigDecimal balance) {
this.time = time;
this.type = type;
this.amount = amount;
this.balance = balance;
}
/**
* 获取
* @return time
*/
public LocalDateTime getTime() {
return time;
}
/**
* 设置
* @param time
*/
public void setTime(LocalDateTime time) {
this.time = time;
}
/**
* 获取
* @return type
*/
public String getType() {
return type;
}
/**
* 设置
* @param type
*/
public void setType(String type) {
this.type = type;
}
/**
* 获取
* @return amount
*/
public BigDecimal getAmount() {
return amount;
}
/**
* 设置
* @param amount
*/
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
/**
* 获取
* @return balance
*/
public BigDecimal getBalance() {
return balance;
}
/**
* 设置
* @param balance
*/
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
public String toString() {
return "Transaction{time = " + time + ", type = " + type + ", amount = " + amount + ", balance = " + balance + "}";
}
}
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
public class BankAccount {
private String accountNumber; // 银行账号
private String accountName; // 银行账户名称
private BigDecimal balance; // 账户余额
private ArrayList<Transaction> transactionHistory = new ArrayList<Transaction>(); // 交易历史记录
private int tcount = 0;
public BankAccount() {
}
public BankAccount(String accountNumber, String accountName, BigDecimal balance) {
this.accountNumber = accountNumber;
this.accountName = accountName;
this.balance = balance;
// this.transactionHistory = transactionHistory;
}
public int getTcount() {
return tcount;
}
public void setTcount(int tcount) {
this.tcount = tcount;
}
/**
* 获取
* @return accountNumber
*/
public String getAccountNumber() {
return accountNumber;
}
/**
* 设置
* @param accountNumber
*/
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
/**
* 获取
* @return accountName
*/
public String getAccountName() {
return accountName;
}
/**
* 设置
* @param accountName
*/
public void setAccountName(String accountName) {
this.accountName = accountName;
}
/**
* 获取
* @return balance
*/
public BigDecimal getBalance() {
return balance;
}
/**
* 设置
* @param balance
*/
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
/**
* 获取
* @return transactionHistory
*/
public ArrayList<Transaction> getTransactionHistory() {
return transactionHistory;
}
/**
* 设置
* @param transactionHistory
*/
public void setTransactionHistory(ArrayList<Transaction> transactionHistory) {
this.transactionHistory = transactionHistory;
}
public String toString() {
return "BankAccount{accountNumber = " + accountNumber + ", accountName = " + accountName + ", balance = " + balance + ", transactionHistory = " + transactionHistory + "}";
}
public void deposit(BigDecimal b){
this.balance = this.balance.add(b);
System.out.println("当前余额为:"+this.balance);
transactionHistory.add(new Transaction());
transactionHistory.get(tcount).setTime(time());
transactionHistory.get(tcount).setType("存款");
transactionHistory.get(tcount).setAmount(b);
transactionHistory.get(tcount).setBalance(this.balance);
tcount++;
System.out.println("操作成功");
queryBalance();
}
// 存款
public void transfer(BigDecimal b){
transactionHistory.add(new Transaction());
transactionHistory.get(tcount).setTime(time());
transactionHistory.get(tcount).setType("转账");
transactionHistory.get(tcount).setAmount(b);
transactionHistory.get(tcount).setBalance(this.balance);
tcount++;
}
public LocalDateTime time(){
Date date = new Date();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
return date.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
}
// 获取当前时间
public void withdraw(BigDecimal b){
if (this.balance.compareTo(b) < 0)
System.out.println("余额不足");
else{
this.balance = this.balance.subtract(b);
System.out.println("操作成功,当前余额为:"+this.balance);
transactionHistory.add(new Transaction());
transactionHistory.get(tcount).setTime(time());
transactionHistory.get(tcount).setType("取款");
transactionHistory.get(tcount).setAmount(b);
transactionHistory.get(tcount).setBalance(this.balance);
tcount++;
}
}
// 取款
public void queryBalance(){
System.out.println("当前余额为:"+this.balance);
}
// 查询余额
public void printTransactionHistory(){
System.out.println("");
for (Transaction transaction : transactionHistory) {
System.out.println("交易时间为:"+transaction.getTime());
System.out.println("交易类型:"+transaction.getType());
System.out.println("交易金额:"+transaction.getAmount());
System.out.println("交易后余额:"+transaction.getBalance());
}
}
// 查询交易记录
}
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Scanner;
public class BankApp {
public static ArrayList<BankAccount> list = new ArrayList<BankAccount>();
private static Scanner sc = new Scanner(System.in);
public static int count = 0;
public static void transfer(String accountName, BigDecimal b){
// list.get(0).withdraw(b);
if (list.get(0).getBalance().compareTo(b) < 0)
System.out.println("余额不足");
else{
list.get(0).setBalance(list.get(0).getBalance().subtract(b));
System.out.println("操作成功,当前余额为:"+list.get(0).getBalance());
}
list.get(0).transfer(b);
// list.get(1).deposit(b);
list.get(1).setBalance(list.get(1).getBalance().add(b));
list.get(1).transfer(b);
}
public static void main(String[] args) {
list.add(new BankAccount("10080606","登哥",new BigDecimal(10000)));
list.get(count).queryBalance();
System.out.println("请输入存款金额");
list.get(count).deposit(sc.nextBigDecimal());
System.out.println("请输入取款金额");
list.get(count).withdraw(sc.nextBigDecimal());
list.get(count).printTransactionHistory();
count++;
list.add(new BankAccount("20160606","宇博",new BigDecimal(100000)));
System.out.println("输入需要转账的账户");
String s1 = sc.next();
System.out.println("输入转账金额");
BigDecimal b1 = sc.nextBigDecimal();
transfer(s1,b1);
list.get(0).printTransactionHistory();
list.get(1).printTransactionHistory();
}
}