HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

LifeCoach project

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
projectlifecoachstackoverflow

Problem

This project is about helping people with common problems like "Partner dumped me" or "I hate my job", recommending self-help books for every problem and specific person according to age and gender.

Can anybody suggest improvements for my project? How can I make this an actual useful project?

```
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// VARIABLES
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);

boolean young = false;
boolean youngadult = false;
boolean adult = false;
boolean elder = false;
boolean male = false;
boolean female = false;

// GENDER
System.out.print("What's your gender? (M/F) ");
String gender = scanner.nextLine();
if (gender.equals("M") || gender.equals("m")) {
male = true;
} else if (gender.equals("F") || gender.equals("f")) {
female = true;
} else {
System.out.println("Invalid input");
System.exit(0);
}

// AGE
System.out.print("What's your age? ");
int age = scanner.nextInt();
if (age 8) {
System.exit(0);
}
System.out.println("");

// SWITCH CHOICE
switch (choice) {
case 1:
if (female && young) {
System.out.println("Recommended book: The Breakup Bible, by Melissa Kantor.");
} else if (female && youngadult || adult || elder) {
System.out.println(
"Recommended book: The Breakup Bible: The Smart Woman’s Guide to Healing From a Breakup or Divorce, by Rachel Sussman");
} else if (male) {
System.out.println("Recommended book: The Power of Now, by Eckhart Tolle.");
}
break;
case 2:
if (female) {
System.out.println(
"Recommended boo

Solution

Methods

You put your entire program in the main method. A better practice is to break up the program into multiple blocks. Then your main method might look like this:

public static void main(String[] args) {
        LifeCoach coach = new LifeCoach();
        try (Scanner scanner = new Scanner(System.in)) {
            coach.inputGender();
            coach.inputAge();
            coach.inputChoice();
        }

        coach.recommend();
    }


This also uses the try-with-resources to automatically close the Scanner when it's finished using it.

There is an argument that you should separate input, logic, and display. Here, we do all three in the LifeCoach class. Further, the logic is embedded in the same methods that do input and display. That's not ideal from a separation of concerns perspective, but in such a small program it may not be worth engineering it more.

enum vs. boolean

boolean young = false;
        boolean youngadult = false;
        boolean adult = false;
        boolean elder = false;
        boolean male = false;
        boolean female = false;


Rather than use six boolean values, consider using two enum values.

public enum AgeCategory {
    YOUNG, YOUNG_ADULT, ADULT, ELDER
}

public enum Gender {
    FEMALE, MALE
}


equalsIgnoreCase

if (gender.equals("M") || gender.equals("m")) {
            male = true;
        } else if (gender.equals("F") || gender.equals("f")) {
            female = true;


Java has a method just for this situation

if (input.equalsIgnoreCase("M")) {
            gender = MALE;
        } else if (input.equalsIgnoreCase("F")) {
            gender = FEMALE;


Now we don't have to write out each case. Or you can use a switch:

switch (scanner.nextLine().toUpperCase()) {
        case "M":
            gender = MALE;
            break;
        case "F":
            gender = FEMALE;
            break;
        default:
            System.out.println("Invalid input");
            System.exit(0);
        }


Simplify

if (female && young) {
                System.out.println("Recommended book: The Breakup Bible, by Melissa Kantor.");
            } else if (female && youngadult || adult || elder) {
                System.out.println(
                        "Recommended book: The Breakup Bible: The Smart Woman’s Guide to Healing From a Breakup or Divorce, by Rachel Sussman");
            } else if (male) {
                System.out.println("Recommended book: The Power of Now, by Eckhart Tolle.");
            }


In each of these, you follow the same pattern. Consider

public void recommend() {
        System.out.println("Recommended book: " + determineRecommendation());
    }


Then the switch statement gets simpler.

public String determineRecommendation() {
        switch (choice) {
        case 1:
            if (gender == MALE) {
                return "The Power of Now, by Eckhart Tolle.";
            } else if (ageCategory == YOUNG) {
                return "The Breakup Bible, by Melissa Kantor.";
            } else {
                return "The Breakup Bible: The Smart Woman’s Guide to Healing From a Breakup or Divorce, by Rachel Sussman.";
            }


Here we just return a string representing the book title and author.

Note that with this order, it's easier to write out the criteria. Either male or not male (female). Two female possibilities. One for the young and one for everyone else. The else represents female and young adult, adult, or elder. This is the same as the original, but with less writing.

This also shows how use of the enum values differs from the original boolean values.

A database

Now, if you want to switch to a database, you just need to change the enum definitions and determineRecommendation. The input collection can stay the same.

Code Snippets

public static void main(String[] args) {
        LifeCoach coach = new LifeCoach();
        try (Scanner scanner = new Scanner(System.in)) {
            coach.inputGender();
            coach.inputAge();
            coach.inputChoice();
        }

        coach.recommend();
    }
boolean young = false;
        boolean youngadult = false;
        boolean adult = false;
        boolean elder = false;
        boolean male = false;
        boolean female = false;
public enum AgeCategory {
    YOUNG, YOUNG_ADULT, ADULT, ELDER
}

public enum Gender {
    FEMALE, MALE
}
if (gender.equals("M") || gender.equals("m")) {
            male = true;
        } else if (gender.equals("F") || gender.equals("f")) {
            female = true;
if (input.equalsIgnoreCase("M")) {
            gender = MALE;
        } else if (input.equalsIgnoreCase("F")) {
            gender = FEMALE;

Context

StackExchange Code Review Q#152042, answer score: 6

Revisions (0)

No revisions yet.