-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41f9765
commit 5664e82
Showing
12 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.3.3.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>ru.sysout</groupId> | ||
<artifactId>inherit2</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>inherit2</name> | ||
|
||
<properties> | ||
<java.version>11</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
12 changes: 12 additions & 0 deletions
12
hibernate-inheritance2/src/main/java/ru/sysout/SpringDataJpaApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ru.sysout; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class SpringDataJpaApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(SpringDataJpaApplication.class, args); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
hibernate-inheritance2/src/main/java/ru/sysout/dao/CustomerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ru.sysout.dao; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import ru.sysout.model.Customer; | ||
|
||
public interface CustomerRepository extends JpaRepository<Customer, Long> { | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
hibernate-inheritance2/src/main/java/ru/sysout/model/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ru.sysout.model; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.*; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@Entity | ||
@Inheritance | ||
public class Customer { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE) | ||
private long id; | ||
private String name; | ||
} |
11 changes: 11 additions & 0 deletions
11
hibernate-inheritance2/src/main/java/ru/sysout/model/EmployeeCustomer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ru.sysout.model; | ||
|
||
import lombok.Data; | ||
|
||
import javax.persistence.Entity; | ||
|
||
@Data | ||
@Entity | ||
public class EmployeeCustomer extends Customer { | ||
private int monthsInCompany; | ||
} |
11 changes: 11 additions & 0 deletions
11
hibernate-inheritance2/src/main/java/ru/sysout/model/ExternalCustomer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ru.sysout.model; | ||
|
||
import lombok.Data; | ||
|
||
import javax.persistence.Entity; | ||
|
||
@Data | ||
@Entity | ||
public class ExternalCustomer extends Customer { | ||
private long sum; | ||
} |
6 changes: 6 additions & 0 deletions
6
hibernate-inheritance2/src/main/java/ru/sysout/service/DiscountCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package ru.sysout.service; | ||
|
||
public interface DiscountCalculator<T> { | ||
double calculate(T customer); | ||
Class<T> getClazz(); | ||
} |
18 changes: 18 additions & 0 deletions
18
...nate-inheritance2/src/main/java/ru/sysout/service/EmployeeCustomerDiscountCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ru.sysout.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import ru.sysout.model.EmployeeCustomer; | ||
|
||
@Service | ||
public class EmployeeCustomerDiscountCalculator implements DiscountCalculator<EmployeeCustomer> { | ||
@Override | ||
public double calculate(EmployeeCustomer customer) { | ||
if (customer.getMonthsInCompany() > 12) | ||
return 0.1; | ||
return 0.05; | ||
} | ||
|
||
public Class getClazz(){ | ||
return EmployeeCustomer.class; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...nate-inheritance2/src/main/java/ru/sysout/service/ExternalCustomerDiscountCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package ru.sysout.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import ru.sysout.model.ExternalCustomer; | ||
|
||
@Service | ||
public class ExternalCustomerDiscountCalculator implements DiscountCalculator<ExternalCustomer> { | ||
@Override | ||
public double calculate(ExternalCustomer customer) { | ||
if (customer.getSum() > 100) | ||
return 0.01; | ||
return 0.05; | ||
} | ||
public Class getClazz(){ | ||
return ExternalCustomer.class; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
hibernate-inheritance2/src/main/java/ru/sysout/service/MakeDiscountsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ru.sysout.service; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import ru.sysout.dao.CustomerRepository; | ||
import ru.sysout.model.Customer; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Service | ||
public class MakeDiscountsService { | ||
@Autowired | ||
private CustomerRepository customerRepository; | ||
@Autowired | ||
private List<DiscountCalculator> calculators; | ||
|
||
private Map<Class, DiscountCalculator> map = new HashMap<>(); | ||
|
||
@PostConstruct | ||
private void init() { | ||
for (DiscountCalculator discountCalculator : calculators) { | ||
map.put(discountCalculator.getClazz(), discountCalculator); | ||
} | ||
} | ||
|
||
public void makeDiscounts() { | ||
List<Customer> customers = customerRepository.findAll(); | ||
for (Customer customer : customers) { | ||
DiscountCalculator discountCalculator = map.get(customer.getClass()); | ||
double discount = discountCalculator.calculate(customer); | ||
System.out.println(discount); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
spring: | ||
h2: | ||
console: | ||
enabled: true | ||
path: /h2-console | ||
datasource: | ||
url: jdbc:h2:mem:testdb | ||
username: sa | ||
password: | ||
initialization-mode: never | ||
jpa: | ||
show-sql: true | ||
hibernate: | ||
ddl-auto: create |
41 changes: 41 additions & 0 deletions
41
hibernate-inheritance2/src/test/java/ru/sysout/CustomerCalculatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ru.sysout; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.context.annotation.Import; | ||
import ru.sysout.dao.CustomerRepository; | ||
import ru.sysout.model.EmployeeCustomer; | ||
import ru.sysout.model.ExternalCustomer; | ||
import ru.sysout.service.EmployeeCustomerDiscountCalculator; | ||
import ru.sysout.service.ExternalCustomerDiscountCalculator; | ||
import ru.sysout.service.MakeDiscountsService; | ||
|
||
@DataJpaTest | ||
@Import({MakeDiscountsService.class, EmployeeCustomerDiscountCalculator.class, ExternalCustomerDiscountCalculator.class}) | ||
public class CustomerCalculatorTest { | ||
@Autowired | ||
private CustomerRepository customerRepository; | ||
|
||
@Autowired | ||
private MakeDiscountsService makeDiscountsService; | ||
|
||
@BeforeEach | ||
private void init() { | ||
EmployeeCustomer employeeCustomer = new EmployeeCustomer(); | ||
employeeCustomer.setMonthsInCompany(10); | ||
employeeCustomer.setName("Petr"); | ||
customerRepository.save(employeeCustomer); | ||
|
||
ExternalCustomer externalCustomer = new ExternalCustomer(); | ||
externalCustomer.setSum(110); | ||
externalCustomer.setName("Vasya"); | ||
customerRepository.save(externalCustomer); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
makeDiscountsService.makeDiscounts(); | ||
} | ||
} |