0

我在将 ISBN 号更改为书名时遇到问题

开始打印 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

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

我遇到的问题是在下面的代码中标记为 1,真的不确定如何检查 isbn 是否匹配,如果是,如何检查什么标题与 isbn 匹配并将其写出来,我认为我的问题是数组列表(我应该做一个第三个数组列表)或者只是把所有东西都变成数组,任何建议都可以,干杯!!!!顺便说一句,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();
4

2 回答 2

3

我会创建一个对象来保存每本书,如下所示:

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;
    }
}

并用 Book 对象填充 ArrayList。这样一本书的所有内容都在一个地方,而不是需要并行数组。

(我编写此类的方式是不可变的,因为一旦从输入文件中读取对象,您就没有任何理由更新对象。如果这是 Python 或 Haskell,我们将只使用元组而无需仪式,但对于 Java,我们没有这个选项。)

于 2012-04-23T19:38:52.487 回答
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());
}
于 2012-04-23T19:41:00.387 回答