Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kenan Turgay #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
# Java Spring Boot Dependency Injection

### Proje Kurulumu

Projeyi öncelikle forklayın ve clone edin.
Daha sonra projeyi IntellijIDEA kullanarak açınız. README.md dosyasını dikkatli bir şekilde okuyarak istenenleri yapmaya çalışın.
Proje sayımız ilerledikçe proje yönetimimizi kolaylaştırmak adına projelerimizi belli klasör kalıplarında saklamak işimizi kolaylaştırmak adına iyi bir alışkanlıktır.
Örnek bir Lokasyon: Workintech/Sprint_1/Etud.
## Hedeflerimiz:

### Hedeflerimiz:

### Spring Boot Dependency Injection
## Spring Boot Dependency Injection

### Görev 1
* Maven dependency management sistemini kullanarak tüm dependencyleri install edin.
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/workintech/s17d2/dto/DeveloperResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.workintech.s17d2.dto;

import com.workintech.s17d2.model.Developer;

public class DeveloperResponse {

private Developer developer;

private int status;
private String message;
public DeveloperResponse(Developer developer, int status, String message) {
this.developer = developer;
this.status = status;
this.message = message;
}

public Developer getDeveloper() {
return developer;
}

public void setDeveloper(Developer developer) {
this.developer = developer;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}
}
58 changes: 58 additions & 0 deletions src/main/java/com/workintech/s17d2/model/Developer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.workintech.s17d2.model;

public class Developer {

private int id;
private String name;
private double salary;
private Experience experience;

public Developer(int id, String name, double salary, Experience experience) {
this.id = id;
this.name = name;
this.salary = salary;
this.experience = experience;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}

public Experience getExperience() {
return experience;
}

public void setExperience(Experience experience) {
this.experience = experience;
}

@Override
public String toString() {
return "Developer{" +
"id=" + id +
", name='" + name + '\'' +
", salary=" + salary +
", experience=" + experience +
'}';
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/workintech/s17d2/model/DeveloperFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.workintech.s17d2.model;

import com.workintech.s17d2.tax.Taxable;

public class DeveloperFactory {
public static Developer createDeveloper(Developer developer, Taxable taxable){
Developer createdDeveloper = null;

if(developer.getExperience().equals(Experience.JUNIOR)){
createdDeveloper = new JuniorDeveloper(developer.getId(), developer.getName(), developer.getSalary()-(developer.getSalary()*taxable.getSimpleTaxRate())/100);
} else if (developer.getExperience().equals(Experience.MID)) {

createdDeveloper = new MidDeveloper(developer.getId(), developer.getName(), developer.getSalary()-(developer.getSalary()*taxable.getMiddleTaxRate())/100);

} else if (developer.getExperience().equals(Experience.SENIOR)) {
createdDeveloper = new MidDeveloper(developer.getId(), developer.getName(), developer.getSalary()-(developer.getSalary()*taxable.getUpperTaxRate())/100);
}else {
System.out.println("unknown experience");
return null;
}

return createdDeveloper;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/workintech/s17d2/model/Experience.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.workintech.s17d2.model;

public enum Experience {

JUNIOR,
MID,
SENIOR
}
7 changes: 7 additions & 0 deletions src/main/java/com/workintech/s17d2/model/JuniorDeveloper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.workintech.s17d2.model;

public class JuniorDeveloper extends Developer{
public JuniorDeveloper(int id, String name, double salary) {
super(id, name, salary, Experience.JUNIOR);
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/workintech/s17d2/model/MidDeveloper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.workintech.s17d2.model;

public class MidDeveloper extends Developer{
public MidDeveloper(int id, String name, double salary) {
super(id, name, salary, Experience.MID);
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/workintech/s17d2/model/SeniorDeveloper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.workintech.s17d2.model;

public class SeniorDeveloper extends Developer{
public SeniorDeveloper(int id, String name, double salary) {
super(id, name, salary, Experience.SENIOR);
}
}
92 changes: 92 additions & 0 deletions src/main/java/com/workintech/s17d2/rest/DeveloperController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.workintech.s17d2.rest;

import com.workintech.s17d2.dto.DeveloperResponse;
import com.workintech.s17d2.model.Developer;
import com.workintech.s17d2.model.DeveloperFactory;
import com.workintech.s17d2.model.Experience;
import com.workintech.s17d2.model.SeniorDeveloper;
import com.workintech.s17d2.tax.Taxable;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController // Component olduğu için bunu ekleyip dependency injection yapıyoruz.
@RequestMapping("/developers")

public class DeveloperController {

public Map<Integer, Developer> developers;

private Taxable taxable;
@Autowired
public DeveloperController( Taxable taxable) {
this.taxable = taxable;
}


@PostConstruct
public void init(){
System.out.println("Load all developers");
this.developers=new HashMap<>();
this.developers.put(1,new SeniorDeveloper(1,"Kenan",97000d));


}




@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public DeveloperResponse save(@RequestBody Developer developer){
Developer createdDeveloper = DeveloperFactory.createDeveloper(developer,taxable);
if(Objects.nonNull(createdDeveloper)){
developers.put(createdDeveloper.getId(),createdDeveloper);
}

return new DeveloperResponse(createdDeveloper,HttpStatus.CREATED.value(), "Developer created");

}

@GetMapping

public List<Developer> getAll(){
return developers.values().stream().toList().stream().toList();
}

@GetMapping("/{id}")
public DeveloperResponse getById(@PathVariable("id") int id){
Developer foundDeveloper = this.developers.get(id);
if(foundDeveloper == null){
return new DeveloperResponse(null,HttpStatus.NOT_FOUND.value(), "Developer not found");
}

return new DeveloperResponse(foundDeveloper,HttpStatus.OK.value(), "Developer found");
}

@PutMapping("/{id}")
public DeveloperResponse update(@PathVariable("id") int id, @RequestBody Developer developer){

developer.setId(id);
Developer newDeveloper= DeveloperFactory.createDeveloper(developer,taxable);
this.developers.put(id,newDeveloper);
return new DeveloperResponse(newDeveloper,HttpStatus.OK.value(), "Developer updated");
}

@DeleteMapping("/{id}")
public DeveloperResponse delete(@PathVariable("id") int id){
Developer developer = this.developers.get(id);
if(developer == null){
return new DeveloperResponse(null,HttpStatus.NOT_FOUND.value(), "Developer not found");
}

this.developers.remove(id);
return new DeveloperResponse(developer,HttpStatus.NO_CONTENT.value(), "Developer deleted");
}

}
21 changes: 21 additions & 0 deletions src/main/java/com/workintech/s17d2/tax/DeveloperTax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.workintech.s17d2.tax;

import org.springframework.stereotype.Component;

@Component // DeveloperTax must be component after that you can call it with Dependency Injection
public class DeveloperTax implements Taxable{
@Override
public double getSimpleTaxRate() {
return 15d;
}

@Override
public double getMiddleTaxRate() {
return 25d;
}

@Override
public double getUpperTaxRate() {
return 35d;
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/workintech/s17d2/tax/Taxable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.workintech.s17d2.tax;

public interface Taxable {

double getSimpleTaxRate();

double getMiddleTaxRate();

double getUpperTaxRate();
}
19 changes: 19 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
spring.application.name=fsweb-s17d2

server.servlet.context-path=/workintech

server.port=8585

spring.devtools.restart.quite-period=1000
spring.devtools.restart.poll-interval=2000

management.info.env.enabled=true
management.endpoints.web.exposure.include=info,health,mappings
info.app.name=S7D2 dependency injection
info.app.description=workintech workspace
info.app.version=v1

project.developer.fullname=Kenan Turgay
course.name=java-parttime
#Actuator


2 changes: 1 addition & 1 deletion src/test/java/com/workintech/s17d2/ResultAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void afterAll(ExtensionContext context) throws Exception {
long failure = summary.get(TestResultStatus.FAILED) != null ? summary.get(TestResultStatus.FAILED) : 0;

double score = (double) success / (success + failure);
String userId = "999999";
String userId = "179820";

JSONObject json = new JSONObject();
json.put("score", score);
Expand Down