Java assignment:
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().
class Address
{
private String streetName;
private String houseNumber;
private String apartmentNumber;
private String zipCode;
Address(){}
Address(String s,String h,String a,String z){
this.apartmentNumber=a;this.houseNumber=h;this.streetName=s;this.zipCode=z;}
//getters and setters
String getStreetName()
{
return this.streetName;
}
String getHouseNumber()
{
return this.houseNumber;
}
String getApartmentNumber()
{
return this.apartmentNumber;
}
String getZipCode()
{
return this.zipCode;
}
void getStreetName(String s)
{
this.streetName=s;
}
void getHouseNumber(String s)
{
this.houseNumber=s;
}
void getApartmentNumber(String s)
{
this.apartmentNumber=s;
}
void getZipCode(String s)
{
this.zipCode=s;
}
}
//class book
class Book
{
private String name;
private String isbn;
private Author author;
Book(){}
Book(String n,String i,Author a){
name=n;isbn=i;author =a;
}
//getters and setters
String getName()
{
return name;
}
String getIsbn()
{
return isbn;
}
Author getAuthor()
{
return author;
}
void setName(String s)
{
name =s;
}
void setIsbn(String s)
{
isbn = s;
}
}
class Date{
private int day,month,year;
Date()
{
}
Date(int d,int m,int y)
{
day=d;month=m;year=y;
}
//getters
String getDate()
{
return
Integer.toString(day)+"/"+Integer.toString(month)+"/"+Integer.toString(year);
}
}
class Author
{
private String firstName;
private String lastName;
private Date dateOfBirth;
Author(){}
Author(String f,String l,Date d){
firstName=f;lastName=l;dateOfBirth=d;}
//getters and setters
String getFirstName()
{
return this.firstName;
}
String getLastName()
{
return this.lastName;
}
Date getDOB()
{
return this.dateOfBirth;
}
void setFirstName(String s)
{
this.firstName=s;
}
void setLastName(String s)
{
this.lastName=s;
}
void setDOB(Date d)
{
this.dateOfBirth=d;
}
}
class BookStore
{
private long id;
private String name;
private Address address;
private Book book;
BookStore(){}
BookStore(long i,String n,Address a,Book b){
id=i;name=n;address=a;book=b;
}
//getters and setters
long getId()
{
return id;
}
String getName()
{
return name;
}
Address getAddress()
{
return address;
}
Book getBook()
{
return book;
}
}
public class BookStoreTester {
public static void main(String argv[])
{
//initializing and testing methods
Book b = new Book("TOC","1895", new Author("Surya","S",new
Date(1,5,1994)));
Address a = new Address("RC center","12-17/1","57","530003");
BookStore B = new BookStore(14567,"George BB STORE",a,b);
//displaying output
System.out.println("Book store id: "+B.getId());
System.out.println("Book store name: "+B.getName());
System.out.println("Address:
street:"+B.getAddress().getStreetName()+",house
no:"+B.getAddress().getHouseNumber()+",apartment
no:"+B.getAddress().getApartmentNumber()+",Zip:"+B.getAddress().getZipCode());
System.out.println("Book name:"+B.getBook().getName());
System.out.println("Book isbn:"+B.getBook().getIsbn());
System.out.println("Book Author
name:"+B.getBook().getAuthor().getFirstName()+"
"+B.getBook().getAuthor().getLastName()+",DOB:"+B.getBook().getAuthor().getDOB().getDate());
}
}
output:
run:
Book store id: 14567
Book store name: George BB STORE
Address: street:RC center,house no:12-17/1,apartment
no:57,Zip:530003
Book name:TOC
Book isbn:1895
Book Author name:Surya S,DOB:1/5/1994
BUILD SUCCESSFUL (total time: 0 seconds)
Get Answers For Free
Most questions answered within 1 hours.