在JavaFX中将文件读入Array然后回写

时间:2014-12-10 04:46:14

标签: java arrays javafx file-read

我正在处理一个小库存程序,我正在尝试确定如何将文件中的数据转换为某种类型的数组,以便将数据绑定到某些控件。该想法是用户可以浏览数据并进行更改,然后将其写回文件(覆盖其中的当前内容)。

这是一个javafx程序。我在数据中读得很好,但到目前为止我所尝试的所有内容都没有在while循环之外访问数据。任何正确方向的指针都会有所帮助。到目前为止,我对ArrayList或者一个对象阵列并不感到高兴,因为在我读完所有信息之后,我似乎无法随意访问。

try (
            ObjectInputStream read_file = new ObjectInputStream(new FileInputStream("stock.dat")) ;
            ) {
        while (true) {

            String invItemName = read_file.readUTF() ;
            double itemCost = read_file.readDouble() ;
            double itemPrice = read_file.readDouble() ;
            double itemStartInv = read_file.readDouble() ;
            double itemUnitsSold = read_file.readDouble() ;
            double itemProjectedSales = read_file.readDouble() ;
            double profit = ((itemPrice - itemCost) * itemUnitsSold) ;
            //double dblActualvsProjectedSalesPercentage = getSalesPercentage(itemUnitsSold, itemProjectedSales) ;
            double dblDefaultOrder = itemProjectedSales + (itemProjectedSales * .1) ;
            double dblBigOrder = dblDefaultOrder + (dblDefaultOrder * .1) ; 

            InventoryItem o = new InventoryItem(invItemName, itemCost, itemPrice, itemStartInv, itemUnitsSold, itemProjectedSales);

            //For each line in the file, format & display in the primary inventory window
            taCurrentInventory.appendText(String.format( "Item: \t\t\t%8s\nUnit Cost: \t\t\t$%.2f\nUnit Price: \t\t\t$%.2f\nBeginning Inventory: \t%.0f\nUnits Sold: \t\t\t%.0f\nEstimated Sales: \t\t%2.0f\n------------------------------------\n", invItemName, itemCost, itemPrice, 
                   itemStartInv, itemUnitsSold, itemProjectedSales ));

            //Fill in taSalesAndProfit with sales firgures
            taSalesAndProfit.appendText(String.format( "Item: \t%s\nSales: \t%.0f\nProfit: \t$%.2f\n-----------------------------\n", invItemName, itemUnitsSold, profit ) ) ;

            //Format and display our profit data
            if (o.getActualvsProjectedSalesPercent(itemUnitsSold, itemProjectedSales) >= 20 || o.getActualvsProjectedSalesPercent(itemUnitsSold, o.getEstSales()) >= -19) { //Just dispay if actual sales vs projected sales percentage is down less than 20%
                taMarketing.appendText(String.format( "%s \t %4.0f%%\n", invItemName, o.getActualvsProjectedSalesPercent(itemUnitsSold, o.getEstSales()) ) ) ;
            }
            else { //Flag item for a sales promotion to get rid of excess inventory because actual sales vs projected sales are down 20% or more
                taMarketing.appendText(String.format( "%s \t %4.0f%% - Sale!!\n", invItemName, o.getActualvsProjectedSalesPercent(itemUnitsSold, o.getEstSales()) ) ) ;    
            }

            //Format and display our ordering needs
            if (o.getActualvsProjectedSalesPercent(itemUnitsSold, itemProjectedSales) >= 20) { //If sales are 20% or more than projected order 10% more than usual
                taOrdering.appendText(String.format("Item: \t\t%s\nOrder Qty: \t%.0f\n---------------------------\n", invItemName, dblBigOrder)) ;                    
            }
            else { //Otherwise, just order the usual +10% of projected sales
                taOrdering.appendText(String.format("Item: \t\t%s\nOrder Qty: \t%.0f\n---------------------------\n", invItemName, dblDefaultOrder)) ;
            }

        }

    }
    catch (EOFException ex) {

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

0 个答案:

没有答案