2
\$\begingroup\$

I thought to spent my free time by solving a puzzle: Best Before Spotify Puzzle.

I coded in Java, and yeah I did not clean up my code (just a rough work) and I have yet to optimize... so I did check for possible test cases (including leap year) and could not find any of these cases failing, but I got a reply from the bot as "wrong answer". Here is my code, could anyone please find where my test case fails?

Algorithm:

  1. Generate 6 different inputs (for 3 inputs, permutation goes as 3*2).
  2. Choose the best earliest date of all 6 inputs.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;


public class bestbefore {
int array[]=new int[3];
ArrayList<Integer> temp=new ArrayList<Integer>();
int prevy=10000;
int prevd=1000;
int prevm=1000;
int flag=0;
String tem;
public static void main(String[] args)
{
bestbefore b4=new bestbefore();
b4.input();
}
void input() throws IllegalArgumentException
{
    InputStreamReader input=new InputStreamReader(System.in);
    BufferedReader inputs=new BufferedReader(input);
    try {
        tem=inputs.readLine();
    String [] temps=tem.split("/");
    try
    {
    array[0]=Integer.valueOf(temps[0]).intValue();
temp.add(array[0]);
    array[1]=Integer.valueOf(temps[1]).intValue();
    temp.add(array[1]);
    array[2]=Integer.valueOf(temps[2]).intValue();
    temp.add(array[2]);
    }
    catch (ArrayIndexOutOfBoundsException e) {
        // TODO: handle exception
error();
    }
    catch (NumberFormatException  e) {
        // TODO: handle exception
        error();
    }
for(int i=0;i<3;i++)
{

for(int j=1;j<=2;j++)
{
if(j==1)
{
int year=temp.get(0);
int month=temp.get(1);
int date=temp.get(2);
//System.out.println("Passing"+year+"/"+month+"/"+date);
calculate(year,month,date);
}
else
{
    int year=temp.get(0);
    int month=temp.get(2);
    int date=temp.get(1);
    //System.out.println("Passing"+year+"/"+month+"/"+date);
calculate(year,month,date);
}
}
Collections.rotate(temp, -1);
}
if((array[0] <=0)|| (array [1]<=0)|| (array[2] <=0) ||(String.valueOf(array[0]).length()==3))
{
    flag=0;

}
if(flag==0)
{
System.out.println(array[0]+"/"+array[1]+"/"+array[2]+" is illegal");   
}
else
{
    if(flag!=0)
    {
    String prem=String.valueOf(prevm);
    String pred=String.valueOf(prevd);
    if(String.valueOf(prevm).length()<=1)
    {
    prem=0+prem;    
    }

    if(String.valueOf(prevd).length()<=1)
    {
    pred=0+pred;    
    }

    System.out.println(prevy+"-"+prem+"-"+pred);    
}
}
    }


    catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("Invalid input");
    }


}
private void error() {
    // TODO Auto-generated method stub
    System.out.println(tem+" is illegal");
    System.exit(0);
}
void calculate(int year,int month,int date)
{

if(String.valueOf(year).length()<3)
{
year=(year+2000);   
}
boolean leap=false;
if((year%4==0)&&((year%100==0)))
{
if(year%400==0)
{
leap=true;  
//System.out.println("Leap year"+year);
}
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
    if(date>=1 && date<=31)
    {

        if(year<prevy)
        {

                //  System.out.println("generated"+year+"-"+month+"-"+date);
                    flag=1;
                    prevy=year;
                    prevm=month;
                    prevd=date;

        }

        else if((year==prevy)&& (month<prevm))
        {
            //System.out.println("generated"+year+"-"+month+"-"+date);
            flag=1;
            prevy=year;
            prevm=month;
            prevd=date;
        }
        else if ((year==prevy) &&(month==prevm))
        {
            if(date<prevd)
            {
        //  System.out.println("generated"+year+"-"+month+"-"+date);
            flag=1;
            prevy=year;
            prevm=month;
            prevd=date;
            }
        }
        else
        {

        }

    }
    break;
case 4:
case 6:
case 9:
case 11:
    if(date>=1 &&date<=30)
    {
        if(year<prevy)
        {

                //  System.out.println("generated"+year+"-"+month+"-"+date);
                    flag=1;
                    prevy=year;
                    prevm=month;
                    prevd=date;

        }

        else if((year==prevy)&& (month<prevm))
        {
            //System.out.println("generated"+year+"-"+month+"-"+date);
            flag=1;
            prevy=year;
            prevm=month;
            prevd=date;
        }
        else if ((year==prevy) &&(month==prevm))
        {
            if(date<prevd)
            {
            //System.out.println("generated"+year+"-"+month+"-"+date);
            flag=1;
            prevy=year;
            prevm=month;
            prevd=date;
            }
        }
        else
        {

        }

    }

break;  
case 2:
if(leap==true)
{
    if(date>=1 &&date<=29)
    {
        if(year<prevy)
        {

        //          System.out.println("generated"+year+"-"+month+"-"+date);
                    flag=1;
                    prevy=year;
                    prevm=month;
                    prevd=date;

        }

        else if((year==prevy)&& (month<prevm))
        {
    //      System.out.println("generated"+year+"-"+month+"-"+date);
            flag=1;
            prevy=year;
            prevm=month;
            prevd=date;
        }
        else if ((year==prevy) &&(month==prevm))
        {
            if(date<prevd)
            {
//          System.out.println("generated"+year+"-"+month+"-"+date);
            flag=1;
            prevy=year;
            prevm=month;
            prevd=date;
            }
        }
        else
        {

        }
    }   
}
if(leap==false)
{

    if(date>=1 && date<=28)
    {

        if(year<prevy)
        {

//                  System.out.println("generated"+year+"-"+month+"-"+date);
                    flag=1;
                    prevy=year;
                    prevm=month;
                    prevd=date;

        }

        else if((year==prevy)&& (month<prevm))
        {
    //      System.out.println("generated"+year+"-"+month+"-"+date);
            flag=1;
            prevy=year;
            prevm=month;
            prevd=date;
        }
        else if ((year==prevy) &&(month==prevm))
        {
            if(date<prevd)
            {
        //  System.out.println("generated"+year+"-"+month+"-"+date);
            flag=1;
            prevy=year;
            prevm=month;
            prevd=date;
            }
        }
        else
        {

        }
    }   
}
    break;

}

}



}
\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

Oh dear, your code looks horrible. Note, that there are not so many people willing to review such a code. It is a must to follow the code conventions and format your code correctly. The latter can easily be done with an IDE.

CodeReview is a site to review your code and not to find errors in it. Print the values you expect to the console or use the debugger to recognize what your code is doing.

Therefore only some hints to make your code more clean:

Class names should be written in UpperCamelCase


Extract complicated code or code duplicates to methods. In method calculate you should extract the code of each case-statement to a method to clear to switch-case up:

switch (month) {
  case 1:
  case ...
  case 12: methodCall1(); break; // rename this method
  case ...
  case 11: methodCall2(); break; // rename this method
  case 2: methodCall3(); break; // rename this method
}

In method calculate in case 2 the code in the if statements are identical. Don't do such a code duplicate. Extract the code lines which are identical to methods (nearly all in this case) or extract the things which differs. In this case the if-header:

int value = 0;
if (leap) {
  value = 29;
}
else {
  value = 28;
}
...
if (date >= 1 && date <= value)
...

The same can be done in the remaining cases.


The logic in input in too complex. For example, what does this if mean:

if (array[0] <= 0 || array[1] <= 0 || array[2] <= 0
    || String.valueOf(array[0]).length() == 3)

Extract it to a method and give it a useful name. Some for other code blocks. You can split up input at least to 4 further methods.


If you clean up your code a lot, probably you will find the errors by yourself. And if not you will get more notice from other people if you post better code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.