Skip to content

Commit

Permalink
Starting point
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Oct 1, 2014
1 parent 40a074e commit a301646
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 112 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package flyinghigh.services.accounts.domain;

import org.springframework.data.annotation.Id;

import java.lang.String;

public class FrequentFlyerMember {
@Id
private String id;
private String accountNumber;
private String firstName;
private String lastName;
private String homeAirportCode;
private int statusPoints;
private Status status = Status.Bronze;

public FrequentFlyerMember() {
}

public FrequentFlyerMember(String id,
String accountNumber,
String firstName,
String lastName) {
this.id = id;
this.accountNumber = accountNumber;
this.firstName = firstName;
this.lastName = lastName;
this.statusPoints = 0;
}

protected FrequentFlyerMember(String id,
String accountNumber,
String firstName,
String lastName,
String homeAirportCode,
int statusPoints,
Status status) {
this.id = id;
this.accountNumber = accountNumber;
this.firstName = firstName;
this.lastName = lastName;
this.homeAirportCode = homeAirportCode;
this.statusPoints = statusPoints;
this.status = status;
}

public FrequentFlyerMember(String accountNumber, String firstName, String lastName) {
this.accountNumber = accountNumber;
this.firstName = firstName;
this.lastName = lastName;
this.statusPoints = 0;
}

public FrequentFlyerMember(String accountNumber, String firstName, String lastName, int statusPoints, String homeAirportCode) {
this.accountNumber = accountNumber;
this.firstName = firstName;
this.lastName = lastName;
this.statusPoints = statusPoints;
this.homeAirportCode = homeAirportCode;
}

public String getId() {
return id;
}

public int getStatusPoints() {
return statusPoints;
}

protected void setStatusPoints(int statusPoints) {
this.statusPoints = statusPoints;
}

protected void setStatus(Status status) {
this.status = status;
}

public String getAccountNumber() {
return accountNumber;
}

public java.lang.String getLastName() {
return lastName;
}

public String getFirstName() {
return firstName;
}

public String getHomeAirportCode() {
return homeAirportCode;
}

public Status getStatus() {
return status;
}

public FrequentFlyerMember withStatusPoints(int statusPoints) {
return new FrequentFlyerMember(id, accountNumber, firstName, lastName, homeAirportCode, statusPoints, status);
}
public FrequentFlyerMember withStatus(Status status) {
return new FrequentFlyerMember(id, accountNumber, firstName, lastName, homeAirportCode, statusPoints, status);
}

public Earner earns(int amount) {
return new Earner(amount, this);
}

public static class Earner {
private final int amount;

private final FrequentFlyerMember account;

public Earner(int amount, FrequentFlyerMember account) {
this.amount = amount;
this.account = account;
}

public void statusPoints() {
account.setStatusPoints(account.getStatusPoints() + amount);
account.setStatus(Status.statusLevelFor(account.getStatusPoints()));
}
}


public static FrequentFlyerBuilder withFrequentFlyerNumber(String frequentFlyerNumber) {
return new FrequentFlyerBuilder(frequentFlyerNumber);
}

public static class FrequentFlyerBuilder {

private String frequentFlyerNumber;

public FrequentFlyerBuilder(String frequentFlyerNumber) {
this.frequentFlyerNumber = frequentFlyerNumber;
}

public FrequentFlyerMember named(String firstName, String lastName) {
return new FrequentFlyerMember(frequentFlyerNumber, firstName, lastName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package flyinghigh.services.accounts.domain;

import java.util.Arrays;
import java.util.Optional;

public enum Status {

Bronze(0), Silver(300), Gold(700), Platinum(1500);

private final int minimumPoints;

Status(int minimumPoints) {
this.minimumPoints = minimumPoints;
}

public static Status statusLevelFor(int points) {
Optional<Status> matchingStatus =
Arrays.asList(Status.values())
.stream()
.sorted(Status::decendingMinimumPoints)
.filter(status -> points >= status.getMinimumPoints())
.findFirst();

return matchingStatus.orElse(Bronze);
}

public int getMinimumPoints() {
return minimumPoints;
}

private static int decendingMinimumPoints(Status status1, Status status2) {
return status2.getMinimumPoints() - status1.getMinimumPoints();
}


}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package flyinghigh.services.accounts.repositories;

import flyinghigh.services.accounts.domain.FrequentFlyerAccount;
import flyinghigh.services.accounts.domain.FrequentFlyerMember;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.List;

@RepositoryRestResource(collectionResourceRel = "accounts", path = "accounts")
public interface AccountRepository extends MongoRepository<FrequentFlyerAccount, String> {
FrequentFlyerAccount findByAccountNumber(@Param("number") String number);
public interface AccountRepository extends MongoRepository<FrequentFlyerMember, String> {
FrequentFlyerMember findByAccountNumber(@Param("number") String number);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package flyinghigh.services.accounts.services.database;

import com.google.common.collect.ImmutableList;
import flyinghigh.services.accounts.domain.FrequentFlyerAccount;
import flyinghigh.services.accounts.domain.FrequentFlyerMember;
import flyinghigh.services.accounts.repositories.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -11,10 +11,10 @@
@Component
public class DatabaseSetupImpl implements DatabaseSetup {

private final static List<FrequentFlyerAccount> DEFAULT_ACCOUNTS = ImmutableList.of(
new FrequentFlyerAccount("123456","Sarah-Jane","Smith",500,"SYD"),
new FrequentFlyerAccount("123457","Harry","Sullivan",1000,"SYD"),
new FrequentFlyerAccount("123458","Jo","Grant", 2000,"SYD")
private final static List<FrequentFlyerMember> DEFAULT_ACCOUNTS = ImmutableList.of(
new FrequentFlyerMember("123456","Sarah-Jane","Smith",500,"SYD"),
new FrequentFlyerMember("123457","Harry","Sullivan",1000,"SYD"),
new FrequentFlyerMember("123458","Jo","Grant", 2000,"SYD")
);

private final AccountRepository accountRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package flyinghigh.services.accounts.web;

import com.google.common.collect.ImmutableList;
import flyinghigh.services.accounts.domain.FrequentFlyerAccount;
import flyinghigh.services.accounts.domain.FrequentFlyerMember;
import flyinghigh.services.accounts.repositories.AccountRepository;
import flyinghigh.services.accounts.services.database.DatabaseSetup;
import flyinghigh.services.accounts.services.destinations.DestinationsCalculatorService;
Expand All @@ -27,12 +26,12 @@ public class AccountsController {
private DestinationsCalculatorService destinationsCalculatorService;

@RequestMapping(method = RequestMethod.GET, value = "/{number}")
public FrequentFlyerAccount viewAccount(@PathVariable String number) {
public FrequentFlyerMember viewAccount(@PathVariable String number) {
return accountRepository.findByAccountNumber(number);
}

@RequestMapping(method = RequestMethod.GET, value = "")
public List<FrequentFlyerAccount> listAllAccounts() {
public List<FrequentFlyerMember> listAllAccounts() {
return accountRepository.findAll();
}

Expand All @@ -43,7 +42,7 @@ public void initializeAccounts() {

@RequestMapping(method = RequestMethod.GET, value = "/{number}/possibleDestinations")
public List<String> findPossibleDestinations(@PathVariable String number) {
FrequentFlyerAccount currentAccount = accountRepository.findByAccountNumber(number);
FrequentFlyerMember currentAccount = accountRepository.findByAccountNumber(number);
return destinationsCalculatorService.findPossibleDestinations(currentAccount.getHomeAirportCode(), currentAccount.getStatusPoints());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package flyinghigh.services.flights.domain

import flyinghigh.services.accounts.domain.FrequentFlyerAccount
import flyinghigh.services.accounts.domain.FrequentFlyerMember
import spock.lang.Specification

class WhenManagingAccounts extends Specification {
def "Accounts start off with zero status poins"() {
def "Accounts start off with zero status points"() {
when:
def account = new FrequentFlyerAccount("123456","Sarah-Jane","Smith")
def account = new FrequentFlyerMember("123456","Sarah-Jane","Smith")
then:
account.statusPoints == 0
}

def "Should be able to earn points"() {
given:
def account = new FrequentFlyerAccount("123456","Sarah-Jane","Smith")
def account = new FrequentFlyerMember("123456","Sarah-Jane","Smith")
when:
account.earns(100).statusPoints()
then:
Expand Down
Loading

0 comments on commit a301646

Please sign in to comment.