Reputation: 840
so i had this exam in university which was to create book class which would have author, title, content and translator.
main requirement was to create only 1 class and create inner classes within it (author and translator)
and one Main class to test it.
Problem was that when i created author and translator objects and tried to change their names, both of them would change names.
Here's my code:
Book Class:
public class Book {
private Author author;
private Translator translator;
private String title;
private String text;
public interface IPerson {
public void setFirstName(String name);
public String getFirstName();
public void setLastName(String name);
public String getLastName();
}
//Author
public class Author implements IPerson{
private String firstName;
private String lastName;
public Author(String f, String l){
firstName = f;
lastName = l;
author = this;
}
public void setFirstName(String name){
this.firstName = name;
}
public String getFirstName(){
return this.firstName;
}
public void setLastName(String name){
this.lastName = name;
}
public String getLastName(){
return this.lastName;
}
}
public Author getAuthor(){
return this.author;
}
//Translator
public class Translator extends Author{
public Translator(String f, String l) {
super(f, l);
translator = this;
}
}
public Translator getTranslator(){
return this.translator;
}
public String getTranslatorFullName(){
return this.getTranslator().getFirstName() + " " + this.getTranslator().getLastName();
}
public String getText() {
return this.text;
}
public void setText(String text) {
this.text = text;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDisplayTitle(){
return getTitle() + " Author: " + this.getAuthor().getFirstName() + " " + this.getAuthor().getLastName();
}
}
Main Class:
public class Main {
public static void main(String[] args){
Book mybook = new Book();
mybook.setTitle("Fight Club");
mybook.setText("First rule of Fight Club, you do not talk about Fight Club");
Book.Author author = mybook.new Author("John","Doe");
Book.Translator translator = mybook.new Translator("Alan","Rickman");
System.out.println(mybook.getDisplayTitle());
System.out.println(mybook.getAuthor().getLastName());
System.out.println(mybook.getAuthor().getFirstName());
System.out.println(mybook.getTranslatorFullName());
System.out.println(mybook.getTranslator().getFirstName());
}
}
It Had to pass these tests, what was my problem?
Don't tell me to make Translator implement IPerson, it was my teachers request to extend
Upvotes: 0
Views: 88
Reputation: 13222
You can fix it like this:
public Author(String f, String l, boolean authorCheck){
firstName = f;
lastName = l;
if(authorCheck)
{
author = this;
}
}
public Author(String f, String l){
firstName = f;
lastName = l;
author = this;
}
public Translator(String f, String l) {
super(f, l, false);
translator = this;
}
Upvotes: 1
Reputation: 1824
It would make more sense if Translator
implements IPerson
instead of extending Author
.
Upvotes: 1
Reputation: 936
You're making a super(f,l)
call in the constructor of Translator
. Translator
is changing the Author
fields as the superclass of Translator
is Author
.
Upvotes: 0