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

📄 datavalidation.java

📁 A bank system
💻 JAVA
📖 第 1 页 / 共 2 页
字号:


import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * this class is for data validation. the readInput method of Command class 
 * will call the methods in this class to check the data.
 * @author CWANG SOMPARB
 *
 */
public class DataValidation 
{
	/**
	 * this method is core method for check a new customer's data.
	 * it will call other method such as checkCusId to check customer ID
	 * and other methods to check name,email, telephone, birthday,
	 * also it call isExistingCustomer class to check whether the customer ID
	 * has been used before.
	 * @param inputCommand
	 * @param customerArrayListObject
	 * @throws CommandException
	 */
	public void checkForNewCustomer(String inputCommand, 
									ArrayList<Customer> customerArrayListObject)throws CommandException
	{
		String delimiters = " ";
		StringTokenizer commandFactory = new StringTokenizer(inputCommand, delimiters);
		String command = commandFactory.nextToken();

		boolean isOK = true;
		String errorMsg = "";
		String cusID = commandFactory.nextToken();
		int cusIDInt = Integer.parseInt(cusID);
		String lName = commandFactory.nextToken();
		String fName = commandFactory.nextToken();
		String bDate = commandFactory.nextToken();
		String email = commandFactory.nextToken();
		String telNO = commandFactory.nextToken();
		
		if(!checkCusID(cusID))
		{
			isOK = false;
			errorMsg = errorMsg + "ERROR "+command+" : Invalid customerID"+"\n";
		}
		if(!checkName(lName))
		{
			isOK = false;
			errorMsg = errorMsg + "ERROR "+command+" : Invalid family name, no number or special character is allowed!"+"\n";
		}
		if(!checkName(fName))
		{
			isOK = false;
			errorMsg = errorMsg + "ERROR "+command+" : Invalid given name, no number or special character is allowed!"+"\n";
		}
		if(!checkBirthday(bDate))
		{
			isOK = false;
			errorMsg = errorMsg + "ERROR "+command+" : Invalid birthday!"+"\n";
		}
		if(!checkEmail(email))
		{
			isOK = false;
			errorMsg = errorMsg + "ERROR "+command+" : Invalid Email!"+"\n";
		}
		if(!checkTelephone(telNO))
		{
			isOK = false;
			errorMsg = errorMsg + "ERROR "+command+" : Invalid telephone number!"+"\n";
		}
		if (isExistingCustomer(cusIDInt, customerArrayListObject)){
			isOK = false;
			errorMsg = errorMsg + "ERROR "+command+" : Existing customer "+"\n";
		}
		if(!(isOK)){
			throw new CommandException(errorMsg);
		}
	}
	
	/**
	 * this is the core method for check a open account
	 * it call other methods to check account ID,open amount,
	 * and customer ID as well.
	 * @param inputCommand
	 * @throws CommandException
	 */
	public void checkForOpenAccount(String inputCommand)throws CommandException
	{
		boolean isOK = true;
		String errorMsg = "";
		String delimiters = " ";
		StringTokenizer commandFactory = new StringTokenizer(inputCommand, delimiters);
		String command = commandFactory.nextToken();
		String 	accNO 	= commandFactory.nextToken();
		String	initAmt = commandFactory.nextToken();
		if(!checkAccountID(accNO))
		{
			isOK = false;
			errorMsg = errorMsg+"ERROR "+command+": Invalid account ID"+"\n";
		}
		if(!checkAmount(initAmt))
			
		{
			isOK = false;
			errorMsg = errorMsg+"ERROR "+command+": Invalid amount"+"\n";
		}
		while(commandFactory.hasMoreTokens())
		{
		  String customerID 	= commandFactory.nextToken();
		  if(!checkCusID(customerID))
			{
			  isOK = false;
			  errorMsg = errorMsg+"ERROR "+command+": Invalid customer ID "+customerID+"\n";
			}
		}
		if(!(isOK)){
			throw new CommandException(errorMsg);
		}
		
	}
	
	/**
	 * This method is for check LoanAccount it slight different it needs to check
	 * the data of interest rate and the number of year.
	 * @param inputCommand
	 * @throws CommandException
	 */
	public void checkForOpenLoanAccount(String inputCommand)throws CommandException
	{
		boolean isOK = true;
		String errorMsg = "";
		String delimiters = " ";
		StringTokenizer commandFactory = new StringTokenizer(inputCommand, delimiters);
		String command = commandFactory.nextToken();
		
		String accNO 	= commandFactory.nextToken();
		String initAmt  = commandFactory.nextToken();
		String interest = commandFactory.nextToken();
		String noOfYear = commandFactory.nextToken();	
		
		if(!checkAccountID(accNO))
		{
			 isOK = false;
			 errorMsg = errorMsg+"ERROR "+command+": Invalid account ID"+"\n";
		}
		if(!checkAmount(initAmt))
		{
			 isOK = false;
			 errorMsg = errorMsg+"ERROR "+command+": Invalid amount"+"\n";
		}
		if(!checkInterest(interest))
		{
			 isOK = false;
			 errorMsg = errorMsg+"ERROR "+command+": Invalid interest"+"\n";
		}
		if(!checkNoOfYear(noOfYear))
		{
			isOK = false;
			errorMsg = errorMsg+"ERROR "+command+": Invalid noOfYear"+"\n";
		}
		while(commandFactory.hasMoreTokens())
		{
		  String customerID 	= commandFactory.nextToken();
		  if(!checkCusID(customerID))
			{
			  isOK = false;
			  errorMsg = errorMsg+"ERROR "+command+": Invalid customer ID "+customerID+"\n";
			}
		}
		if(!(isOK)){
			throw new CommandException(errorMsg);
		}
	}
	
	/**
	 * This method is check for DEPOSIT command
	 * it call other method to check the format of the account number is ok
	 * it also check whether this account is exist or closed
	 * it also check the format of the amount 
	 * and whether this amount is valid amount(>0)
	 * @param inputCommand
	 * @param accountArrayListObject
	 * @throws CommandException
	 */
	public void checkForDepositAccount(	String inputCommand,
										ArrayList<Account> accountArrayListObject)throws CommandException
	{
		boolean isOK = true;
		String errorMsg = "";
		String delimiters = " ";
		StringTokenizer commandFactory = new StringTokenizer(inputCommand, delimiters);
		String command = commandFactory.nextToken();
		String accNO = commandFactory.nextToken();
		String 	depositAmt = commandFactory.nextToken();
		int searchAccountResult = -1;
		
		if(!checkAccountID(accNO))
		{
			isOK = false;
			errorMsg = errorMsg+"ERROR "+command+" : Invalid account ID"+"\n";
		}else{
			int accNOInt	= Integer.parseInt(accNO);
			Command commandObject = new Command();
			searchAccountResult = commandObject.searchAccount(accNOInt,accountArrayListObject);
			
			if(searchAccountResult == -1){
				isOK = false;
				errorMsg = errorMsg+"ERROR "+command+" : There is no account "+accNO+" in the system"+"\n";
			}else{
				if(accountArrayListObject.get(searchAccountResult).getStatus() == 'C'){
					isOK = false;
					errorMsg = errorMsg+"ERROR "+command+" : This account "+accNO+" is closed already"+"\n";
				}
			}
		}
		if(!checkAmount(depositAmt))
		{
			isOK = false;
			errorMsg = errorMsg+"ERROR "+command+" : Invalid amount"+"\n";;
		}else{
			double 	depositAmtDouble = Double.parseDouble(depositAmt);
			if(depositAmtDouble <= 0){
				isOK = false;
				errorMsg = errorMsg+"ERROR "+command+" : The amount must be larger than 0"+"\n";
			}
		}
		if(!(isOK)){
			throw new CommandException(errorMsg);
		}
	}
	
	/**
	 * This method is similar to checkForDepositAccount, while it is for checking 
	 * WITHDRAW command
	 * @param inputCommand
	 * @param accountArrayListObject
	 * @throws CommandException
	 */
	public void checkForWithdrawAccount(String inputCommand,
										ArrayList<Account> accountArrayListObject)throws CommandException
	{
		boolean isOK = true;
		String errorMsg = "";
		String delimiters = " ";
		StringTokenizer commandFactory = new StringTokenizer(inputCommand, delimiters);
		String command = commandFactory.nextToken();
		String accNO = commandFactory.nextToken();
		String withdrawAmt = commandFactory.nextToken();
		int searchAccountResult = -1;
		
		if(!checkAccountID(accNO))
		{
			isOK = false;
			errorMsg = errorMsg+"ERROR "+command+" : Invalid account ID"+"\n";
		}else{
			int accNOInt = Integer.parseInt(accNO);
			Command commandObject = new Command();
			searchAccountResult = commandObject.searchAccount(accNOInt,accountArrayListObject);
			if(searchAccountResult == -1){
				isOK = false;
				errorMsg = errorMsg+"ERROR "+command+" : There is no account "+accNO+" in the system"+"\n";
			}else{
				if(accountArrayListObject.get(searchAccountResult).getStatus() == 'C'){
					isOK = false;
					errorMsg = errorMsg+"ERROR "+command+" : This account "+accNO+" is closed already"+"\n";
				}
			}
		}
		if(!checkAmount(withdrawAmt))
		{
			isOK = false;
			errorMsg = errorMsg+"ERROR "+command+" : Invalid amount"+"\n";
		}else{
			double 	withdrawAmtDouble = Double.parseDouble(withdrawAmt);
			if(withdrawAmtDouble <= 0){
				isOK = false;
				errorMsg = errorMsg+"ERROR "+command+" : The amount must be larger than 0"+"\n";
			}
		}
		if(!(isOK)){
			throw new CommandException(errorMsg);
		}
	}
	
	/**
	 * This method is for checking BALANCE command
	 * Mainly it check the format of the account and whether this account is closed or exist.
	 * @param inputCommand
	 * @param accountArrayListObject
	 * @throws CommandException
	 */
	public void checkForAccountBalance(	String inputCommand,
										ArrayList<Account> accountArrayListObject)throws CommandException
	{
		boolean isOK = true;
		String errorMsg = "";
		String delimiters = " ";
		StringTokenizer commandFactory = new StringTokenizer(inputCommand, delimiters);
		String command = commandFactory.nextToken();
		String accNO = commandFactory.nextToken();
		int searchAccountResult = -1;

		
		if(!(checkAccountID(accNO)))
		{	
			isOK = false;
			errorMsg = errorMsg+"ERROR "+command+" : Invalid account ID"+"\n";
		}else{
			int accNOInt = Integer.parseInt(accNO);
			Command commandObject = new Command();
			searchAccountResult = commandObject.searchAccount(accNOInt, accountArrayListObject);
			if(searchAccountResult == -1){
				isOK = false;
				errorMsg = errorMsg+"ERROR "+command+" : There is no account "+accNO+" in the system"+"\n";
			}
		}
		if(!(isOK)){
			throw new CommandException(errorMsg);
		}
	}
	
	/**
	 * This method is for checking TRANSACTION command
	 * Mainly it check the format of the account and whether this account is closed or exist.
	 * @param inputCommand
	 * @param accountArrayListObject
	 * @throws CommandException
	 */
	public void checkForAccountTransaction( String inputCommand,
											ArrayList<Account> accountArrayListObject)throws CommandException
	{
		boolean isOK = true;
		String errorMsg = "";
		String delimiters = " ";
		StringTokenizer commandFactory = new StringTokenizer(inputCommand, delimiters);
		String command = commandFactory.nextToken();
		String accNO = commandFactory.nextToken();
		int searchAccountResult = -1;

		
		if(!checkAccountID(accNO))
		{
			isOK = false;
			errorMsg = errorMsg+"ERROR "+command+" : Invalid account ID"+"\n";
		}else{
			int accNOInt = Integer.parseInt(accNO);
			Command commandObject = new Command();
			searchAccountResult = commandObject.searchAccount(accNOInt, accountArrayListObject);
			if(searchAccountResult == -1){
				isOK = false;
				errorMsg = errorMsg+"ERROR "+command+" : There is no account "+accNO+" in the system"+"\n";
			}
		}
		if(!(isOK)){
			throw new CommandException(errorMsg);
		}
	}
	
	/**
	 * This method is for checking CLOSE command
	 * Mainly it check the format of the account and whether this account is closed or exist.
	 * @param inputCommand
	 * @param accountArrayListObject
	 * @throws CommandException
	 */
	public void checkForCloseAccount(	String inputCommand, 
										ArrayList<Account> accountArrayListObject)throws CommandException
	{
		boolean isOK = true;
		String errorMsg = "";
		String delimiters = " ";
		StringTokenizer commandFactory = new StringTokenizer(inputCommand, delimiters);
		String command = commandFactory.nextToken();
		String accNO = commandFactory.nextToken();
		int searchAccountResult = -1;
		
		if(!checkAccountID(accNO))
		{
			isOK = false;
			errorMsg = errorMsg+"ERROR "+command+" : Invalid account ID"+"\n";
		}else{
			int accNOInt = Integer.parseInt(accNO);
			Command commandObject = new Command();
			searchAccountResult = commandObject.searchAccount(accNOInt,accountArrayListObject);
			if(searchAccountResult == -1){
				isOK = false;
				errorMsg = errorMsg+"ERROR "+command+" : There is no account "+accNO+" in the system"+"\n";
			}else{
				if(accountArrayListObject.get(searchAccountResult).getStatus() == 'C'){
					isOK = false;
					errorMsg = errorMsg+"ERROR "+command+" :This account "+accNO+" is closed already"+"\n";
				}
			}
		}
		if(!(isOK)){
			throw new CommandException(errorMsg);
		}
	}
	
	
	/**
	 * This method is for checking the format of interest rate.
	 * @param str
	 * @return
	 */
	public boolean checkInterest(String str)
	{
		double interest=0;
		try
		{
			interest = Double.parseDouble(str);
		}
		catch(NumberFormatException e)
		{
			return false;
		}
		if(interest <= 0)
		{
			return false;
		}
		return true;
	}
	
	
	/**
	 * This method is for checking the format of number of year.
	 * @param str
	 * @return
	 */
	public boolean checkNoOfYear(String str)
	{
		int noOfYear=0;
		try
		{
			noOfYear = Integer.parseInt(str);
		}
		catch(NumberFormatException e)
		{
			System.out.println("ERROR OPEN_SAVING: NumberFormatException interest ");
			return false;
		}
		if(noOfYear < 0)
		{
			return false;
		}
		return true;
	}
	
	/**
	 * This method is for checking the format of account number.
	 * @param str
	 * @return
	 */
	public boolean checkAccountID(String str)
	{
		int accountIDint;
		try
		{
			accountIDint = Integer.parseInt(str);
		}
		catch(NumberFormatException e)
		{
			return false;
		}
		if(accountIDint > 9999999 || accountIDint<1000000)
		{
			return false;
		}
		return true;
	}
	
	/**
	 * This method is for checking the format of amount.
	 * @param str
	 * @return
	 */
	public boolean checkAmount(String str)
	{
		
		double amount=0;
		try
		{
			amount = Double.parseDouble(str);
		}
		catch(NumberFormatException e)
		{
			return false;
		}
		if(amount < 0)

⌨️ 快捷键说明

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