C#/과제

<인벤토리> 0.5차

최승길 2019. 3. 27. 22:00


실행영상

App.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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace inventory_00

{

    class App

    {

        Inventory inventory = new Inventory();

 

        public App()

        {

            while(true)

            {

                inventory.ConsoleMain();

                var input = Console.ReadLine();

                if (input == "1")

                {

                    //목록보기

                    Console.WriteLine("목록보기");

                    inventory.List();

                }

                else if (input == "2")

                {

                    //아이템 넣기

                    Console.WriteLine("아이템넣기");

                    inventory.AddItem();

                }

                else if (input == "3")

                {

                    //아이템 버리기

                    Console.WriteLine("아이템버리기");

                    inventory.RemoveItem();

                }

                else if (input == "0")

                {

                    Console.WriteLine("종료합니다.");

                    break;

                }

                else

                {

                    Console.WriteLine("잘못된 입력입니다.");

                }

            }

        }

    }

}

 

Colored by Color Scripter

cs


Inventory.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

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace inventory_00

{

    class Inventory

    {

        List<string> inventory = new List<string>();

        Item redPotion = new Item("빨간물약""물약");

        Item bluePotion = new Item("파란포션""물약");

        Item longSword = new Item("롱소드""장비");

        Item shortSword = new Item("숏소드""장비");

 

        private int itemTotalStack = 0;

 

        public int ItemTotalStack

        {

            get

            {

                return itemTotalStack;

            }

            set

            {

                itemTotalStack = value;

            }

        }//itemTotalStack의 속성정의

 

        public void ConsoleMain()

        {

            Console.Write("==인벤토리==\n(1.목록/2.아이템 넣기/3.아이템 버리기/0.종료)(숫자입력):");

        }

 

        public void List()

        {

            if (itemTotalStack <= 0)

            {

                itemTotalStack = 0;

                Console.WriteLine("인벤토리에 아이템이 없습니다.");

            }

            else

            {

                redPotion.Stack = 0;

                bluePotion.Stack = 0;

                longSword.Stack = 0;

                shortSword.Stack = 0;

 

                foreach (var i in inventory)

                {

                    if (i == "빨간포션")

                    {

                        redPotion.Stack++;

                    }

                    else if (i == "파란포션")

                    {

                        bluePotion.Stack++;

                    }

                    else if (i == "롱소드")

                    {

                        longSword.Stack++;

                    }

                    else if (i == "숏소드")

                    {

                        shortSword.Stack++;

                    }

 

                }

                if (redPotion.Stack != 0)

                {

                    Console.WriteLine("빨간포션:{0}", redPotion.Stack);

                }

                if(bluePotion.Stack != 0)

                {

                    Console.WriteLine("파란포션:{0}", bluePotion.Stack);

                }

                if (longSword.Stack != 0)

                {

                    Console.WriteLine("롱소드:{0}", longSword.Stack);

                }

                if (shortSword.Stack != 0)

                {

                    Console.WriteLine("숏소드:{0}", shortSword.Stack);

                }

 

            }

        }

 

        public void AddItem()

        {

            Console.Write("(빨간포션, 파란포션, 롱소드, 숏소드)\n어떤 아이템을 넣으시겠습니까?:");

            var input = Console.ReadLine();

 

            if (input == "빨간포션")

            {

                Console.WriteLine("빨간포션을 넣었습니다.");

                inventory.Add("빨간포션");

                itemTotalStack++;

            }

            else if (input == "파란포션")

            {

                Console.WriteLine("파란포션을 넣었습니다.");

                inventory.Add("파란포션");

                itemTotalStack++;

            }

            else if (input == "롱소드")

            {

                Console.WriteLine("롱소드를 넣었습니다.");

                inventory.Add("롱소드");

                itemTotalStack++;

            }

            else if (input == "숏소드")

            {

                Console.WriteLine("숏소드를 넣었습니다.");

                inventory.Add("숏소드");

                itemTotalStack++;

            }

            else

            {

                Console.WriteLine("잘못된 입력입니다.");

            }

        }

 

        public void RemoveItem()

        {

            if (itemTotalStack == 0)

            {

                Console.WriteLine("버릴 아이템이 없습니다.");

            }

            else

            {

                Console.Write("(빨간포션, 파란포션, 롱소드, 숏소드)\n어떤 아이템을 버리시겠습니까?");

                var input = Console.ReadLine();

                if (input == "빨간포션")//빨간포션 버리기

                {

                    redPotion.Stack = 0;

                    foreach (var i in inventory)//빨간포션이 몇개인지 확인

                    {

                        if (i == "빨간포션")

                        {

                            redPotion.Stack++;

                        }

                    }

                    if (redPotion.Stack <= 0)

                    {

                        Console.WriteLine("빨간포션이 없습니다.");

                    }

                    else

                    {

                        Console.WriteLine("빨간포션를 버렸습니다.");

                        inventory.Remove("빨간포션");

                        itemTotalStack--;

                    }

                }

                else if (input == "파란포션")//파란포션 버리기

                {

                    bluePotion.Stack = 0;

                    foreach (var i in inventory)//파란포션 몇개인지 확인

                    {

                        if (i == "파란포션")

                        {

                            bluePotion.Stack++;

                        }

                    }

                    if (bluePotion.Stack <= 0)

                    {

                        Console.WriteLine("파란포션가 없습니다.");

                    }

                    else

                    {

                        Console.WriteLine("파란포션를 버렸습니다.");

                        inventory.Remove("파란포션");

                        itemTotalStack--;

                    }

                }

                else if (input == "롱소드")//롱소드 버리기

                {

                    longSword.Stack = 0;

                    foreach (var i in inventory)//롱소드가 몇개인지 확인

                    {

                        if (i == "롱소드")

                        {

                            longSword.Stack++;

                        }

                    }

                    if (longSword.Stack <= 0)

                    {

                        Console.WriteLine("롱소드가 없습니다.");

                    }

                    else

                    {

                        Console.WriteLine("롱소드를 버렸습니다.");

                        inventory.Remove("롱소드");

                        itemTotalStack--;

                    }

                }

                else if (input == "숏소드")//숏소드 버리기

                {

                    shortSword.Stack = 0;

                    foreach (var i in inventory)//숏소드 몇개인지 확인

                    {

                        if (i == "숏소드")

                        {

                            shortSword.Stack++;

                        }

                    }

                    if (shortSword.Stack <= 0)

                    {

                        Console.WriteLine("숏소드 없습니다.");

                    }

                    else

                    {

                        Console.WriteLine("숏소드 버렸습니다.");

                        inventory.Remove("숏소드");

                        itemTotalStack--;

                    }

                }

                else

                {

                    Console.WriteLine("잘못된 입력입니다.");

                }

            }

        }

    }

}

Colored by Color Scripter

cs

Item.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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace inventory_00

{

    class Item

    {

        private string name;

        private int stack;

        private string type;

 

        //속성정의 3개

        public string Name

        {

            get

            {

                return name;

            }

            set

            {

                name = value;

            }

        }

 

        public int Stack

        {

            get

            {

                return stack;

            }

            set

            {

                stack = value;

            }

        }

 

        public string Type

        {

            get

            {

                return type;

            }

            set

            {

                type = value;

            }

        }

 

        public Item(string name,string type)

        {

            this.name = name;

            this.type = type;

        }

    }

}

 

Colored by Color Scripter

cs

Program.cs

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace inventory_00

{

    class Program

    {

        static void Main(string[] args)

        {

            new App();

 

            Console.ReadKey();

        }

    }

}

 

Colored by Color Scripter

cs