수업시간 JAVA/문제
책 등록/조회/수정/삭제
by SEOKIHOUSE
2023. 4. 8.
package practice30day;
public class MyBook2 {
private int num;
private String name;
private String title;
private String writer;
private String publish;
private int price;
public void bookTake(int num, String name) {
this.num = num;
this.name = name;
}
public int getNum() {
return num;
}
public String getName() {
return name;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getWriter() {
return writer;
}
public void setPublish(String publish) {
this.publish = publish;
}
public String getPublish() {
return publish;
}
public void setPrice(int price) {
this.price = price;
}
public int getPrice() {
return price;
}
public void printLittle() {
System.out.print(num + " ");
System.out.print(name + " ");
}
public void printAll() {
System.out.print("등록번호는:" +num + " ");
System.out.print("이름은:" +name + " ");
System.out.print("책 제목은:" + title + " ");
System.out.print("글쓴이는:" +writer + " ");
System.out.print("출판사는:"+ publish + " ");
System.out.println("가격은:"+price);
}
}
package practice30day;
import java.util.Scanner;
public class MyBookManager2 {
MyBook2[] bookStore;
int index = 0;
Scanner sc = new Scanner(System.in);
MyBookManager2() {
bookStore = new MyBook2[3];
}
public void getInformation(MyBook2 b) {
b.printAll();
storeInfo(b);
}
// 저장소
public void storeInfo(MyBook2 b) {
bookStore[index] = b;
index++;
}
// 수정
public MyBook2 repairInfo(int num) { // num에 수정할 숫자 들어가고
for (int i = 0; i < bookStore.length; i++) {
// if(bookStore[i] == bookStore[num-1]) { // 이거는 배열번호 골라서 가져오기
if (bookStore[i].getNum() == num) {
return bookStore[i];
}
}
return null;
}
// 삭제
public void delete(int num) {
MyBook2 d = new MyBook2();
index--;
if (bookStore.length == 1) { // 만약에 배열크기가 1일때
bookStore[num - 1] = d;
} else {
for (int i = 0; i < bookStore.length - 1; i++) {
bookStore[i] = bookStore[i + 1];
}
bookStore[bookStore.length - 1] = d;
}
}
public MyBook2 check(int num) {
if (num == 1) {
System.out.println("등록하신 번호가 어케되십니깡");
int yourNum = sc.nextInt();
System.out.println("등록하신 이름이 어케되십니깡");
sc.nextLine();
String yourName = sc.nextLine();
for (int i = 0; i < index; i++) {
if (bookStore[i].getNum() == yourNum && bookStore[i].getName().equals(yourName)) {
bookStore[i].printAll();
}
}
} else if (num == 2) {
for (int i = 0; i < index; i++) {
bookStore[i].printAll();
}
}
return null; // 기본형 int 같은경우는 값이 없으면 0으로 쓰는데 참조형은 null을 쓴다
}
}
package practice30day;
import java.util.Scanner;
public class MyBookMain2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
MyBookManager2 bm = new MyBookManager2();
while (true) {
System.out.println("1. 등록하기 2. 정보보기 3. 수정하기 4. 삭제하기 0.종료");
int select = sc.nextInt();
if (select == 1) {
System.out.println("책정보 관리시스템입니다");
System.out.print("마음에 드는 번호로 등록하세요>> ");
int number = sc.nextInt();
System.out.print("이름을 입력하세요>> ");
sc.nextLine();
String name = sc.nextLine();
System.out.print("책 제목입력하쇼>> ");
String title = sc.nextLine();
System.out.print("저자 입력하쇼>> ");
String writer = sc.nextLine();
System.out.print("출판사 입력하쇼>> ");
String publish = sc.nextLine();
System.out.print("가격 입력하쇼>> ");
int price = sc.nextInt();
MyBook2 b = new MyBook2();
b.bookTake(number, name);
b.setTitle(title);
b.setWriter(writer);
b.setPublish(publish);
b.setPrice(price);
bm.getInformation(b);
}
// check
else if (select == 2) {
System.out.println("1. 개인정보를 가져올지 2. 전체 정보를 가져올지");
int takeNum = sc.nextInt();
MyBook2 a = bm.check(takeNum); // ==bm.check(takeNum); 타입은 MyBook2
}
//수정
else if(select ==3) {
while(true) {
//ㅁㄴㅇㄹ\n next는 띄워쓰기 안대고
//int실행되면 \n앞에 값만 가져가서 nextline 쓰려면
//nextline으로 한번 쓰레기 처리하고 다음에 또 써야지 값이 가져간다 (이건 띄워쓰기 가능)
System.out.print("등록 번호를 고르세요");
int renum = sc.nextInt();
MyBook2 rpn= bm.repairInfo(renum);
System.out.println("이름이?");
String rename = sc.next();
System.out.println("어떤 부분을 수정하시겠습니까?");
System.out.print("1.번호,이름 2.제목 3.작가 4.출판 5.가격");
int choose = sc.nextInt();
if(choose ==1) {
System.out.print("수정해서 넣고싶은 번호를 입력하세용>> ");
renum = sc.nextInt();
System.out.print("수정할 이름을 적으세요>> ");
sc.nextLine();
rename = sc.nextLine();
rpn.bookTake(renum, rename);
}else if (choose==2) {
System.out.print("수정할 제목을 적으쇼>> ");
sc.nextLine();
rename = sc.nextLine();
rpn.setTitle(rename);
}else if (choose==3) {
System.out.print("수정할 작가이름 적으쇼>> ");
sc.nextLine();
rename = sc.nextLine();
rpn.setWriter(rename);
}else if (choose==4) {
System.out.print("수정할 출판사 명 적으쇼>> ");
sc.nextLine();
rename = sc.nextLine();
rpn.setPublish(rename);
}else if (choose==5) {
System.out.print("수정할 가격 적으쇼>> ");
renum = sc.nextInt();
rpn.setPrice(renum);
}
System.out.println("1. 계속수정하기 0. 메뉴로 가기");
choose = sc.nextInt();
if(choose ==0) {
break;
}
}
}
//삭제 //뒤에꺼 가져오고 맨 뒷배열 삭제
else if(select ==4) {
System.out.print("삭제하고 싶은 배열번호(1~) 고르세요>> ");
int del = sc.nextInt();
bm.delete(del);
}
else if (select == 0) {
break;
}
}
}
}