Reputation: 414
I wrote time conversion program(i.e.,seconds to minute and minute seconds etc), but later I found that these classes perform similar operation.Is there any way to relate these classes, if so please give some solution and suggestion. Here is my code....
Second.java
import java.util.concurrent.*;
public class Second {
private long secondsValue;
public Second() {
secondsValue = 0L;
}
public Second(String from, String to, long unitValue) {
unitSelection(from, to, unitValue);
}
private void convertSecondToMinute(long unitValue) {
unitValue = TimeUnit.MINUTES.convert(unitValue, TimeUnit.SECONDS);
secondsValue = unitValue;
}
private void convertSecondToHour(long unitValue) {
unitValue = TimeUnit.HOURS.convert(unitValue, TimeUnit.SECONDS);
secondsValue = unitValue;
}
private void convertSecondToDay(long unitValue) {
unitValue = TimeUnit.DAYS.convert(unitValue, TimeUnit.SECONDS);
secondsValue = unitValue;
}
private void convertSecondToWeek(long unitValue) {
unitValue = unitValue/60/60/24/7;
secondsValue = unitValue;
}
public long getSeconds() {
return secondsValue;
}
private void unitSelection(String from, String to,
long unitValue) {
if( from.equalsIgnoreCase("second")) {
if(to.equalsIgnoreCase("minute")) {
convertSecondToMinute(unitValue);
}
else if(to.equalsIgnoreCase("hour")) {
convertSecondToHour(unitValue);
}
else if(to.equalsIgnoreCase("day")) {
convertSecondToDay(unitValue);
}
else if(to.equalsIgnoreCase("week") ) {
convertSecondToWeek(unitValue);
}
else {
System.out.println("Invalid argument...!");
}
}
}
}
Minute.java
import java.util.concurrent.TimeUnit;
public class Minute {
private long unitMinute;
public Minute() {
unitMinute = 0L;
}
public Minute(String from, String to,
long unitValue) {
unitSelection(from, to, unitValue);
}
private void convertMinuteToSecond(long unitValue) {
unitValue = TimeUnit.SECONDS.convert(unitValue, TimeUnit.MINUTES);
unitMinute = unitValue;
}
private void convertMinuteToHour(long unitValue) {
unitValue = TimeUnit.HOURS.convert(unitValue, TimeUnit.MINUTES);
unitMinute = unitValue;
}
private void convertMinuteToDay(long unitValue) {
unitValue = TimeUnit.DAYS.convert(unitValue, TimeUnit.MINUTES);
unitMinute = unitValue;
}
private void convertMinuteToWeek(long unitValue) {
long value = unitValue /60/24/7;
unitMinute = value;
}
public long getUnitMinute() {
return unitMinute;
}
private void unitSelection(String from, String to,
long unitValue) {
if( from.equalsIgnoreCase("minute")) {
if(to.equalsIgnoreCase("second")) {
convertMinuteToSecond(unitValue);
}
else if(to.equalsIgnoreCase("hour")) {
convertMinuteToHour(unitValue);
}
else if(to.equalsIgnoreCase("day")) {
convertMinuteToDay(unitValue);
}
else if(to.equalsIgnoreCase("week") ) {
convertMinuteToWeek(unitValue);
}
else {
System.out.println("Invalid argument...!");
}
}
}
}
Upvotes: 4
Views: 195
Reputation: 5940
I see 2 options:
1. Re-define both as a single class: which suggest the use of a universal value standard (number of milliseconds for example), and to offer what it means in different measures like minutes, hours, ..etc
class Duration
{
private long secondsValue;
public Duration() {
secondsValue = 0L;
}
public Duration(String format, long value) {
if (format.equals("SECONDS")) secondsValue = value;
else if (format.equals("MINUTES")) secondsValue = value*60;
else if (format.equals("HOURS")) secondsValue = value*60*60;
else if (format.equals("DAYS")) secondsValue = value*60*60*24;
else if (format.equals("WEEKS")) secondsValue = value*60*60*24*7;
}
public long asMinutes() {
return value/60;
}
public long asHours() {
return value/(60*60);
}
public long asDays() {
return value/(60*60*24);
}
public long asWeeks() {
return unitValue/(60*60*24*7);
}
public long asSeconds() {
return secondsValue;
}
}
2. use a converter utility class: like how you use TimeUnit.MINUTES.convert(unitValue, TimeUnit.SECONDS);
to convert from seconds to minutes
Upvotes: 0
Reputation: 425033
The best way to improve your code is to delete all of your classes and use TimeUnit
instead.
TimeUnit
has all this functionality (and more) and comes with the JDK.
Don't reinvent the wheel.
Upvotes: 5
Reputation: 14470
When you say "related" is very wide topic. However, the right way to start is using java Packaging mechanism to relate classes at very highest level. You can manage your code (if possible) using Inheritance and Polymorphism . As @Bohemian suggested , use existing java libraries until or unless your are doing sth new and doing it for learning purpose.
Upvotes: 0