001package edu.pdx.cs410J.di;
002
003import com.google.common.collect.ImmutableMap;
004import com.google.inject.Inject;
005
006import javax.ws.rs.GET;
007import javax.ws.rs.Path;
008import javax.ws.rs.Produces;
009import java.util.Map;
010
011/**
012 * A Credit Card service that exposes its behavior via REST
013 */
014@Path("/rest/creditCard")
015public class RestfulCreditCardService
016{
017    private final CreditCardDatabase cards;
018
019    @Inject
020    public RestfulCreditCardService(CreditCardDatabase cards) {
021        this.cards = cards;
022    }
023
024    @GET
025    @Produces("text/plain")
026    public String allCardBalances() {
027        StringBuilder sb = new StringBuilder();
028
029        ImmutableMap<CreditCard, Double> balances = cards.getBalances();
030        for ( Map.Entry<CreditCard, Double> entry : balances.entrySet() ) {
031            sb.append(entry.getKey().getNumber());
032            sb.append("\t");
033            sb.append(entry.getValue());
034            sb.append("\n");
035        }
036
037        return sb.toString();
038    }
039
040}