Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the best way to represent the business opening hours in a Java object?

Tags:

java

I need to represent opening hours, and a method which returns true/false for a certain day and time. Is there any package which has this functionality already?

Edit: Basically I would need to construct an object with data out of db or a file, then perform a basic check against the object, like if it's closed at a certain moment.

The problem was that some businesses will have working hours after 00:00 so it overlaps the next day. In this case I figure out that the object should be able to support multiple time frames per day, also to cover lunch brakes.

like image 458
Igor Toma Avatar asked Nov 02 '11 12:11

Igor Toma


People also ask

What is the class object in Java?

Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake. A Class is like an object constructor, or a "blueprint" for creating objects.

How to declare object in Java class?

In Java, we can create Objects in various ways: Using a new keyword. Using the newInstance () method of the Class class. Using the newInstance() method of the Constructor class.

What are classes for in Java?

A class — in the context of Java — is a template used to create objects and to define object data types and methods. Classes are categories, and objects are items within each category. All class objects should have the basic class properties.

How to define object of a class?

an object is an element (or instance) of a class; objects have the behaviors of their class. The object is the actual component of programs, while the class specifies how instances are created and how they behave. method: a method is an action which an object is able to perform.


2 Answers

You can create a class by using Calendar and see if its working day and working hour now.

class BusinessHour{

public void isOpenNow(){
  Calendar calNow = Calendar.getInstance();
    //check the rules for example , if day is MOn-FRi and time is 9-18.
  }
}

I don't think there would be a ready made because each business has its own specification, You could probably make if configurable so that it fixes for all the business, provide the conf parameter externally to the class for better design

like image 191
jmj Avatar answered Nov 09 '22 13:11

jmj


I use following Object to store OpeningHours in HashSet. Example bellow can be easily simplified using seconds from Midnight instead of "800" or "1600" for hours but I like it this way.

public class OpeningHours {
  public enum DAY {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
  }

public OpeningHours(DAY day, Integer from, Integer to) {
    this.day = day;
    this.from = from; // format time using 800 for 8:00am or 2300 for 23:00
    this.to = to;
}

@Override
public String toString() {
    return "OpeningHours [day=" + day + ", from=" + from + ", to=" + to + ", isAllDay=" + isAllDay + "]";
}

public OpeningHours() {

}

public DAY day;
public Integer from;
public Integer to;
public boolean isAllDay = false;

public void isOpenx(DateTime start) {

}

public boolean isOpen(DateTime start) {

    if (day.ordinal() != start.getDayOfWeek() - 1) {
        return false;
    }

    if (isAllDay)
        return true;

    String f = String.format("%04d", from);
    String t = String.format("%04d", to);

    Integer fh = Integer.valueOf(f.substring(0, 2));
    Integer fm = Integer.valueOf(f.substring(2));

    Integer th = Integer.valueOf(t.substring(0, 2));
    Integer tm = Integer.valueOf(t.substring(2));

    DateTime intStart = start.withHourOfDay(fh).withMinuteOfHour(fm);
    DateTime intEnd = start.withHourOfDay(th).withMinuteOfHour(tm);

    if (intStart.equals(start) || intEnd.equals(start)) {
        return true;
    }
    if (intStart.isBefore(start) && intEnd.isAfter(start)) {
        return true;
    }

    return false;

}
}
HashSet<OpeningHours> hours = new HashSet<OpeningHours>();
hours.add(new OpeningHours(OpeningHours.DAY.MONDAY, 800, 1200));
hours.add(new OpeningHours(OpeningHours.DAY.MONDAY, 1230, 1600));


DateTime dateToCheck = new DateTime(2012, 9, 4, 8, 00, 0, 0);

for (OpeningHours item : hours) {
  boolean isOpen = item.isOpen(dateToCheck );
  if (isOpen){
    System.out.println("Is Open!");
  }
}
like image 35
vladaman Avatar answered Nov 09 '22 11:11

vladaman