Last active
August 29, 2015 14:22
-
-
Save Async1/5093a8daaff6e7ccf53e to your computer and use it in GitHub Desktop.
Pennys Deck App
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 com.penny; | |
import deck.Deck; | |
import deck.DeckProvider; | |
import deck.XmlDeckLoader; | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
XmlDeckLoader xmlLoader = new XmlDeckLoader("C:\\Users\\Dell\\Desktop\\out.xml"); | |
DeckProvider provider = new DeckProvider(xmlLoader); | |
provider.addDeck(new Deck("Julian", Deck.Color.BLACK, 54, 23)); | |
Deck d1 = provider.getHeaviestDeck(); | |
System.out.printf("\nThe heaviest deck belongs to %s which is %skg \n", d1.getName(), d1.getWeight()); | |
Deck d2 = provider.getCheapestDeck(); | |
System.out.printf("The cheapest deck belongs to %s which costs $%s \n\n", d2.getName(), d2.getPrice()); | |
for (Deck d : provider.getDeckByColor(Deck.Color.BLACK)) | |
System.out.printf("Deck owner: %s \nPrice: $%s \nWeight : %skg \nColor: %s \n\n", d.getName(), d.getPrice(), | |
d.getWeight(), d.getColor()); | |
//XmlDeckLoader.storeDecks(provider.getAllDecks(), "C:\\Users\\Dell\\Desktop\\decks.xml"); | |
SqlDeckLoader.storeDecks(provider.getAllDecks()); | |
//Scanner in = new Scanner(System.in); | |
//String input = in.nextLine(); | |
} | |
} |
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 deck; | |
import javax.xml.bind.annotation.XmlRootElement; | |
@XmlRootElement | |
public class Deck { | |
private String name; | |
private double price; | |
private double weight; | |
private Color color; | |
public Deck(String name, Color color, double weight, double price) { | |
this.name = name; | |
this.color = color; | |
this.weight = weight; | |
this.price = price; | |
} | |
public Deck() { | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public double getPrice() { | |
return price; | |
} | |
public void setPrice(double price) { | |
this.price = price; | |
} | |
public double getWeight() { | |
return weight; | |
} | |
public void setWeight(double weight) { | |
this.weight = weight; | |
} | |
public Color getColor() { | |
return color; | |
} | |
public void setColor(Color color) { | |
this.color = color; | |
} | |
public enum Color { | |
BLACK, | |
BLUE, | |
GREEN, | |
RED | |
} | |
} |
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 deck; | |
import java.util.List; | |
public interface DeckLoader { | |
List<Deck> loadDecks(); | |
} |
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 deck; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class DeckProvider { | |
private List<Deck> deckList; | |
public DeckProvider(DeckLoader deckInterface) { | |
this.deckList = deckInterface.loadDecks(); | |
} | |
public void addDeck(Deck d) { | |
deckList.add(d); | |
} | |
public void deleteDeck(Deck d) { | |
deckList.remove(d); | |
} | |
public void deleteDeck(int index) { | |
deckList.remove(index); | |
} | |
public Deck getDeck(int index) throws Exception { | |
if (index < 0) | |
throw new Exception("Index cannot be less than zero"); | |
else | |
return deckList.get(index); | |
} | |
public List<Deck> getAllDecks() { | |
return this.deckList; | |
} | |
public Deck getCheapestDeck() { | |
int smallestIndex = 0; | |
for(int i = 0; i < this.deckList.size() - 1; ++i) | |
if(deckList.get(i).getPrice() < deckList.get(smallestIndex).getPrice()) | |
smallestIndex = i; | |
return deckList.get(smallestIndex); | |
} | |
public Deck getHeaviestDeck() { | |
int heaviestIndex = 0; | |
for(int i = 0; i < this.deckList.size() - 1; ++i) | |
if(deckList.get(i).getWeight() > deckList.get(heaviestIndex).getWeight()) | |
heaviestIndex = i; | |
return deckList.get(heaviestIndex); | |
} | |
public List<Deck> getDeckByColor(Deck.Color color) { | |
return this.deckList.stream().filter(d -> d.getColor() == color).collect(Collectors.toList()); | |
} | |
} |
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 deck; | |
import java.io.File; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
public class JsonDeckLoader implements DeckLoader { | |
private String filePath; | |
public JsonDeckLoader(String filePath) throws Exception { | |
if (! Files.exists(Paths.get(filePath))) | |
throw new Exception(filePath + " is an invalid file path"); | |
this.filePath = filePath; | |
} | |
@Override | |
public ArrayList<Deck> loadDecks() { | |
ArrayList<Deck> deckList = new ArrayList<>(); | |
File file = Paths.get(filePath).toFile(); | |
//Pretend it's parsing a JSON file and converting each obj to a Deck obj | |
deckList.add(new Deck("Susan", Deck.Color.BLACK, 13, 18)); | |
deckList.add(new Deck("Robin", Deck.Color.RED, 424, 8)); | |
deckList.add(new Deck("Emily", Deck.Color.BLACK , 98, 1)); | |
deckList.add(new Deck("Duke", Deck.Color.GREEN, 9, 34)); | |
return deckList; | |
} | |
public static void storeDeck(ArrayList<Deck> deckList, String location) throws Exception { | |
StringBuilder sb = new StringBuilder(); | |
//for (Deck d : deckList) | |
// | |
System.out.println("Item saved in JSON format..."); | |
} | |
} |
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 deck; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class SqlDeckLoader implements DeckLoader { | |
@Override | |
public List<Deck> loadDecks() { | |
String sql = "SELECT * FROM DeckTable"; | |
//Pretend to get data from SQL tables | |
List<Deck> deckList = new ArrayList<>(); | |
deckList.add(new Deck("Davis", Deck.Color.RED, 1, 17)); | |
deckList.add(new Deck("Rosie", Deck.Color.BLACK ,4, 3)); | |
deckList.add(new Deck("Hazan", Deck.Color.RED ,34, 65)); | |
deckList.add(new Deck("Paula", Deck.Color.BLUE, 91, 34)); | |
return deckList; | |
} | |
public static void storeDecks(List<Deck> deckList) { | |
if(deckList.size() != 0) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append("INSERT INTO DeckTable(Name, Price, Weight, Color) Values "); | |
for (Deck d : deckList) | |
sb.append(String.format("('%s', %s, %s, '%s'),", d.getName(), d.getPrice(), d.getWeight(), d.getColor())); | |
sb.deleteCharAt(sb.length() - 1); | |
System.out.println(sb.toString()); | |
} | |
} | |
} |
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 deck; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.Marshaller; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class XmlDeckLoader implements DeckLoader { | |
private String filePath; | |
public XmlDeckLoader(String filePath) throws Exception { | |
this.filePath = filePath; | |
} | |
@Override | |
public List<Deck> loadDecks() { | |
List<Deck> deckList = new ArrayList<>(); | |
//Lets pretend it parses an XML object and translates each object to a Deck object. | |
deckList.add(new Deck("Glaser", Deck.Color.BLACK, 21, 53)); | |
deckList.add(new Deck("Jordan", Deck.Color.BLACK, 12, 4)); | |
deckList.add(new Deck("Michael", Deck.Color.RED, 122,41)); | |
return deckList; | |
} | |
public static void storeDecks(List<Deck> allDecks, String filePath) throws Exception { | |
if(allDecks.size() != 0) { | |
JAXBContext context = JAXBContext.newInstance(Deck.class); | |
Marshaller m = context.createMarshaller(); | |
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); | |
for (Deck d : allDecks) | |
m.marshal(d, System.out); | |
//This is why I hate Java, why is saving an array of objects to XML format so hard! | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment