Last active
November 30, 2019 11:12
-
-
Save Dysta/f4f5c7fbe2ea5f571d1db6536a09d22d to your computer and use it in GitHub Desktop.
Td6 AO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.ubordeaux.ao.domain; | |
import java.io.IOException; | |
public interface BasketRepository { | |
public void save(Basket b, String filename) throws IOException; | |
public void save(Basket b); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.ubordeaux.ao.infra; | |
import fr.ubordeaux.ao.domain.Basket; | |
import fr.ubordeaux.ao.domain.BasketRepository; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import com.google.gson.*; | |
public class BasketRepositoryJSON implements BasketRepository { | |
public void save(Basket b, String filename) throws IOException { | |
Gson gson = new Gson(); | |
String json = gson.toJson(b); | |
FileWriter fw = new FileWriter(filename); | |
fw.write(json); | |
fw.close(); | |
} | |
@Override | |
public void save(Basket b) { | |
Gson gson = new Gson(); | |
String json = gson.toJson(b); | |
System.out.println(json); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.ubordeaux.ao.infra; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.Map; | |
import fr.ubordeaux.ao.domain.Basket; | |
import fr.ubordeaux.ao.domain.BasketRepository; | |
public class BasketRepositoryMem implements BasketRepository { | |
private Map<Integer, Basket> map = new HashMap<>(); | |
public void save(Basket b, String filename) throws IOException { | |
FileWriter fw = new FileWriter(filename); | |
StringBuilder sb = new StringBuilder(); | |
sb.append(b.getId() + ":" + b + "\n"); | |
fw.write(sb.toString()); | |
fw.close(); | |
} | |
@Override | |
public void save(Basket b) { | |
map.put(b.getId(), b); | |
// pour test | |
for (Basket ba : map.values()) { | |
System.out.println(ba); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.ubordeaux.ao; | |
import java.io.IOException; | |
import fr.ubordeaux.ao.domain.*; | |
import fr.ubordeaux.ao.infra.BasketRepositoryJSON; | |
import fr.ubordeaux.ao.infra.BasketRepositoryMem; | |
public class Main { | |
public static void main(String[] args) { | |
AlphabetWord pain = new AlphabetWord("pain"); | |
Price p = new Price(50); | |
Reference r1 = new Reference(1, pain, "Miom miom", p); | |
Basket b = new Basket(1); | |
b.order(r1, 50); | |
BasketRepositoryJSON brjson = new BasketRepositoryJSON(); | |
try { | |
brjson.save(b, "baskets.json"); | |
} catch(IOException e) { | |
System.out.println(e); | |
} | |
BasketRepositoryMem brmem = new BasketRepositoryMem(); | |
brmem.save(b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment