In: Computer Science
Encapsulation
You are tasked with building a BookStore. Your Architect has told you that the BookStore class has the following members : long id; String name; Address address; Book book; The Address class has the following attributes : String streetName; String houseNumber; String apartmentNumber; String zipCode; The book class has the following members String name; String isbn; Author author; The Author class within the book class has the following member : String firstName; String lastName; Date dateOfBirth; Address address; //Address class has already been created You have to create your BookStore class. You have to use encapsulation to make sure all the variables of the bookstore class are protected. For the classes that are within the bookstore class (Address and Book and Author within book) you have to use encapsulation as well for the member variables. Now you need to create another class that will have a main method. Call this class App. Populate your variables with data using the setter methods and then print them out using System.out.println().
Use Java
For the above program, we need to have the following thing into mind for encapsulation to be there
Following is the program.
import java.util.Date;
class Author{
private String firstName, lastName;
private Date dateOfBirth;
private Address address;
public Author(String firstName, String lastName, Date
dateOfBirth, Address address) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth =
dateOfBirth;
this.address = address;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth =
dateOfBirth;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Author [firstName=" +
firstName + ", lastName=" + lastName + ", dateOfBirth=" +
dateOfBirth
+ ", address=" + address + "]";
}
}
class Book{
private String name;
private String isbn;
private Author author;
public Book(String name, String isbn, Author author)
{
super();
this.name = name;
this.isbn = isbn;
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
@Override
public String toString() {
return "Book [name=" + name + ",
isbn=" + isbn + ", author=" + author + "]";
}
}
class Address{
private String streetName;
private String houseNumber;
private String appartmentNumber;
private String zipCode;
public Address(String streetName, String houseNumber,
String appartmentNumber, String zipCode) {
super();
this.streetName = streetName;
this.houseNumber =
houseNumber;
this.appartmentNumber =
appartmentNumber;
this.zipCode = zipCode;
}
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public String getHouseNumber() {
return houseNumber;
}
public void setHouseNumber(String houseNumber) {
this.houseNumber =
houseNumber;
}
public String getAppartmentNumber() {
return appartmentNumber;
}
public void setAppartmentNumber(String
appartmentNumber) {
this.appartmentNumber =
appartmentNumber;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
@Override
public String toString() {
return "Address [streetName=" +
streetName + ", houseNumber=" + houseNumber + ",
appartmentNumber="
+ appartmentNumber + ", zipCode=" + zipCode +
"]";
}
}
class BookStore{
private long id;
private String name;
private Address address;
private Book book;
public BookStore(long id, String name, Address
address, Book book) {
super();
this.id = id;
this.name = name;
this.address = address;
this.book = book;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
@Override
public String toString() {
return "BookStore [id=" + id + ",
name=" + name + ", address=" + address + ", book=" + book +
"]";
}
}
public class App {
public static void main(String args[]) {
Address address = new
Address("Street Name", "houseNumber", "appartmentNumber",
"zipCode");
Author author = new Author("John",
"Charlie", new Date(1997, 12, 12), address);
Book book = new Book("name",
"isbn", author);
BookStore bookStore = new
BookStore(12356, "name", address, book);
System.out.println(bookStore);
}
}
Following is the snippet of the output:
That was a nice
question to answer
Friend, If you have any doubts in understanding do let me know in
the comment section. I will be happy to help you further.
Thanks