在Java中编写,删除和搜索特定数据

时间:2014-03-18 22:54:24

标签: java bufferedreader bufferedwriter

我刚刚在2个月前开始使用Java,所以我在使用FILE I / O时不够好。我是Stackoverflow的新手,这是我的第一篇文章,希望有人能帮我解决问题。顺便说一句,我正在创建一个可以使用JOptionPane.showInputDialog在文本文件中“写”,“删除”项目,价格等的清单系统,并且还可以允许用户“搜索”所选项目并显示JOptionPane.showMessageDialog中的适当输出。到目前为止,我完成了在文本文件中编写项目和其他信息。我现在的问题是更新价格,删除和搜索它。

这是示例输入和输出:

//sample data in text file                                                                
ITEM NAME: PAPER                                                                          
QUANTITY: 15                                                                         
ORIGINAL PRICE: P10.00                                                                     
SRP PRICE: P11.00                                                                            

ITEM NAME: BALLPEN                                                                          
QUANTITY: 20                                                                         
ORIGINAL PRICE: P15.00                                                                     
SRP PRICE: P16.00                                                                          

//DELETE and SEARCH sample input and output:

IF USER WANT TO DELETE                                                                     
Example:                                                                                  
ITEM YOU WANT TO DELETE: PAPER                                                            
//all information of Item paper in the text file will be deleted

IF USER WANT TO SEARCH                                                                        
Example:                                                                                  
ITEM YOU WANT TO SEARCH: BALLPEN                                                            
Output In JOptionPane:                                                                          
ITEM NAME: BALLPEN                                                                            
//the quantity, original and srp price will be displayed

提前致谢: - )

这是我在文本文件中写入数据的代码     公共类DataInventory {

public void AddNewItem (String itemName, String qntity, String costPrice, String sellPrice) throws IOException{

    BufferedWriter writeOnFile = new BufferedWriter(new FileWriter(new File(
            "Storage.txt"), true));

    double cnvrtCostPrice = Double.parseDouble(costPrice);
    double cnvrtSellPrice = Double.parseDouble(sellPrice);


    for (int i = 0; i < invStorage.length; i++){


        if (invStorage[i] == null){

            invStorage[i] = itemName;

            JOptionPane.showMessageDialog(null,  qntity + " " + itemName.toUpperCase() 
                    + " SUCCESSFULLY ADDED TO INVENTORY","Done", JOptionPane.PLAIN_MESSAGE);


            writeOnFile.write("ITEM NAME: " + itemName.toUpperCase() + "\n");
            writeOnFile.write("QUANTITY: " + qntity + "\n");
            writeOnFile.write("ORIGINAL PRICE: P" + String.format("%.2f", cnvrtCostPrice) + "\n");
            writeOnFile.write("SRP PRICE: P" + String.format("%.2f", cnvrtSellPrice) + "\n\n");
            writeOnFile.write("___________________________________________\n\n");
            writeOnFile.close();

            return;
        }
   }
  }
private static String invStorage[] = new String [1000];
}

0 个答案:

没有答案