C#/C# 연습

Class : Car Manager

HicKee 2022. 12. 18. 21:28

고객 차량 관리 클래스
4개의 클래스를 사용

Car 클래스 모델명 색상 연식 제조사
매개변수가 있는 생성자 추가
getter setter 추가
모든 속성을 출력하는 메서드

고객(customer) 클래스 고객명 전화번호 주소 Car 클래스 속성 생성하고 접근 제한
매개변수가 있는 생성자 추가
getter setter 추가
모든 속성을 출력하는 메서드 
고객 정보와 차량정보를 출력하는 메서드 추가

RandomData 클래스를 추가

10명의 고객 정보를 위한 객체생성
모든 고객 정보와 차량정보를 랜덤으로 설정하고 출력

 

Main

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Class_CarMG {
    internal class Program {
        static void Main(string[] args) {

            RandData rd = new RandData();
            List<Customer> customer = new List<Customer>();

            for (int i = 0; i < 10; i++) {

                customer.Add(new Customer(rd.getName(), rd.getTel(), rd.getAddr(), 
                    new Car(rd.getModel(), rd.getColor(), rd.getYear(), rd.getFac())));
            }

            for (int i = 0; i < 10; i++) {

                customer[i].CustomerInfo();
            }
        }
    }
}

Car

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Class_CarMG {
    internal class Car {

        private string model;
        private string color;
        private int year;
        private string mFac;

        public Car(string model,string color, int year,string mFac) { 
        
            this.model = model;
            this.color = color;
            this.year = year;
            this.mFac = mFac;
        }

        public void carInfo() {
            Console.WriteLine($"모델명 : {model}");
            Console.WriteLine($"색상 : {color}");
            Console.WriteLine($"연도 : {year}");
            Console.WriteLine($"제조사 : {mFac}");
        }

        #region Getter
        public string getModel() {
            return model;
        }
        public string getColor() {
            return color;
        }
        public int getYear() {
            return year;
        }
        public string getFac() {
            return mFac;
        }
        #endregion

        #region Setter
        public void modelSet(string model) {
            this.model = model;
        }
        public void colorSet(string color) {
            this.color = color;
        }
        public void yearSet(int year) {
            this.year = year;
        }
        public void mFacSet(string mFac) {
            this.mFac = mFac;
        }
        #endregion

    }
}

Customer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Class_CarMG {
    internal class Customer {

        private string cName;
        private string cTel;
        private string cAddr;
        private Car car; 

        public Customer(string name, string tel, string addr, Car car) {

            this.cName = name;
            this.cTel = tel;    
            this.cAddr = addr;
            this.car = car;
        }

        public void CustomerInfo() {

            Console.WriteLine($"고객명 : {cName}");
            Console.WriteLine($"  전화 : {cTel}");
            Console.WriteLine($"  주소 : {cAddr}");
            Console.WriteLine("-------------------------------");
            Console.WriteLine($"{cName} 고객의 차량 정보");
            Console.WriteLine($"모델명 : {car.getModel()}");
            Console.WriteLine($"  색상 : {car.getColor()}");
            Console.WriteLine($"  연식 : {car.getYear()}");
            Console.WriteLine($"제조사 : {car.getFac()}");
        }

        #region Getter
        public string getName() {
            return cName;
        }
        public string getTel() {
            return cTel;
        }
        public string getAddr() {
            return cAddr;
        }
        public Car getFac() {
            return car;
        }
        #endregion

        #region Setter
        public void cNameSet(string cName) {
            this.cName = cName;
        }
        public void cTelSet(string cTel) {
            this.cTel = cTel;
        }
        public void cAddrSet(string cAddr) {
            this.cAddr = cAddr;
        }
        public void carSet(Car car) {
            this.car = car;
        }
        #endregion



    }
}

Random Data

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Class_CarMG {
    internal class RandData {

        Random rd= new Random();

        
        private string[] name = { "Hong", "Kim", "Pack", "Lee", "Choi" };
        private string[] tel = { "010-1111-2222", "010-3333-4444", "010-5555-6666", "010-7777-8888", "010-9999-0000" };
        private string[] addr = { "서울시", "대전시", "대구시", "부산시", "인천시" };
                
        private string[] model = { "SM6", "그랜져", "K7", "소나타", "산타페" };
        private string[] color = { "Black", "Red", "Blue", "White", "Pink" };
        private int[] year = { 2016, 2017, 2018, 2019 };
        private string[] fac = { "르노", "현대", "기아", "포드" };


        


        //고객 랜덤
        public string getName() {
            return name[rd.Next(name.Length)];
        }
        public string getTel() {
            return tel[rd.Next(tel.Length)];
        }
        public string getAddr() {
            return addr[rd.Next(addr.Length)];
        }

        //자동차 랜덤
        public string getModel() {
            return model[rd.Next(model.Length)];
        }
        public string getColor() {
            return color[rd.Next(color.Length)];
        }
        public int getYear() {
            return year[rd.Next(year.Length)];
        }
        public string getFac() {
            return fac[rd.Next(fac.Length)];
        }


    }
}

 

'C# > C# 연습' 카테고리의 다른 글

Class : Student manage  (0) 2022.12.20
Class : Student Score  (0) 2022.12.18
Class : Car  (0) 2022.12.17
IF : Leap year (윤년)  (0) 2022.12.12
Array : reverse order (역순)  (0) 2022.11.30