patternjavaMinor
Quote of the day program
Viewed 0 times
quoteprogramtheday
Problem
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:
I have created an interface
Is there any way to make this code better? Also is there any way to ensure that this class is instantiated only once? Since otherwise the different objects will overwrite each other's history in the history file.
QuoteProvider.java
HardCodedQuoteProvider.java
```
package beg_assignment4;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class HardCodedQuoteProvider implements QuoteProvider {
// quotes to be used if the time is A.M.
private final String AM_QUOTES[] = {
"Be yourself; everyone else is
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:
- User types "java QuoteOfTheDay" on the command line.
- System prints out a quote of the day, with an attribution.
I have created an interface
QuoteProvider and implemented it with the class HardCodedQuoteProvider. I am storing the times when a quote was previously used in a file 'history.txt'. A quote is provided only if it has not been used before. Is there any way to make this code better? Also is there any way to ensure that this class is instantiated only once? Since otherwise the different objects will overwrite each other's history in the history file.
QuoteProvider.java
package beg_assignment4;
import java.io.IOException;
import java.util.List;
public interface QuoteProvider {
public String getQuote() throws IOException;
public List getHistory() throws IOException;
public void clearHistory() throws IOException;
}HardCodedQuoteProvider.java
```
package beg_assignment4;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class HardCodedQuoteProvider implements QuoteProvider {
// quotes to be used if the time is A.M.
private final String AM_QUOTES[] = {
"Be yourself; everyone else is
Solution
Single-responsibility principle
Composition can be very useful here. For example, instead of
you could have:
And for managing the history, it would be good to create an interface, so that you can have different implementations, for example file-based, in-memory, database-backed.
Your question
Also is there any way to ensure that this class is instantiated only once? Since otherwise the different objects will overwrite each other's history in the history file.
You can use the Singleton pattern. (See for example Item 3 in Effective Java by Joshua Bloch.)
HardCodedQuoteProvider has too many responsibilities:- Provide quotes
- Provide quotes depending on the time of the day
- Manage history
Composition can be very useful here. For example, instead of
HardCodedQuoteProvider managing the quotes for AM and PM,you could have:
- A simple quote provider with no notion of AM and PM
- A composite quote provider that has two simple quote providers, one for AM and one for PM, and delegate to them appropriately depending on the time of day. Note that this composite quote provider wouldn't manage the history directly, the contained simple quote providers would do that
And for managing the history, it would be good to create an interface, so that you can have different implementations, for example file-based, in-memory, database-backed.
Your question
Also is there any way to ensure that this class is instantiated only once? Since otherwise the different objects will overwrite each other's history in the history file.
You can use the Singleton pattern. (See for example Item 3 in Effective Java by Joshua Bloch.)
Context
StackExchange Code Review Q#123941, answer score: 4
Revisions (0)
No revisions yet.