Advertisement
Guest User

Koduppgift 2

a guest
Jan 20th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class TechShip2 {
  4.  
  5.     /**
  6.      * Confirms that input from user is valid for registration
  7.      * @param invoice_address Input array
  8.      * @return String with specific error message if invalid input, null if valid input
  9.      * @author Mathias Lammers
  10.      */
  11.     static public String validate_input (ArrayList<String> invoice_address) {
  12.  
  13.         // Here I assume that fields that hasn't been entered is considered empty Strings
  14.         // rather than null objects. If that was the case, then null checking has to be performed:
  15.         // If String is null, then either set to empty String (which results in error message) or
  16.         // immediately return error message
  17.  
  18.         int countryInt;
  19.         String address = invoice_address.get(0);
  20.         String zipCode = invoice_address.get(1);
  21.         String countryID = invoice_address.get(2);
  22.         String address2 = invoice_address.get(3);
  23.         String city = invoice_address.get(4);
  24.  
  25.         // Since TechShip has customers from all over the world, with different languages and
  26.         // address formats, I opted for simply checking the length of the String rather than
  27.         // matching with regular expressions. One improvement would be allowing special
  28.         // characters but no digits when checking for example city input
  29.  
  30.         // Confirm that address is at least three characters long
  31.         if (address.length() < 3 )
  32.             return "Please enter valid primary address";
  33.  
  34.         // Confirms that zip code is only digits and at least three numbers
  35.         if (!zipCode.chars().allMatch(Character::isDigit) || zipCode.length() < 3)
  36.             return "Please enter valid zip code";
  37.  
  38.         // Parsing countryID String to int. If null, set to 0 (results in error message)
  39.         try {
  40.             if(countryID != null)
  41.                 countryInt = Integer.parseInt(countryID);
  42.             else
  43.                 countryInt = 0;
  44.         } catch (NumberFormatException e) {
  45.             countryInt = 0;
  46.         }
  47.  
  48.         // Confirms that a country in the list has been selected (range of 1-250)
  49.         if (countryInt < 1 || countryInt > 250)
  50.             return "Please select country";
  51.  
  52.         // Confirms that Address 2 field is either empty or at least three characters
  53.         if (!address2.isEmpty() && address2.length() < 3)
  54.             return "Please enter valid secondary address";
  55.  
  56.         // Confirms that city is at least three characters
  57.         if (city.length() < 3)
  58.             return "Please enter valid city";
  59.  
  60.         // Returns null if valid input
  61.         return null;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement