Wikipedista:Sanchowiki/Pískoviště
tebusko potrebujem tombola implm konstruktor
posielam ti konstruktor ale aj atributy tej triedy,
v konstruktore zatvaram prud lebo mi to automaticky vygenerovalo, tie vynimky neviem ako mas v zadani to uprav
mam aj zbytok triedy ale len tak kostrbato, nie za plny pocet b
public TombolaImpl(File file) {
BufferedReader br = null;
try {
this.file = file;
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
while ((line = br.readLine()) != null) {
String[] words = line.split(";");
String donor = words[0];
if(!tombolaPrizeMap.containsKey(donor)){
tombolaPrizeMap.put(donor, new TreeSet<TombolaPrize>());
}
for (int i = 1; i < words.length; i++) {
String[] prize = words[i].split(":");
TombolaPrize tomPrize = new TombolaPrize(prize[0], donor, Integer.parseInt(prize[1]));
tombolaPrizeMap.get(donor).add(tomPrize);
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(TombolaImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(TombolaImpl.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
br.close();
} catch (IOException ex) {
Logger.getLogger(TombolaImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
public Set<TombolaPrize> getPrizesSortedByNumber() {
Set<TombolaPrize> prizeSet = new TreeSet<TombolaPrize>();
Set<String> donors = tombolaPrizeMap.keySet();
for (String donor : donors) {
prizeSet.addAll(tombolaPrizeMap.get(donor));
}
return prizeSet;
}
@Override
public Set<TombolaPrize> getPrizesSortedByPrice() {
Set<TombolaPrize> prizeSet = new TreeSet<TombolaPrize>();
Set<String> donors = tombolaPrizeMap.keySet();
for (String donor : donors) {
prizeSet.addAll(getPrizesOfDonor(donor));
}
return prizeSet;
}
@Override
public Set<TombolaPrize> getPrizesOfDonor(String donor) {
if (donor == null) {
donor="";
}
if(!tombolaPrizeMap.containsKey(donor)){
return new HashSet<TombolaPrize>();
}
return tombolaPrizeMap.get(donor);
}
@Override
public TombolaPrize givePrize(int number) {
Set<TombolaPrize> prizeSet = getPrizesSortedByPrice();
for (TombolaPrize pr : prizeSet) {
if (pr.getNumber() == number) {
prizeSet.remove(pr);
return pr;
}
}
return null;
}
@Override
public void printListOfPrizes(OutputStream os) throws TombolaException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
Set<TombolaPrize> prizeSet = getPrizesSortedByPrice();
for (TombolaPrize pr : prizeSet) {
try {
bw.write(pr.toString());
bw.newLine();
bw.flush();
} catch (IOException ex) {
throw new TombolaException("Wrong outputstream.",ex);
}
}
}
public class TombolaException extends Exception{
public TombolaException(String msg){
super(msg);
}
public TombolaException(String msg, Throwable cause){
super(msg,cause);
}
}