20220302 º¹½À³ëÆ®


< (»ç¹°) °´Ã¼ÁöÇâ¾ð¾î >

°´Ã¼ÁöÇâÀÌ·ÐÀÇ ±âº» °³³ä :
½ÇÁ¦ ¼¼°è´Â °´Ã¼·Î ÀÌ·ç¾îÁ® ÀÖÀ¸¸ç, ¹ß»ýÇÏ´Â ¸ðµç »ç°ÇµéÀº »ç¹°°£ÀÇ »óÈ£ÀÛ¿ëÀÌ´Ù.
= ½ÇÁ¦ °´Ã¼ÀÇ ¼Ó¼º°ú ±â´ÉÀ¸ ºÐ¼®ÇÑ µÚ, ÄÄÇ»ÅÍ ¼Ó °¡»ó ¼¼°è¿¡¼­ ±¸Çö.

< class ¶õ? >
°´Ã¼¿¡ ´ëÇÑ ³»¿ëÀ» Á¤ÀÇÇØ ³õÀº °Í.
±× ³»¿ë¸¸ ºÁµµ ¹«½¼ classÀÎÁö ÆľÇÇÒ ¼ö ÀÖ¾î¾ß ÇÑ´Ù.

Ŭ·¡½º·ÎºÎÅÍ ¸¸µç °´Ã¼ ( ÀνºÅϽº ) ÀÌÇØÇϱâ

°´Ã¼ Ŭ·¡½º À̸§°ú µ¿ÀÏÇÑ ÇÔ¼ö = »ý¼ºÀÚ
(»ý¼ºÀÚ´Â ¾Õ¿¡ ¹«Á¶°Ç public ºÙÀ̱â)

< Á¢±ÙÁ¦¾îÀÚ >
private = °°Àº Ŭ·¡½º ³»¿¡¼­¸¸ Á¢±Ù °¡´É ( getter, setter·Î °æÀ¯ )
default = °°Àº ÆÐÅ°Áö
protected = °°Àº ÆÐÅ°Áö + »ó¼Ó¹ÞÀº ÀÚ¼ÕŬ·¡½º
public = Á¢±Ù Á¦ÇÑ ¾øÀ½


Addr Ŭ·¡½º·ÎºÎÅÍ ad °´Ã¼¸¦ ¸¸µç ÈÄ, getter&setter¸¦ ÅëÇØ °æÀ¯ÇÏ¿© private ¸â¹öº¯¼öµé¿¡ Á¢±ÙÇϱâ

package addr.view;

import java.util.Scanner;
import addr.controller.Addr;

public class AddMain {

     public static void main(String[] args) { // mainÇÔ¼ö´Â ¹Ýµå½Ã ÇÑ °÷¿¡¸¸ »ý¼º!

          System.out.println("---AddMain---");
          Scanner sc = new Scanner(System.in); //Scanner Ŭ·¡½º ºÒ·¯¿À±â

          Addr ad = new Addr("Á¤±Õö","010-1234-5678","jgc@naver.com","cheoan");// Addr Ŭ·¡½º ºÒ·¯¿Í¼­ ad¶ó´Â ÀνºÅϽº(°´Ã¼)»ý¼º
          System.out.println(ad.getName());
     }

}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------
package addr.controller;

public class Addr {
     //Ŭ·¡½º = º¯¼ö + ÇÔ¼ö => ¸â¹öº¯¼ö + ¸Þ¼­µå

     private String name; // public + ¸â¹öº¯¼ö = ´Ù¸¥ Ŭ·¡½º¿¡¼­µµ Á¢±Ù °¡´É
     private String phone; // private + ¸â¹öº¯¼ö = °°Àº Ŭ·¡½º ¾È¿¡¼­¸¸ Á¢±Ù °¡´É
     private String email;
     private String address;

     public Addr(String name, String phone, String email, String address){ // »ý¼ºÀÚ ¾Õ¿¡ ¹«Á¶°Ç public ºÙÀ̱â
          System.out.println(name);
          System.out.println(phone);
          System.out.println(email);
          System.out.println(address);
          this.name=name; // this.¸â¹öº¯¼ö¿¡ »ý¼ºÀÚ·Î ¹Þ¾Æ¿Â ¸Å°³º¯¼öµéÀ» ´ëÀÔ½ÃÅ´
          this.phone=phone;
          this.email=email;
          this.address=address;
     }

     public String getName() { // set&getÀ» °æÀ¯ÇÏ¿© private º¯¼ö¿¡ Á¢±ÙÇÏ´Â ¸Þ¼­µå (¸Þ´º - Source - Generate getters and setters)
          return name;
     }
     public void setName(String name) {
          this.name = name;
     }
     public String getPhone() {
          return phone;
     }
     public void setPhone(String phone) {
          this.phone = phone;
     }
     public String getEmail() {
          return email;
     }
     public void setEmail(String email) {
          this.email = email;
     }
     public String getAddress() {
          return address;
     }
     public void setAddress(String address) {
          this.address = address;
     }

}


add.view ÆÐÅ°Áö (mainÇÔ¼ö) & add.controller ÆÐÅ°Áö (RCUD ¸Þ¼­µå) ·Î ºÐ¸®ÇÏ¿© ÁÖ¼Ò·Ï ÇÁ·Î±×·¥ ½ÇÇà
package add.view;

import java.util.Scanner;
import add.controller.Addr; //´Ù¸¥ ÆÐÅ°Áö(controller)¾È¿¡ ÀÖ´Â Addr Ŭ·¡½º¸¦ ºÒ·¯¿È.

public class AddMain {

     public static void main(String[] args) {
          Addr ad = new Addr("address.txt");
          Scanner sc = new Scanner(System.in);
          String select="";

          while (select!="6") {

               ad.Menu();
               System.out.println("-¹øÈ£¸¦ ÀÔ·ÂÇØÁÖ¼¼¿ä.-");
               select=sc.nextLine();

               switch (select) {
               case "1":
                    ad.Read();
                    break;
               case "2":
                    ad.Create();
                    break;
               case "3":
                    ad.Update();
                    break;
               case "4":
                    ad.Delete();
                    break;
               case "5":
                    ad.Search();
                    break;
               case "6":
                    ad.Backup();
                    return;

               default:
                    System.out.println("À߸øµÈ ¹øÈ£. ´Ù½Ã ÀÔ·ÂÇϽÿÀ.");
               }

          }

     }

}


package add.controller;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Addr {

     public String fname;

     public Addr(String fname) { //»ý¼ºÀÚ

          this.fname=fname;
     }

     public void Read() {

          String line="";

          File f = new File(fname);
          if (!f.exists()) {

               try {
                    FileWriter fw = new FileWriter(fname);
                    BufferedWriter bw = new BufferedWriter(fw);
                    bw.close();

               } catch (IOException e) {

               }
          }
          //-------------------------------------------
          try {
               FileReader fr=new FileReader(fname);
               BufferedReader br=new BufferedReader(fr);
               int i;
               for (i = 0; ( line= br.readLine() ) != null; i++) {
                    System.out.println(line);
               }
               if(i==0) {
                    System.out.println("ÆÄÀÏÀÌ ºñ¾î ÀÖ½À´Ï´Ù.");
               }
               br.close(); // ¿­·ÁÀÖ´Â brÀ» ²À ´Ù½Ã ´Ý¾ÆÁÖ±â
          }catch(IOException e){
               System.out.println("¿¡·¯¹ß»ý");
          }
     }

     public void Create() {
          Scanner sc = new Scanner(System.in);
          String name;
          String phone;
          String email;
          String address;
          String list = "";

          try{
               FileWriter fw = new FileWriter(fname, true);
               BufferedWriter bw = new BufferedWriter(fw);
               System.out.println("Ãß°¡ÇÒ À̸§À» ÀÔ·ÂÇϽÿÀ.");
               name = sc.nextLine();
               System.out.println("Ãß°¡ÇÒ ÀüÈ­¹øÈ£¸¦ ÀÔ·ÂÇϽÿÀ.");
               phone = sc.nextLine();
               System.out.println("Ãß°¡ÇÒ À̸ÞÀÏÀ» ÀÔ·ÂÇϽÿÀ.");
               email = sc.nextLine();
               System.out.println("Ãß°¡ÇÒ ÁÖ¼Ò¸¦ ÀÔ·ÂÇϽÿÀ.");
               address = sc.nextLine();
               list = name+","+phone+","+email+","+address;
               System.out.println("Ãß°¡ÇÑ ³»¿ë: "+list);
               bw.write(list);
               bw.newLine();
               bw.close();

          } catch (IOException e) {
               System.out.println("¿¡·¯¹ß»ý(FileWriting-FAIL)");
          }

     }

     public void Update() {
          System.out.println("¾÷µ¥ÀÌÆ®!!!");
     }

     public void Delete() {
          System.out.println("»èÁ¦!!!");
     }

     public void Search() {
          System.out.println("¼­Ä¡!!!");
     }

     public void Backup() {
          System.out.println("¹é¾÷!!!");
     }

     public void Menu() {
          System.out.println("1. ÁÖ¼ÒÃâ·Â");
          System.out.println("2. ÁÖ¼ÒÃß°¡");
          System.out.println("3. ÁÖ¼Ò¼öÁ¤");
          System.out.println("4. ÁÖ¼Ò»èÁ¦");
          System.out.println("5. ÁÖ¼Ò°Ë»ö");
          System.out.println("6. Á¾·á");
     }
}