U E D R , A S I H C RSS

서지혜/Calendar


1. Lesson Learned

  • 으아니 왜 루비로 짜도 깔끔하고 예쁘지 않지.. 코드량의 감소가 얼마없어..
  • 루비의 attribute accessor 좀 편한거같다. lambda도 괜찮은거같고(디버깅할땐 빼고)..
    • 나 요새 ASP.NET 하면서 C# 써봤는데 attribute accessor나 lambda C#에서도 많이 쓰더라ㅋㅋㅋㅋ - 김수경
      • 글쿤 많이 지원하는구나.. 사실 attribute accessor나 lambda가 이해되는건아닌데ㅜㅜ attribute accessor가 어떻게 필드를 public처럼 접근가능하게 하면서 encapsulation을 지원하는지 잘 몰게뜸ㅠㅠ code block을 넘긴다는 말도 그렇고.. - 서지혜

2. java

  • Calendar class

public class Calendar {
    public static void main(String[] args) {
        System.out.println("Input year pleas : ex) 2013");
        System.out.print(">> ");

        Scanner scanner = new Scanner(System.in);
        int year = Integer.parseInt(scanner.nextLine());

        new Year(year).print();
    }
}
  • Month class

public class Month {
    private int month;
    private int length;
    private int startDate; // 1:mon 2:tue 3:wed 4:thu 5:fri 6 :sat 0:sun

    public Month(int month, int length, int startDate) {
        this.month = month;
        this.length = length;
        this.startDate = startDate;
    }

    public void print() {
        System.out.println("MONTH : " + month);
        System.out.println("sun\tmon\ttue\twed\tthu\tfri\tsat");

        String days = "";

        for (int i = 1; i <= startDate; i++) {
            days += "\t";
        }

        for (int i = 1; i <= length; i++) {
            days += i;
            days += ((i + startDate) % 7 == 0 ? "\n" : "\t");
        }

        System.out.println(days + "\n");
    }
}
  • Year class

public class Year {
    private int year;
    private List<Month> months;

    public Year(int year) {
        this.year = year;
        setMonths();
    }

    private void setMonths() {
        months = MonthFactory.getMonths(year);
    }

    public void print() {
        System.out.println("THIS YEAR IS : " + year + "\n\n");

        for (Month m : months) {
            m.print();
        }
    }
}
  • MonthFactory

public class MonthFactory {
    public static List<Month> getMonths(int year) {
        List<Month> months = new ArrayList();

        for (int i = 1; i <= 12; i++) {
            months.add(new Month(i, getDaysOfMonth(year, i), getStartDate(year, i)));
        }

        return months;
    }

    public static int getDaysOfYear(int year) {
        return isLeap(year) ? 366 : 365;
    }

    public static int getDaysOfMonth(int year, int month) {
        switch (month) {
            case 1: case 3: case 5: 
            case 7: case 8: case 10: case 12:
                return 31;
            case 4: case 6: case 9: case 11:
                return 30;
            case 2:
                return (isLeap(year) ? 29 : 28);
            default:
                return 0;
        }
    }

    public static boolean isLeap(int year) {
        return (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0);
    }

    public static int getStartDate(int year, int month) {
        int totalDays = 0;

        for (int i = 1; i < year; i++) {    // add days of years
            totalDays += getDaysOfYear(i);
        }

        for (int i = 1; i < month; i++) {   // add days of months
            totalDays += getDaysOfMonth(year, i);
        }

        return (totalDays + 1) % 7;   // +1 for sunday
    }
}

3. ruby

  • Calendar class

class Calendar
  attr_accessor :year

  def initialize(year)
    @year = CalendarFactory.new.create(year)
  end

  def print_year
    @year.print_all
  end
end
  • Year class

class Year
  attr_accessor :year, :months, :is_leap

  def initialize(year, months, is_leap)
    @year = year
    @months = months
    @is_leap = is_leap
  end

  def print_all
    puts @year
    months.each { |month| month.print_month }
  end
end
  • Month class

class Month
  attr_accessor :month, :start_date, :length

  def initialize(month, length, start_date)
    @month = month
    @length = length
    @start_date = start_date
  end

  def print_month
    days = ""

    @start_date.times do
      days += "\t"
    end

    (1..@length).to_a.each { |day|
      days += day.to_s
      days += ((day + @start_date) % 7 == 0 ? "\n" : "\t")
    }

    puts month.to_s + "월", days, "\n"
  end
end
  • CalendarFactory class

class CalendarFactory
  def initialize
  end

  def create(year)
    Year.new(year, months_of_year(year), is_leap(year))
  end

  def months_of_year(year)
    months = [];
    (1..12).to_a.each { |month|
      months << Month.new(month, length_of_month(year, month), start_date(year, month))
    }
    months
  end

  def length_of_month(year, month)
    case month
      when 1, 3, 5, 7, 8, 10, 12
        return 31
      when 4, 6, 9, 11
        return 30
      when 2
        return (is_leap(year) ? 29 : 28)
    end
  end

  def start_date(year, month)
    total = 0;

    (1..year - 1).to_a.each { |x|
      total += (is_leap(x) ? 366 : 365)
    }
    (1..month - 1).to_a.each { |x|
      total += length_of_month(year, x)
    }

    (total + 1) % 7
  end

  def is_leap(year)
    (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0)
  end
end
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:30:12
Processing time 0.0140 sec