'C#/수업내용'에 해당되는 글 13건

  1. 2019.04.01 List를 이용하여 Inventory 만들기
  2. 2019.04.01 Array를 이용하여 Inventory 만들기
  3. 2019.03.29 공부할것
  4. 2019.03.27 2. for, foreach, while
  5. 2019.03.24 1. Method

List를 이용하여 Inventory 만들기

C#/수업내용 2019. 4. 1. 14:56

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ArrayInventory
{
    class Inventory
    {
        //Item형식의 값을 저장할 공간 
        private List<Item> items = new List<Item>();
        public int inventoryCount = 10;
 
        //기본 생성자 
        public Inventory()
        {
            Console.WriteLine("Inventory클래스의 생성자");
            Console.WriteLine("items.Length: {0}", items.Count);
        }
 
        //넣고 (매개변수로 Item인스턴스를 받아 배열에 넣는다)
        public void AddItem(Item item)
        {
            //배열의 방에 현재 위치에 있는지 확인후 없으면 다음방에 넣는다.
            //Item, Item, null, null,null, null,null, null,null, null
            //Item, Item, Item, Item,Item, Item,Item, Item,Item, Item
            //더이상 넣을수 없습니다.
            if (this.GetItemCount() == inventoryCount)
            {
                Console.WriteLine("인벤토리가 꽉찼습니다.");
            }
            else
            {
                items.Add(item);
                Console.WriteLine("{0}의 제작이 완료되었습니다.", item.name);
            }
 
 
        }
 
        //뺀다 
        public void GetItemByName(string searchName)
        {
            //배열안에 있는 요소들중에 serachName 문자열과 비교해서 같은거 찾음
            //찾은거 반환, 못찾으면? null반환 
            //같은게 있다면? 하나만 가져오자 
            //돌도끼, 돌도끼, null, null,null, null,null, null,null, null
            //null, 돌도끼, null, null,null, null,null, null,null, null
            //반환값 : 돌도끼
            //null, 돌도끼, null, null,null, null,null, null,null, null
            //null, 돌도끼, null, 장검,null, null,null, null,null, null
            //돌도끼, 장검, null, null, null, ,null, null,null, null,null, null
            //손도끼, 돌도끼, null, null,null, null,null, null,null, null
            int index = 0;
            var readKey = ConsoleKey.D0;
            foreach (Item checkInventory in items)
            {
                if (checkInventory.name == searchName)
                {
                    Console.WriteLine("{0}가 인벤토리에 있습니다. 버리시겠습니까?(1.버린다 2.넣어둔다)", searchName);
                    readKey = Console.ReadKey().Key;
                    break;
                }
                index++;
            }
            if (readKey == ConsoleKey.D1)
            {
                items.RemoveAt(index);
                Console.WriteLine("{0}을 버렸습니다.", searchName);
            }
        }
 
        //목록출력
        public void GetItemList()
        {
            foreach (Item itemList in items)
            {
                if (itemList == null)
                {
                    break;
                }
                Console.Write("{0}  ", itemList.name);
            }
        }
 
        //인벤토리에 있는 아이템 갯수 가져오기 
        public int GetItemCount()
        {
            //배열안에 있는 값이 null이 아닐경우 카운트 해서 반환함 
            int cnt = 0;
            foreach (var item in items)
            {
                if (item != null)
                {
                    cnt++;
                }
            }
            return cnt;
        }
    }
}
cs


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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ArrayInventory
{
    class App
    {
        public App()
        {
            this.Start();
        }
        public void Start()
        {
            Inventory inventory = new Inventory();
            var inputKey = ConsoleKey.D0;
            while (inputKey != ConsoleKey.D4)
            {
                Console.WriteLine("\n1.아이템제작 2.인벤토리 3.아이템검색 4.종료하기");
                inputKey = Console.ReadKey().Key;
                string inputItemName;
                switch(inputKey)
                {
                    case ConsoleKey.D1:
                        Console.Write("제작하실 아이템 이름을 입력하세요 : ");
                        inputItemName = Console.ReadLine();
                        inventory.AddItem(new Item(inputItemName));
                        break;
                    case ConsoleKey.D2:
                        inventory.GetItemList();
                        break;
                    case ConsoleKey.D3:
                        Console.Write("검색하실 아이템 이름을 입력하세요 : ");
                        inputItemName = Console.ReadLine();
                        inventory.GetItemByName(inputItemName);
                        break;
                    case ConsoleKey.D4:
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
 
cs
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 ArrayInventory
{
    public class Item
    {
        public string name;
 
        public Item(string name) //매개변수로  이름을 받아 멤버변수에 저장함
        {
            this.name = name;
        }
    }
}
cs


'C# > 수업내용' 카테고리의 다른 글

Inventory를 다시 Array로 되돌리기  (0) 2019.04.01
제너릭 클래스, 상속을 이용하여 Inventory 만들기  (0) 2019.04.01
Array를 이용하여 Inventory 만들기  (0) 2019.04.01
공부할것  (0) 2019.03.29
2. for, foreach, while  (0) 2019.03.27
:

Array를 이용하여 Inventory 만들기

C#/수업내용 2019. 4. 1. 14:46

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ArrayInventory
{
    class App
    {
        public App()
        {
            this.Start();
        }
        public void Start()
        {
            Inventory inventory = new Inventory();
            var inputKey = ConsoleKey.D0;
            while (inputKey != ConsoleKey.D4)
            {
                Console.WriteLine("\n1.아이템제작 2.인벤토리 3.아이템검색 4.종료하기");
                inputKey = Console.ReadKey().Key;
                string inputItemName;
                switch(inputKey)
                {
                    case ConsoleKey.D1:
                        Console.Write("제작하실 아이템 이름을 입력하세요 : ");
                        inputItemName = Console.ReadLine();
                        inventory.AddItem(new Item(inputItemName));
                        break;
                    case ConsoleKey.D2:
                        inventory.GetItemList();
                        break;
                    case ConsoleKey.D3:
                        Console.Write("검색하실 아이템 이름을 입력하세요 : ");
                        inputItemName = Console.ReadLine();
                        inventory.GetItemByName(inputItemName);
                        break;
                    case ConsoleKey.D4:
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
 
cs

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 ArrayInventory
{
    public class Item
    {
        public string name;
 
        public Item(string name) //매개변수로  이름을 받아 멤버변수에 저장함
        {
            this.name = name;
        }
    }
}
cs


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
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ArrayInventory
{
    class Inventory
    {
        //Item형식의 값을 저장할 공간 
        private Item[] items = new Item[10];
 
        //기본 생성자 
        public Inventory()
        {
            Console.WriteLine("Inventory클래스의 생성자");
            Console.WriteLine("items.Length: {0}", items.Length);
        }
 
        //넣고 (매개변수로 Item인스턴스를 받아 배열에 넣는다)
        public void AddItem(Item item)
        {
            //배열의 방에 현재 위치에 있는지 확인후 없으면 다음방에 넣는다.
            //Item, Item, null, null,null, null,null, null,null, null
            //Item, Item, Item, Item,Item, Item,Item, Item,Item, Item
            //더이상 넣을수 없습니다.
            for(int index = 0; index < items.Length; index++)
            {
                if(this.GetItemCount() == 10)
                {
                    Console.WriteLine("인벤토리가 꽉찼습니다.");
                    break;
                }
                if(items[index] == null)
                {
                    items[index] = item;
                    Console.WriteLine("{0}의 제작이 완료되었습니다.", item.name);
                    break;
                }
            }
            
            
        }
 
        //뺀다 
        public void GetItemByName(string searchName)
        {
            //배열안에 있는 요소들중에 serachName 문자열과 비교해서 같은거 찾음
            //찾은거 반환, 못찾으면? null반환 
            //같은게 있다면? 하나만 가져오자 
            //돌도끼, 돌도끼, null, null,null, null,null, null,null, null
            //null, 돌도끼, null, null,null, null,null, null,null, null
            //반환값 : 돌도끼
            //null, 돌도끼, null, null,null, null,null, null,null, null
            //null, 돌도끼, null, 장검,null, null,null, null,null, null
            //돌도끼, 장검, null, null, null, ,null, null,null, null,null, null
            //손도끼, 돌도끼, null, null,null, null,null, null,null, null
            int index = 0;
            Item temp;
            foreach(Item checkInventory in items)
            {
                if(checkInventory.name == searchName)
                {
                    Console.WriteLine("{0}가 인벤토리에 있습니다. 버리시겠습니까?(1.버린다 2.넣어둔다)", searchName);
                    var readKey = Console.ReadKey().Key;
                    if(readKey == ConsoleKey.D1)
                    {
                        items[index] = null;
                        if(items.Length != 0)
                        {
                            for (int moveIndex = 0; moveIndex < items.Length - 1; moveIndex++)
                            {
                                if (items[moveIndex] == null)
                                {
                                    temp = items[moveIndex + 1];
                                    items[moveIndex + 1= items[moveIndex];
                                    items[moveIndex] = temp;
                                }
                            }
                            Console.WriteLine("{0}이 삭제되고 인벤토리가 정렬되었습니다.", searchName);
                        }
                        Console.WriteLine("{0}이 삭제되었습니다.", searchName);
                        break;
                    }
                }
                index++;
            }
        }
        
        //목록출력
        public void GetItemList()
        {
            foreach(Item itemList in items)
            {
                if(itemList == null)
                {
                    break;
                }
                Console.Write("{0}  ", itemList.name);
            }
        }
 
        //인벤토리에 있는 아이템 갯수 가져오기 
        public int GetItemCount()
        {
            //배열안에 있는 값이 null이 아닐경우 카운트 해서 반환함 
            int cnt = 0;
            foreach (var item in items)
            {
                if (item != null)
                {
                    cnt++;
                }
            }
            return cnt;
        }
    }
}
cs


'C# > 수업내용' 카테고리의 다른 글

제너릭 클래스, 상속을 이용하여 Inventory 만들기  (0) 2019.04.01
List를 이용하여 Inventory 만들기  (0) 2019.04.01
공부할것  (0) 2019.03.29
2. for, foreach, while  (0) 2019.03.27
1. Method  (0) 2019.03.24
:

공부할것

C#/수업내용 2019. 3. 29. 18:19

object

일단 느낌은 var타입 같은 느낌, method의 매개변수를 해놓으면

어떤 타입이건 전달받기 가능

모든 type은 object class를 상속받았기 때문에 가능하다.






0. Table File load

1. 월드생성

2. 오브젝트들 인스턴스화

3. Table참조하여 id에 맞는 오브젝트 인스턴스화하고 이름같은 속성 저장



아이템 타입 = enum에 열거하고 파싱


table(excel)->file(txt)->instance


txt에서 itemId를 기준으로 참조하여 나머지 값들을 인스턴스화한다.



data binding class는 table과 value가 같아야한다. (무결성)



C# : Generic, IEnumerable, IEnumerator, Collection, delegate, event...



ArrayList //안쓰는걸 권유 참조형이 boxing/unboxing되는 과정에서 성능적문제

Hashtable

Queue

Stack


Boxing -> 값형식을 object형식으로 변환하여 래핑 힙에 저장한다.

암시적으로

UnBoxing -> 개체에서 값형식이 추출된다.

명시적으로


일반화 : class/method design 형식 매개변수 개념

런타임 캐스팅, boxing 작업에 대한 비용이나 위험 발생 x

Collection Class 만드는데 이용,

Interface, class, method, event, delegate 만들 수 있음

<T>를 통하여 다양한 타입의 클래스를 다양하게 인스턴스화가능

(중복안시키고)



함수형언어, 람다식

'C# > 수업내용' 카테고리의 다른 글

제너릭 클래스, 상속을 이용하여 Inventory 만들기  (0) 2019.04.01
List를 이용하여 Inventory 만들기  (0) 2019.04.01
Array를 이용하여 Inventory 만들기  (0) 2019.04.01
2. for, foreach, while  (0) 2019.03.27
1. Method  (0) 2019.03.24
:

2. for, foreach, while

C#/수업내용 2019. 3. 27. 14:19
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace study0327
{
    class App
    {
        public App()
        {
            int i = 0;
            
            string[] arrNames = { "홍길동""임꺽정""홍상직" };
 
            for (i = 0;  i<arrNames.Length; i++)
            {
                Console.Write(" {0}", arrNames[i]);
            }
 
            i = 0;
            while(i<arrNames.Length)
            {
                Console.Write(" {0}", arrNames[i]);
                i++;
            }
 
            foreach(string arr in arrNames)
            {
                Console.Write(" {0}", arr);
            }
            Console.ReadKey();
        }
    }
}
 
cs

foreach를 이용하여 배열의 원소들을 차례로 출력하는 예시를 작성해보았다.
for와 while도 같은 역할을 하게끔 작성했다.

결과 값은 : " 홍길동 임꺽정 홍상직 홍길동 임꺽정 홍상직 홍길동 임꺽정 홍상직"이다.



'C# > 수업내용' 카테고리의 다른 글

제너릭 클래스, 상속을 이용하여 Inventory 만들기  (0) 2019.04.01
List를 이용하여 Inventory 만들기  (0) 2019.04.01
Array를 이용하여 Inventory 만들기  (0) 2019.04.01
공부할것  (0) 2019.03.29
1. Method  (0) 2019.03.24
:

1. Method

C#/수업내용 2019. 3. 24. 23:33

c#에서 Method(메서드)는 일련의 문을 포함하는 코드 블록으로서, Class 또는 Struct 내에서 서명한다.

서명의 범위는 액세스 수준(public, private), 선택적 한정자, 리턴 값, 이름, 매개 변수를 포함한다.

C, C++에서 얘기하는 Function(함수)와 형식이 같다.


예제


위의 예제에서는 Computer에 속한 public수준을 가진 out_info라는 Method를 서명하고 Main에서 호출하는 예제이다.

이 예제에서 액세스 수준은 public, 리턴 값은 void형이므로 없음, 이름은 out_info, 매개 변수는 int형 number이다.

호출시 A_Computer라는 개체에 존재하는 out_info를 불러야 하므로 온점(' . ')을 사용하여 호출 후, 괄호안에 number에 해당하는 값을 전달한다.

그 결과 전달된 "1"이라는 값이 number의 값에 대입되어 "1번째 삼보의 가격 : 100"이라는 문자열이 출력된다.



참조 : Microsoft Docs(메서드(C# 프로그래밍 가이드))

   https://docs.microsoft.com/ko-kr/dotnet/csharp/programming-guide/classes-and-structs/methods

'C# > 수업내용' 카테고리의 다른 글

제너릭 클래스, 상속을 이용하여 Inventory 만들기  (0) 2019.04.01
List를 이용하여 Inventory 만들기  (0) 2019.04.01
Array를 이용하여 Inventory 만들기  (0) 2019.04.01
공부할것  (0) 2019.03.29
2. for, foreach, while  (0) 2019.03.27
: