Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Project;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Locale;
- import java.util.Scanner;
- public class Customer {
- private final Scanner sc = new Scanner(System.in);
- private String customerFirstName;
- private String customerLastName;
- private String customerAddress;
- private Date customerDateOfBirth;
- private int customerPhoneNumber;
- private int customerCPR;
- private int customerPostCode;
- private String customerUsername = "";
- private String customerPassword = "";
- public Customer createAccount()
- {
- System.out.println("-- Account creator --");
- Customer c = new Customer();
- //Prompts the user to enter their first name and checks if it contains numbers.
- System.out.println("Please enter your first name:");
- String temp = sc.next();
- while(!isNameValid(temp))
- {
- System.out.println("Your name should not contain numbers. Please try again.");
- temp = sc.next();
- }
- c.customerFirstName = temp;
- //Prompts the user to enter their last name and checks if it contains numbers.
- System.out.println("Please enter your last name:");
- temp = sc.next();
- while(!isNameValid(temp))
- {
- System.out.println("Your name should not contain numbers. Please try again");
- temp = sc.next();
- }
- c.customerLastName = temp;
- //Prompts the user to enter their address with no checks.
- System.out.println("Please enter your address:");
- c.customerAddress = sc.next();
- //Prompts the user to enter their postal code and check if it's within it's boundaries.
- System.out.println("Please enter your 4-digit postal code. The code has to be less or equal to 2500.");
- temp = sc.next();
- while(!isPostCodeValid(temp))
- {
- System.out.println("Invalid postal code. Please try again.");
- temp = sc.next();
- }
- c.customerPostCode = Integer.parseInt(temp);
- //Prompts the user to enter their date of birth and checks if it's a valid date.
- System.out.println("Please enter your date of birth in the format: dd-mm-yyyy. Please be PRECISE:");
- SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
- Date date = null;
- while (date == null)
- {
- try
- {
- date = format.parse(sc.next());
- c.customerDateOfBirth = date;
- }
- catch (ParseException e)
- {
- System.out.println("You entered an invalid day of birth. Please try again with the format dd-mm-yyyy");
- }
- }
- //Prompts the user to enter their phone number and check if it's valid.
- System.out.println("Please enter your 8-digit phone number. Only numbers are accepted");
- temp = sc.next();
- while(!isPhoneNumberValid(temp))
- {
- System.out.println("Your number was either too long, too short or had characters in it. Please try again.");
- temp = sc.next();
- }
- c.customerPhoneNumber = Integer.parseInt(temp);
- //Prompts the user to enter their CPR number and check if it's valid.
- System.out.println("Please enter your 10-digit CPR number in the format ddmmyy-XXXX");
- temp = sc.next();
- while(!isCPRValid(temp))
- {
- System.out.println("You entered an invalid CPR. Please try again in the proper format");
- temp = sc.next();
- }
- String[] parts = temp.split("-");
- String ddmmyy = parts[0];
- String security = parts[1];
- c.customerCPR = Integer.parseInt((ddmmyy + security));
- //Creates a username and password for the customer.
- String tempUsername = createUsername(c);
- String tempPassword = createPassword(c);
- if((tempUsername == null || tempPassword == null))
- {
- 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.");
- return null;
- }
- c.customerUsername = tempUsername;
- c.customerPassword = tempPassword;
- //Prints a recipe for the customer with his/her account details
- printRecipe(c);
- System.out.println("");
- return c;
- }
- // create methods
- private String createPassword(Customer c)
- {
- if(!(c.customerLastName.length() < 3))
- {
- String last = c.customerLastName.substring(0 , 3);
- String pass = Integer.toString(c.customerCPR);
- pass = pass.substring(pass.length() - 4, pass.length());
- return last + pass;
- }
- return null;
- }
- private String createUsername(Customer c)
- {
- if(!(c.customerFirstName.length() == 0 || c.customerLastName.length() < 3))
- {
- String first = c.customerFirstName.substring(0, 1);
- String last = c.customerLastName.substring(0, 3);
- return first + last;
- }
- return null;
- }
- // isValid methods
- private boolean isPostCodeValid(String s)
- {
- String n = ".*[0-9].*";
- if(s.matches(n) && s.length() == 4)
- {
- int temp = Integer.parseInt(s);
- if (temp <= 2500)
- {
- return true;
- }
- return false;
- }
- return false;
- }
- private boolean isCPRValid(String s)
- {
- if (!s.contains("-"))
- {
- return false;
- }
- String[] parts = s.split("-");
- String ddmmyy = parts[0];
- String security = parts[1];
- if (!(ddmmyy.length() != 6 || security.length() != 4))
- {
- String n = ".*[0-9].*";
- if(ddmmyy.matches(n) && security.matches(n))
- {
- return true;
- }
- }
- return false;
- }
- private boolean isPhoneNumberValid(String s)
- {
- if (s.length() != 8)
- {
- return false;
- }
- String n = ".*[0-9].*";
- return (s.matches(n));
- }
- private boolean isNameValid(String s)
- {
- if(s.isEmpty())
- {
- return false;
- }
- String n = ".*[0-9].*";
- return !(s.matches(n));
- }
- private void printRecipe(Customer c)
- {
- System.out.println("");
- System.out.println("Your account has succesfully been created with the following details: ");
- System.out.println("");
- System.out.println("------ Account details ------");
- System.out.println("Customer name: " + customerFirstName + " " + c.customerLastName);
- System.out.println("Customer DOB (Date of birth): " + (c.customerDateOfBirth.getDate()) + "-" + (c.customerDateOfBirth.getMonth() + 1) + "-" + c.customerDateOfBirth.getYear());
- System.out.println("Customer address: " + c.customerAddress);
- System.out.println("Customer post code: " + c.customerPostCode);
- System.out.println("Customer phone number: " + c.customerPhoneNumber);
- System.out.println("Customer CPR: " + c.customerCPR);
- System.out.println("");
- System.out.println("Please use the below information to login into our webshop: ");
- System.out.println("");
- System.out.println("##### Login Details #####");
- System.out.println("#-----------------------#");
- System.out.println("# Username: " + c.customerUsername + " #");
- System.out.println("# Password: " + c.customerPassword + " #");
- System.out.println("#########################");
- System.out.println("");
- }
- public String getCustomerFirstName() {
- return customerFirstName;
- }
- public void setCustomerFirstName(String customerFirstName) {
- this.customerFirstName = customerFirstName;
- }
- public String getCustomerLastName() {
- return customerLastName;
- }
- public void setCustomerLastName(String customerLastName) {
- this.customerLastName = customerLastName;
- }
- public String getCustomerAddress() {
- return customerAddress;
- }
- public void setCustomerAddress(String customerAddress) {
- this.customerAddress = customerAddress;
- }
- public Date getCustomerDateOfBirth() {
- return customerDateOfBirth;
- }
- public void setCustomerDateOfBirth(Date customerDateOfBirth) {
- this.customerDateOfBirth = customerDateOfBirth;
- }
- public int getCustomerPhoneNumber() {
- return customerPhoneNumber;
- }
- public void setCustomerPhoneNumber(int customerPhoneNumber) {
- this.customerPhoneNumber = customerPhoneNumber;
- }
- public int getCustomerCPR() {
- return customerCPR;
- }
- public void setCustomerCPR(int customerCPR) {
- this.customerCPR = customerCPR;
- }
- public int getCustomerPostCode() {
- return customerPostCode;
- }
- public void setCustomerPostCode(int customerPostCode) {
- this.customerPostCode = customerPostCode;
- }
- public String getCustomerUsername() {
- return customerUsername;
- }
- public void setCustomerUsername(String customerUsername) {
- this.customerUsername = customerUsername;
- }
- public String getCustomerPassword() {
- return customerPassword;
- }
- public void setCustomerPassword(String customerPassword) {
- this.customerPassword = customerPassword;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement