Skip to content
Snippets Groups Projects
Commit bb671554 authored by Mahboobe Shakeri's avatar Mahboobe Shakeri
Browse files

class example added

parent 9ece034c
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@ out/
!**/src/test/**/out/
.idea/
.iml
note.java
### Eclipse ###
.apt_generated
.classpath
......
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
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;
}
}
}
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
......@@ -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());
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment