📄 datavalidation.java
字号:
{
return false;
}
return true;
}
/**
* This method is for checking the format of customer ID.
* @param str
* @return
*/
public boolean checkCusID(String str)
{
int cusIDint;
try
{
cusIDint = Integer.parseInt(str);
}
catch(NumberFormatException e)
{
return false;
}
if(cusIDint > 9999 || cusIDint<1000)
{
return false;
}
return true;
}
/**
* This method is for checking the format of customer ID.
* @param str
* @return
*/
public boolean checkName(String str)
{
if(!isLetter(str))
return false;
else
return true;
}
/**
* different system has a different meaning of a valid email
* but there are some common thing a email must has such as
* a email should only has one "@" and one or more "."
*
* @param str
* a String for check
* @return if it is, return true,otherwise return false
*/
public boolean checkEmail(String email)
{
String regex = "^[a-zA-Z0-9_]+@{1}[a-zA-Z0-9_]+[.]{1}[a-zA-Z]+[.a-zA-Z]*$";
Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(email);
if (!m.find())
{
return false;
}
return true;
}
/**
* only Telephone number"0123456789"
* length = 10
* @param str
* a String for check
* @return if it is return true,otherwise return false
*/
public boolean checkTelephone(String str)
{
if(!isNumber(str))
return false;
else if(str.length() != 10)
return false;
else return true;
}
/**
* DD/MM/YYYY
* DD between 01 and 31 MM between 01 and 12, YYYY not in future or
* not more than 180 years old
* @param str
* a String for check the format
* @return if it is return true,otherwise return false
*/
public boolean checkBirthday(String str)
{
String regex = "^[0-9]{2}/[0-9]{2}/[0-9]{4}$";
Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);
if (!m.find())
{
return false;
}
String delimiters = "/";
StringTokenizer birthday = new StringTokenizer(str, delimiters);
int date;
int month;
int year;
try
{
date = Integer.parseInt(birthday.nextToken());
month = Integer.parseInt(birthday.nextToken());
year = Integer.parseInt(birthday.nextToken());
}
catch(NumberFormatException e)
{
return false;
}
GregorianCalendar now = new GregorianCalendar();
int yearNow=now.get(GregorianCalendar.YEAR);
int monthNow=now.get(GregorianCalendar.MONTH);
int dateNow=now.get(GregorianCalendar.DATE);
if(month > 12 || month < 1)
{
return false;
}
if(date < 0 || date >31 )
{
return false;
}
if(month == 2 && date >28 )
{
return false;
}
if((month == 4 || month == 6 ||month ==9 ||month ==11)&& date>30)
{
return false;
}
// check this person is not a furture one or larger than 180
if(yearNow-year>=180 || yearNow-year<0)
{
return false;
}
if(year == 0)
{
if(monthNow-month<0)
return false;
else if(dateNow-date<0)
return false;
}
// end of check this person is not a furture one
return true;
}
/**
* only number"0123456789"
*
* @param str
* a String for check
* @return if it is return true,otherwise return false
*/
public boolean isNumber(String str)
{
int i, j;
String strTemp = "0123456789";
for (i = 0; i < str.length(); i++)
{
j = strTemp.indexOf(str.charAt(i));
if (j == -1)
{//
return false;
}
}
return true;
}
/**
* str has strTemp
*
* @param str,strTemp
*
* @return if it is return true,otherwise return false
*/
public boolean hasSomething(String str,char strTemp)
{
int j = str.indexOf(strTemp);
if (j == -1)
{
return false;
}
return true;
}
/**
* whether it is Letter"abcdefghijklnmopqrstuvwxyzABCDEFGHIJKLNMOPQRSTUVWXYZ"
*
* @param str
* a String for check
* @return if it is return true,otherwise return false
*/
public boolean isLetter(String str)
{
int i, j;
String strTemp = "abcdefghijklnmopqrstuvwxyzABCDEFGHIJKLNMOPQRSTUVWXYZ";
for (i = 0; i < str.length(); i++)
{
j = strTemp.indexOf(str.charAt(i));
if (j == -1)
{// it has something else not only letter "abc..."
return false;
}
}
return true;
}
/**
* This method is checking of Business rule such as how many account a customer can have
* one account can be owned buy how many customer.(call other method to do the specific thing)
* @param inputCommand
* @param minValue
* @param accountArrayListObject
* @param customerArrayListObject
* @throws CommandException
*/
public void checkBusinessRuleForOpenAccount(String inputCommand,
double minValue,
ArrayList<Account> accountArrayListObject,
ArrayList<Customer> customerArrayListObject)throws CommandException{
String delimiters = " ";
StringTokenizer commandFactory = new StringTokenizer(inputCommand, delimiters);
String command = commandFactory.nextToken();
int accNO = Integer.parseInt(commandFactory.nextToken());
double initAmt = Double.parseDouble(commandFactory.nextToken());
if(command.equalsIgnoreCase("OPEN_LOAN")){
double interest= Double.parseDouble(commandFactory.nextToken());
double noOfYear= Double.parseDouble(commandFactory.nextToken());
}
String errorMsgExistingCustomer ="";
String errorMsgNumberOfAccountOwn ="";
String errorMsgPermitCustomerAge ="";
String errorMsg ="";
boolean isPermitAge = true;
boolean isMeetCriteria = true;
boolean checkCustomerID = true;
boolean checkNumberOfAccount = true;
int numberOfAccountOwner = 0;
while(commandFactory.hasMoreTokens()){
int customerID = Integer.parseInt(commandFactory.nextToken());
if(!(isExistingCustomer(customerID, customerArrayListObject))){
checkCustomerID = false;
isMeetCriteria = false;
errorMsgExistingCustomer = errorMsgExistingCustomer+" "+customerID;
} else{
if(command.equalsIgnoreCase("OPEN_CREDIT")){
if(!(checkCustomerAge(customerID, customerArrayListObject))){
isPermitAge = false;
isMeetCriteria = false;
errorMsgPermitCustomerAge = errorMsgPermitCustomerAge+" "+customerID;
}
}
}
if(!(checkNumberOfAccountOwn(customerID, accountArrayListObject))){
checkNumberOfAccount = false;
isMeetCriteria = false;
errorMsgNumberOfAccountOwn = errorMsgNumberOfAccountOwn+" "+customerID;
}
numberOfAccountOwner++;
}
if(initAmt < 0){
isMeetCriteria = false;
errorMsg = errorMsg+"ERROR "+command+" : The initial amount must not be less than 0" + "\n";
}
if(!(checkCustomerID)){
isMeetCriteria = false;
errorMsg = errorMsg+"ERROR "+command+" : There is no customer "+errorMsgExistingCustomer+" in the system" + "\n";
}
if(!(checkNumberOfAccount)){
isMeetCriteria = false;
errorMsg = errorMsg+"ERROR "+command+" : Customer "+errorMsgNumberOfAccountOwn+" cannot have more than "+Command.maxAccount+" accounts"+"\n" ;
}
if(checkExistingAccount(accNO, accountArrayListObject)){
isMeetCriteria = false;
errorMsg = errorMsg+"ERROR "+command+" : There an account "+accNO+" in system already"+"\n" ;
}
if(numberOfAccountOwner > Command.maxOwner){
isMeetCriteria = false;
errorMsg = errorMsg+"ERROR "+command+" : Cannot have account owner more than "+Command.maxOwner+"\n";
}
//if(initAmt < SavingAccount.minimumBalance){
if(initAmt < minValue){
isMeetCriteria = false;
errorMsg = errorMsg+"ERROR "+command+" : The minimum initial deposit or credit limit or minimum loan is invalid"+"\n" ;
}
if(command.equalsIgnoreCase("OPEN_CREDIT")){
if(!(isPermitAge)){
errorMsg = errorMsg+"ERROR "+command+" : Customer"+errorMsgPermitCustomerAge+" must be older than"+CreditAccount.minCreditAge+"\n";
}
}
if(!(isMeetCriteria)){
throw new CommandException(errorMsg);
}
}
/**
* This method is for checking whether the account exist.
* @param accountNumber
* @param accountArrayListObject
* @return
*/
public boolean checkExistingAccount(int accountNumber, ArrayList<Account> accountArrayListObject){
for(Account account :accountArrayListObject){
if(account.getAccountNumber() == accountNumber){
return true;
}
}
return false;
}
/**
* This method is checking a customer can not have more than maxAccount
* @param cusID
* @param accountArrayListObject
* @return
*/
public boolean checkNumberOfAccountOwn(int cusID, ArrayList<Account> accountArrayListObject){
int numberOfCustomer = 0;
//if this customer appear numberOfCustomer times,then it means this customer has numberOfCustomer times account.
for(Account account :accountArrayListObject){
ArrayList<Customer> cAl = new ArrayList<Customer>();
cAl =accountArrayListObject.get(accountArrayListObject.indexOf(account)).getAccountOwnerArrayList();
for(Customer customer: cAl){
if(customer.getCustomerID() == cusID){
numberOfCustomer++;
}
}
}
if(numberOfCustomer < Command.maxAccount){
return true;
}else{
return false;
}
}
/**
* This method is checking whether the cusomer is exist.
*
* @param customerID
* @param customerArrayListObject
* @return
*/
public boolean isExistingCustomer(int customerID, ArrayList<Customer> customerArrayListObject){
for(Customer customer :customerArrayListObject){
if(customer.getCustomerID() == customerID){
return true;
}
}
return false;
}
/**
* This method is checking when open a credit account the age should above minCreditAge
* @param customerID
* @param customerArrayListObject
* @return
*/
public boolean checkCustomerAge(int customerID, ArrayList<Customer> customerArrayListObject){
for(Customer customer :customerArrayListObject){
if(customer.getCustomerID() == customerID){
if(getAge(customerID,customer.getBirthDate()) >= CreditAccount.minCreditAge){
return true;
}
}
}
return false;
}
/**
* This method is get the year from a customer's birthday
* @param str
* @return
*/
public String getYear(String str)
{
int i;
String strTemp="";
for (i = 6; i < str.length(); i++)
{
strTemp=strTemp+str.charAt(i);
}
return strTemp;
}
/**
* This method is to get the age of a customer
* @param customerID
* @param str
* @return
*/
public int getAge(int customerID,String str)
{
int year=Integer.parseInt(getYear(str));
GregorianCalendar now = new GregorianCalendar();
int yearNow=now.get(GregorianCalendar.YEAR);
int age = yearNow - year;
return age;
}
/**
* This method is check whether the user input a complete command.
* @param inputCommand
* @throws CommandException
*/
public void isCompleteCommand(String inputCommand)throws CommandException
{
boolean isComplete = true;
String delimiters = " ";
StringTokenizer commandFactory = new StringTokenizer(inputCommand, delimiters);
String command = commandFactory.nextToken();
if (command.equalsIgnoreCase("NEW")){
if(commandFactory.countTokens() != 6){
isComplete = false;
}
}
else if(command.equalsIgnoreCase("OPEN_SAVING")){
if(commandFactory.countTokens() < 3){
isComplete = false;
}
}
else if(command.equalsIgnoreCase("OPEN_CREDIT")){
if(commandFactory.countTokens() < 3){
isComplete = false;
}
}
else if(command.equalsIgnoreCase("OPEN_LOAN")){
if(commandFactory.countTokens() < 5){
isComplete = false;
}
}
else if(command.equalsIgnoreCase("DEPOSIT")){
if(commandFactory.countTokens() != 2){
isComplete = false;
}
}
else if(command.equalsIgnoreCase("WITHDRAW")){
if(commandFactory.countTokens() != 2){
isComplete = false;
}
}
else if(command.equalsIgnoreCase("BALANCE")){
if(commandFactory.countTokens() != 1){
isComplete = false;
}
}
else if(command.equalsIgnoreCase("TRANSACTIONS")){
if(commandFactory.countTokens() != 1){
isComplete = false;
}
}
else if(command.equalsIgnoreCase("CLOSE")){
if(commandFactory.countTokens() != 1){
isComplete = false;
}
}
if(!(isComplete)){
throw new CommandException("ERROR INPUT COMMAND : Input command is not complete\n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -