#512 Code Review of Organization Creation
Complete all subexercises from 1 through 7:
// 1. Add the missing annotations
public class OrganizationController {
private final OrganizationService organizationService;
public void create(List<Organization> organizations) {
organizationService.create(organizations);
}
}
public interface OrganizationService {
void create(List<Organization> organizations);
}
public class OrganizationServiceImpl implements OrganizationService {
@Override
public void create(List<Organization> organizations) {
// Validate organization attributes
validate(organizations);
// Map organizations to entities
Set<OrganizationEntity> entities = map(organizations);
// Save organizations to the database
save(entities);
}
private void validate(List<Organization> organizations) {
// 2. Check that the organization list is not empty
// 3. Check that no organization has no accounts
// 4. Count the total number of accounts across all organizations
// and check that the number of accounts is not greater than 10
// 5. For every account, check that
// - the account is not empty
// - the account contains only digits
}
private Set<OrganizationEntity> map(List<Organizatio> organizations) {
// 6. Remove duplicate accounts
// Organization(accounts=[1, 1]) -> Organization(accounts=[1])
// 7. Build a set of entities from the organization list
// Organization(accounts=[1, 2]) -> [OrganizationEntity(account=1), OrganizationEntity(account=2)]
return null;
}
private void save(Set<OrganizationEntity> entities) {
}
}
@Getter
@Setter
public class Organization {
/** Accounts */
private List<String> accounts;
}
@Getter
@Setter
@Entity
@Table(name = "ORGANIZATION")
@EqualsAndHashCode(of = "account")
public class OrganizationEntity {
/** Record identifier */
@Id
@GeneratedValue
@Column(name = "ID")
private UUID id;
/** Account */
@Column(name = "ACCOUNT")
private Integer account;
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor