小图书馆管理程序的麻烦

时间:2012-11-01 13:18:35

标签: java

我遇到了很大麻烦。我有基本的读写功能。我需要的是检查文件'Books.txt'的输入,以便:

  • ISBN有效
  • CopyNumber,Year和Statistics应为数字
  • 标题,作者和出版商必须包含值
  • 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'并带有错误消息,例如错误的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方法:

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

1 个答案:

答案 0 :(得分:0)

因为它是家庭作业,我会指明你的方向,而不是给你代码

1)你这里有很多混乱,也就是说我不确定为什么你在主课上有比较?而不是在bookInfo中创建getAll方法(以java命名约定命名)只是覆盖toString方法

2)为什么你有字符串列表?读取一行,将其转换为书籍,如果书籍有效,请将其添加到列表中,否则报告错误

3)将您的isISBN13Valid方法移至预定

4)写入文件 - &gt;遍历您的列表,并按bw.write(book.toString())

将每个元素保存到文件中 5)创建第二个方法createErrorFile,然后将每个错误添加到您的错误列表中,并在调用该方法后,将每个元素放入给定文件中,它不是perfetc解决方案,如果你将更好每次发生时都会向文件添加错误。

6)在你的main方法中创建一个库实例,然后只需调用你的所有方法,并避免在你的项目中使用静态字段(有时你必须,但如果你不需要,只需要避免它们)< / p>

7)方法导入/导出的名称我认为听起来比从文件中读取的文件

更好