From bb6715549b27ff8ece200ed2d0e37947526594ba Mon Sep 17 00:00:00 2001
From: mahboobe <mahboobe.shakerinadr@ucalgary.ca>
Date: Wed, 23 Oct 2024 13:42:19 -0600
Subject: [PATCH] class example added

---
 .gitignore        |   1 +
 employees.txt     |  15 +++++++
 src/Car.java      |  60 +++++++++++++++++++++++++++
 src/Employee.java |  25 +++++++++++
 src/Main.java     | 103 ++++++++++++++++++++++++++++++----------------
 5 files changed, 168 insertions(+), 36 deletions(-)
 create mode 100644 employees.txt
 create mode 100644 src/Car.java
 create mode 100644 src/Employee.java

diff --git a/.gitignore b/.gitignore
index d030669..3a23e49 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,7 @@ out/
 !**/src/test/**/out/
 .idea/
 .iml
+note.java
 ### Eclipse ###
 .apt_generated
 .classpath
diff --git a/employees.txt b/employees.txt
new file mode 100644
index 0000000..14c17d8
--- /dev/null
+++ b/employees.txt
@@ -0,0 +1,15 @@
+John Doe,25,50000
+Jane Smith,30,45000
+Sam Brown,17,35000
+Emily White,40,29000
+Robert Black,50,70000
+Alice Green,22,32000
+Michael Johnson,65,75000
+Susan Lee,45,52000
+David Turner,18,30000
+Carol King,27,41000
+Thomas Wright,34,60000
+Nancy Adams,28,33000
+Peter Clark,38,55000
+Sophia Harris,42,48000
+Daniel Lewis,55,80000
\ No newline at end of file
diff --git a/src/Car.java b/src/Car.java
new file mode 100644
index 0000000..0a75242
--- /dev/null
+++ b/src/Car.java
@@ -0,0 +1,60 @@
+public class Car {
+    private String brand;
+    private String model;
+    private int year;
+    private double price;
+    private static int df_year = 2000;
+    private static double df_price = 0.0;
+
+    public Car(String brand, String model, int year, double price) {
+        this.brand = brand;
+        this.model = model;
+        this.year = year;
+        if(price < 0)
+            this.price = df_price;
+        else
+            this.price = price;
+
+
+    }
+    public Car(String brand, String model, double price) {
+        this(brand,model,df_year, price);
+    }
+    public Car(String brand,String model, int year) {
+        this(brand,model,year,df_price);
+    }
+    public Car(String brand, String model) {
+        this(brand, model, df_year, df_price);
+    }
+
+
+
+    public String getBrand() {
+        return brand;
+    }
+    public String getModel() {
+        return model;
+    }
+    public int getYear() {
+        return year;
+    }
+    public double getPrice() {
+        return price;
+    }
+    public void printDetails() {
+        System.out.println("The car details : \n Brand: " + brand + ", Model: " + model + ", Year: " + year + ", Price: " + price);
+    }
+    public void setPrice(double price) {
+        if(price < 0) {
+            throw new IllegalArgumentException("Price cannot be negative.");
+
+        }
+        else{
+            this.price = price;
+        }
+
+    }
+
+
+
+}
diff --git a/src/Employee.java b/src/Employee.java
new file mode 100644
index 0000000..af7aa38
--- /dev/null
+++ b/src/Employee.java
@@ -0,0 +1,25 @@
+public class Employee {
+}
+
+/*
+You're given a file named employees.txt with employee details. Each line in the file is formatted as:
+
+EmployeeName,Age,Salary
+
+1.Create an Employee class that has:
+
+1.1 Private fields: name (String), age (int), and salary (double).
+1.2 A constructor to initialize these fields.
+1.3 Getter and Setter methods for each field.
+1.4 Add a method validateAge() in the Employee class. This method should throw an IllegalArgumentException if the age is less than 18 or more than 65.
+
+1.5 Add another method validateSalary() in the Employee class. This method should throw an IllegalArgumentException if the salary is less than 30000.
+
+2. Read the employees.txt file, and for each line:
+
+2.1 Create an Employee object.
+2.2 Use the setter methods to set the name, age, and salary. Ensure you use the validateAge() and validateSalary() methods when setting these values.
+2.3 Add the employee object to a list of employees.
+
+3. Finally, print out all the employees' names, ages, and salaries. Handle potential exceptions when reading from the file.
+ */
\ No newline at end of file
diff --git a/src/Main.java b/src/Main.java
index 384b5e1..480b229 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -8,6 +8,37 @@ import java.io.*;
 
 public class Main {
     public static void main(String[] args) {
+
+        Car mahboobeCar = new Car("Tesla", "T5", 2020, 50000);
+
+//        System.out.println(mahboobeCar.getBrand());
+//        System.out.println(mahboobeCar.getModel());
+//        System.out.println(mahboobeCar.getPrice());
+//        System.out.println(mahboobeCar.getYear());
+//        mahboobeCar.printDetails();
+        mahboobeCar.setPrice(40000);
+//        mahboobeCar.printDetails();
+        try {
+            mahboobeCar.setPrice(-10);
+        }catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
+        mahboobeCar.printDetails();
+        try {
+            Car testCar = new Car("Toyota", "LR4", 2024, -50000);
+            testCar.printDetails();
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
+
+        Car carWithoutYear = new Car("Toyota", "LR4",  50000.0);
+        carWithoutYear.printDetails();
+        Car test1 = new Car("Toyota", "LR4", 2024);
+        test1.printDetails();
+        Car test2 = new Car("Tesla", "LR4");
+        test2.printDetails();
+
+
 //         Exception Examples
 //        try {
 //            int[] grades = {97,78,80};
@@ -23,45 +54,45 @@ public class Main {
 //            System.out.println("EECEPTION : " + e.getMessage());
 //        }
 
-        String filename = "example.txt";
-        String data = "This is an example of writing to and reading from a file.\n the second line modified";
-
-        // Step 1: Create and write to the file
-        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
-            writer.write(data);
-            System.out.println("Data written to file: " + filename);
-        } catch (IOException e) {
-            e.printStackTrace();
-            System.out.println("Error writing to file: " + e.getMessage());
-        }
-//
+//        String filename = "example.txt";
+//        String data = "This is an example of writing to and reading from a file.\n the second line modified";
 //
+//        // Step 1: Create and write to the file
+//        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
+//            writer.write(data);
+//            System.out.println("Data written to file: " + filename);
+//        } catch (IOException e) {
+//            e.printStackTrace();
+//            System.out.println("Error writing to file: " + e.getMessage());
+//        }
 ////
-////        // Step 2: Close the file (automatically closed with try-with-resources)
-        filename ="C:\\Users\\mahbo\\OneDrive - University of Calgary\\CPSC219\\example2.txt";
-        // Step 3: Reopen and read from the file
-        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
-            String line;
-            System.out.println("Reading from file: " + filename);
-            while ((line = reader.readLine()) != null) {
-                System.out.println(line);
-            }
-        } catch (IOException e) {
-            e.printStackTrace();
-            System.out.println("Error reading from file: " + e.getMessage());
-        }
+////
+//////
+//////        // Step 2: Close the file (automatically closed with try-with-resources)
+//        filename ="C:\\Users\\mahbo\\OneDrive - University of Calgary\\CPSC219\\example2.txt";
+//        // Step 3: Reopen and read from the file
+//        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
+//            String line;
+//            System.out.println("Reading from file: " + filename);
+//            while ((line = reader.readLine()) != null) {
+//                System.out.println(line);
+//            }
+//        } catch (IOException e) {
+//            e.printStackTrace();
+//            System.out.println("Error reading from file: " + e.getMessage());
+//        }
+////
+//        data = "\n Writing this massage as an append";
+//
+//        // Step 1: Create and write to the file
+//        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename, true))) {
+//            writer.write(data);
+//            System.out.println("Data written to file: " + filename);
+//        } catch (IOException e) {
+//            System.out.println("Error writing to file: " + e.getMessage());
+//        }
+//
 //
-        data = "\n Writing this massage as an append";
-
-        // Step 1: Create and write to the file
-        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename, true))) {
-            writer.write(data);
-            System.out.println("Data written to file: " + filename);
-        } catch (IOException e) {
-            System.out.println("Error writing to file: " + e.getMessage());
-        }
-
-
 
 
 
-- 
GitLab