프로그래머스

C# - 개인정보 수집 유효기간

ybbro 2023. 1. 18. 16:41

https://school.programmers.co.kr/learn/courses/30/lessons/150370

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

using System;
using System.Collections.Generic;

public class Solution {
    public int[] solution(string today, string[] terms, string[] privacies)
    {
        List<int> answer_List = new List<int>();

        // DateTime 타입을 써보기
        // 년,월,일 구분값은 [스페이스],[/],[-] 으로만 구분 가능(섞어서 사용 가능)
        // 해당 문제에서 년, 월, 일은 [.] 로 구분되어 있으니,
        // 해당 구분자만 유효 구분자로 치환
        today = today.Replace(".", "/");
        // 오늘 날짜를 DateTime 형태로 바꿔줌
        DateTime today_DateTime = Convert.ToDateTime(today);
        DateTime temp_DateTime = new DateTime();

        // 각 개인정보마다
        for (int i = 0; i < privacies.Length; i++)
        {
            // 해당 개인 정보를 점, 공백 을 기준으로 연, 월, 일, 약관 종류를 나눔
            string[] temp = privacies[i].Split('.', ' ');
            // 약관종류를 검색하여
            for (int j = 0; j < terms.Length; j++)
            {
                // 약관 종류가 같은 것을 찾아서
                if (temp[3][0] == terms[j][0])
                {
                    // 개인 정보 가입 연, 월을 계산을 위해 int형으로 변환
                    int temp_year = Convert.ToInt32(temp[0]);
                    int temp_month = Convert.ToInt32(temp[1]);
                    int temp_day = Convert.ToInt32(temp[2]);
                    // 개인정보를 가입한 달에 보관기간을 더해줌
                    string[] this_term = terms[j].Split(' ');
                    temp_month += Convert.ToInt32(this_term[1]);
                    // 월이 13을 넘어가면 12를 빼주고 연에 1을 추가
                    while(temp_month > 12)
                    {
                        temp_month -= 12;
                        temp_year++;
                    }
                    // 산출한 만료일을 DateTime 형으로
                    temp_DateTime = new DateTime(temp_year, temp_month, temp_day);
                    break;
                }
            }
            /* public static int Compare (DateTime A, DateTime B);
               A가 B보다 이전인 경우, 반환 값은 0보다 작음
               동일한 날짜인 경우, 0을 반환
               A가 B보다 이후인 경우, 반환 값은 0보다 큼
            */
            if(DateTime.Compare(today_DateTime, temp_DateTime) >= 0)
            {
                answer_List.Add(i + 1);
            }
        }
        int[] answer =  answer_List.ToArray();
        return answer;
    }
}