How To retain the last viewed items from the cart in Java across different sessions

Category : Java | Sub Category : Java Interview questions | By Prasad Bonam Last updated: 2023-09-12 01:36:17 Viewed : 305


To retain the last viewed items from the cart in Java across different sessions :

To retain the last viewed items from the cart in Java across different sessions or even program executions, you will need a more persistent storage mechanism than the in-memory data structure used in the previous example. Here is a high-level overview of how you can achieve this using file-based storage:

  1. Serialize and Deserialize Data: You can use Java`s Serialization mechanism to convert your cart data to a serialized form and save it to a file. When you want to retrieve the last viewed items, you can deserialize the data from the file.

  2. File I/O: Use Java`s File I/O operations to save and load data to/from a file.

Here is a modified version of the ShoppingCart class that includes methods for saving and loading the cart data to/from a file to retain the last viewed items:

java
import java.io.*; import java.util.ArrayList; import java.util.List; public class ShoppingCart { private List<String> cartItems = new ArrayList<>(); private List<String> lastViewedItems = new ArrayList<>(); private String cartFileName = "cart.ser"; // Name of the file to store cart data // Constructor to load cart data from file if available public ShoppingCart() { loadCartData(); } // Method to add an item to the cart public void addItemToCart(String item) { cartItems.add(item); saveCartData(); // Save cart data after adding an item } // Method to view an item in the cart (which also updates the last viewed items) public void viewItemInCart(String item) { if (cartItems.contains(item)) { lastViewedItems.clear(); // Clear the previous last viewed items lastViewedItems.add(item); // Add the current item as the last viewed item } } // Method to retrieve the last viewed items public List<String> getLastViewedItems() { return lastViewedItems; } // Save cart data to a file private void saveCartData() { try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(cartFileName))) { oos.writeObject(cartItems); } catch (IOException e) { e.printStackTrace(); } } // Load cart data from a file private void loadCartData() { try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(cartFileName))) { List<String> loadedCart = (List<String>) ois.readObject(); cartItems.addAll(loadedCart); } catch (IOException | ClassNotFoundException e) { // Handle exceptions if the file does not exist or cannot be read } } public static void main(String[] args) { ShoppingCart cart = new ShoppingCart(); cart.addItemToCart("Item 1"); cart.addItemToCart("Item 2"); cart.addItemToCart("Item 3"); // Viewing an item will set it as the last viewed item cart.viewItemInCart("Item 2"); // Retrieve and print the last viewed items List<String> lastViewedItems = cart.getLastViewedItems(); System.out.println("Last Viewed Items: " + lastViewedItems); } }

In this modified example, the loadCartData method is called in the constructor to load previously saved cart data when a new ShoppingCart object is created. The saveCartData method is called after adding an item to the cart to save the updated cart data to the file.

Please note that this example uses Java`s Serialization, and you should be cautious when using it in a production environment. Depending on your requirements, you may consider other serialization formats or databases for a more robust solution.


Search
Related Articles

Leave a Comment: