Skip to content

Instantly share code, notes, and snippets.

@ncapponi
Created October 12, 2012 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ncapponi/3879131 to your computer and use it in GitHub Desktop.
Save ncapponi/3879131 to your computer and use it in GitHub Desktop.
Coding dojo cara : SRP
package cara;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Cart implements Serializable {
private static final long serialVersionUID = 1L;
private List<Product> products = new ArrayList<Product>();
private Client client;
private boolean hasPaid = false;
private final Date creationDate;
public Cart(Client client, Date creationDate) {
this.client = client;
this.creationDate = creationDate;
}
public void addProduct(Product prod) {
products.add(prod);
}
public void removeProduct(Product prod) {
products.remove(prod);
}
public List<Product> getProducts() {
return products;
}
public List<String> getProductsNames() {
List<String> names = new ArrayList<String>();
for (Product product : products) {
names.add(product.getName());
}
return names;
}
public float totalPrice() {
int total = 0;
for (Product product : products) {
total += product.getPrice();
}
return total;
}
public boolean validate() {
boolean ok = true;
if (client.isSolvent()) {
client.pay(totalPrice());
hasPaid = true;
}
else {
ok = false;
}
return ok;
}
public void save() throws IOException {
ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream("cart.ser"));
stream.writeObject(this);
}
public String computeMailContent() {
String content = "Bonjour,\nVotre panier composé le " + creationDate
+ " comporte les éléments suivants :\n";
for (Product product : products) {
content += "- " + product.getName() + " au prix de "
+ product.getPrice() + "\n";
}
return content;
}
}
public interface Client {
boolean isSolvent();
void pay(float totalPrice);
}
public interface Product {
float getPrice();
String getName();
}
public class Status {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment