How to bind classes in java?

I wrote a time conversion program (i.e. seconds, minutes and minutes, etc.), but later I found that these classes perform a similar operation. Is there a way to link 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...!"); } } } } 
+4
source share
3 answers

The best way to improve the code is to remove all your classes and use TimeUnit .

TimeUnit has all this functionality (and much more) and comes with the JDK.
Do not reinvent the wheel.

+5
source

When you say "related," a very broad topic. However, the right way to get started is to use the java Packaging engine to bind classes at the highest level. You can manage your code (if possible) using Inheritance and polymorphism . As @Bohemian suggested, use existing java libraries until you make a new one and do it for training.

0
source

I see 2 options:

1. Redefine as one class: that suggests using a universal standard of value (for example, milliseconds) and offering what it means in different dimensions, such as 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 the converter utility class: for example, how do you use TimeUnit.MINUTES.convert(unitValue, TimeUnit.SECONDS); to convert from seconds to minutes

0
source

Source: https://habr.com/ru/post/1485357/


All Articles