-1

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?

Fred Kibuchi
  • 123
  • 1
  • 4
  • 15

2 Answers2

0

I would suggest a Map rather than an array. You could either map from day to gender to name or gender to day to name.

EnumMap<DayOfWeek,Map<String,String>> names = new EnumMap<>(DayOfWeek.class);
names.put(MONDAY, Map.of("Male", "Kwasi", "Female", "Akosua"));
...

Then getting the appropriate name is just names.get(day).get(gender).

sprinter
  • 27,148
  • 6
  • 47
  • 78
  • If user is a Male:- Sunday: Kwasi Monday: Kwadwo Tuesday: Kwabena Wednesday: Kwaku Thursday:  Yaw Friday: Kofi Saturday: Kwame If user is a Female:- Sunday: Akosua Monday: Adwoa Tuesday: Abenaa Wednesday: Akua Thursday:  Yaa Friday: Afua Saturday: Ama How about performance of the system for such data set??? – Fred Kibuchi Sep 19 '19 at 22:53
  • 1
    I'm sure you can create the statements to add all the values: I was just showing you how to do it. On performance: it's irrelevant for such a tiny set of data - but since you ask, performance of maps is very good. – sprinter Sep 19 '19 at 22:57
  • when i do this i'm getting this error for Map.of : cannot find symbol symbol: method of(String,String,String,String) location: interface Map – Fred Kibuchi Sep 20 '19 at 12:16
0

For cleaner code, you can give the DayOfWeek enum two variables FName and Mname with getters for each instance of the enum. From their your code becomes roughly:

if(gender.equals("male") 
    System.out.println(dayofweek.getMname);
else 
    System.out.println(dayofweek.getFname);```
randypaq13
  • 432
  • 3
  • 9