Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class TechShip2 {
- /**
- * Confirms that input from user is valid for registration
- * @param invoice_address Input array
- * @return String with specific error message if invalid input, null if valid input
- * @author Mathias Lammers
- */
- static public String validate_input (ArrayList<String> invoice_address) {
- // Here I assume that fields that hasn't been entered is considered empty Strings
- // rather than null objects. If that was the case, then null checking has to be performed:
- // If String is null, then either set to empty String (which results in error message) or
- // immediately return error message
- int countryInt;
- String address = invoice_address.get(0);
- String zipCode = invoice_address.get(1);
- String countryID = invoice_address.get(2);
- String address2 = invoice_address.get(3);
- String city = invoice_address.get(4);
- // Since TechShip has customers from all over the world, with different languages and
- // address formats, I opted for simply checking the length of the String rather than
- // matching with regular expressions. One improvement would be allowing special
- // characters but no digits when checking for example city input
- // Confirm that address is at least three characters long
- if (address.length() < 3 )
- return "Please enter valid primary address";
- // Confirms that zip code is only digits and at least three numbers
- if (!zipCode.chars().allMatch(Character::isDigit) || zipCode.length() < 3)
- return "Please enter valid zip code";
- // Parsing countryID String to int. If null, set to 0 (results in error message)
- try {
- if(countryID != null)
- countryInt = Integer.parseInt(countryID);
- else
- countryInt = 0;
- } catch (NumberFormatException e) {
- countryInt = 0;
- }
- // Confirms that a country in the list has been selected (range of 1-250)
- if (countryInt < 1 || countryInt > 250)
- return "Please select country";
- // Confirms that Address 2 field is either empty or at least three characters
- if (!address2.isEmpty() && address2.length() < 3)
- return "Please enter valid secondary address";
- // Confirms that city is at least three characters
- if (city.length() < 3)
- return "Please enter valid city";
- // Returns null if valid input
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement