Question

I need help with converting the java class into a python class. this is what i...

I need help with converting the java class into a python class. this is what i have so far :

class Library:
  
def __init__(self):
  
def addAuthor(self, Author):
  
def addPublisher(self, Publisher):
  
def addBook(self, Book):
  

Here is the original java class:

import java.util.ArrayList;
import java.util.List;

public class Library {

private List<Book> books;
private List<Author> authors;
private List<Publisher> publishers;


public Library(){
this.books=new ArrayList<>();
this.authors=new ArrayList<>();
this.publishers=new ArrayList<>();
}


public void addAuthor(String name){
authors.add(new Author(name));
}

public void addPublisher(String name, String address){
publishers.add( new Publisher(name,address));
}

public void addBook(String title,String location, int yearPub, String authorName, String publisherName ) throws AuthorNotFoundException, PublisherNotFoundException {
//Find Author
Author author=null;
for (Author a:authors){
if (a.getName().equals(authorName)){
author = a;
break;
}
}
if (author==null) {
throw new AuthorNotFoundException("Author is not in the system");
}

//Find Publisher
Publisher publisher=null;
for (Publisher p:publishers){
if (p.getName().equals(publisherName)){
publisher=p;
break;
}
}
if (publisher==null){
throw new PublisherNotFoundException("Publisher is not in the system");
}


books.add(new Book(title, location,yearPub,author,publisher));
}

public List<Book> getBooks(){return books;}
public List<Author> getAuthors(){return authors;}
public List<Publisher> getPublishers(){return publishers;}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Author:
    def __init__(self, name):
        self._name = name

    def get_name(self):
        return self._name

    def set_name(self, name):
        self._name = name


class Publisher:
    def __init__(self, name, address='unknown'):
        self._name = name
        self._address = address

    def get_name(self):
        return self._name

    def get_address(self):
        return self._address

    def set_name(self, name):
        self._name = name

    def set_address(self, address):
        self._address = address

    def __str__(self):
        return "Publisher '%s' at '%s'" % (self._name, self._address)


class Book:
    def __init__(self, title, locationCode, yearPub, author=None, publisher=None):
        self._title = title
        self._locationCode = locationCode
        self._yearPub = yearPub
        self._author = author
        self._publisher = publisher

    def get_title(self):
        return self._title

    def get_locationCode(self):
        return self._locationCode

    def get_yearPub(self):
        return self._yearPub

    def get_author(self):
        return self._author

    def get_publisher(self):
        return self._publisher

    def set_title(self, title):
        self._title = title

    def set_locationCode(self, locationCode):
        self._locationCode = locationCode

    def set_yearPub(self, yearPub):
        self._yearPub = yearPub

    def __str__(self):
        return "A book called '%s' written by '%s' and published in %d by '%s'" % (
            self._title, self._author.get_name(), self.get_yearPub(), self._publisher.get_name())


class AuthorNotFoundException(Exception):
    pass


class PublisherNotFoundException(Exception):
    pass


class Library:
    def __init__(self):
        self.books = []
        self.authors = []
        self.publishers = []

    def add_author(self, author):
        self.authors.append(author)

    def add_publisher(self, name, address):
        p = Publisher(name, address)
        self.publishers.append(p)

    def add_book(self, title, location, yearPub, authorName, publisherName):
        author = None
        for a in self.authors:
            if a.get_name() == authorName:
                author = a
                break
        if author is None:
            raise AuthorNotFoundException("Author is not in the system")

        publisher = None
        for p in self.publishers:
            if p.get_name() == publisherName:
                publisher = p
                break
        if publisher is None:
            raise PublisherNotFoundException("Publisher is not in the system")
        b = Book(title, location, yearPub, author, publisher)
        self.books.append(b)

    def get_books(self):
        return self.books

    def get_authors(self):
        return self.authors

    def get_publishers(self):
        return self.publishers
Add a comment
Know the answer?
Add Answer to:
I need help with converting the java class into a python class. this is what i...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT