i'm implementing a naming system for a country that is based on the day of a week that a person was born. The user should provide the system with Birthday date and the gender. Then the system should give their first name. For example... if birthday date is 4th June 1993, the day of week should be Friday. If gender is Male the system assigns first name as Kofi but if the gender is female the first name would be Afua. I have done this using Switch statement but i feel it's not the best implementation for this. I seek your best approach for such a scenario.
try( Scanner input= new Scanner(System.in)){
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/uuuu");
System.out.println("Enter your birthdate of the form dd/mm/yy : ");
String birthDate = input.nextLine();
System.out.println("Enter your gender in capital letters: ");
String gender = input.nextLine();
try {
if(birthDate.trim().equals("") || birthDate.length()>10){
System.out.println("There are issues with your date input. Re-enter again: ");
input.nextLine();
}
}else{
//date validation of the form dd/MM/uuuu and ensures
//you can not input something like 31/02/2019
LocalDate localDate = LocalDate.parse(birthDate, dateFormat.withResolverStyle(ResolverStyle.STRICT));
//to find the age of a person
LocalDate today = LocalDate.now();
long diffInYears = ChronoUnit.YEARS.between(localDate, today);
//to get the day of week for a person not old than 120 years.
if (localDate.isAfter(LocalDate.now().minusYears(120))) {
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
System.out.println("You are : "+diffInYears+" years and your birthday was on : "+ dayOfWeek);
// String[][] genderDayofWeekArray = new String[][]{{"MALE","FEMALE"},{"SUNDAY","MONDAY","TUESDAY"
// ,"WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"}};
String firstName = "";
if (gender.equals("MALE")){
switch(dayOfWeek){
case SUNDAY: firstName ="Kwasi";
System.out.println("Your first name is: "+firstName);
break;
case MONDAY: firstName ="Kwadwo";
System.out.println("Your first name is: "+firstName);
break;
case TUESDAY: firstName ="Kwabena";
System.out.println("Your first name is: "+firstName);
break;
case WEDNESDAY: firstName ="Kwaku";
System.out.println("Your first name is: "+firstName);
break;
case THURSDAY: firstName ="Yaw";
System.out.println("Your first name is: "+firstName);
break;
case FRIDAY: firstName ="Kofi";
System.out.println("Your first name is: "+firstName);
break;
case SATURDAY: firstName ="Kwame";
System.out.println("Your first name is: "+firstName);
break;
default:
System.out.println("Invalid operation.");
}
//if gender input is FEMALE then name given as per day of week changes
}else if (gender.equals("FEMALE")){
switch(dayOfWeek){
case SUNDAY: firstName ="Akosua";
System.out.println("Your first name is: "+firstName);
break;
case MONDAY: firstName ="Adwoa";
System.out.println("Your first name is: "+firstName);
break;
case TUESDAY: firstName ="Abenaa";
System.out.println("Your first name is: "+firstName);
break;
case WEDNESDAY: firstName ="Akua";
System.out.println("Your first name is: "+firstName);
break;
case THURSDAY: firstName ="Yaa";
System.out.println("Your first name is: "+firstName);
break;
case FRIDAY: firstName ="Afua";
System.out.println("Your first name is: "+firstName);
break;
case SATURDAY: firstName ="Ama";
System.out.println("Your first name is: "+firstName);
break;
default:
System.out.println("Invalid operation.");
}
}
} else {
System.out.println("Your age limit exceeds 120 years of age. Stone Age ***");
} }
} catch (DateTimeParseException e) {
System.out.print(e);
}
Would arrays be the best implementation to avoid repetitive code and improve performance or i'm on the right track?