⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transactmanager.java

📁 一个java rmi实现的分布式银行管理系统
💻 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 + -