Sunteți pe pagina 1din 3

//application developed by Cacior Marius Andrei

//Date: 31.10.2018
//the problem: 3 , laboratory 5

package Problema3;

public class Test {

public static void main(String[] args) {

CarRegistrationNumbers RegNum=new CarRegistrationNumbers();


RegNum.setCarRegistrationNumbers("B 59 CMA");

try{

if(RegNum.check()==true){
System.out.println("Registration numbers is correct!");
}

}
catch(MyException e){
System.out.println(e.getMessage());
}

}//end main

}//end Test
==========================================================
package Problema3;

public class CarRegistrationNumbers {

private String CarRegistrationNumbers;

public void setCarRegistrationNumbers(String CarRegistrationNumbers){


this.CarRegistrationNumbers=CarRegistrationNumbers;

}//end method

public String getCarRegistrationNumbers(){


return CarRegistrationNumbers;

}//end method

public boolean check() throws MyException{//[L{L}][NN{N}][LLL]


int []v= new int [2];//here we will store the positions of the free
spaces between the fields
int j=0;//with this variable we count the free spaces

//to check if the registration number is corect, we will go through following


steps:
//Step 1: we check if The size of the number is correct
//Step 2: we delimit registration number
//Step 3: we check the delimited fields (we must to have three fields)
//Step 3.1: we check if the first field is made up of letters
//Step 3.2: we check if the second field is made up of digits
//Step 3.3: we check if the last field is made up of letters
//Step 4: we check if the fields have the required size
//Step 4.1: we check first field
//Step 4.2: we check second field
//Step 4.3: we check last field

//Step 1: we check if The size of the number is correct


if((CarRegistrationNumbers.length()>9)||
(CarRegistrationNumbers.length()<8)){
throw new MyException("The size of the registration number is
incorrect!");
}//end if

//Step 2: we delimit registration number


for(int i=0;i<CarRegistrationNumbers.length();i++){
if(CarRegistrationNumbers.charAt(i) ==' '){
if(j>1){
throw new MyException("The registration number can't
have more than three fields!");
}
v[j]=i;
j++;
}
}//end for

//Step 3: we check the delimited fields (we must to have three fields)
//Step 3.1: we check if the first field is made up of letters
for(int i=0;i<v[0];i++){

if((CarRegistrationNumbers.charAt(i)>'Z')||
(CarRegistrationNumbers.charAt(i)<'A')){
throw new MyException("The first field of the registration
number can't contain digits!");

}//end if
}//end for

//Step 3.2: we check if the second field is made up of digits


for(int i=(v[0]+1);i<v[1];i++){

if((CarRegistrationNumbers.charAt(i)>'9')||
(CarRegistrationNumbers.charAt(i)<'0')){
throw new MyException("The second field of the registration
number can't contain letters!");

}//end if
}//end for

//Step 3.3: we check if the last field is made up of letters


for(int i=(v[1]+1);i<CarRegistrationNumbers.length();i++){

if((CarRegistrationNumbers.charAt(i)>'Z')||
(CarRegistrationNumbers.charAt(i)<'A')){
throw new MyException("The last field of the registration
number can't contain digits!");

}//end if
}//end for
//Step 4: we check if the fields have the required size
//Step 4.1: we check first field
if((v[0]>2)){
throw new MyException("The first field size of the registration number
is greater than two !");
}
if((v[0]<1)){
throw new MyException("The first field size of the registration number
is less than one !");
}

//Step 4.2: we check second field


int nr=v[1]-v[0]-1;
if(nr<2){
throw new MyException("The second field size of the registration
number is less than two !");
}
if(nr>3){
throw new MyException("The second field size of the registration
number is greater than three !");
}

//Step 4.3: we check last field


if(CarRegistrationNumbers.length()-(v[1]+1)!=3){
throw new MyException("The last field size of the registration number
is different than three !");
}//end if

return true;
}//end method

}//end Registration Numbers

===========================================================
package Problema3;

class MyException extends Exception{


//constructor
public MyException(String s){
super(s);
}

}//end MyException

S-ar putea să vă placă și