본문 바로가기

   
Programming/C#

주소록

반응형

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.Collections;

 

namespace ConsoleEx

{

       class MiniProject

       {

             //address.dat의 복사본(읽기용)

             private static ArrayList list;

 

             static void Main(string[] args)

             {

                    //1. 주소 입력하기

                    //2. 주소록 보기

                    //3. 검색하기

                    //4. 종료

 

                    //address.dat의 내용을 ArrayList(list)에 전체 복사

                    list = new ArrayList();

                    LoadData();

 

 

                    //메뉴

                    bool loop = true;

 

                    while (loop)

                    {

                           //메뉴 선택

                           Console.Clear();

                           Console.WriteLine("==================");

                           Console.WriteLine(" 1. 주소 입력하기");

                           Console.WriteLine(" 2. 주소록 보기");

                           Console.WriteLine(" 3. 검색하기");

                           Console.WriteLine(" 4. 수정하기");

                           Console.WriteLine(" 5. 삭제하기");

                           Console.WriteLine(" 6. 종료");

                           Console.WriteLine("==================");

 

                           Console.Write(" 메뉴 선택 : ");

                           string sel = Console.ReadLine();

 

                           switch (sel)

                           {

                                 case "1": Input(); break;

                                 case "2": Output(); break;

                                 case "3": Search(); break;

                                 default:

                                        loop = false;

                                        Console.WriteLine("\r\n사용해주셔서 감사합니다.");

                                        break;

                           }

                    }

 

             }

 

             private static void LoadData()

             {

                    list.Clear();

 

 

                    //address.dat의 한줄 -> Member객체 -> list에 추가

                    FileInfo dat = new FileInfo("address.dat");

                    StreamReader reader = dat.OpenText();

 

                    string item = "";

 

                    while ((item = reader.ReadLine()) != null)

                    {

                           //**이곳의 item -> Member의 객체로 생성됨

                           string[] result = item.Split('|');

 

                           //텍스트파일의 정보 -> 메모리상의 인스턴스

                           Member m = new Member(result[0], result[1], result[2], result[3]);

 

                           //Member 관리하는 list에 추가

                           list.Add(m);

                    }

 

                    reader.Close();

             }

 

 

 

 

 

 

             private static void Search()

             {

                    //검색하기

                    // 이름 검색

                    Console.Clear();

 

                    Console.WriteLine("==================");

                    Console.WriteLine(" 주소록 검색");

                    Console.WriteLine("==================");

 

                    Console.Write(" 이름 입력 : ");

                    string name = Console.ReadLine();

 

                    //홍길동 -> 길동, -> 부분 검색

                    // m.Name == "홍길동" // X

                    // m.Name.Contains("길동") == true

                    // m.Name.IndexOf("길동") > -1

 

                    Console.WriteLine("====================================");

                    Console.WriteLine("[이름]\t[주소]\t[나이]\t[전화]");

                    Console.WriteLine("====================================");

 

 

                    for (int i = 0; i < list.Count; i++)

                    {

                           Member m = (Member)list[i];

 

                           //검색 : name(검색어)

                           if (m.Name.Contains(name))

                           {

                                 Console.WriteLine("{0}\t{1}\t{2}\t{3}", m.Name, m.Address, m.Age, m.Tel);

                           }

                    }

 

 

                    //안내 멘트

                    Console.WriteLine("\r\n엔터를 치시면 계속 진행됩니다.");

                    Console.ReadLine();

             }

 

 

 

 

 

 

 

 

 

 

             private static void Output()

             {

                    //주소록 보기(리스트 출력)

                    Console.Clear();//화면전환

 

                    Console.WriteLine("====================================");

                    Console.WriteLine("[이름]\t[주소]\t[나이]\t[전화]");

                    Console.WriteLine("====================================");

 

                    //address.dat -> 읽기(라인) -> 출력

                    //1. FileInfo -> 2. OpenText() -> 3. StreamReader -> 4. reader.ReadLine()

                    //FileInfo dat = new FileInfo("address.dat");

                    //StreamReader reader = dat.OpenText();

 

                    ////무한 반복 -> 모든 라인 읽기

                    //string item = "";

 

                    //while ((item = reader.ReadLine()) != null)

                    //{

                    //    //item -> "홍길동|서울시|20|010-222-5555"

                    //    string[] result = item.Split('|');

 

                    //    //화면 출력

                    //    Console.WriteLine("{0}\t{1}\t{2}\t{3}", result[0], result[1], result[2], result[3]);

                    //}

 

                    ////스트림 닫기(파일 닫기)

                    //reader.Close();

 

 

                    //ArrayList 사용해서 출력

                    //Console.WriteLine(list.Count);

                    for (int i = 0; i < list.Count; i++)

                    {

                           Member m = (Member)list[i];

                           Console.WriteLine("{0}\t{1}\t{2}\t{3}", m.Name, m.Address, m.Age, m.Tel);

                    }

 

 

                    //안내 멘트

                    Console.WriteLine("\r\n엔터를 치시면 계속 진행됩니다.");

                    Console.ReadLine();//잠시 멈춤

             }

 

             private static void Input()

             {

                    //주소 입력하기

                    Console.Clear();//화면전환

 

                    Console.WriteLine("==================");

                    Console.WriteLine(" 새 주소 입력");

                    Console.WriteLine("==================");

 

                    //사용자로부터 입력 데이터를 받기

                    Console.Write(" 이름 : ");

                    string name = Console.ReadLine();

                    Console.Write(" 주소 : ");

                    string address = Console.ReadLine();

                    Console.Write(" 나이 : ");

                    string age = Console.ReadLine();

                    Console.Write(" 전화 : ");

                    string tel = Console.ReadLine();

 

 

                    //입력받은 데이터를 address.dat에 추가하기

                    //1. FileInfo -> 2. AppendText() -> 3. StreamWriter -> 4. writer.WriteLine(새주소)

                    FileInfo dat = new FileInfo("address.dat");

                    StreamWriter writer = dat.AppendText();

 

                    //이름|주소|나이|전화

                    writer.WriteLine("{0}|{1}|{2}|{3}", name, address, age, tel);

 

                    //스트림 닫기(파일 닫기)

                    writer.Close();

 

 

                    //list에도 동기화

                    //1. Member 객체 생성-> Add()

                    //Member m = new Member(name, address, age, tel);

                    //list.Add(m);

 

 

                    //2. 메서드 재활용

                    LoadData();

 

 

 

 

                    //안내 멘트

                    Console.WriteLine("\r\n입력이 완료되었습니다.\r\n엔터를 치시면 계속 진행됩니다.");

                    Console.ReadLine();//잠시 멈춤

             }

       }

}

 

 

 

 

Member

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleEx

{

       class Member

       {

             private string name;

             private string address;

             private string age;

             private string tel;

 

             public string Name

             {

                    get { return name; }

             }

 

             public string Address

             {

                    get { return address; }

             }

 

             public string Age

             {

                    get { return age; }

             }

 

             public string Tel

             {

                    get { return tel; }

             }

 

             public Member(string name, string address, string age, string tel)

             {

                    this.name = name;

                    this.address = address;

                    this.age = age;

                    this.tel = tel;

             }

       }

}

 

 
반응형