Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lab-java-standard-input-and-classes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
.kotlin

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
10 changes: 10 additions & 0 deletions lab-java-standard-input-and-classes/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lab-java-standard-input-and-classes/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions lab-java-standard-input-and-classes/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lab-java-standard-input-and-classes/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions lab-java-standard-input-and-classes/employees.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Name: Alice Smith, Email: [email protected], Age: 30, Salary: 70000.0
Name: Bob Jones, Email: [email protected], Age: 35, Salary: 80000.0
Name: Charlie Brown, Email: [email protected], Age: 28, Salary: 60000.0
Name: Dana White, Email: [email protected], Age: 40, Salary: 75000.0
Name: Eve Adams, Email: [email protected], Age: 45, Salary: 90000.0
Name: Frank Wright, Email: [email protected], Age: 50, Salary: 85000.0
Name: Grace Lee, Email: [email protected], Age: 32, Salary: 72000.0
Name: Harry Styles, Email: [email protected], Age: 27, Salary: 68000.0
Name: Ivy Green, Email: [email protected], Age: 22, Salary: 20000.0
Name: Jack Frost, Email: [email protected], Age: 24, Salary: 15000.0
17 changes: 17 additions & 0 deletions lab-java-standard-input-and-classes/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>lab-java-standard-input-and-classes</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.example;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Employee {
protected String name;
protected String email;
protected int age;
protected double salary;

public Employee(String name, String email, int age, double salary) {
this.name = name;
this.email = email;
this.age = age;
this.salary = salary;
}

public void setAge(int age){
if (age <= 18){
System.err.println("Age must be 18 or over!");
return;
}
this.age = age;
}
public void setSalary(double salary){
if (salary < 0){
System.err.println("Salary must be positive!");
return;
}
this.salary = salary;
}
public void setTitle(String name){
if(name == null || name.isBlank()){
System.out.println(" [REJECTED] Name cannot be null or blank.");
return;
}
this.name = name;
}
public void setEmail(String email){
if(isValidEmail(email)){
System.out.println(" [REJECTED] Please enter a valid email adress.");
return;
}
this.email = email;
}
public int getAge(){
return age;
}
public String getEmail(){
return email;
}
public String getName(){ return name; }
public double getSalary(){
return salary;
}

public boolean isValidEmail(String email) {
String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
Pattern pattern = Pattern.compile(emailRegex);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
@Override
public String toString() {
return "Name: " + name + ", Email: " + email + ", Age: " + age + ", Salary: " + salary;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.example;

public class Intern extends Employee{
private static final double MAX_SALARY = 20000;

public Intern(String name, String email, int age, double salary) {
super(name, email, age, validateSalary(salary));
}

public static double validateSalary(double salary){
if (salary < 0){
throw new IllegalArgumentException("Salary must be positive1");
} else if (salary > MAX_SALARY) {
throw new IllegalArgumentException("Salary exceeds the maximum intern salary limit of " + MAX_SALARY);
}
return salary;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.example;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
public static void main(String[] args) {
Employee employee = new Employee("Susan", "[email protected]", 23, 55000);
System.out.println(employee);

Intern intern = new Intern("Alice", "[email protected]", 19, 25000);
System.out.println(intern);

Employee[] employees = {
new Employee("Alice Smith", "[email protected]", 30, 70000),
new Employee("Bob Jones", "[email protected]", 35, 80000),
new Employee("Charlie Brown", "[email protected]", 28, 60000),
new Employee("Dana White", "[email protected]", 40, 75000),
new Employee("Eve Adams", "[email protected]", 45, 90000),
new Employee("Frank Wright", "[email protected]", 50, 85000),
new Employee("Grace Lee", "[email protected]", 32, 72000),
new Employee("Harry Styles", "[email protected]", 27, 68000),
new Intern("Ivy Green", "[email protected]", 22, 20000), // Intern
new Intern("Jack Frost", "[email protected]", 24, 15000), // Intern
};
try (BufferedWriter writer = new BufferedWriter(new FileWriter("employees.txt"))) {
for (Employee emp : employees) {
writer.write(emp.toString() + "\n");
}
} catch (IOException e) {
System.err.println("An error occurred while writing to the file: " + e.getMessage());
}
}
}