Member.java
package kosta.oop;
public class Member {
//상태(필드, 멤버변수)
String name;
int age;
String address;
//기능(메소드) 회원등록
public void addMember(String name, int age, String address) {//지역변수(매개변수)
this.name = name;
this.age = age;
this.address = address;
}
//기능(메소드) 회원출력
public void printMember() {
System.out.println("회원 이름 :"+name);
System.out.println("회원 나이 :"+age);
System.out.println("회원 주소 :"+address);
System.out.println("===================");
}
}
MemberMain.java
package kosta.oop;
import java.util.Scanner;
public class MemberMain {
public static void main(String[] args) {
//2명의 회원을 등록하고 전체목록을 출력
Member arr[] = new Member[2];// Member객체를 담기 위한 배열을 생성
Scanner sc = new Scanner(System.in);
for(int i = 0; i <arr.length; i++) {
System.out.print("이름을 입력하세요 :");
String name = sc.nextLine();
System.out.print("나이를 입력하세요 :");
int age = Integer.parseInt(sc.nextLine());
System.out.print("주소를 입력하세요 :");
String address = sc.nextLine();
// Member m = new Member();
// arr[i] = m;
arr[i] = new Member();
arr[i].addMember(name, age, address);
}
System.out.println("-----------회원목록 출력------------");
for(Member m : arr) {
m.printMember();
}
}
}
Board.java
package kosta.oop;
public class Board {
String sub;
String writer;
String contents;
public void addBoard(String sub, String writer, String contents) {
this.sub = sub;
this.writer = writer;
this.contents = contents;
}
public void print() {
System.out.println("글제목 : "+sub);
System.out.println("작성자 : "+writer);
System.out.println("글내용 : "+contents);
System.out.println("==================");
}
}
BoardMain.java
package kosta.oop;
import java.util.Scanner;
public class BoardMain {
public static void main(String[] args) {
Board arr[] = new Board[3];
Scanner sc = new Scanner(System.in);
for(int i = 0; i < arr.length; i++) {
System.out.print("글제목을 입력하세요 : ");
String sub = sc.nextLine();
System.out.print("작성자를 입력하세요 : ");
String writer = sc.nextLine();
System.out.print("글내용을 입력하세요 : ");
String contents = sc.nextLine();
arr[i] = new Board();
arr[i].addBoard(sub, writer, contents);
}
System.out.println("---------글 목록----------");
for(Board b : arr) {
b.print();
}
}
}