Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class PlateChecker {
  5.  
  6. public static final int OLD_PLATE=-1;
  7. public static final int NEW_PLATE=1;
  8. public static final int UNRECOGNIZED=0;
  9.  
  10. public PlateChecker(){
  11.  
  12. }
  13. /*
  14. * metodo che controlla se il tipo di targa inserita è una targa vecchio stile, nuovo stile o sconosciuta.
  15. * @param String plate : la targa inserita;
  16. * @return int result : -1 se vecchio stile, 1 se nuovo stile o 0 se sconosciuta;
  17. */
  18. public static int check(String plate){
  19.  
  20. assert(plate!=null):"la targa non può essere nulla";
  21.  
  22. char[]plateArray= plate.toCharArray();
  23. for (int j=0;j<plate.length();j++) {
  24. assert(Character.isLetterOrDigit(plateArray[j])==true):"la stringa nn è composta da caratteri alfaumerici";
  25. //assert(plateArray[j]>='A'&& plateArray[j]<='Z'||plateArray[j]>=0 && plateArray[j]<=9 ):"la stringa nn è composta da caratteri alfaumerici";
  26. }
  27.  
  28. int result=-9;
  29.  
  30. int contOld=0;
  31. int contNew=0;
  32. char lookMe;
  33.  
  34.  
  35. boolean isTotalOld=false;
  36.  
  37.  
  38. boolean isTotalNew=false;
  39.  
  40. //-------------check for old_plate--------------------------------------------
  41. if (plate.length()==8) {
  42.  
  43. for (int i=0;i<2;i++) {
  44. if(plate.charAt(i)>='A' && plate.charAt(i)<='Z'){
  45. contOld+=1;
  46. }
  47. }
  48. for (int i=3;i<8;i++) {
  49. if(plate.charAt(i)>=0 && plate.charAt(i)<=9){
  50. contOld+=1;
  51.  
  52. }
  53. }
  54. if (plate.charAt(2)>='A' && plate.charAt(2)<='Z' || plate.charAt(2)>=0 && plate.charAt(2)<=9){
  55. contOld+=1;
  56. }
  57. }
  58. //--------------check for new_plate-------------------------------------------------------------
  59. if (plate.length()==7) {
  60.  
  61. for (int j=2;j<5;j++) {
  62.  
  63. if(plate.charAt(j)>='A' && plate.charAt(j)<='Z' && plate.charAt(j)!='G' && plate.charAt(j)!='I' &&
  64. plate.charAt(j)!='O' && plate.charAt(j)!='Q' && plate.charAt(j)!='U' ) contNew+=1;
  65. }
  66. for (int j=0;j<2;j++) {
  67. lookMe=plate.charAt(j);
  68. if( plate.charAt(j)<=9){//&&plate.charAt(j)>=0
  69. contNew+=1;
  70.  
  71. }
  72.  
  73. }
  74. for (int j=5;j<7;j++) {
  75. if( plate.charAt(j)<=9){//&& plate.charAt(j)>=0
  76. contNew+=1;
  77. }
  78. }
  79. }
  80.  
  81. if(contOld==8)isTotalOld=true;
  82. if(contNew==7)isTotalNew=true;
  83.  
  84. if(isTotalOld) result=-1;
  85. if(isTotalNew) result=1;
  86. if(!isTotalOld && !isTotalNew) result=0;
  87.  
  88. return result;
  89.  
  90. }
  91.  
  92.  
  93.  
  94. public static void main(String[]args){
  95.  
  96. //System.out.println("inserisci una targa");
  97. Scanner in=new Scanner(System.in);
  98. //String inPlate=in.next();
  99.  
  100. String esNewPlate="12PLL34";
  101. String esOldPlate="PL312345";
  102.  
  103.  
  104.  
  105. if (check(esNewPlate)==OLD_PLATE)System.out.println("OLD-PLATE");
  106. if (check(esNewPlate)==NEW_PLATE)System.out.println("NEW-PLATE");
  107. if (check(esNewPlate)==UNRECOGNIZED)System.out.println("UNRECOGNIZED");
  108.  
  109. if (check(esOldPlate)==OLD_PLATE)System.out.println("OLD-PLATE");
  110. if (check(esOldPlate)==NEW_PLATE)System.out.println("NEW-PLATE");
  111. if (check(esOldPlate)==UNRECOGNIZED)System.out.println("UNRECOGNIZED");
  112.  
  113.  
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement