Есть у кого какие идеи?
Буду рад любой помощи
package com.javarush.task.task18.task1828;
import java.io.*;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class CrUD {
private String dbName;
private Map<Integer, Goods> listGoods = new TreeMap<>();
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
public Map<Integer, Goods> getListGoods() {
return listGoods;
}
public void setListGoods(Map<Integer, Goods> listGoods) {
this.listGoods = listGoods;
}
public void initListGoods() throws IOException {
BufferedReader dbFile = new BufferedReader(new FileReader(dbName));
while (dbFile.ready()) {
Goods entry = parseString(dbFile.readLine());
listGoods.put(entry.ID, entry);
}
dbFile.close();
}
private Goods parseString(String entry) {
int id = Integer.parseInt(entry.substring(0, 8).trim());
String name = entry.substring(8, 38).trim();
double price = Double.parseDouble(entry.substring(38, 46).trim());
int quantity = Integer.parseInt(entry.substring(46).trim());
return new Goods(id, name, price, quantity);
}
public void updateAddEntry(int id, Goods entry) throws IOException {
listGoods.put(id, entry);
writeDB(listGoods);
}
public void deleteEntry(int id) throws IOException {
listGoods.remove(id);
writeDB(listGoods);
}
private void writeDB(Map<Integer, Goods> data) throws IOException {
BufferedWriter dbFile = new BufferedWriter(new FileWriter(dbName));
for (Map.Entry<Integer, Goods> pair: data.entrySet()) {
dbFile.write(pair.getValue().toString());
if (pair.getKey() != Collections.max(data.keySet()))
dbFile.write("\n");
}
dbFile.close();
}
}
