写出到文本文件时递减整数变量的问题

时间:2019-03-28 02:25:39

标签: java jdialog decrement

我有两个文本文件,一个叫做“ BookDetails.txt”,另一个叫做“ CheckOutDetails.txt”。

BookDetails.txt包含以下内容(书名:作者:出版者:分支机构电话号码:份数)

  

Java图书:John Doe:Scholastic:985.2 Doe:25

我的第二个文本文件“ CheckOutDetails.txt”包含内容(名字:姓氏:书名:日期:更新的副本数),以便当签出特定的书时,使用此信息更新文本文件。该文本文件包含正在签出书籍的顾客

这是我要写入CheckOutDetails.txt文本文件的方式,如果同一本书每次两次签出两次到另一家图书馆的赞助人那里

  

Matthew:Manning:Java书:3/26/2019:24

     

Oliver:Twist:Java图书:3/26/2019:23 [此数字是图书馆目录中剩余的副本数]

现在,我的程序能够从BookDetails.txt文件中找到特定的书,但是当将其写入CheckOutDetails.txt文件时,它并不能正确地减少numberOfCopies

private void jButtonCheckOutActionPerformed(java.awt.event.ActionEvent evt) {                                                
 firstName = jTextFieldFirstName.getText();
 lastName = jTextFieldLastName.getText();
 bookTitle = jTextFieldBookTitle.getText();
 checkOutDate = jTextFieldDate.getText();




 CheckOut checkOutInfo = new CheckOut(firstName, lastName, bookTitle, checkOutDate);

 checkOuts.add(checkOutInfo);

CheckOutCopy();

}                                               

public void CheckOutCopy() //This method works fine
{
 try {

    File f = new File("BookDetails.txt");
    Scanner fileRead = new Scanner(f);

    boolean foundTitle = false;
    fileRead.nextLine();
    while(fileRead.hasNextLine())
    {
        String textLine = fileRead.nextLine();
        String[] bookInfo = textLine.split(" : ");
        String tempBookTitle = bookInfo[0];
        numberOfCopies = Integer.parseInt(bookInfo[4]);


        if(tempBookTitle.trim().equals(bookTitle))
        {
            foundTitle = true;
            break;
        }
    }
    if(foundTitle && numberOfCopies > 0)
    { 
       numberOfCopies--;
       WriteCheckOut();
       this.setVisible(false);
    }
        else if(foundTitle && numberOfCopies == 0)
        {
            if(JOptionPane.showConfirmDialog(null, "Would you like to add the patron to the queue?", "No copies available", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
            {
                AddPatronQueue();
            }
            else
            {
                JOptionPane.getRootFrame().dispose();
            }
        }
    else
    {
        JOptionPanes.messageBox("Book was not found in Library Catalog", "Check Out Error");
    }

} catch (FileNotFoundException ex) {
    ex.printStackTrace();
}
}


private void AddPatronQueue()
{
//String patronName = jTextFieldFirstName.getText();
//String bookQueue = jTextFieldBookTitle.getText();


}

private void WriteCheckOut() 
{

WriteFile wf = new WriteFile("CheckOutDetails.txt");

for(int i = 0; i < checkOuts.size(); i++)
{
    CheckOut c = checkOuts.get(i);

    String checkOutDetails =  c.getFirstName() + " : " + c.getLastName() + " : " + c.getBookTitle() + " : " + c.getCheckOutDate() + " : " + numberOfCopies;

    wf.write(checkOutDetails);
}
wf.close();


}

程序写入时,CheckOutDetails.txt文件的内容是

  

Matthew:Manning:Java图书:3/27/19:24

     

Oliver:Twist:Java图书:3/27/19:24(应该是23,而不是24)

0 个答案:

没有答案