Erinevus lehekülje "ITI0011-2015:harjutus 10" redaktsioonide vahel
48. rida: | 48. rida: | ||
== Mall == | == Mall == | ||
+ | |||
+ | BookShop.java | ||
+ | <source lang="java"> | ||
+ | import java.util.ArrayList; | ||
+ | import cs.ttu.ee.BookShopApi; | ||
+ | import cs.ttu.ee.SimpleBookShop; | ||
+ | |||
+ | |||
+ | /** | ||
+ | * BookShop class. | ||
+ | * Simplified implementation of BookShop | ||
+ | * Does filtering and basic queries. | ||
+ | */ | ||
+ | public class BookShop extends SimpleBookShop { | ||
+ | |||
+ | /** | ||
+ | * Instance of BookShopApi, used to access | ||
+ | * Google Books | ||
+ | */ | ||
+ | private BookShopApi bookShopApi; | ||
+ | |||
+ | /** | ||
+ | * Last api query result. | ||
+ | */ | ||
+ | private String lastResponse = ""; | ||
+ | |||
+ | public BookShop(BookShopApi _bookShopApi) { | ||
+ | bookShopApi = _bookShopApi; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Make api query. | ||
+ | * Uses BookStoreApi functionality to get XML response from Google Books | ||
+ | * Every time this function is called, latest XML state is saved. | ||
+ | * If previous query failed lastResponse equals to empty string. | ||
+ | * Otherwise lastResult is | ||
+ | * @param search title of book | ||
+ | */ | ||
+ | public void query(String searchString) { | ||
+ | // TODO: implement | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Find total items matched by searchString. | ||
+ | * Api uses paging, not all results are | ||
+ | * displayed under result, however, total | ||
+ | * results is displayed inside <totalItems> element | ||
+ | * | ||
+ | * If last query was unsuccessful, 0 is returned. | ||
+ | * | ||
+ | * @return Total items matched by query. | ||
+ | */ | ||
+ | public int getTotalRecordsInResponse() { | ||
+ | // TODO: implement | ||
+ | return Integer.MIN_VALUE; | ||
+ | } | ||
+ | |||
+ | |||
+ | /** | ||
+ | * Finds all possbile book titles. | ||
+ | * Duplicates may occur. | ||
+ | * | ||
+ | * Title can be found inside <title> tag. | ||
+ | * If last query was unsuccessful, empty ArrayList is returned. | ||
+ | * | ||
+ | * @return All possible book names matched by searchString | ||
+ | */ | ||
+ | public ArrayList<String> getPossibleBookTitles() { | ||
+ | // TODO: implement | ||
+ | return null; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Finds all authors found in last response. | ||
+ | * Does not include duplicate authors. | ||
+ | * If last query was unsuccessful, empty ArrayList is returned. | ||
+ | * | ||
+ | * Author can be found inside <authors> element. (Notice non plural form) | ||
+ | * NB! Multiple <authors> element may occur. | ||
+ | * | ||
+ | * @return List of authors found in last response | ||
+ | */ | ||
+ | public ArrayList<String> getPossibleAuthors() { | ||
+ | // TODO: implement | ||
+ | return null; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Finds book with the most number of pages | ||
+ | * | ||
+ | * Page count can be found inside <pageCount> element | ||
+ | * @return Title of book. | ||
+ | */ | ||
+ | public String getBookWithMostPagesTitle() { | ||
+ | // TODO: implement | ||
+ | return null; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Finds all book titles where pageCount < n | ||
+ | * | ||
+ | * Page count can be found inside <pageCount> element | ||
+ | * @param n maximum amount of pages | ||
+ | * @return list of books with less than n pages | ||
+ | */ | ||
+ | public ArrayList<String> getBookWithLessThanNPagesTitles(int n) { | ||
+ | // TODO: implement | ||
+ | return null; | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | </source> | ||
+ | |||
+ | Main.java: | ||
+ | <source lang="java"> | ||
+ | import java.util.ArrayList; | ||
+ | |||
+ | import cs.ttu.ee.BookShopApi; | ||
+ | |||
+ | public class Main { | ||
+ | |||
+ | public static void main(String args[]) { | ||
+ | BookShopApi bookShopApi = new BookShopApi(); | ||
+ | BookShop studentBookShop = new BookShop(bookShopApi); | ||
+ | |||
+ | // Test your code here | ||
+ | studentBookShop.query("Intermediate Java programming"); | ||
+ | System.out.println(studentBookShop.getTotalRecordsInResponse()); | ||
+ | System.out.println(studentBookShop.getPossibleBookTitles()); | ||
+ | System.out.println(studentBookShop.getPossibleAuthors()); | ||
+ | System.out.println(studentBookShop.getBookWithMostPagesTitle()); | ||
+ | ArrayList<String> result = studentBookShop.getBookWithLessThanNPagesTitles(100); | ||
+ | System.out.println(result); | ||
+ | // etc ... | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | } | ||
+ | </source> |
Redaktsioon: 24. märts 2015, kell 18:24
Testid alates 25. märtsist (või kui see teade ära kaob :) )
Harjutuse tähtaeg on 26.-27. märts.
Üldine informatsioon harjutuste kohta: ITI0011:harjutused.
Kirjeldus
Tuleb realiseerida klass BookStore
, mis sõltub BookShopApi
library'st, mille saab tõmmata Meedia:ITI0011-2015-BookStoreApiV02.zip.
BookStoreApi library võimaldab pealkirjade (või pealkirja osa) järgi pärida infot raamatu(te) kohta.
Näiteks:
<BookStoreInstance>.getBookByTitle("Tõde ja Õigus")
tagastab xml dokumendi http://pastebin.com/dSfNCHH8
BookStore
peab laiendama SimpleBookStore
klassi, mis tuleb kaasa BookShopApi
teegiga.
BookStore
peab olema eraldi defineeritud klass, mille konstruktor võtab argumendina ette BookShopApi
objekti. BookShop
klassis peab olema
defineeritud 5 meetodit: getTotalRecordsInResponse
, getPossibleBookTitles
, getBookWithMostPagesTitle
, getPossbileAuthors
, query
.
- getTotalRecordsInResponse tagastab XML tulemusest
<totalItems>X</totalItems>
X väärtuse. Kui tulemus puudub või getBookByTitle viskab erindi, siis tagastada 0. - getPossibleBookTitles tagastab kõik raamatute nimed (see tähendab kõik väärtused, mis on <title> tagide vahel). Duplikaadid lubatud. Kui tulemus puudub võib getBookByTitle viskab erindi, siis tagatada tühi list.
- getBookWithMostPagesTitle, tagastab raamatu pealkirja(<title>), millel on antud XML korral kõige rohkem lehekülgi. Kui getBookByTitle viskab erindi, siis tagastada tühi string ("")
- getPossbileAuthors, tagastab kõik leitud raamatute autorid, seejuures tuleb arvestada, et ühte autorit ei tohi olla listis rohkem kui 1 kord. Raamatu autori leiab <authors> tagide vahelt. Tähelepanu <authors> tag sisaldab tegelikult ainulte ühte autorit, mitme autori puhul on lisatud <authors> tage mitu korda. Erindi korral tagastada tühi list.
- getBookWithLessThanNPagesTitles tagastab listi pealkirjadest, mille puhul on lehekülgi vähem kui n. Erindi puhul tagastada tühi list.
- query teeb päringu BookShopApi abiga ja salvestab viimase tulemuse klassimuutujasse lastResult. Kui tekib erind, siis viimane tulemus on võrdne tühja stringiga ("").
Kasulikke materjale:
- http://www.freeformatter.com/xml-formatter.html - väljundi loetavamaks muutmiseks võib arenduse käigus kasutada järgnevat XML ilustajat.
- http://dom4j.sourceforge.net/dom4j-1.6.1/ - xml väljundi mugavamaks parsimiseks
- http://en.wikipedia.org/wiki/Regular_expression - regulaaravalidstega on võimalik mugavalt matchida konkreetseid elemente
- http://www.freeformatter.com/xpath-tester.html - võimaldab kiiresti leida kõik otsitavad elem endid, näiteks //title leiab kohe kõik pealkirjad
Loodavat funktsioonid
- public int getTotalRecordsInResponse()
- public ArrayList<String> getPossibleBookTitles()
- public String getBookWithMostPagesTitle()
- public ArrayList<String> getPossibleAuthors()
- public ArrayList<String> getBookWithLessThanNPagesTitles()
- public void query(String search)
Example BookShopApi call:
http://pastebin.com/9DcpBHe7
Mall
BookShop.java <source lang="java"> import java.util.ArrayList; import cs.ttu.ee.BookShopApi; import cs.ttu.ee.SimpleBookShop;
/**
* BookShop class. * Simplified implementation of BookShop * Does filtering and basic queries. */
public class BookShop extends SimpleBookShop {
/** * Instance of BookShopApi, used to access * Google Books */ private BookShopApi bookShopApi;
/** * Last api query result. */ private String lastResponse = "";
public BookShop(BookShopApi _bookShopApi) { bookShopApi = _bookShopApi; }
/** * Make api query. * Uses BookStoreApi functionality to get XML response from Google Books * Every time this function is called, latest XML state is saved. * If previous query failed lastResponse equals to empty string. * Otherwise lastResult is * @param search title of book */ public void query(String searchString) { // TODO: implement }
/** * Find total items matched by searchString. * Api uses paging, not all results are * displayed under result, however, total * results is displayed inside <totalItems> element * * If last query was unsuccessful, 0 is returned. * * @return Total items matched by query. */ public int getTotalRecordsInResponse() { // TODO: implement return Integer.MIN_VALUE; }
/**
* Finds all possbile book titles.
* Duplicates may occur.
*
* Title can be found inside <title> tag.
* If last query was unsuccessful, empty ArrayList is returned.
*
* @return All possible book names matched by searchString
*/
public ArrayList<String> getPossibleBookTitles() {
// TODO: implement
return null;
}
/** * Finds all authors found in last response. * Does not include duplicate authors. * If last query was unsuccessful, empty ArrayList is returned. * * Author can be found inside <authors> element. (Notice non plural form) * NB! Multiple <authors> element may occur. * * @return List of authors found in last response */ public ArrayList<String> getPossibleAuthors() { // TODO: implement return null; }
/** * Finds book with the most number of pages * * Page count can be found inside <pageCount> element * @return Title of book. */ public String getBookWithMostPagesTitle() { // TODO: implement return null; }
/** * Finds all book titles where pageCount < n * * Page count can be found inside <pageCount> element * @param n maximum amount of pages * @return list of books with less than n pages */ public ArrayList<String> getBookWithLessThanNPagesTitles(int n) { // TODO: implement return null; }
}
</source>
Main.java: <source lang="java"> import java.util.ArrayList;
import cs.ttu.ee.BookShopApi;
public class Main {
public static void main(String args[]) { BookShopApi bookShopApi = new BookShopApi(); BookShop studentBookShop = new BookShop(bookShopApi);
// Test your code here studentBookShop.query("Intermediate Java programming"); System.out.println(studentBookShop.getTotalRecordsInResponse()); System.out.println(studentBookShop.getPossibleBookTitles()); System.out.println(studentBookShop.getPossibleAuthors()); System.out.println(studentBookShop.getBookWithMostPagesTitle()); ArrayList<String> result = studentBookShop.getBookWithLessThanNPagesTitles(100); System.out.println(result); // etc ...
} } </source>