Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Tutoria_Examples_T03
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package registry
Container Registry
Operate
Terraform modules
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mahboobe Shakeri
Tutoria_Examples_T03
Commits
bb671554
Commit
bb671554
authored
5 months ago
by
Mahboobe Shakeri
Browse files
Options
Downloads
Patches
Plain Diff
class example added
parent
9ece034c
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
employees.txt
+15
-0
15 additions, 0 deletions
employees.txt
src/Car.java
+60
-0
60 additions, 0 deletions
src/Car.java
src/Employee.java
+25
-0
25 additions, 0 deletions
src/Employee.java
src/Main.java
+67
-36
67 additions, 36 deletions
src/Main.java
with
168 additions
and
36 deletions
.gitignore
+
1
−
0
View file @
bb671554
...
...
@@ -4,6 +4,7 @@ out/
!**/src/test/**/out/
.idea/
.iml
note.java
### Eclipse ###
.apt_generated
.classpath
...
...
This diff is collapsed.
Click to expand it.
employees.txt
0 → 100644
+
15
−
0
View file @
bb671554
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
This diff is collapsed.
Click to expand it.
src/Car.java
0 → 100644
+
60
−
0
View file @
bb671554
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
;
}
}
}
This diff is collapsed.
Click to expand it.
src/Employee.java
0 → 100644
+
25
−
0
View file @
bb671554
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
This diff is collapsed.
Click to expand it.
src/Main.java
+
67
−
36
View file @
bb671554
...
...
@@ -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
());
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment