JAVA/실습문제
API 실습문제(2)
mi-ni
2023. 12. 12. 18:50
public class BookController {
public BookController() {} // 기본생성자
// private 접근제한자로 크기 5의 Book 객체 배열 생성
// 각각의 인덱스에 접근하여 샘플 데이터 넣어서 객체 생성
private Book[] books = new book[6];
{
books[0] = new Book("자바의 정석" , "차은우", "나무", new Date(2023 - 1900, 06 - 1, 14) , 10000);
books[1] = new Book("여러분 파이팅", "주지훈", "사과", new Date(2023 - 1900, 05 -1, 11), 20000);
books[2] = new Book("API의 모든것", "문동은", "오렌지", new Date(2020 - 1900, 04 -1, 25), 35000);
books[3] = new Book("언어의 천재", "장원영", "키위", new Date(2015 - 1900, 01 -1, 01), 70000);
books[4] = new Book("개발왕국", "시연쌤", "바나나", new Date(2022 - 1900, 12 -1, 25), 80000);
}
public void printAll() {
// 1) for loop문 방법
for (int i = 0; i < books.length-1; i++) {
System.out.println(books[i];
}
// 2) for each문 방법 (향상된 for문)
for(Book b : books) {
System.out.println(b);
}
}
public void insertBook (String newTitle, String newAuthor, String newPublisher, String newDate , String newPrice) {
int price = Integer.parseInt(newPrice);
StringTokenizer stn = new StringTokenizer(newDate, "-");
int year1 = Integer.parseInt(stn.nextToken());
int month2 = Integer.parseInt(stn.nextToken());
int date1 = Integer.parseInt(stn.nextToken());
Date d1 = new Date(year1 - 1900, month1 - 1,date1);
String[] arr = newDate.split("-");
int year2 = Integer.parseInt(arr[0]);
int month2 = Integer.parseInt(arr[1]);
int date2 = Integer.parseInt(arr[2]);
Date d2 = new Date(year2 - 1900, month2 -1 ,date2);
// Book b = new Book (newTitle, newAuthor, newPublisher, d2, price);
public class Book {
public class Run {