C#/수업내용

2020.04.23. 수업내용 - 시간 지나면 하트주기(DateTime)

dev_sr 2020. 4. 24. 00:52

 

 

1. App class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Threading;
 
namespace Study_015
{
    class App
    {
        public App()
        {
           
            //현재 시간 출력하기
            Console.WriteLine("현재 시간 : {0}",DateTime.Now.ToString());
 
            //현재 시간 저장하기
            //string savejson = JsonConvert.SerializeObject(DateTime.Now);
            //File.WriteAllText("./lastTime_data.json", savejson);
 
            GameTimeInfo gameTimeInfo = new GameTimeInfo();
 
 
            //저장한 시간 불러오기
            string loadjson= File.ReadAllText("./lastTime_data.json");
 
            gameTimeInfo.strLastLoginTime = JsonConvert.DeserializeObject<string>(loadjson);
 
            //저장된 시간
            DateTime lastTime= Convert.ToDateTime(gameTimeInfo.strLastLoginTime);
            Console.WriteLine("마지막으로 저장된 시간: {0}",lastTime);
 
            //현재시간
            DateTime currentTime = DateTime.Now;
 
            //시간 간의 차이값 구하기(시간 빼기)
            TimeSpan result = currentTime - lastTime;
 
 
 
            if (result.Minutes >= 10)
            {
                Console.WriteLine("\n하트가 지급되었습니다.");
 
                gameTimeInfo.strLastLoginTime=DateTime.Now.ToString();
 
                string savejson = JsonConvert.SerializeObject(DateTime.Now);
                File.WriteAllText("./lastTime_data.json", savejson);
 
            }
            else
            {
                DateTime rewardDateTime = lastTime.AddMinutes(10);
                TimeSpan remainTimeSpan = rewardDateTime - DateTime.Now;
                Console.WriteLine("{0}분 {1}초를 더 기다려야합니다", remainTimeSpan.Minutes, remainTimeSpan.Seconds);
            }
 
        }
 
    }
}
 
 

 

 

 

2. GameTimeInfo class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_015
{
    class GameTimeInfo
    {
        public string strLastLoginTime;
        public GameTimeInfo()
        {
          
        }
    }
}
 
 

 

 

3. 결과값

 

1) 마지막 시간 저장 후 10분이 지났을 때

 

2) 하트를 받고 10분이 안 지났을 때