- https://visionacademy.online/en/courses/742-mbady-nthm-otkny-almaalomat-Certainly! Below is an example implementation of the classes you described, including the main method:
- ```java
- import java.io.*;
- import java.util.*;
- class Product {
- private String name;
- private int quantity;
- private double price;
- public Product(String name, int quantity, double price) {
- this.name = name;
- this.quantity = quantity;
- this.price = price;
- }
- @Override
- public String toString() {
- return "Name: " + name + ", Quantity: " + quantity + ", Price: " + price;
- }
- }
- class WriteTextFile {
- private PrintWriter writer;
- public void openFile(String fileName) {
- try {
- writer = new PrintWriter(new FileWriter(fileName));
- } catch (IOException e) {
- System.out.println("Error opening the file: " + e.getMessage());
- }
- }
- public void writeFile(Product p) {
- try {
- writer.println(p.toString());
- } catch (Exception e) {
- System.out.println("Error writing to the file: " + e.getMessage());
- }
- }
- public void closeFile() {
- try {
- writer.close();
- } catch (Exception e) {
- System.out.println("Error closing the file: " + e.getMessage());
- }
- }
- }
- class ReadTextFile {
- private Scanner scanner;
- public void openFile(String fileName) {
- try {
- scanner = new Scanner(new File(fileName));
- } catch (FileNotFoundException e) {
- System.out.println("File not found: " + e.getMessage());
- } catch (SecurityException e) {
- System.out.println("Security exception: " + e.getMessage());
- }
- }
- public void readFile() {
- try {
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine();
- System.out.println(line);
- }
- } catch (NoSuchElementException e) {
- System.out.println("Error reading from the file: " + e.getMessage());
- } catch (IllegalStateException e) {
- System.out.println("Illegal state: " + e.getMessage());
- }
- }
- public void closeFile() {
- try {
- scanner.close();
- } catch (Exception e) {
- System.out.println("Error closing the file: " + e.getMessage());
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- Product product1 = new Product("Product 1", 10, 9.99);
- Product product2 = new Product("Product 2", 5, 19.99);
- Product product3 = new Product("Product 3", 3, 4.99);
- WriteTextFile writer = new WriteTextFile();
- writer.openFile("ProductTextFile.txt");
- writer.writeFile(product1);
- writer.writeFile(product2);
- writer.writeFile(product3);
- writer.closeFile();
- ReadTextFile reader = new ReadTextFile();
- reader.openFile("ProductTextFile.txt");
- reader.readFile();
- reader.closeFile();
- }
- }
- ```
- The code above creates a `Product` class with a constructor and an overridden `toString()` method. It then defines the `WriteTextFile` class with methods for opening, writing, and closing a text file. Similarly, the `ReadTextFile` class has methods for opening, reading, and closing a text file.
- In the `main` method, three `Product` objects are created, an instance of `WriteTextFile` is created to write the objects' information to a file, and an instance of `ReadTextFile` is created to read the data from the file sequentially.
- Note: Make sure to have the appropriate file write and read permissions for the directory where you run the program.
[text] nnb
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: