?? transactmanager.java
字號:
package BS.server.businesslayer;
/**
* @author 束罡
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
import BS.server.extendedlayer.Project;
import BS.server.xmllayer.LockingManager;
/**
* 類:TransactManager
* 功能:將用戶的請求轉化為具體的write & read語句
* */
public class TransactManager
{
/**
* 方法:withDraw
* 功能:取錢
* 調用參數:客戶customer_id,取錢數量transaction_amount
* 返回值:成功為true;否則false;
* */
public static synchronized boolean withDraw (int customer_id, float transaction_amount)
{
try
{
boolean flag = (new LockingManager()).synchronizedRequestLocking(customer_id,'x');
if (flag)
{
String rStr = Project.read(customer_id);
int x1 = rStr.indexOf("\t");
int x2 = rStr.indexOf("\t",x1+1);
String oldValueStr = rStr.substring(x2,rStr.length());
float oldValue = Float.parseFloat(oldValueStr);
boolean bool = false;
if (oldValue - transaction_amount >= 0.000001)
{
float newValue = oldValue - transaction_amount;
bool = Project.write (customer_id, newValue);
}
else
{
System.out.println("----------------------------");
System.out.println("ERROR>> 存款余額不夠!");
System.out.println("----------------------------");
bool = false;
}
(new LockingManager()).synchronizedReleaseLocking(customer_id,'x');
return bool;
}
else
{
return false;
}
}
catch(Exception e)
{
System.out.println("----------------------------");
System.out.println("ERROR>> 銀行數據文件錯誤");
System.out.println("----------------------------");
(new LockingManager()).synchronizedReleaseLocking(customer_id,'x');
return false;
}
}
/**
* 方法:deposit
* 功能:取錢
* 調用參數:客戶customer_id,存錢數量transaction_amount
* 返回值:成功為true;否則false;
* */
public static synchronized boolean deposit (int customer_id, float transaction_amount)
{
try
{
boolean flag = (new LockingManager()).synchronizedRequestLocking(customer_id,'x');
boolean bool = false;
if (flag)
{
String rStr = Project.read(customer_id);
int x1 = rStr.indexOf("\t");
int x2 = rStr.indexOf("\t",x1+1);
String oldValueStr = rStr.substring(x2,rStr.length());
float oldValue = Float.parseFloat(oldValueStr);
float newValue = oldValue + transaction_amount;
bool = Project.write (customer_id, newValue);
(new LockingManager()).synchronizedReleaseLocking(customer_id,'x');
}
return bool;
}
catch(Exception e)
{
System.out.println("----------------------------");
System.err.println("ERROR>> 銀行數據文件錯誤");
System.out.println("----------------------------");
(new LockingManager()).synchronizedReleaseLocking(customer_id,'x');
return false;
}
}
/**
* 方法:balanceCheck
* 功能:查詢帳戶
* 調用參數:客戶customer_id
* 返回值:成功返回查詢得到的結果字符串String,否則返回null
* */
public static synchronized String balanceCheck (int customer_id)
{
boolean flag = (new LockingManager()).synchronizedRequestLocking(customer_id,'s');
String str = null;
if (flag)
{
str = Project.read(customer_id);
(new LockingManager()).synchronizedReleaseLocking(customer_id,'s');
}
return str;
}
/**
* 方法:main
* 功能:提供測試以上幾個函數的功能。
* 調用參數:
* 返回值:
* */
public static void main(String[] args) {
/*
//測試數據1:
if (TransactManager.withDraw (101,200)==true)
{
System.out.println("通過啦");
}
else
{
System.out.println("出錯啦");
}
*/
/*
//測試數據2:
if (TransactManager.deposit(101,200)==true)
{
System.out.println("通過啦");
}
else
{
System.out.println("出錯啦");
}
*/
/*
//測試數據3:
String flagStr = null;
flagStr = TransactManager.balanceCheck(101);
if (flagStr!=null)
{
System.out.println(flagStr);
}
else
{
System.out.println("出錯啦");
}
*/
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -