본문 바로가기

C#/복습

2020-05-02 이름 자동으로 섞기

교육받는 곳에서 자리를 바꾸기위해 뽑기함이 필요했는데, 코딩복습도 할겸 자동으로 이름을 섞어주는 프로그램을 코딩해봤다.

뽑기가아닌, 자동으로 섞어버리는걸 만들어버렸는데, 뭐 그거나 그거나 똑같다고 생각한다ㅎㅎ

RandomMachine.zip
5.25MB

-----------------------------------------------------------------------------------------------------------------------------------

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
namespace RandomMachine
{
    class App
    {
        DataManager dataManager;
        Random rand;
        SeatInfo seatInfo;
        List<string> peopleList;
 
        public App()
        {
            this.Init();
            dataManager.LoadData();
 
            Console.WriteLine("자리를 섞으시려면 엔터를 입력하세요.");
            Console.ReadLine();
 
            this.GetMySeat();
            //this.SeatOfPrint();
            this.SeatOfPrint();
 
 
            Console.ReadLine();
        }
 
        public void Init()
        {
            this.dataManager = DataManager.GetInstance();
            this.rand = new Random();
            this.peopleList = new List<string>();
            this.seatInfo = new SeatInfo();
        }
 
        public void GetMySeat()
        {
            int randomNum = rand.Next(0this.dataManager.GetDicLangth());  //16
            
            for (int i = 0; i < this.dataManager.GetDicLangth(); i++)
            {
                peopleList.Add(this.dataManager.GetNameById(i+1).name);
            }
 
            this.SuffleSeat(this.peopleList);
        }
 
        public void SuffleSeat(List<string> list)
        {
            int randominx1 = 0;
            int randominx2 = 0;
            int SeatNum = 0;
 
            string temp = null;
 
            for (int index = 0; index < list.Count; index++)
            {
                randominx1 = rand.Next(0list.Count);
                randominx2 = rand.Next(0list.Count);
 
                temp = list[randominx1];
                list[randominx1] = list[randominx2];
                list[randominx2] = temp;
            }
 
 
            foreach (var element in list)
            {
                this.seatInfo.dicSeatInfo.Add(string.Format("{0}번 자리",SeatNum+1), element);
                SeatNum++;
            }
 
 
            foreach (var pair in this.seatInfo.dicSeatInfo)
            {
                Console.WriteLine("[{0}]:{1}",pair.Key,pair.Value);
            }
            this.SaveToLocal(this.seatInfo);
        }
 
        public void SaveToLocal(SeatInfo info)
        {
            File.WriteAllText("./data/random_seat_result.json",JsonConvert.SerializeObject(info.dicSeatInfo));
        }
 
        public void SeatOfPrint()
        {
            Console.WriteLine("=====================================좌석배치표========================================");
            Console.Write("[   1   ] [   2   ] [   3   ] [   4   ]\t\t");
            Console.WriteLine("[   5   ] [   6   ] [   7   ] [   8   ]");
 
            Console.Write("                    [   9   ] [   10  ]\t\t");
            Console.WriteLine("[   11  ] [   12  ] [   13  ] [   14  ]");
 
            Console.Write("                                       \t\t");
            Console.WriteLine("[   15  ] [   16  ]                    ");
            Console.WriteLine("=======================================================================================");
        }
    }
}
 
 
 

DataManager.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
namespace RandomMachine
{
    class DataManager
    {
        private static DataManager instance;
        private Dictionary<int, NameData> dicNameData;
        private const string NAME_DATA_PATH = "./data/name_data.json";
 
        private DataManager()
        {
            this.Init();
        }
        public static DataManager GetInstance()
        {
            if (instance == null)
            {
                instance = new DataManager();
            }
            return DataManager.instance;
        }
        public void Init()
        {
            this.dicNameData = new Dictionary<int, NameData>();
        }
        public void LoadData()
        {
            this.dicNameData = (JsonConvert.DeserializeObject<NameData[]>
                (File.ReadAllText(NAME_DATA_PATH))).ToDictionary(x => x.id, x => x);
        }
 
        public NameData GetNameById(int id)
        {
            return this.dicNameData[id];
        }
 
        public int GetDicLangth()
        {
            return this.dicNameData.Count;
        }
    }
}
 
 
 

NameData.Class

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace RandomMachine
{
    class NameData
    {
        public int id;
        public string name;
    }
}
 
 

SeatInfo.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 RandomMachine
{
    class SeatInfo
    {
        public Dictionary<stringstring> dicSeatInfo;
        public SeatInfo()
        {
            this.dicSeatInfo = new Dictionary<stringstring>();
        }
    }
}
 
 
 

 

참고 :https://minhyeokism.tistory.com/16