[text] nnb

Viewer

  1. 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:
  2.  
  3. ```java
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7. class Product {
  8.     private String name;
  9.     private int quantity;
  10.     private double price;
  11.  
  12.     public Product(String name, int quantity, double price) {
  13.         this.name = name;
  14.         this.quantity = quantity;
  15.         this.price = price;
  16.     }
  17.  
  18.     @Override
  19.     public String toString() {
  20.         return "Name: " + name + ", Quantity: " + quantity + ", Price: " + price;
  21.     }
  22. }
  23.  
  24. class WriteTextFile {
  25.     private PrintWriter writer;
  26.  
  27.     public void openFile(String fileName) {
  28.         try {
  29.             writer = new PrintWriter(new FileWriter(fileName));
  30.         } catch (IOException e) {
  31.             System.out.println("Error opening the file: " + e.getMessage());
  32.         }
  33.     }
  34.  
  35.     public void writeFile(Product p) {
  36.         try {
  37.             writer.println(p.toString());
  38.         } catch (Exception e) {
  39.             System.out.println("Error writing to the file: " + e.getMessage());
  40.         }
  41.     }
  42.  
  43.     public void closeFile() {
  44.         try {
  45.             writer.close();
  46.         } catch (Exception e) {
  47.             System.out.println("Error closing the file: " + e.getMessage());
  48.         }
  49.     }
  50. }
  51.  
  52. class ReadTextFile {
  53.     private Scanner scanner;
  54.  
  55.     public void openFile(String fileName) {
  56.         try {
  57.             scanner = new Scanner(new File(fileName));
  58.         } catch (FileNotFoundException e) {
  59.             System.out.println("File not found: " + e.getMessage());
  60.         } catch (SecurityException e) {
  61.             System.out.println("Security exception: " + e.getMessage());
  62.         }
  63.     }
  64.  
  65.     public void readFile() {
  66.         try {
  67.             while (scanner.hasNextLine()) {
  68.                 String line = scanner.nextLine();
  69.                 System.out.println(line);
  70.             }
  71.         } catch (NoSuchElementException e) {
  72.             System.out.println("Error reading from the file: " + e.getMessage());
  73.         } catch (IllegalStateException e) {
  74.             System.out.println("Illegal state: " + e.getMessage());
  75.         }
  76.     }
  77.  
  78.     public void closeFile() {
  79.         try {
  80.             scanner.close();
  81.         } catch (Exception e) {
  82.             System.out.println("Error closing the file: " + e.getMessage());
  83.         }
  84.     }
  85. }
  86.  
  87. public class Main {
  88.     public static void main(String[] args) {
  89.         Product product1 = new Product("Product 1", 10, 9.99);
  90.         Product product2 = new Product("Product 2", 5, 19.99);
  91.         Product product3 = new Product("Product 3", 3, 4.99);
  92.  
  93.         WriteTextFile writer = new WriteTextFile();
  94.         writer.openFile("ProductTextFile.txt");
  95.         writer.writeFile(product1);
  96.         writer.writeFile(product2);
  97.         writer.writeFile(product3);
  98.         writer.closeFile();
  99.  
  100.         ReadTextFile reader = new ReadTextFile();
  101.         reader.openFile("ProductTextFile.txt");
  102.         reader.readFile();
  103.         reader.closeFile();
  104.     }
  105. }
  106. ```
  107.  
  108. 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.
  109.  
  110. 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.
  111.  
  112. Note: Make sure to have the appropriate file write and read permissions for the directory where you run the program.

Editor

You can edit this paste and save as new:


File Description
  • nnb
  • Paste Code
  • 29 Apr-2024
  • 3.72 Kb
You can Share it: