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

📄 banksys.java

📁 这是一个关于银行存取款的程序
💻 JAVA
字号:
/***********************************************
about : the program about bank exchange system.
Author: lipengyu
Date  : 17-6-02
***********************************************/
/**********************************************
className: BanksysTest
function : Test the bank system about all his
	   operations.
**********************************************/
public class Banksys{
	public static void main(String[] args)
	{
                try{
                	System.out.println("***************************************************");
                	System.out.println("The Bank system ");
                	System.out.println("now there are three customer in the bank.");
                	System.out.println("They are Depositing or withdrawing or exchanging ");
                	System.out.println("***************************************************");
                	System.out.println("  ");
			Bank bankSys = new Bank();
			int i ;
			for(i = 0; i < INITIAL_CUSTOMER; i++)
			{
				String   strName     = "customer"+i;
				int      iThreadID   = i+1;
				Customer customerObj = new Customer(bankSys, iThreadID, strName);
				customerObj.setPriority(Thread.NORM_PRIORITY + i % 2);
				customerObj.start();
			}
			Thread.currentThread().sleep(4000);
			bankSys.ShowAllAccounts();
		   }
		   catch(InterruptedException e){}
	}
	public static final int INITIAL_CUSTOMER = 3;
}

/*********************************************
className: Bank
function : a Bank with all its operation,including
	   deposite , exchange, withDraw.
*********************************************/
class Bank{
	Rate    rateArray[] ;     //accurency rate array.
	Account accountArray[] ;  //account array.
	public Bank()
	{
                rateArray    = new Rate[INITIAL_RATE];
                accountArray = new Account[INITIAL_ACCOUNT];
		rateArray[0] = new Rate("RMB", "RMB", 1.00f);
		rateArray[1] = new Rate("USD", "RMB", 8.1f);
	}
	public synchronized boolean OpenAccount(Customer customerObj, float InitialCnt)
	{
		String strName    = customerObj.GetName();
		String strID      = "Acc"+ currentAccount;
		if(currentAccount > INITIAL_ACCOUNT)
		{
			return false;
		}
		accountArray[currentAccount] = new Account(strName, strID, "RMB.", InitialCnt);
		customerObj.SetLoginID(strID);
		currentAccount++;
		return true;
	}
	private int FindRateNum(String TargetType, String BaseType)
	{
		int i;
		for(i = 0; i < INITIAL_RATE; i++)
		{
			if(rateArray[i].IsRate(TargetType, BaseType))
			return i;
		}
		return i;
	}
	public Money  exchangeMoney(String TargetType, Money exchangeMoney)
	{
		Money DefaultMoney = new RMB(-1);
                Money retMoney;
		String BaseType = exchangeMoney.toString();
                float   exchangeCnt    =     exchangeMoney.GetValue();
		int   rateNum  = FindRateNum(TargetType, BaseType);
		if(rateNum >= INITIAL_RATE)
		{
			System.out.println("the exchangeRate does not exist");
			return DefaultMoney;
                }
		float rate     = rateArray[rateNum].GetRate(TargetType, BaseType);
		if(rate < 0)
		{
			System.out.println("the exchangeRate does not exist");			retMoney = new RMB(-1);
			return DefaultMoney;
		}
		if(0 == BaseType.compareTo("RMB"))
		{
			retMoney = new RMB(exchangeCnt/rate);
			return retMoney;
		}
		if(0 == BaseType.compareTo("USD"))
		{
			retMoney = new USD(exchangeCnt/rate);
			return retMoney;
		}
		return DefaultMoney;
	}
	private int FindAccountIndex(String strName, String strID)
	{
		int i;
		for(i = 0; i < currentAccount; i++)
		{
			if(accountArray[i].IsAccount(strName, strID))
			return i;
		}
		return INITIAL_ACCOUNT;

	}
	public boolean Deposit(String strName, String strID, Money DepositMoney)
	{
		boolean retValue = false;
		float   fWantNum;
		Money   wantMoney ;
		String  currencyType = DepositMoney.toString();
		int AccountIndex = FindAccountIndex(strName, strID);
		if(AccountIndex >= INITIAL_ACCOUNT)
		{
			System.out.println("the Account does not exist");
			return retValue;
		}
		if(0 == currencyType.compareTo("RMB"))
		{
			wantMoney = exchangeMoney("RMB", DepositMoney);
			fWantNum = wantMoney.GetValue() ;
			if( fWantNum< 0)
			{
				System.out.println("the Deposit error");
				return retValue;
			}
		}
		else{
                        wantMoney = DepositMoney;
			fWantNum = wantMoney.GetValue();
		}
		retValue = accountArray[AccountIndex].Deposit(strName, strID, fWantNum);
		return retValue;
	}
	public boolean WithDraw(String strName, String strID, Money withDrawMoney)
	{
		boolean retValue = false;
		float   fWantNum;
		Money   wantMoney;
		String  currencyType = withDrawMoney.toString();
		int AccountIndex = FindAccountIndex(strName, strID);
		if(AccountIndex >= INITIAL_ACCOUNT)
		{
			System.out.println("the Account does not exist");
			return retValue;
		}
		if(0 == currencyType.compareTo("RMB"))
		{
			wantMoney = exchangeMoney("RMB", withDrawMoney);
			fWantNum  = wantMoney.GetValue();
			if(fWantNum < 0)
			{
				System.out.println("the Deposit error");
				return retValue;
			}
		}
		else{
                        wantMoney = withDrawMoney;
			fWantNum  = wantMoney.GetValue();
		}
		retValue = accountArray[AccountIndex].WithDraw(strName, strID, fWantNum);
		return retValue;
	}
	private void SortByName()
	{
		int i,j;
		for(i = 0 ; i < currentAccount; i++ )
		{
			String stri = accountArray[i].GetName();
		        for(j = i+1; j < currentAccount; j++)
		   	{
		   		String strj = accountArray[j].GetName();
		   		if(stri.compareTo(strj) > 0)
		   		{
		   			Account accounti = accountArray[i];
		   			accountArray[i]  = accountArray[j];
		   			accountArray[j]  = accounti;
		   		}
		   	}
		}
	}
	private void SortByAccountNum()
	{
		int i,j;
		for(i = 0 ; i < currentAccount; i++ )
		{
			float Numi = accountArray[i].GetAccountNum();
		        for(j = i+1; j < currentAccount; j++)
		   	{
		   		float Numj = accountArray[j].GetAccountNum();
		   		if(Numj < Numi)
		   		{
		   			Account accounti = accountArray[i];
		   			accountArray[i]  = accountArray[j];
		   			accountArray[j]  = accounti;
		   		}
		   	}
		}		
	}
	public void ShowAllAccounts()
	{
		int i;
		String pStr;
		System.out.println(" " );
		System.out.println("the Account Infomation of the Bank is order by name:" );
		SortByName();
		for(i = 0; i < currentAccount; i++ )
		{
			pStr = accountArray[i].toString();
			System.out.println(pStr);
		}
		System.out.println("the Account Infomation of the Bank is order by AccountNum:" );
		SortByAccountNum();
		for(i = 0; i < currentAccount; i++ )
		{
			pStr = accountArray[i].toString();
			System.out.println(pStr);
		}		
	}
	public String toString()
	{
		String pStr;
		pStr = "the Bank system has the functions of deposit, withdraw, exchange and so on.";
		return pStr;
	}
	static int    currentAccount = 0;
	public static final int INITIAL_RATE    = 2;
	public static final int INITIAL_ACCOUNT = 10;
}

/**************************************************
className: Rate
function : currency Rate in exchanging.
**************************************************/
class Rate{
	private String currency;
	private String baseCurrency;
	private float  exchangeRate;
	public  Rate(String strCurrency, String strBaseCurrency, float fExchangeRate)
	{
		currency     = strCurrency;
		baseCurrency = strBaseCurrency;
		exchangeRate = fExchangeRate;
	}
	public boolean IsRate(String strCurrency, String strBaseCurrency)
	{
		if((0 == baseCurrency.compareTo(strBaseCurrency)) && (0 == currency.compareTo(strCurrency)))
		{
			return true;
		}
		if((0 == baseCurrency.compareTo(strCurrency)) && (0 == currency.compareTo(strBaseCurrency)))
		{
			return true;
		}
		return false;
	}
	public void Reset(String strCurrency, String strBaseCurrency, float fExchangeRate)
	{
		currency     = strCurrency;
		baseCurrency = strBaseCurrency;
		exchangeRate = fExchangeRate;
	}
	public float GetRate(String TargetType, String BaseType)
	{
		if((0 == baseCurrency.compareTo(BaseType)) && (0 == currency.compareTo(TargetType)))
		{
			return exchangeRate;
		}
		if((0 == baseCurrency.compareTo(TargetType)) && (0 == currency.compareTo(BaseType)))
		{
			return (1/exchangeRate);
		}
		return -1;

	}
	public String toString()
	{
		String pStr = currency+" to "+baseCurrency+" is "+exchangeRate;
		return pStr;
	}
}

/*************************************
className:  Account
function :  Recode all the account infomation
	    of a customer.
*************************************/
class Account{
	private String name;
	private String ID;
	private String currencyType;
	private float  accountNum;
	public  Account(String strName, String strID, String strType, float fAccountNum)
	{
		name         = strName;
		ID           = strID;
		currencyType = strType;
		accountNum   = fAccountNum;
	}
	public String GetName()
	{
		return name;
	}
	public float  GetAccountNum()
	{
		return accountNum;
	}
	public boolean IsAccount(String strName, String strID)
	{
		if((0 == name.compareTo(strName)) && (0 == ID.compareTo(strID)))
		{
			return true;
		}
		return false;

	}
	public boolean  Deposit(String strName, String strID, float fDepositNum)
	{
		boolean retValue = false;
		try{
			if((0 == name.compareTo(strName)) && (0 == ID.compareTo(strID)))
			{
				accountNum += fDepositNum;
				System.out.println(name+" has deposited "+ fDepositNum+" RMB. money");
				retValue = true;
			}
			else{
			System.out.println("the wrong name or ID");
			}
		}
		catch(RuntimeException e)
                {}
                return   retValue;
	}
	public boolean WithDraw(String strName, String strID, float fWithDrawNum)
	{
		boolean retValue = false;
		try{
			if((0 == name.compareTo(strName)) && (0 == ID.compareTo(strID)))
			{
				if((accountNum- fWithDrawNum)< 0)
				{
					System.out.println(name+" has not enough money now!");
					return false;
				}
				accountNum -= fWithDrawNum;
				System.out.println(name+" has withDrawed "+fWithDrawNum+" RMB. money");
				retValue   =  true;
			}
			else{
			        System.out.println("the wrong name or ID");
			}

		}
		catch(RuntimeException e)
            	{}
                return   retValue;
	}
	public String toString()
	{
		String pStr ="the account info is\n"+ "\t\tName: "+name+"\n";
		pStr += "\t\tID: "+ID+"\n";
		pStr += "\t\tcount: "+accountNum+"  "+currencyType;
		return pStr;
	}
}

/******************************************************
className: Money
function : a the abstract object discribed money.
******************************************************/
abstract class Money{
	public abstract void SetValue(float fValue);
	public abstract float GetValue();
	public abstract  String  toString();
}
class RMB extends Money{
	float moneyNum;
	public RMB(float fValue)
	{
		moneyNum = fValue;
	}
	public void SetValue(float fValue)
	{
		moneyNum = fValue;
	}
	public float GetValue()
	{
		return moneyNum;
	}
	public String toString()
	{
		String pStr = "RMB";
		return pStr;
	}
}
class USD extends Money{
	float moneyNum;
	public USD(float fValue)
	{
		moneyNum = fValue;
	}
	public void SetValue(float fValue)
	{
		moneyNum = fValue;
	}
	public float GetValue()
	{
		return moneyNum;
	}
	public String toString()
	{
		String pStr = "USD";
		return pStr;
	}
}
/*****************************************
className : Customer
function  : the customer of the bank system
	    can deposit,withdraw money.
******************************************/
class Customer extends Thread{
	int threadId;
	String name;
	String loginID;
	Bank   bankSys;
        public   static   final   float INITIAL_NUM  =500.00f;
	public Customer(Bank bankObj, int iThreadId, String strName)
	{
		bankSys  = bankObj;
		threadId = iThreadId;
		name     = strName;
	}
	public void SetLoginID(String strLoginID)
	{
		loginID = strLoginID;
	}
	public String toString()
	{
		String pStr = "customer name: "+name+"\n";
		return pStr;
	}
	public String GetName()
	{
		return name;
	}
	public String GetLoginID()
	{
		return loginID;
	}
	public void run()
	{
		Money depositMoney, withDrawMoney, exchangeMoney;
		bankSys.OpenAccount(this,   INITIAL_NUM  );
		float fDepositNum = (int)(threadId*Math.random()*100);
		if(fDepositNum > 500)
		{
			fDepositNum = 100;
		}
		depositMoney = new RMB(fDepositNum);
		bankSys.Deposit(name, loginID, depositMoney);

		float fWithDrawNum = (int)(threadId*Math.random()*100);;
		if(fWithDrawNum > 500)
		{
			fWithDrawNum = 100;
		}
		withDrawMoney = new USD(fWithDrawNum);
		bankSys.WithDraw(name, loginID, withDrawMoney);
		float exchangeCnt = (int)(threadId*Math.random()*100);;
		if(exchangeCnt > 500)
		{
			exchangeCnt = 100;
		}
		exchangeMoney = new RMB(exchangeCnt);
		Money wantMoney =  bankSys.exchangeMoney("USD", exchangeMoney);
		float fwantNum  =  wantMoney.GetValue();
		System.out.println(name+" has exchange "+fwantNum+" USD. money");
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -