ExchangeMoney, 돈 송수금 하기

[설명]

Bob과 Joe의 돈을 은행을 통해 송금하자.
송금할 때 마다 수수료가 1달러씩 은행에 지불됨.
계속 송금하다 보면 결국 은행만 수수료로 돈을 벌고 Bob과 Joe는 거지가 됨.
Head First에 있는 예제와는 코드가 다소 다름.
어딘가 헛점이 많고 더 단축할 수 있는 코드이나 수정하기엔 할 일이 많다~

 

[실행화면] 

 

[소스코드]

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace ExMoney
{
 
    public partial class Form1 : Form
    {
 
        int bank = 0;
        int interest = 1;
        Guy bob = new Guy();
        Guy joe = new Guy();
 
        public Form1()
        {
            bob.cash = 100;
            joe.cash = 100;
            InitializeComponent();
            UpdateForm(); // InitializeComponent() 보다 먼저 수행하면 레퍼런스 에러!
 
        }
 
        private void btn_1_Click(object sender, EventArgs e)
        {
            int temp = bob.GiveCash(5, interest);
            if (temp != 0) // GiveCash()에서 0이 리턴될 경우는 송금한 돈이 없는 것이므로, 0이 아닐 때 수행되어야 함!
            {
                bank += temp;
                bank -= joe.ReceiveCash(temp - interest);
            }
            UpdateForm(); // Form 내용 갱신
        }
 
        private void btn_2_Click(object sender, EventArgs e)
        {
            int temp = joe.GiveCash(10, interest);
            if (temp != 0) // GiveCash()에서 0이 리턴될 경우는 송금한 돈이 없는 것이므로, 0이 아닐 때 수행되어야 함!
            {
                bank += temp;
                bank -= bob.ReceiveCash(temp - interest);
            }
            UpdateForm(); // Form 내용 갱신
        }
         
        public void UpdateForm()
        {
            bobMoney.Text = "Bob은 " + bob.cash + " 달러를 가지고 있습니다.";
            joeMoney.Text = "Joe는 " + joe.cash + " 달러를 가지고 있습니다.";
            bankMoney.Text = "은행은 " + bank + " 달러를 가지고 있습니다.";
        }
    }
 
    public class Guy
    {
        public string Name;
        public int cash;
 
        public int GiveCash(int amount, int interest)
        {
            if(cash >= amount+interest)
            {
                cash -= amount + interest;
                return amount + interest;
            }
            else
            {
                MessageBox.Show("송금할 돈이 부족합니다!");
                return 0;
            }
 
        }
 
        public int ReceiveCash(int amount)
        {
            cash += amount;
            return amount;
        }
 
    }
}
  Comments,     Trackbacks