본문 바로가기

C#

C# 개발- 배열 /반복문/DATETIME으로 WINFORM UI 만들어 보기

반응형

Customer class

using System;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;

namespace AnimalShelter
{
    public class Customer //같은 네임 스페이스에서만 사용 가능 밖으로 못가 !  
    {
        public string FirstName;
        public string LastName;
        private DateTime _Birthday;
       
        private bool _IsQualified;
        //private 으로 수정이 불가능하게하여 보안을 강화 -> 이걸 변경할수 있는 메서드를 만들어주자
        public string Address;
        public string Description;


        public Customer(string FirstName , string LastName, DateTime Birthday)
        {
            this.FirstName = FirstName;
            this.LastName = LastName;
            this._Birthday = Birthday;
            this._IsQualified = (Age>= 18);
        }
        
        public DateTime Birthday
        {
            get { return _Birthday; }
            set {
                _Birthday = value;
                _IsQualified = Age  >= 18;
                }
        }
        //하지만 사용이 제한적인다 ! get은 인자가 없어야하고 /set은 하나의 인자만 가져야한다!
        public int Age //속성 프로퍼티 //필드처럼 보이나 안에 설정이 가능하다
        {
            get { return DateTime.Now.Year-_Birthday.Year; }
         
        }
        public bool GetIsQualified()
        {
            return _IsQualified;
        }
        public bool IsQualified
        {   //get 만있는 읽기 전용
            get { return _IsQualified; }
        }
        
        
        public string FullName { get { return FirstName + " " + LastName; } }
        public String GetFullName()
        {
            return FirstName + " " + LastName;
        }
    }
}

Form.class

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnimalShelter
{
    public partial class Form1 : Form
    {
        public Customer customer;
        public Customer[] customerArray = new Customer[10];
        public int customerArrayIndex = 0;
     

        public Form1()
        {
            InitializeComponent();
        }

        private void CreateCustomer_Click(object sender, EventArgs e)
        {
            


            DateTime birthday = new DateTime();
            birthday = DateTime.Parse(CusNewBirthday.Text);
            customerArray[customerArrayIndex] = new Customer(CusNewFirstName.Text,CusNewLastName.Text, birthday);
            customerArray[customerArrayIndex].Address = CusAddress.Text;
            customerArray[customerArrayIndex].Description = CustDesc.Text;

            CustomerList.Items.Add(customerArray[customerArrayIndex].FirstName);

            customerArrayIndex++;

            

            //DateTime dateTime = new DateTime(2020,09,23);
            //DateTime current = DateTime.Now;
        }
        public void ShowDetail(Customer customer)
        {
            CusFullName.Text = customer.FullName;
            CusIsQualified.Text = customer.IsQualified.ToString();

            CusAge.Text = customer.Age.ToString();
            CusAddress.Text = customer.Address;
            CustDesc.Text = customer.Description;
           
        }

        private void CustomerList_Click(object sender, EventArgs e)
        {
            string firstName = CustomerList.SelectedItem.ToString();
            for (int i = 0; i < customerArrayIndex; i++)
            {
                if (customerArray[i].FirstName==firstName)
                {
                    ShowDetail(customerArray[i]);
                    break;
                }
                
            }
        }
    }
}

반응형