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().
Bookstore:
public class BookStore {
private long id;
private String name;
private Address address;
private Book book;
public BookStore(){
}
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;
}
}
Book:
public class Book {
private String name;
private String isbn;
private Author author;
public Book(){
}
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;
}
public String toString(){
return this.getIsbn()+"
"+this.getName()+" "+this.getAuthor().toString();
}
}
Author:
import java.util.Date;
public class Author {
private String firstName;
private String lastName;
private Date dateOfBirth;
private Address address;
public Author(){
}
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;
}
public String toString(){
return this.getFirstName()+"
"+this.getLastName()+" "+this.getAddress().toString()+" "+
this.getDateOfBirth();
}
}
Address:
public class Address {
private String streetName;
private String houseNumber;
private String apartmentNumber;
private String zipCode;
public Address(){
}
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 getApartmentNumber() {
return apartmentNumber;
}
public void setApartmentNumber(String apartmentNumber)
{
this.apartmentNumber =
apartmentNumber;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String toString(){
return this.getApartmentNumber()+"
"+this.getHouseNumber()+" "+this.getStreetName()+" "+
this.getZipCode();
}
}
App:
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class App {
public static void main(String[] args) {
// TODO Auto-generated method
stub
BookStore bookStore=new
BookStore();//creating new bookstore object
Book b1=new Book();//creating book
1
Author a=new Author();//creating
author for book object
a.setFirstName("kalam");
a.setLastName("apj");
SimpleDateFormat datefrmt = new
SimpleDateFormat("dd-MM-yyyy");
java.util.Date dob = null;
try {
dob =
datefrmt.parse("15-10-1931");
} catch (ParseException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
a.setDateOfBirth(dob);
Address address=new
Address();//creating address for author
address.setApartmentNumber("A-122");
address.setHouseNumber("1-5");
address.setStreetName("Oak
street");
address.setZipCode("500000");
a.setAddress(address);
b1.setName("wings of fire");
b1.setAuthor(a);
b1.setIsbn("1245");
bookStore.setId(1);
bookStore.setBook(b1);
bookStore.setName("Royal
BookStore");
bookStore.setAddress(address);
//Displaying the bookstore
information
System.out.println(bookStore.getId());
System.out.println(bookStore.getName());
System.out.println(bookStore.getAddress().toString());
//displaying book information
System.out.println(bookStore.getBook().toString());
//displaying author
information
System.out.println(bookStore.getBook().getAuthor().toString());
}
}
Expected output: