2012-04-23 2 views
0

나는 ISBN, 저자, 제목과 책의 수준을 인쇄코드에서 ArrayList 문제, 어떤 조언을?

시작 왼쪽 책 제목의 이름으로 ISBN 번호를 변경하는 데 문제가 :

0201403765 Jan Skansholm Ada 95 from the Beginning 100 
0202535665 M. Ben-Ari   Software Engineering   25 
034565976X Michael Feldman Program Construction   12 
080539057X M.A. Weiss   Data Structures    30 
0805645782 Ken Arnold   Java for Programmers   10 
0905297568 A. Badone   Chaos Theory     15 

인쇄합니다 뭐죠에서 transactions.txt :

,691 :

0201403765  -55 
0201403765  2 
0202535665  10 
0202535665  -28 
034565976X  -7 
080539057X  -15 
0905297568  13 
0905297568  -5 

그래서 기본적으로 내가해야 할 일이 일치하는 경우이처럼에 책의 제목을 ISBN을 변경할 수있다

Java from the Beginning -55 
Java from the Beginning 2 
Software Engineering  10 
Software Engineering -28 
Program Construction  -7 
Data Structures -15 
Chaos Theory  13 
Chaos Theory  -5 

im은 아래 코드에 1로 표시되어 있습니다. isbn이 일치하는지 확인하는 방법이 정말 불확실합니다. 그렇다면 isbn과 일치하는 제목을 확인하고 작성하는 방법은 내 문제가 arraylist라고 생각합니다. (나는 세 번째 arraylist를 만들어야 만한다.) 배열로 모든 것을 만들면 어떤 충고라도 할 것이다.

class Book { 
    public final String isbn; 
    public final String title; 
    public final String author; 
    public final int level; 

    public Book(String isbn, String title, String author, int level) { 
     this.isbn = isbn; this.title = title; this.author = author; this.level = level; 
    } 
} 

및 예약 물체로 ArrayList를 채울 : BTW 1

import java.util.*; 
import java.io.*; 
class inventory{ 
    static void intial(){ 
     try{ 
      RandomAccessFile in = new RandomAccessFile("books.dat","r"); 
      ArrayList<String> list1=new ArrayList<String>(); 
      String author ,title , isbn; 
      int level=0; 
      while(in.getFilePointer()<in.length()){ 
       author = in.readUTF(); // author, at most 20 characters 
       title = in.readUTF(); // title, at most 40 characters 
       isbn = in.readUTF(); // ISBN 
       level = in.readInt(); // level, i.e. copies in stock (>=0) 
      //System.out.printf("%5d", isbn+author+title+level); 
      System.out.println(isbn+" "+author+" "+title+" "+level); 
      list1.add(title); 
      list1.add(isbn); 
      //list1.add(level); 
     } 
     in.close(); 
     System.out.println(" "); 
     String isbn2; 
     int level2=0; 
     //try{ 
      Scanner out = new Scanner(new File ("transactions.txt")); 
      ArrayList<String> list2=new ArrayList<String>(); 
      while(out.hasNextLine()){ 
       isbn2 = out.next(); 
       level2 = out.nextInt(); 
       System.out.println(isbn2 +" "+level2); 
       list2.add(isbn2); 
       //list2.add(level2); 
      } 
      out.close(); 

    1)  for (isbn: list1){ 
      for(isbn2: list2){ 
       if(isbn.contains(isbn2)){ 
        System.out.println(title+" "+level); 
       } 
      } 
     } 
     } 
     catch(IOException f){ 
      System.out.println("file error"); 
      f.printStackTrace(); 
     } 
    } 

} 
class BookShop{ 
    public static void main(String[]args){ 
     inventory x = new inventory(); 
     x.intial(); 
+0

들여 쓰기 BTW –

+1

내 눈 –

+1

그래서 들여 쓰기를 수정 ... 상처 꺼져 – MByD

답변

3

I는 다음과 같이, 각각의 책을 보유하는 개체를 만드는 것입니다 .... 완전히 ludacrisly 잘못된 것입니다. 그런 식으로 한 책에 대한 모든 것이 한 곳에서 이루어지며, 병렬 배열이 필요합니다.

(이 클래스를 작성한 방법은 입력 파일에서 객체를 읽은 후에도 객체를 업데이트 할 이유가 없기 때문에이 클래스를 작성한 방법입니다.)이 코드가 파이썬이나 하스켈이라면 튜플 및 의식없이 할 수 있지만 자바에서는 옵션이 없습니다.)

+0

This.isbn ??? 이 책을 작성하고 있습니까? –

+0

@Andrew : 죄송합니다. 귀하의 질문이 이해가되지 않습니다. –

3

먼저 책 데이터를 저장할 Book 개체를 만듭니다.

public Book{ 
    private String ISBN =""; 
    private String title=""; 
    private String author=""; 
    private int level; 
    public Book(String ISBN, String title,String author, int level){ 
     this.ISBN=ISBN; 
     this.title=title; 
     this.author=author; 
     this.level=level; 
    } 
    public String getTitle(){ 
     return title; 
    } 
    public String getISBN(){ 
     return ISBN; 
    } 
    public String getAuthor(){ 
     return author; 
    } 
    public int getLevel(){ 
     return level; 
    } 
} 

Book 개체를 ArrayList에 추가하십시오.

ArrayList<Book> bkList = new ArrayList<Book>(); 
bkList.add(new Book('ISBN','Title','Author')); 

get() 메소드로 도서 데이터를 가져옵니다.

Book tempBook; 
for (int x=0;x<bkList.Size();x++){ 
    tempBook=bkList.get(x); 
    System.out.println(tempBook.getTitle()+" "+tempBook.getLevel()); 
} 
+0

도서 목록을 randomAccessFile ("books.dat")에서 읽어야하고 변경 사항을 txt 파일 ("transactions.txt")을 사용하면 다음과 같이 간단하게 처리 할 수 ​​있습니다. while (in.getFilePointer()

+0

정확하지 않습니다. 북 생성자는 ISBN, 작성자, 수준 및 제목에 대한 값을 가져야합니다. – eabraham

+0

그래서 bkList.add (새 도서 ('ISBN', '제목', '저자', '레벨'))); –