Account类的核心操作
package com.downeyjr.bean;
import com.downeyjr.tool.AccountTool;
import com.downeyjr.tool.DateTool;
/**
* 1.创建账户类,账户类里面 2.
*
* @author 糖纸疯了
*
*/
public class Account {
private int accountId;// 银行卡号
private double accountMoney;// 卡上余额(真实应该从数据库获取)
public int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}
public double getAccountMoney() {
return accountMoney;
}
public void setAccountMoney(double accountMoney) {
this.accountMoney = accountMoney;
}
// 构造函数
public Account(int accountId, double accountMoney) {
this.setAccountId(accountId);
this.setAccountMoney(accountMoney);
}
public Account() {
super();
}
// 账户之间转账
public synchronized static void transfer(Account fromAccount, Account toAccount,
double amount) {
if (fromAccount.getAccountMoney() < amount) {
System.out.println("对不起,当前余额不足,请重新输入金额!");
return;
}
System.out.println(Thread.currentThread());
fromAccount.setAccountMoney(fromAccount.getAccountMoney() - amount);
int fromID = fromAccount.getAccountId();
int toID = toAccount.getAccountId();
double balance = fromAccount.getAccountMoney();
System.out.println(AccountTool.getAccountPrintStringSuccess(fromID, toID, amount,balance));
}
//用户取款
public synchronized static void drawMoney(Account account, double amount){
if (account.getAccountMoney() < amount) {
int accountID = account.getAccountId();
System.out.println(AccountTool.getAccountDrawMoneyStringFail(accountID,amount));
}else {
account.setAccountMoney( account.getAccountMoney()-amount);
int accountID = account.getAccountId();
double balance = account.getAccountMoney();
System.out.println(AccountTool.getAccountDrawMoneyStringSuccess(accountID,amount,balance));
}
}
//用户存款
public synchronized static void saveMoney(Account account, double amount){
account.setAccountMoney( account.getAccountMoney()+amount);
int accountID = account.getAccountId();
double balance = account.getAccountMoney();
System.out.println(AccountTool.getAccountDrawMoneyStringSuccess(accountID,amount,balance));
}
}
将三个方法,对应成线程操作
1.AccountDrawRunable 类
package com.downeyjr.bean;
public class AccountDrawRunable implements Runnable{
private Account account;
private double amount;
public AccountDrawRunable(Account account,double amount){
this.account = account;
this.amount = amount;
}
@Override
public void run() {
Account.drawMoney(account, amount);
try {
Thread.sleep((long) (100L * Math.random()));
} catch (Exception e) {
}
}
}
2.AccountSaveRunable 类
package com.downeyjr.bean;
public class AccountSaveRunable implements Runnable{
private Account account;
private double amount;
public AccountSaveRunable(Account account,double amount){
this.account = account;
this.amount = amount;
}
@Override
public void run() {
Account.saveMoney(account, amount);
try {
Thread.sleep((long) (100L * Math.random()));
} catch (Exception e) {
}
}
}
3.AccountTransRunable 类
package com.downeyjr.bean;
public class AccountTransRunable implements Runnable{
private Account fromAccount;
private Account toAccount;
private double amount = 0;
//转账的
public AccountTransRunable(Account fromAccount,Account toAccount,double amount) {
this.fromAccount = fromAccount;
this.toAccount = toAccount;
this.amount = amount;
}
@Override
public void run() {
Account.transfer(fromAccount, toAccount, amount);
try {
Thread.sleep((long) (100L * Math.random()));
} catch (Exception e) {
}
}
}
几个工具类
1.AccountTool
package com.downeyjr.tool;
/**
* Account转账提示语
* @author 糖纸疯了
*/
public class AccountTool {
public static String getAccountPrintString(int fromID, int toID,
double amount) {
String printString = "在北京时间" + DateTool.getCurrentTimeStamp()
+ "\\n从账号ID:" + fromID + "转出金额" + amount + "\\n到账户ID:" + toID;
return printString;
}
//账号之间转账
public static String getAccountPrintStringSuccess(int fromID, int toID,
double amount, double fromBalance) {
String printString = "在北京时间" + DateTool.getCurrentTimeStamp()
+ "\\n从账号ID:" + fromID + "转出金额" + amount + "\\n到账户ID:" + toID
+ "\\n当前账户余额:" + fromBalance;
return printString;
}
//用户取款提示语
public static String getAccountDrawMoneyStringFail(int accountID, double amount){
String printString = "在北京时间" + DateTool.getCurrentTimeStamp()+
"\\n从账号ID:" + accountID+"取出金额失败"+
"原因:存款余额不足!";
return printString;
}
public static String getAccountDrawMoneyStringSuccess(int accountID,double amount,double balance){
String printString = "在北京时间" + DateTool.getCurrentTimeStamp()+
"\\n从账号ID:" + accountID+"取出金额:"+amount +
"\\n当前账户余额:" + balance;
return printString;
}
public static String getAccountSaveMoneyStringSuccess(int accountID,double amount,double balance){
String printString = "在北京时间" + DateTool.getCurrentTimeStamp()+
"\\n账号ID:" + accountID+"存入金额:"+amount +
"\\n当前账户余额:" + balance;
return printString;
}
public static String getAccountSaveMoneyStringFail(int accountID,double amount,double balance){
String printString = "在北京时间" + DateTool.getCurrentTimeStamp()+
"\\n从账号ID:" + accountID+"取出金额:"+amount +
"\\n当前账户余额:" + balance;
return printString;
}
}
2.DateTool
package com.downeyjr.tool;
import java.util.Calendar;
/**
* Date工厂类
* @author 糖纸疯了
*/
public class DateTool {
public static String getCurrentTimeStamp() {
String currentString = null;
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int date = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
currentString = year + "/" + month + "/" + date + " " + hour + ":"
+ minute + ":" + second;
return currentString;
}
}
3.FileWriterTool
package com.downeyjr.tool;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
/**
* 文件写入工厂类
* @author 糖纸疯了
*/
public class FileWriterTool {
/**
* @param filePath 文件的写入路径
* @param string 写入的文本
*/
public static void writerToFilePath(String filePath,String string) {
try {
File file = new File(filePath);
PrintStream ps = new PrintStream(new FileOutputStream(file));
ps.println("http://www.jb51.net");// 往文件里写入字符串
ps.append("http://www.jb51.net");// 在已有的基础上添加字符串
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}