Model
Getter Setter 만 선언해준다
import java.util.Scanner;
public class Member {
private int id;
private String name;
private int age;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Controller
데이터를 조작하는 메소드및 Member[] 배열에 정보를 담음
public class MemberDao {
Scanner scanner = new Scanner(System.in);
Member[] member;
FileClass fileClass;
int id;
String str = "";
public MemberDao() {
// TODO Auto-generated constructor stub
member = new Member[10];
for (int i = 0; i < member.length; i++) {
member[i] = new Member();
member[i].setId(-1);
}
str = "";
id = 0;
}
public void insert() {
member[this.id].setId(this.id);
System.out.println("이름을 입력하세요");
String name = scanner.next();
member[this.id].setName(name);
System.out.println("나이를 입력하세요");
int age = scanner.nextInt();
member[this.id].setAge(age);
System.out.println("주소를 입력하세요");
String address = scanner.next();
member[this.id].setAddress(address);
this.id++;
}
public void delete() {
System.out.println("삭제할 대상의 이름을 입력하세요");
String name = scanner.next();
int findIndex = -1;
for (int i = 0; i < member.length; i++) {
if (name.equals(member[i].getName())) {
findIndex = i;
break;
}
}
member[findIndex].setName("");
member[findIndex].setAddress("");
member[findIndex].setAge(0);
member[findIndex].setId(-1);
}
public void select() {
System.out.println("검색할 대상의 이름을 입력하세요");
String name = scanner.next();
int findIndex = -1;
for (int i = 0; i < member.length; i++) {
if (name.equals(member[i].getName())) {
findIndex = i;
break;
}
}
if (findIndex == -1) {
System.out.println("검색할 대상을 찾을 수 없습니다");
} else {
System.out.println("id = " + member[findIndex].getId());
System.out.println("name = " + member[findIndex].getName());
System.out.println("age = " + member[findIndex].getAge());
System.out.println("address = " + member[findIndex].getAddress());
System.out.println("검색할 대상을 찾을 수 없습니다.");
}
}
public void update() {
System.out.println("업데이트 할 대상의 이름을 입력하세요");
String name = scanner.next();
int findIndex = -1;
for (int i = 0; i < member.length; i++) {
if (name.equals(member[i].getName())) {
findIndex = i;
break;
}
}
if (findIndex == -1) {
System.out.println("검색할 대상을 찾을 수 없습니다");
} else {
System.out.println("변경할 주소를 입력하세요");
member[findIndex].setAddress(scanner.next());
System.out.println("변경 완료!");
}
}
public void allPrint() {
for (int i = 0; i < member.length; i++) {
if(member[i].getId() != -1) {
System.out.println("==========================");
System.out.println("id = " + member[i].getId());
System.out.println("name = " + member[i].getName());
System.out.println("age = " + member[i].getAge());
System.out.println("address = " + member[i].getAddress());
}
}
}
}
View
콘솔환경에서 Scanner scanner로 값을 받아서 MemberDao에 있는 메소드를 호출한다.
import java.util.Scanner;
public class mainClass {
public static void main(String[] args) {
MemberDao memberDao = new MemberDao();
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("1. 데이터 추가하기 ");
System.out.println("2. 데이터 삭제하기");
System.out.println("3. 데이터 업데이트 ");
System.out.println("4. 데이터 전부 출력하기");
int choice = scanner.nextInt();
if(choice == 1) {
memberDao.insert();
} else if (choice == 2) {
memberDao.delete();
} else if (choice == 3) {
memberDao.update();
} else if (choice == 4) {
memberDao.allPrint();
}
}
}
}
'Java > java 기초' 카테고리의 다른 글
Generic(제네릭) (0) | 2019.11.29 |
---|---|
학생 성적 관리 프로그램 MVC 패턴으로 나누기 (0) | 2019.11.29 |
final (0) | 2019.11.28 |
객체 오버라이딩 및 상속을 통한 구현 (0) | 2019.11.28 |
객체지향 프로그래밍 기본 개념 (0) | 2019.11.27 |