-
16. characterstream- (11) ReaderDemo ( 도서 관리 시스템 )java/코드 리뷰 2020. 4. 27. 16:481234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465package characterstream;public class Book {private int no;private String title;private String writer;private String publisher;private int price;public Book() {}public Book(int no, String title, String writer, String publisher, int price) {super();this.no = no;this.title = title;this.writer = writer;this.publisher = publisher;this.price = price;}public int getNo() {return no;}public void setNo(int no) {this.no = no;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getWriter() {return writer;}public void setWriter(String writer) {this.writer = writer;}public String getPublisher() {return publisher;}public void setPublisher(String publisher) {this.publisher = publisher;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}}123456789101112131415161718192021222324252627282930313233343536373839404142434445package characterstream;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;public class ReaderDemo1 {public static void main(String[] args) throws IOException {FileReader fr = new FileReader("c:/files/data.txt");BufferedReader br = new BufferedReader(fr);// String text1 = br.readLine();// String text2 = br.readLine();// String text3 = br.readLine();// String text4 = br.readLine();// String text5 = br.readLine();// String text6 = br.readLine();//// System.out.println(text1);// System.out.println(text2);// System.out.println(text3);// System.out.println(text4);// System.out.println(text5);// System.out.println(text6);String text = null;int totalOrderPrice = 0;while ((text = br.readLine()) != null) {if (!text.isEmpty()) {// System.out.println(text); // 위의 text들을 한 줄로 요약String[] items = text.split(",");// System.out.println(items[4]); // 4번째 가격만 출력int price = Integer.parseInt(items[4]);totalOrderPrice += price;}}System.out.println("총 구매금액 : " + totalOrderPrice);br.close();}}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596package characterstream;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class ReaderDemo2 {public static void main(String[] args) throws IOException {Scanner sc = new Scanner(System.in);// data.txt의 도서 정보 읽어서 Book 객체에 담고, ArrayList에 Book 객체 저장List<Book> books = new ArrayList<Book>();FileInputStream fis = new FileInputStream("c:/files/data.txt");InputStreamReader isr = new InputStreamReader(fis, "ms949");BufferedReader reader = new BufferedReader(isr);String text = null;while ((text = reader.readLine()) != null) {if (!text.isEmpty()) {String[] items = text.split(",");int no = Integer.parseInt(items[0]);String title = items[1];String writer = items[2];String publisher = items[3];int price = Integer.parseInt(items[4]);Book book = new Book();book.setNo(no);book.setTitle(title);book.setWriter(writer);book.setPublisher(publisher);book.setPrice(price);books.add(book);}}reader.close();while (true) {System.out.println("========================================");System.out.println("1.도서내역조회 2.상세조회 0.종료");System.out.println("========================================");System.out.print("메뉴 번호를 입력하세요 : ");int menuNo = sc.nextInt();System.out.println();if (menuNo == 1) {System.out.println("[도서내역조회]");System.out.printf("%-3s %-30s %-15s %-7s\n", "번호", "제목", "저자", "가격");for (Book book : books) {int no = book.getNo();String title = book.getTitle();String writer = book.getWriter();int price = book.getPrice();System.out.printf("%-3s %-30s %-15s %-7s\n", no, title, writer, price);}System.out.println();} else if (menuNo == 2) {System.out.println("[도서정보 보기]");System.out.print("책번호를 입력하세요 : ");int bookNo = sc.nextInt();System.out.println();for (Book book : books) {if (book.getNo() == bookNo) {System.out.println("책번호 : " + book.getNo());System.out.println("제목 : " + book.getTitle());System.out.println("저자 : " + book.getWriter());System.out.println("출판사 : " + book.getPublisher());System.out.println("가격 : " + book.getNo());}}System.out.println();} else if (menuNo == 0) {break;}}sc.close();}}
'java > 코드 리뷰' 카테고리의 다른 글
16. serialization - (12) SerializationDemo ( 직렬화 ) (0) 2020.04.27 16. bytestream - (10) FileOutputStreamDemo1 ( 콘솔을 파일에 저장 ) (0) 2020.04.27 16. bytestream - (9) FileInputStreamDemo1 ( 파일을 콘솔에 출력 ) (0) 2020.04.27 16. bytestream - (8) FileCopyDemo8 ( 사진주소 복사 - url 사용 ) (0) 2020.04.27 16. bytestream - (7) FileCopyDemo7 ( 사진파일 복사 - url 사용 ) (0) 2020.04.27