package uprapham; import java.io.BufferedWriter; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; public class itemsBean { private static int idGenerator = 1; //private static String datafile = "/Applications/apache-tomcat-6.0.41/webapps/swe432/WEB-INF/data/uprapham/data.txt"; private static String datafile = "/data/apps-swe432/swe432/WEB-INF/data/uprapham/data.txt"; public itemsBean() { } public static String addBid(String id, String desc, String bid, String bidder) { try{ BufferedReader fileIn = new BufferedReader(new FileReader(datafile)); String line = null; String result = ""; boolean found = false; //update the existing line if necessary while ((line=fileIn.readLine()) != null){ String[] pieces = line.split(","); if (id != null && bid != null && bidder != null && pieces[0].equals(id)){ if (Integer.parseInt(bid) > Integer.parseInt(pieces[2])){ found = true; if( !pieces[4].equals(bidder)) line = pieces[0]+","+pieces[1]+","+bid+","+bidder+","+pieces[4]; else return "

Sorry, you can't bid on your own item, "+bidder+"!

"; } else return "

Sorry, you can't bid with a lower amount, "+bidder+"!

"; } result += line+"\n"; } fileIn.close(); //add a new item to the end if necessary if (!found) result += ++idGenerator + "," + desc + "," + bid + "," + bidder + "," + bidder + "\n"; BufferedWriter fileOut = new BufferedWriter(new FileWriter(datafile)); fileOut.append(result); fileOut.close(); if (found) return "

Congratulations, "+bidder+", you have the highest bid!

"; else return "

New item, [" + desc+ "] id[" + id + "] recorded for "+bidder+" with bid "+bid+"

"; }catch (Exception e){ return "

Error: database file not found when trying to bid!

"; } } public String getCurrentItems(){ /* * Example data file: * * ID,description,maxBid,maxBidder,creator * 1,dresser,500,jDoe,upsorn */ try{ BufferedReader file = new BufferedReader(new FileReader(datafile)); String line = null; ArrayList items = new ArrayList(); String result = ""; }catch (Exception e){ return "

Error: database file not found!

"; } } }