The reason I am asking this is because it seems (for me) a lot easier to set-up a file management system in Java than to set up a complicated relational database in SQL. What would having an SQL relational database benefit me over the following example:
Example: a Products file that allows variable pricing for the same product
in Java:
public class Product implements Serializable {
public static int productCount = 0;
private final int productID;
private final long productRegisterDate;
private String productDescription;
private List<ProductPrice> productPrices;
Product (String desc) {
productID = ++productCount;
productRegisterDate = Calendar.getInstance().getTimeInMillis();
productDescription = desc;
productPrices = new ArrayList<ProductPrice>();
}
public Product addPrice(double price, String priceDesc) {
productPrices.add(new ProductPrice(price, priceDesc);
return this;
}
public void updateProduct(String desc, ArrayList<ProductPrice> prices) {
productDescription = desc;
productPrices = prices;
}
public int getID() return productID;
public long getDateRegistered() return productRegisterDate;
public String getDescription() return productDescription;
public ArrayList<ProductPrice> getPrices() return productPrices;
}
public class ProductPrice implements Serializable {
private double price;
private String priceDescription;
ProductPrice(double price, String priceDesc) {
this.price = price;
priceDescription = priceDesc;
}
}
inserting products:
...
List<Product> myProducts = new ArrayList<Product>();
myProducts.add(new Product("product a")
.addPrice(1.99,"250g pack")
.addPrice(2.99,"500g pack");
find Product objects:
...
public Product findProductById(int pid) {
for (Product p:myProducts)
if (p.getID() == pid) return p;
return null;
}
public List<Product> searchProducts(String searchTerm, int limit) {
List<Product> results = new ArrayList<Product>();
int count = 0;
for (Product p:myProducts) {
if (p.getDescription().indexOf(searchTerm)>-1) {
results.add(p);
count++;
if (count >= limit) break;
}
return results;
}
query product info with object:
...
String productDescription = p.getDescription();
update product info without object:
public boolean updateProductByID(int pid, String desc, ArrayList<ProductPrice> prices) {
Product p = null;
try {
p = findProductById(pid);
p.updateProduct(desc, prices);
} catch (NullPointerException e) {
return false;
}
return true;
}
save/load data:
...
private final String SAVE_PATH = "C:/";
private final String PRODUCTS_FILE = "Products.dat";
public static boolean saveProducts(ArrayList<Product> myProducts) {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream(SAVE_PATH+PRODUCTS_FILE));
out.writeObject(myProducts);
out.close();
} catch (IOException e) {
return false;
}
return true;
}
public static boolean loadProducts(ArrayList<Product> myProducts) {
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream(SAVE_PATH+PRODUCTS_FILE));
myProducts = (ArrayList<Product>) in.readObject();
return true;
} catch (IOException e1) {
return false;
} catch (ClassNotFoundException e2) {
return false;
}
}
Please advise me, I want to know all of the details.
Bear in mind I already have a Java application with class objects (i.e. Products, Orders, etc) and I am wondering if I should use a database to serve and store the data rather than the local file system.
You can reinvent anything you want to, if you really want to.
Pros:
Cons:
So the question is, how much time are you willing to spend creating this?