Bàsicament el que vull veure es com gestiones el tema dels fors utilizant Higher-Order functions i evitant utilizar cap loop foreach
Per altra banda, hi ha un refactor molt interessant en el qual hauras d'intentar aconseguir un codi el máxim SOLID possible.
I finalment, aquí a Revo intentem treballar seguint la metodologia TDD, l'últim exercici haurás de resoldre el problema a partir de tests unitaris.
Pots utilizar el llenguatge que vulguis per a resoldre els excercisis.
Collections
GitHub provides a public API endpoint that returns all of a user’s recent public activity. The JSON response it gives you is an array of objects shaped generally like this (simplified a bit for brevity):
[
{
"id" : "3898913063",
"type" : "PushEvent",
"public" : true,
"actor" : "adamwathan",
"repo" : "tightenco/jigsaw",
"payload" : { /* ... */ }
},
// ...
]
The task is to take these events and determine a user’s “GitHub Score”,
based on the following rules:
- Each PushEvent is worth 5 points.
- Each CreateEvent is worth 4 points.
- Each IssuesEvent is worth 3 points.
- Each CommitCommentEvent is worth 2 points.
- All other events are worth 1 point.
Per fer la crida i obtenir les dades en un array simplement pots fer servir el seguent (badchoice és el meu usuari de github):
$url = "https://api.github.com/users/badchoice/events";
$events = json_decode(file_get_contents($url), true);
Collections 2
Given a JSON feed of products from a store, figure out how much it would cost to buy every variant of every single lamp and wallet that store has for sale.
{
"products": [
{
"title": "Small Rubber Wallet",
"product_type": "Wallet",
"variants": [
{ "title": "Blue", "price": 29.33 },
{ "title": "Turquoise", "price": 18.50 }
]
}, {
"title": "Sleek Cotton Shoes",
"product_type": "Shoes",
"variants": [
{ "title": "Sky Blue", "price": 20.00 }
]
},
// ...
]
}
Refactor
You are asked to add 3 new types of movies, as well as 2 new outputs, for the customer rentals (Html and Xml), and the frequentRenterPoints rules will change. Refactor this code to allow these new features to be added with ease.
public class Movie {
public static final int CHILDRENS = 2;
public static final int NEW_RELEASE = 1;
public static final int REGULAR = 0;
private String _title;
private int _priceCode;
public Movie(String title, int priceCode) {
_title = title;
_priceCode = priceCode;
}
public int getPriceCode() {
return _priceCode;
}
public void setPriceCode(int arg) {
_priceCode = arg;
}
public String getTitle() {
return _title;
}
}
public class Rental {
private Movie _movie;
private int _daysRented;
public Rental(Movie movie, int daysRented) {
_movie = movie;
_daysRented = daysRented;
}
public int getDaysRented() {
return _daysRented;
}
public Movie getMovie() {
return _movie;
}
}
public class Customer {
private String _name;
private List<Rental> _rentals = new ArrayList<Rental>();
public Customer(String name) {
_name = name;
}
public void addRental(Rental arg) {
_rentals.add(arg);
}
public String getName() {
return _name;
}
public String statement() {
double totalAmount = 0;
int frequentRenterPoints = 0;
String result = "Rental Record for " + getName() + "\n";
for (Rental each: _rentals) {
double thisAmount = 0;
//determine amounts for each line
switch (each.getMovie().getPriceCode()) {
case Movie.REGULAR:
thisAmount += 2;
if (each.getDaysRented() > 2)
thisAmount += (each.getDaysRented() - 2) * 1.5;
break;
case Movie.NEW_RELEASE:
thisAmount += each.getDaysRented() * 3;
break;
case Movie.CHILDRENS:
thisAmount += 1.5;
if (each.getDaysRented() > 3)
thisAmount += (each.getDaysRented() - 3) * 1.5;
break;
}
// add frequent renter points
frequentRenterPoints++;
// add bonus for a two day new release rental
if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1)
frequentRenterPoints++;
// show figures for this rental
result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + "\n";
totalAmount += thisAmount;
}
// add footer lines
result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points";
return result;
}
}
Testing
Write a program that prints the prime numbers of a given input using TDD (Test Driven Development)
