0

I was given a bus schedule like 1101011 (7 days of week starting from SUNDAY to SATURDAY)
1 represents - yes the bus runs on that DAY
0 represents - No the bus doesn't runs on that day
Assume for a given date 09-Oct, can anyone tell how to find if the bus runs or not?

(I am still trying to find the better way to do this.

user1514499
  • 762
  • 7
  • 26
  • 63

3 Answers3

1

You can store your schedule in array, and then retrieve value from there basing on day of week index.

private static final int[] SCHEDULE = new int[] {1, 0, 1, 0, 1, 1, 1};

public static void main(String[] args) {
    LocalDate testDate = LocalDate.of(2016, Month.OCTOBER, 8);
    System.out.println("testDate = " + testDate);

    DayOfWeek dayOfWeek = testDate.getDayOfWeek();
    System.out.println("Day of week = " + dayOfWeek);

    System.out.println("The bus " + (1 == SCHEDULE[dayOfWeek.getValue()-1] ? "goes" : "does not go") + " on " + testDate);
}

Please note that I have moved value for Sunday in the schedule to the end, since index for DayOfWeek enum appears as

The int value follows the ISO-8601 standard, from 1 (Monday) to 7 (Sunday).

Bogdan
  • 310
  • 6
  • 12
  • FYI, the old [`Calendar`](http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html) and [`Date`](http://docs.oracle.com/javase/8/docs/api/java/util/Date.html) classes are now [legacy](https://en.wikipedia.org/wiki/Legacy_system). Supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Oct 09 '16 at 20:56
1

tl;dr

Parse that input string into an EnumSet of DayOfWeek constants to represent the days of the week when the bus runs. Ask if the set contains the day-of-week for any particular date.

Set<DayOfWeek> busRunDays = EnumSet. …
…
Boolean busRunsOn20161009 = 
    busRunDays.contains( 
        DayOfWeek.from( LocalDate.of( 2016 , Month.OCTOBER , 9 ) ) 
    ) 
;

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

DayOfWeek

Java includes the DayOfWeek enum defining seven instances as constants, one for each day of the week.

DayOfWeek dow = DayOfWeek.from( ld );

You can test for equality.

if( dow.equals( DayOfWeek.TUESDAY ) ) …

They are numbered and sorted 1-7 for Monday to Sunday, per the ISO 8601 standard.

EnumSet

The EnumSet class is a very efficient implementation of Set for holding enum-defined objects. Represented internally as bit vectors, an EnumSet takes very little memory and executes very quickly.

Loop through your input string characters, and if a 1 add that day of week to the set.

Set<DayOfWeek> busRunDays = EnumSet.noneOf( DayOfWeek.class );
…
busRunDays.add( DayOfWeek.SUNDAY );

Elsewhere in your code you ask if a day of the week is in the set.

if( busRunDays.contains( DayOfWeek.WEDNESDAY ) ) …

Example code, all put together and ready to run (at your own risk, no guarantees).

Set<DayOfWeek> busRunDays = EnumSet.noneOf ( DayOfWeek.class );

String input = "1101011";

DayOfWeek[] inputWeek = { DayOfWeek.SUNDAY , DayOfWeek.MONDAY , DayOfWeek.TUESDAY , DayOfWeek.WEDNESDAY , DayOfWeek.THURSDAY , DayOfWeek.FRIDAY , DayOfWeek.SATURDAY };  // Define the ordering of days in a non-standard week. 
int i = 0;
for ( char c : input.toCharArray () ) {
    if ( c == '1' ) {
        DayOfWeek dow = inputWeek[ i ];
        busRunDays.add ( dow );
    }
    i = ( i + 1 );  // Prepare for next loop.
}

Dump to console.

System.out.println ( "input: " + input + " | busRunDays: " + busRunDays );

input: 1101011 | busRunDays: [MONDAY, WEDNESDAY, FRIDAY, SATURDAY, SUNDAY]

You can use the same sort of logic if you need to write out strings in your 1101011 format.

Test if the bus runs today.

LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;
Boolean busRunsToday = busRunDays.contains( DayOfWeek.from( today ) ) ;

Test if the bus runs on a particular date. This specifically answers the Question.

LocalDate ld = LocalDate.of( 2016 , Month.OCTOBER , 9 ) ;
Boolean busRunsOnDate = busRunDays.contains( DayOfWeek.from( ld ) ) ;

I strongly suggest you get in the habit of passing around these DayOfWeek and Set objects rather than using mere digits like 1-7 and strings like your 1101011. Doing so provides type-safety, ensures valid values, eliminates ambiguity, and makes your code more self-documenting.

Tip: Life is easier if you and your users stick with the standard Monday-Sunday week like much of the world. But I know that change can be challenging, especially for us Americans. So be sure to carefully document in your code that the 1101011 input values use non-standard Sun-Mon week.

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Where to obtain the java.time classes?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

How about the using queue ?

Offer the each data (char 0 or 1) and each day you poll the data and check whether runs or not.

Also, you have to determine the result for empty queue.

So you can code like the daily system.

RedEye
  • 1