Package testService

Source Code of testService.GoodCode

package testService;

import java.util.Scanner;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import model.MyDate;

@ContextConfiguration(locations = { "classpath:application.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class GoodCode {
  int[] days = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  int[] days2 = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

  @Test
  public void testLeap() {
    System.out.println(isLeapYear(1987));
  }

  @Test
  public void testCorrect() {
    System.out.println(isCorrect(1987, 30, 33));
    System.out.println(isCorrect(1987, 12, 31));
    System.out.println(isCorrect(1987, 4, 31));
  }

  @Test
  public void testGetDate() {
    // System.out.println(getDate(1987, 88));
    System.out.println(getDate(1989, 355));
    System.out.println(getDate(1998, 266));
    System.out.println(getDate(2012, 367));
  }

  @Test
  public void testGetDays() {
    // System.out.println(getDate(1987, 88));
    System.out.println(getDays(1989, 2, 1));
    System.out.println(getDays(1998, 3, 5));
    System.out.println(getDays(2012, 6, 7));
  }

  @Test
  public void testGetNext() {
    // System.out.println(getDate(1987, 88));
    System.out.println(getNext(new MyDate(1989, 2, 1)));
    System.out.println(getNext(new MyDate(1998, 3, 5)));
    System.out.println(getNext(new MyDate(2012, 6, 7)));
  }

  @Test
  public void testGetPrevious() {
    // System.out.println(getDate(1987, 88));
    System.out.println(getPrevious(new MyDate(1989, 2, 1)));
    System.out.println(getPrevious(new MyDate(1998, 3, 5)));
    System.out.println(getPrevious(new MyDate(2012, 6, 7)));
  }

  public MyDate getNext(MyDate input) {
    int y = input.getYear();
    int m = input.getMonth();
    int d = input.getDay() + 1;

    do {
      d += this.days[m--];
    } while (m > 0);
    return getDate(y, d);
  }

  public MyDate getPrevious(MyDate input) {
    int y = input.getYear();
    int m = input.getMonth();
    int d = input.getDay() - 1;

    do {
      d += this.days[m--];
    } while (m > 0);
    return getDate(y, d);
  }

  public int getDays(int year, int month, int day) {
    int all = 0;
    while (--month >= 0) {
      all += days[month];
      if (month == 2 && !isLeapYear(year)) {
        all--;
      }
    }
    all += day;
    return all;
  }

  public MyDate getDate(int year, int days) {
    MyDate myDate = new MyDate();
    int m = 0;
    if ((days > 365 && !isLeapYear(year))
        || (days > 366 && isLeapYear(year))) {
      myDate.setYear(year++);
      myDate.setMonth(1);
      myDate.setDay(1);
      return myDate;
    }
    do {
      days = days - this.days[m++];
      if (m == 2 && !isLeapYear(year)) {
        days++;
      }
    } while (days > 0);
    myDate.setDay(this.days[--m] + days);
    myDate.setMonth(m);
    myDate.setYear(year);
    return myDate;

  }

  public MyDate getInput() {
    Scanner in = new Scanner(System.in);
    MyDate myDate = new MyDate();
    try {
      myDate.setYear(in.nextInt());
      myDate.setMonth(in.nextInt());
      myDate.setDay(in.nextInt());
    } catch (Exception e) {
      System.out.println("请输入正确的数字");
      return null;
    }
    return myDate;
  }

  public boolean isCorrect(int year, int month, int day) {
    if (year < 1 || month < 1 || day < 1 || month > 12) {
      return false;
    }
    if (isLeapYear(year)) {
      if (day > days[month - 1]) {
        return false;
      }
    } else {
      if (day > days2[month - 1]) {
        return false;
      }
    }
    return true;
  }

  public boolean isLeapYear(int year) {
    boolean result = false;
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
      result = true;
    }
    return result;
  }
}
TOP

Related Classes of testService.GoodCode

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.