5
\$\begingroup\$

Problem Statement:

Create a class diagram and Java code for the following system and scenario, taking into account the possibility of future extensions. "The system is a command line utility that prints a short 'quote of the day' on the user's terminal when run. To begin with the quote is selected randomly from a set of hard-coded strings within the program itself, but that might change later on -- the quotes might be based on the user's history, the time of day, the date, etc.. Scenario:

  1. User types "java QuoteOfTheDay" on the command line.
  2. System prints out a quote of the day, with an attribution.

This is my code for the following:

Class Containing the main method

package quoteOfTheDay;
import java.util.Random;

public class QuoteOfTheDay {

    public static void main(String[] args) {
        System.out.println();

        Random rand = new Random();

        System.out.println(Quote.QUOTES[rand.nextInt(Quote.QUOTES.length)]);

    }
}

Quote Class containng the quotes.

    package quoteOfTheDay;

public class Quote {
    public static final String  QUOTES[] = {
            "Be yourself; everyone else is already taken.― Oscar Wilde",
            "A room without books is like a body without a soul. ― Marcus Tullius Cicero",
            "Be the change that you wish to see in the world. ― Mahatma Gandhi",
            "If you tell the truth, you don't have to remember anything. ― Mark Twain",
            "If you want to know what a man's like, take a good look at how he treats his inferiors, not his equals.― J.K. Rowling",
            "To live is the rarest thing in the world. Most people exist, that is all.― Oscar Wilde",
            "Without music, life would be a mistake. ― Friedrich Nietzsche",
            "Always forgive your enemies, nothing annoys them so much. ― Oscar Wilde",
            "Life isn't about getting and having, it's about giving and being. –Kevin Kruse",
            "Whatever the mind of man can conceive and believe, it can achieve. –Napoleon Hill",
            "Strive not to be a success, but rather to be of value. –Albert Einstein",                          
            };    
}

Is this the right approach? Am I missing some things from the problem requirements? I am open for any kind of suggestions.

\$\endgroup\$
2
  • \$\begingroup\$ I think you should start by creating a class diagram like the question says. Right now, it would not contain all that many classes, right? And keeping in mind future extensions: What if for example I wanted to add the birth and death of a person to the attribution (so I get an idea of what time period that quote is from)? I would have to add it everywhere that person is mentioned. If you followed my advice to your first question, this would be easier (I would just have to add it once). \$\endgroup\$ – tim Nov 5 '14 at 10:47
  • 1
    \$\begingroup\$ +1 For your awesome quotes, I am reading Napoleon Hill's book "think and grow rich" atm :D \$\endgroup\$ – BoJack Horseman Nov 5 '14 at 13:54
6
\$\begingroup\$

The using of a public field should be avoided and instead you should use a method to get the value.

If you take into account that the class will be extended (like written in the problem statement), I would suggest that you add a getRandomQuote() method to the Quote class.

Also you should make your String array QUOTES private. I don't know what the conventions for the array braces [] are , but I would write it private static final String[] QUOTES

A Random should be created once and then used as often one needs.So I would suggest to create it at the constructor.

private Random random;
public Quotes(){
    random = new Random();
} 

public String getRandomQuote(){
// return your random quote here

}  

You should always name your variables and methods in a meaningful easily readable way. So I have renamed rand to random.

\$\endgroup\$
3
\$\begingroup\$

I think the important info is in

taking into account the possibility of future extensions

So I think a bit of object-oriented design is expected.

First we define an interface for obtaining data:

public interface Source<T>
{
    public T getNext();
}

Then we need an interface for the data sink:

public interface Sink<T>
{
    void put(T elem);
}

In this scenario the data sink will be a simple command line printer.

public class CommandLinePrinter implements Sink<String>
{
    @Override
    public void put(String elem)
    {
        System.out.println(elem);
    }
}

And the data source is a random quote provide:

public class RandomQuoteProvider implements Source<String>
{
    private final Random randomNumberGenerator;

    private final List<String> quotes;


    public RandomQuoteProvider(List<String> quotes)
    {
        this.quotes = quotes;
        this.randomNumberGenerator = new Random();
    }

    @Override
    public String getNext()
    {
        return this.quotes.get(this.randomNumberGenerator.nextInt(this.quotes.size()));
    }
}

As controller we can keep the QuoteOfTheDay class:

public class QuoteOfTheDay
{
    private final Source<String> dataSource;

    private final Sink<String> dataSink;


    public QuoteOfTheDay()
    {
        List<String> data = new ArrayList<String>(11);
        data.add("Be yourself; everyone else is already taken.― Oscar Wilde");
        data.add("A room without books is like a body without a soul. ― Marcus Tullius Cicero");
        data.add("Be the change that you wish to see in the world. ― Mahatma Gandhi");
        data.add("If you tell the truth, you don't have to remember anything. ― Mark Twain");
        data.add("If you want to know what a man's like, take a good look at how he         treats his inferiors, not his equals.― J.K. Rowling");
        data.add("To live is the rarest thing in the world. Most people exist, that is all.― Oscar Wilde");
        data.add("Without music, life would be a mistake. ― Friedrich Nietzsche");
        data.add("Always forgive your enemies, nothing annoys them so much. ― Oscar Wilde");
        data.add("Life isn't about getting and having, it's about giving and being. –Kevin Kruse");
        data.add("Whatever the mind of man can conceive and believe, it can achieve. –Napoleon Hill");
        data.add("Strive not to be a success, but rather to be of value. –Albert Einstein");

        this.dataSource = new RandomQuoteProvider(data);
        this.dataSink = new CommandLinePrinter();
    }

    public void print()
    {
        this.dataSink.put(this.dataSource.getNext());
    }

    public static void main(String[] args)
    {
        QuoteOfTheDay quoteOfTheDay = new QuoteOfTheDay();
        quoteOfTheDay.print();
    }
}

In the exercise it is already mentioned that in future implementations

the quotes might be based on the user's history, the time of day, the date, etc.

Now you can easily exchange the RandomQuoteProvider with something more sophisticated. You could also decide to not print the quote to the command line but display it in a popup window.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.