0

我在拼凑这个时遇到了很大的麻烦。我有基本的读写功能。我需要检查来自文件“Books.txt”的输入,以便:

  • ISBN 有效
  • CopyNumber、Year 和 Statistics 应该是数字
  • Title、Author 和 Publisher 必须包含值
  • BorrowDate 必须是有效日期
  • ReturnDate(如果可用)必须是有效日期
  • LibraryCardNumber(如果可用)必须是数字。

如果没有借书,则最后两个字段不存在。

来自“Books.txt”的 2 个示例行:

9780140455168#2#The Twelve Caesars#Suetonius#Penguin Classics#2007#3#101009#101030#5478
9780141188607#1#Claudius the God#Robert Graves#Penguin Classics#2006#2#080123

错误行应写入带有错误消息的“ErrorLines.txt”,例如 Wrong ISBN。无错误的书籍应写入按作者姓名排序的“NewBooks.txt”。

这是我到目前为止所得到的。我不是在寻找一个完整的解决方案,因为我显然还有很长的路要走,但是如果有人愿意给我一些指示,我将非常感激!是的,这是家庭作业:D

我是否需要进行尝试循环来验证输入...?

图书馆类:

import java.util.ArrayList;
import java.util.*;
import java.io.*;
import java.io.IOException;

public class Library {

public void readFromFile (String filename) throws IOException {

String inLine;
File inFile;

    inFile = new File("Books.txt");
    BufferedReader fIn = new BufferedReader(new FileReader(inFile));

        inLine = fIn.readLine();
        while (inLine != null) {
            inLine = fIn.readLine();
            aBookList.add(inLine + "\n");
        }
        fIn.close();
    }

public void writeToFile (String fileName) throws IOException {

    BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(fileName));

            bw.write("???"); //Dont know what to put here...
            bw.newLine();
        } catch (IOException e) {
            System.out.println("Error writing file.");
        } finally {
            bw.close();
        }
    }

 public static boolean isISBN13Valid(isbn) {
        int check = 0;
        for (int i = 0; i < 12; i += 2) {
            check += Integer.valueOf(isbn.substring(i, i + 1));
        }
        for (int i = 1; i < 12; i += 2) {
            check += Integer.valueOf(isbn.substring(i, i + 1)) * 3;
        }
        check += Integer.valueOf(isbn.substring(12));
        return check % 10 == 0;
    }
}

这是 Book 类:

import java.util.*;
import java.io.*;

public class Book {
Book b = new Book();
private static ArrayList<String> aBookList = new ArrayList<String>();

private String Isbn;
private int CopyNumber;
private String Title;
private String Author;
private String Publisher;
private int Year;
private int Statistics;
private String BorrowDate;
private String ReturnDate;
private int LibraryCardNumber;

public void bookInfo (String nIsbn, int nCopyNumber, String nTitle, String nAuthor, String nPublisher, int nYear,
        int nStatistics, String nBorrowDate, String nReturnDate, int nLibraryCardNumber) {

    Isbn = nIsbn; 
    CopyNumber = nCopyNumber; 
    Title = nTitle; 
    Author = nAuthor; 
    Publisher = nPublisher; 
    Year = nYear;
    Statistics = nStatistics; 
    BorrowDate = nBorrowDate; 
    ReturnDate = nReturnDate; 
    LibraryCardNumber = nLibraryCardNumber;
}

public void bookInfo (String Row) {
    StringTokenizer sT = new StringTokenizer(Row);
    Isbn = sT.nextToken("#");
    CopyNumber = Integer.parseInt(sT.nextToken("#") );
    Title = sT.nextToken("#");
    Author = sT.nextToken("#");
    Publisher = sT.nextToken("#");
    Year = Integer.parseInt(sT.nextToken("#") );
    Statistics = Integer.parseInt(sT.nextToken("#") );
    BorrowDate = sT.nextToken("#");
    ReturnDate = sT.nextToken("#");
    LibraryCardNumber = Integer.parseInt(sT.nextToken("#") );
}
public void setIsbn(String nIsbn) {
    Isbn = nIsbn;       
}
public void setCopynumber(int nCopyNumber) {
    CopyNumber = nCopyNumber;       
}
public void setTitle(String nTitle) {
    Title = nTitle;     
}
public void setAuthor(String nAuthor) {
    Author = nAuthor;       
}
public void setPublisher(String nPublisher) {
    Publisher = nPublisher;     
}
public void setYear(int nYear) {
    Year = nYear;       
}
public void setStatistics(int nStatistics) {
    Statistics = nStatistics;       
}
public void setBorrowDate(String nBorrowDate) {
    BorrowDate = nBorrowDate;       
}
public void setReturnDate(String nReturnDate) {
    ReturnDate = nReturnDate;
}
public void setLibraryCardNumber(int nLibraryCardNumber) {
    LibraryCardNumber = nLibraryCardNumber;
}

 public String getAll () {
    String s = " ";
    return (Isbn + s + CopyNumber + s + Title + s + Author + s + Publisher + s +
                   Year + s + Statistics + s + BorrowDate + s + ReturnDate + s +
                   LibraryCardNumber);
}
public void showAll () {
    String t = "\t";
    System.out.println(Isbn + t + CopyNumber + t + Title + t + Author + t +
                       Publisher + t + Year + t + Statistics + t + 
                       BorrowDate + t + ReturnDate + t + LibraryCardNumber);
}
}

最后是带有 main 方法的 Main 类:

public class Main<aBookList> implements Comparable<aBookList> {
public static void main(String [] args) throws Exception {

    new Library().readFromFile("Books.txt");

    new Library().writeToFile("NewBooks.txt");
    new Library().writeToFile("ErrorLines.txt");

}

@Override
public int compareTo(aBookList o) {
    return 0;
}
}
4

1 回答 1

0

as it is homework, i will point you direction, not give you code

1) you have lot of mess here, ie i'm not sure why you have compare in your main class? instead of creating getAll method in bookInfo(which is named against java nameing convention) just override toString method

2) why do you have list of strings? read a line, convert this into book, if book is valid add it to your list, otherwise report an error

3) move your isISBN13Valid method to book

4) write to file -> loop through your list, and save each element into file by bw.write(book.toString()),

5) create second method createErrorFile, then each error what you will have add into your error list, and after you call that method, you will sace each element into given file, it is not perfetc solution, better will be if you add error to file each time when it occur.

6) create one instance of library in your main method, and just call on it all your method, and avoid using static fields in your project(sometimes you must, but if you don;t need, just avoid them)

7) names for method import/ export i think sounds nicer than read from file read from file

于 2012-11-01T14:19:18.160 回答