Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1. package Project;
  2.  
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.Locale;
  7. import java.util.Scanner;
  8.  
  9. public class Customer {
  10.  
  11. private final Scanner sc = new Scanner(System.in);
  12.  
  13. private String customerFirstName;
  14. private String customerLastName;
  15. private String customerAddress;
  16. private Date customerDateOfBirth;
  17. private int customerPhoneNumber;
  18. private int customerCPR;
  19. private int customerPostCode;
  20. private String customerUsername = "";
  21. private String customerPassword = "";
  22.  
  23. public Customer createAccount()
  24. {
  25. System.out.println("-- Account creator --");
  26. Customer c = new Customer();
  27.  
  28. //Prompts the user to enter their first name and checks if it contains numbers.
  29. System.out.println("Please enter your first name:");
  30. String temp = sc.next();
  31.  
  32. while(!isNameValid(temp))
  33. {
  34. System.out.println("Your name should not contain numbers. Please try again.");
  35. temp = sc.next();
  36. }
  37. c.customerFirstName = temp;
  38.  
  39. //Prompts the user to enter their last name and checks if it contains numbers.
  40. System.out.println("Please enter your last name:");
  41. temp = sc.next();
  42.  
  43. while(!isNameValid(temp))
  44. {
  45. System.out.println("Your name should not contain numbers. Please try again");
  46. temp = sc.next();
  47. }
  48. c.customerLastName = temp;
  49.  
  50. //Prompts the user to enter their address with no checks.
  51. System.out.println("Please enter your address:");
  52. c.customerAddress = sc.next();
  53.  
  54. //Prompts the user to enter their postal code and check if it's within it's boundaries.
  55. System.out.println("Please enter your 4-digit postal code. The code has to be less or equal to 2500.");
  56. temp = sc.next();
  57.  
  58. while(!isPostCodeValid(temp))
  59. {
  60. System.out.println("Invalid postal code. Please try again.");
  61. temp = sc.next();
  62. }
  63. c.customerPostCode = Integer.parseInt(temp);
  64.  
  65. //Prompts the user to enter their date of birth and checks if it's a valid date.
  66. System.out.println("Please enter your date of birth in the format: dd-mm-yyyy. Please be PRECISE:");
  67. SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
  68. Date date = null;
  69. while (date == null)
  70. {
  71. try
  72. {
  73. date = format.parse(sc.next());
  74. c.customerDateOfBirth = date;
  75. }
  76. catch (ParseException e)
  77. {
  78. System.out.println("You entered an invalid day of birth. Please try again with the format dd-mm-yyyy");
  79.  
  80. }
  81. }
  82.  
  83. //Prompts the user to enter their phone number and check if it's valid.
  84. System.out.println("Please enter your 8-digit phone number. Only numbers are accepted");
  85. temp = sc.next();
  86.  
  87. while(!isPhoneNumberValid(temp))
  88. {
  89. System.out.println("Your number was either too long, too short or had characters in it. Please try again.");
  90. temp = sc.next();
  91. }
  92. c.customerPhoneNumber = Integer.parseInt(temp);
  93.  
  94. //Prompts the user to enter their CPR number and check if it's valid.
  95. System.out.println("Please enter your 10-digit CPR number in the format ddmmyy-XXXX");
  96. temp = sc.next();
  97.  
  98. while(!isCPRValid(temp))
  99. {
  100. System.out.println("You entered an invalid CPR. Please try again in the proper format");
  101. temp = sc.next();
  102.  
  103. }
  104. String[] parts = temp.split("-");
  105. String ddmmyy = parts[0];
  106. String security = parts[1];
  107. c.customerCPR = Integer.parseInt((ddmmyy + security));
  108.  
  109. //Creates a username and password for the customer.
  110. String tempUsername = createUsername(c);
  111. String tempPassword = createPassword(c);
  112.  
  113. if((tempUsername == null || tempPassword == null))
  114. {
  115. System.out.println("Something went wrong when creating your password and username. Most likely you see this error because your name is too special to create a login.");
  116. return null;
  117. }
  118.  
  119. c.customerUsername = tempUsername;
  120. c.customerPassword = tempPassword;
  121.  
  122. //Prints a recipe for the customer with his/her account details
  123. printRecipe(c);
  124.  
  125. System.out.println("");
  126.  
  127. return c;
  128.  
  129.  
  130. }
  131.  
  132. // create methods
  133. private String createPassword(Customer c)
  134. {
  135. if(!(c.customerLastName.length() < 3))
  136. {
  137. String last = c.customerLastName.substring(0 , 3);
  138. String pass = Integer.toString(c.customerCPR);
  139.  
  140. pass = pass.substring(pass.length() - 4, pass.length());
  141.  
  142.  
  143.  
  144. return last + pass;
  145. }
  146. return null;
  147. }
  148.  
  149. private String createUsername(Customer c)
  150. {
  151. if(!(c.customerFirstName.length() == 0 || c.customerLastName.length() < 3))
  152. {
  153. String first = c.customerFirstName.substring(0, 1);
  154. String last = c.customerLastName.substring(0, 3);
  155. return first + last;
  156. }
  157.  
  158. return null;
  159. }
  160.  
  161.  
  162. // isValid methods
  163. private boolean isPostCodeValid(String s)
  164. {
  165. String n = ".*[0-9].*";
  166. if(s.matches(n) && s.length() == 4)
  167. {
  168. int temp = Integer.parseInt(s);
  169.  
  170. if (temp <= 2500)
  171. {
  172. return true;
  173. }
  174.  
  175. return false;
  176. }
  177.  
  178. return false;
  179. }
  180.  
  181. private boolean isCPRValid(String s)
  182. {
  183. if (!s.contains("-"))
  184. {
  185. return false;
  186. }
  187.  
  188. String[] parts = s.split("-");
  189. String ddmmyy = parts[0];
  190. String security = parts[1];
  191.  
  192. if (!(ddmmyy.length() != 6 || security.length() != 4))
  193. {
  194. String n = ".*[0-9].*";
  195. if(ddmmyy.matches(n) && security.matches(n))
  196. {
  197. return true;
  198. }
  199. }
  200. return false;
  201. }
  202.  
  203. private boolean isPhoneNumberValid(String s)
  204. {
  205. if (s.length() != 8)
  206. {
  207. return false;
  208. }
  209. String n = ".*[0-9].*";
  210. return (s.matches(n));
  211. }
  212.  
  213. private boolean isNameValid(String s)
  214. {
  215. if(s.isEmpty())
  216. {
  217. return false;
  218. }
  219. String n = ".*[0-9].*";
  220. return !(s.matches(n));
  221. }
  222.  
  223.  
  224. private void printRecipe(Customer c)
  225. {
  226. System.out.println("");
  227. System.out.println("Your account has succesfully been created with the following details: ");
  228. System.out.println("");
  229. System.out.println("------ Account details ------");
  230. System.out.println("Customer name: " + customerFirstName + " " + c.customerLastName);
  231. System.out.println("Customer DOB (Date of birth): " + (c.customerDateOfBirth.getDate()) + "-" + (c.customerDateOfBirth.getMonth() + 1) + "-" + c.customerDateOfBirth.getYear());
  232. System.out.println("Customer address: " + c.customerAddress);
  233. System.out.println("Customer post code: " + c.customerPostCode);
  234. System.out.println("Customer phone number: " + c.customerPhoneNumber);
  235. System.out.println("Customer CPR: " + c.customerCPR);
  236.  
  237. System.out.println("");
  238. System.out.println("Please use the below information to login into our webshop: ");
  239. System.out.println("");
  240. System.out.println("##### Login Details #####");
  241. System.out.println("#-----------------------#");
  242. System.out.println("# Username: " + c.customerUsername + " #");
  243. System.out.println("# Password: " + c.customerPassword + " #");
  244. System.out.println("#########################");
  245. System.out.println("");
  246. }
  247.  
  248.  
  249. public String getCustomerFirstName() {
  250. return customerFirstName;
  251. }
  252. public void setCustomerFirstName(String customerFirstName) {
  253. this.customerFirstName = customerFirstName;
  254. }
  255. public String getCustomerLastName() {
  256. return customerLastName;
  257. }
  258. public void setCustomerLastName(String customerLastName) {
  259. this.customerLastName = customerLastName;
  260. }
  261. public String getCustomerAddress() {
  262. return customerAddress;
  263. }
  264. public void setCustomerAddress(String customerAddress) {
  265. this.customerAddress = customerAddress;
  266. }
  267. public Date getCustomerDateOfBirth() {
  268. return customerDateOfBirth;
  269. }
  270. public void setCustomerDateOfBirth(Date customerDateOfBirth) {
  271. this.customerDateOfBirth = customerDateOfBirth;
  272. }
  273. public int getCustomerPhoneNumber() {
  274. return customerPhoneNumber;
  275. }
  276. public void setCustomerPhoneNumber(int customerPhoneNumber) {
  277. this.customerPhoneNumber = customerPhoneNumber;
  278. }
  279. public int getCustomerCPR() {
  280. return customerCPR;
  281. }
  282. public void setCustomerCPR(int customerCPR) {
  283. this.customerCPR = customerCPR;
  284. }
  285. public int getCustomerPostCode() {
  286. return customerPostCode;
  287. }
  288. public void setCustomerPostCode(int customerPostCode) {
  289. this.customerPostCode = customerPostCode;
  290. }
  291. public String getCustomerUsername() {
  292. return customerUsername;
  293. }
  294. public void setCustomerUsername(String customerUsername) {
  295. this.customerUsername = customerUsername;
  296. }
  297. public String getCustomerPassword() {
  298. return customerPassword;
  299. }
  300. public void setCustomerPassword(String customerPassword) {
  301. this.customerPassword = customerPassword;
  302. }
  303.  
  304.  
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement