Skip to content

Instantly share code, notes, and snippets.

@mohamed-gara
mohamed-gara / Person.java
Last active March 25, 2022 04:45
Person class with properties, equals, hashcode and toString methods
public final class Person {
private final String firstName;
private final String lastName;
private final Integer age;
Person(String firstName, String lastName, Integer age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
@mohamed-gara
mohamed-gara / Person.java
Last active September 24, 2021 19:37
Person class without properties, equals, hashcode and toString methods
public class Person {
String firstName;
String lastName;
Integer age;
}
@RestController
@RequiredArgsConstructor
public class CustomArgumentResolverController {
private final PersonRepository personRepository;
@Fetch
public Person fetchPerson(@PathVariable String id) {
return personRepository.findBy(id);
}
@Test
public void should_patch_the_person_name() throws Exception {
when(personRepository.findBy(id)).thenReturn(personOldVersion());
mockMvc.perform(patchPersonName())
.andExpect(status().isOk());
verify(personRepository).save(personNewVersion());
}
@RestController
@RequiredArgsConstructor
public class PatchUsingModelAttributeController {
private final PersonRepository personRepository;
@ModelAttribute
public Person populatePerson(@PathVariable String id) {
return personRepository.findBy(id);
}
@Data
@Wither
@Builder
@AllArgsConstructor
public class PersonDto {
private String id;
private String name;
private Integer age;
}
@Data
@Wither
@Builder
@AllArgsConstructor
public class Person {
private String id;
private String name;
private Integer age;
}
import {Order} from './order';
import {OrderDiscount} from './order-discount';
export class Customer {
constructor(public orders: Order[]) {}
orderDiscount(): OrderDiscount {
return new OrderDiscount(this.orders.length);
}
import {OrderDiscount} from './order-discount';
export class Order {
constructor(public amount: number, public discount: OrderDiscount) {}
discountedAmount() {
return (100 - this.discount.discount()) * this.amount;
}
}
export class OrderDiscount {
constructor(private ordersNumber: number) {}
discount() {
if (this.ordersNumber > 5) {
return 10;
} else {
return 3;
}