I just want to say thank you for taking the time to look into my question. I am currently working on an inventory interface. The issue I am having is that when I read an update file, to update my database file, this is what I get: database: abc,123,10.0,5.0,false,10 (partName,partNumber,listPrice,salePrice,onSale,quantity)
after I read in the update file: abc,123,10.0,5.0,false,10 is first in my db followed by abc,123,20.0,10.0,true,2
how do I fix this so that it updates the string in the db file, instead of appending it to the file?
here is some of my code:
//findBp method
public static BikePart findBp(ArrayList<BikePart> bpal, BikePart bp) {
BikePart found = null;
for(BikePart b : bpal) {
if(b.getPartName().equals(bp.getPartName()) && b.getPartNumber() == bp.getPartNumber()) {
found = b;
break;
}
}
return found;
}
//sort by partName
public static void sortName(ArrayList<BikePart> bpal) {
Collections.sort(bpal, BikePart.bpNameComp);
}
//sort by partNumber
public static void sortNumber(ArrayList<BikePart> bpal) {
Collections.sort(bpal, BikePart.bpPartNumComp);
}
//readFile method
public static void readFile(String fileName, ArrayList<BikePart> bpal) {
if(fileName == null || fileName.equals("")) {
return;
}
File file = new File(fileName);
try {
Scanner read = new Scanner(file);
while(read.hasNextLine()) {
String line = read.nextLine();
String pv[] = line.split(",");
BikePart bp = BikePart.toObject(pv);
BikePart found = findBp(bpal, bp);
if(found == null) {
bpal.add(bp);
} else {
found.setQuantity(found.getQuantity() + bp.getQuantity());
found.setPartName(bp.getPartName());
found.setPartNumber(bp.getPartNumber());
found.setListPrice(bp.getListPrice());
found.setSalesPrice(bp.getSalesPrice());
found.setPartOnSale(bp.isPartOnSale());
}
}
read.close();
}
catch (FileNotFoundException e) {
System.out.println(fileName + " is not found!");
System.out.println("Enter another file name.");
String fileName2 = in.nextLine();
readFile(fileName2, bpal);
}
}
//writeFile method
public static void writeFile(String fileName, ArrayList<BikePart> bpal) {
try {
BufferedWriter outStream = new BufferedWriter(new FileWriter(fileName, true));
for(BikePart bp : bpal) {
outStream.write(bp.toString());
outStream.newLine();
}
outStream.close();
}
catch (IOException e) {
System.out.println("file not found!");
}
}
public static void main(String[] args) {
//calendar field
Calendar cal = Calendar.getInstance();
//ArrayList for DB
ArrayList<BikePart> bpal = new ArrayList<BikePart>();
readFile("DB.txt", bpal);
//user input variable
String usrIn = "";
//loop for user choice
while(usrIn != "Quit") {
//prompts user to select choice
System.out.println("Please select your option from "
+ "the following menu: ");
System.out.println("Read: Read an inventory delivery file");
System.out.println("Enter: Enter a part");
System.out.println("Sell: Sell a part");
System.out.println("Display: Display a part");
System.out.println("SortName: Sort parts by part name");
System.out.println("SortNumber: Sort parts by part number");
System.out.println("Quit: ");
System.out.println("Enter your choice: ");
//initiates usrIn
usrIn = in.nextLine();
//switch for input choice
switch(usrIn) {
case "Read":
//read method
System.out.println("Enter file name: ");
String fileName = in.nextLine();
readFile(fileName, bpal);
break;
case "Enter":
//enter method
break;
case "Sell":
//sell method
break;
case "Display":
//display method
break;
case "SortName":
//sortName method
break;
case "SortNumber":
//sortNum method
break;
case "Quit":
//quit method
writeFile("DB.txt", bpal);
System.out.println("good bye! ");
break;
}
if(usrIn.equals("Quit")) {
break;
}
}
}
I haven't filled in the rest of the code yet, because I want to fix this issue first before I move on. I just want to say thanks again for taking a look.