728x90
반응형
1. BookInfo 클래스를 정의하시오. 클래스의 속성은 다음과 같습니다. 적당한 영어를 사용하고 속성 type은 일단 String으로 하시오.
- 순번, 브랜드, 분야, 도서형태, 도서명, 저자, 역자, 발행일, 페이지, 가격, ISBN, 도서상태
2. 책의 정보를 한 줄 읽을 때마다 BookInfo 객체를 하나 생성한 후, 읽어온 정보를 조기화하시오.
3. 책의 정보를 저장할 수 있는 ArrayList<BookInfo> book_infos 리스트를 생성하시오.
4. book_infos 리스트에 BookInfo 객체를 추가하시오.
import java.io.*;
import java.util.ArrayList;
public class Book {
public static void main(String[] args) throws IOException {
// 1. 파일 읽어 오기
File file=new File("C:\\Users\\5호실\\Desktop\\BookList_tab.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis,"euc-kr");
BufferedReader br = new BufferedReader(isr);
// 2. 파일 출력하기
PrintStream ps = new PrintStream("BookList_out.txt");
br.readLine();
br.readLine();
ArrayList<BookInfo> book_infos = new ArrayList<>();
while(true) {
String line = br.readLine();
if(line==null) break;
// 3. 문자열 처리
String[] tokens=line.split("\t");
BookInfo bi=new BookInfo(
tokens[0],
tokens[1],
tokens[2],
tokens[3],
tokens[4],
tokens[5],
tokens[6],
tokens[7],
tokens[8],
tokens[9],
tokens[10],
tokens[11]
);
book_infos.add(bi);
}
br.close();
ps.close();
}
}
BookInfo 클래스
class BookInfo {
String num;
String brand;
String field;
String type;
String name;
String author;
String interpreter;
String publish_day;
String num_pages;
String price;
String ISBN;
String state;
BookInfo(
String num,
String brand,
String field,
String type,
String name,
String author,
String interpreter,
String publish_day,
String num_pages,
String price,
String ISBN,
String state) {
this.num=num;
this.brand=brand;
this.field=field;
this.type=type;
this.name=name;
this.author=author;
this.interpreter=interpreter;
this.publish_day=publish_day;
this.num_pages=num_pages;
this.price=price;
this.ISBN=ISBN;
this.state=state;
}
}
바빠를 포함한 제목의 책을 검색하여 출력해보시오.
for(BookInfo b : book_infos) {
if(b.name.contains("바빠")) {
System.out.println(b.name);
}
}
Do it 시리즈를 찾아 책 제목과 전체 권수를 출력해보시오.
int book_cnt=0;
for(BookInfo b : book_infos) {
if(b.name.contains("Do it")) {
System.out.println(b.name);
book_cnt++;
}
}
System.out.printf("책 개수 : %d\n", book_cnt);
System.out.println(book_infos.size());
코틀린을 포함한 제목의 책을 찾아 제거하고, 남은 책의 권수를 출력하시오.
for(int idx=0; idx<book_infos.size(); idx++) {
BookInfo b2 = book_infos.get(idx);
if(b2.name.contains("코틀린")) {
book_infos.remove(idx);
}
}
System.out.printf("남은 책 권수 : "+book_infos.size());
참고사항!
1. 키보드 입력
InputStream() : byte stream
InputStreamReader(inputstream) : char stream
BufferedReader(reader) : buffer + string
InputStream() Scanner(inputstream or file) : token
System.in : InputStream - keyboard
2. 파일 입력
File(path)
FileInputStream(file or path) < InputStream
InputStreamReader(inputstream) : char stream
BufferedReader(reader) : buffer + string
File(path)
FileInputStream()
Scanner(inputstream or file) : token
System.out : PrintStream < FilterOutputStream < OutputStream
File(path)
FileOutputStream(path or file) < OutputStream
FileWriter(path or file) < OuputStreamWriter < Writer
PrintWriter(path or file or outputstream or writer) < Writer
728x90
반응형
'즐거운프로그래밍' 카테고리의 다른 글
[자바] 자바 클래스(class)의 이해 (0) | 2023.11.13 |
---|---|
[자바] 자바 객체 지향 프로그래밍(계산기 예제) (0) | 2023.11.13 |
[자바] 자바 외워두면 좋은 명령어 23가지 (0) | 2023.11.09 |
[자바] 자바 기본개념 학습하기(배열의 길이, 리스트, 리스트 정렬, (hash)map.get, (hash)map.keyset) (0) | 2023.11.09 |
[자바] List 제네릭스(generics) (0) | 2023.11.09 |