DEV Community

Saravanan Lakshmanan
Saravanan Lakshmanan

Posted on

Java Learning Series - Updated Product Management System

Today, I learnt how to import a file using File class object instead of creating multiple objects.

Products.java

public class Product{

    private int id;
    private String name;
    private String category;
    private int price;

    public Product(int i, String n, String c, int p) {
        this.id = i;
        this.name = n;
        this.category = c;
        this.price = p;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategory() {
        return this.category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public int getPrice() {
        return this.price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String toString() {
        return this.getId() + " , " + this.getName() + " , " + this.getCategory() + " , " + this.getPrice();
    }
}
Enter fullscreen mode Exit fullscreen mode

ProductService.java

import java.util.ArrayList;

public class ProductService{

    ArrayList<Product> arr = new ArrayList();

    public void addProduct(Product product) {
        boolean addStatus = arr.add(product);
        if (addStatus) {
            System.out.println("Product is Added");
        } else {
            System.out.println("Product is not Added");
        }
    }

    public void viewProduct() {
        for (Product product : arr) {
            System.out.println(product);
        }
    }

    public void searchProduct(int id) {
        System.out.println();
        boolean found = false;

        for (Product product : arr) {
            if (product.getId() == id) {
                System.out.println(product.getId() + " - " + product.getName() + " - " + product.getCategory() + " - " + product.getPrice());
                found = true;
                break;
            }
        }

        if (found == true) {
            System.out.println("Product is Found");
        } else {
            System.out.println("Product is not Found");
        }
    }

    public void updateProduct(Product newProduct) {
        for (Product existingProduct : arr) {
            if (existingProduct.getId() == newProduct.getId()) {
                existingProduct.setName(newProduct.getName());
                existingProduct.setCategory(newProduct.getCategory());
                existingProduct.setPrice(newProduct.getPrice());
            }
        }
    }

    public void deleteProduct(int id) {
        boolean deleteStatus = false;

        for (Product product : arr) {
            if (product.getId() == id) {
                arr.remove(product);
                deleteStatus = true;
                break;
            }
        }

        if (deleteStatus == true) {
            System.out.println("Product is Deleted");
        } else {
            System.out.println("Product is Not Found");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Main.java

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;

public class Main{

    public static void main(String[] args) {

        ProductService service = new ProductService();

        File file = new File("products.csv");
//        System.out.println(file.canRead());

        try{
            FileReader reader = new FileReader(file);
            BufferedReader br = new BufferedReader(reader);
            String line = br.readLine();

            line = br.readLine();
            while(line!=null){
                String[] data = line.split(",");
//                System.out.println(data);
//                System.out.println(line);

                    int id = Integer.parseInt(data[0]);
                    String name = data[1];
                    String category = data[2];
                    int price = Integer.parseInt(data[3]);

                Product product = new Product(id, name, category, price);
                service.addProduct(product);

//                    System.out.println(data[0]);
//                    System.out.println(data[1]);
//                    System.out.println(data[2]);
//                    System.out.println(data[3]);

//                for(int i = 0; i<data.length; i++){
//                    System.out.println(data[i]);
//                }

//                for(String str : data){
//                    System.out.println(str);
//                }
                line = br.readLine();
            }



        }catch(FileNotFoundException file_Obj){
            System.out.println("FileNotFoundException - Error");
        }catch(IOException io_Obj){
            System.out.println("IOException - Error");
        }


//        Product p1 = new Product(101, "Laptop", "Electronics", 50000);
//        Product p2 = new Product(102, "Mobile", "Electronics", 30000);
//        Product p3 = new Product(102, "Chair", "Furniture", 5000);
//        service.addProduct(p2);
//
        service.viewProduct();
//
        service.searchProduct(102);

//        service.updateProduct(p3);

        service.viewProduct();

        service.deleteProduct(101);

        service.viewProduct();
//
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Product is Added
Product is Added
Product is Added
101 , Laptop , Electronics , 50000
102 , Mobile , Electronics , 20000
103 , Chair , Furniture , 10000

102 - Mobile - Electronics - 20000
Product is Found
101 , Laptop , Electronics , 50000
102 , Mobile , Electronics , 20000
103 , Chair , Furniture , 10000
Product is Deleted
102 , Mobile , Electronics , 20000
103 , Chair , Furniture , 10000

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
ezhil_abinayak_e38eec8fb profile image
Ezhil Abinaya K

Excellent Work 💥🤩