Given this xml file, please help me complete the following: The root element is
ID: 664761 • Letter: G
Question
Given this xml file, please help me complete the following:
The root element is CATALOG and contains a series of BOOK elements, each of which represents a single BOOK. Each BOOK element has a number of child elements such as AUTHOR, TITLE, GENRE, PRICE and so on.
Write a function called book_info that prints the title, author, and price of the book with a certain id (passed as a parameter). Build a list of all book ids in the XML file and call this function on each of them. Example call: book_info(“bk111”)
Print the total cost of buying all the books in the “Computer” genre.
Print the unique genres in the file.
Use list comprehensions to minimize the code required in parts a – b.
Use list comprehensions AND dictionary comprehensions to minimize the code required in part c.
Should be in python, with appropriately named variables and using list comprehensions to minimize the code.
Link to the xml file:
https://drive.google.com/file/d/0B5FX84Uyz9fjR0p4aUpkaUJaaFE/view?usp=sharing
Explanation / Answer
public class Library { private ArrayList allBook = new ArrayList(); public Library(ArrayList other) { if (other == null) { throw new NullPointerException("null pointer"); } else this.allBook = other; } public Library() { this.allBook = new ArrayList(); } public boolean add(Book book) { if (book != null && !book.equals("")) { throw new IllegalArgumentException("Can't be empty"); } allBook.add(book); return true; } public ArrayList findTitles(String title) { for(Book b: allBook) { if(title.compareTo(b.getTitle())== 0) { return allBook; } } return null; } public void sort() { Collections.sort(allBook); } public String toString() { return Library.this.toString(); } } public class Book implements Comparable { private String bookTitle; private ArrayList bookAuthor; public Book(String title, ArrayList authors) { if(title == null && authors == null) { throw new IllegalArgumentException("Can't be null"); } if(title.isEmpty() && authors.isEmpty()) { throw new IllegalArgumentException("Can't be empty"); } bookTitle = title; bookAuthor = authors; } public String getTitle() { return bookTitle; } public ArrayList getAuthors( ) { return bookAuthor; } public String toString( ) { return bookTitle + bookAuthor; } public int compareTo(Book other){ return bookTitle.compareTo(other.bookTitle); } public boolean equals(Object o) { if(!(o instanceof Book)) { return false; } Book b = (Book) o; return b.bookTitle.equals(bookTitle) && b.bookAuthor.equals(bookAuthor); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.