im pretty new to OOP in java and im having trouble of changing the output of the email from as it should run from [email protected] to [email protected] to [email protected],i also did an argument on the setEmail parameter if relations wether if the input email is wrong then it will be Invalid email! Set to unknown and then the EMAIL : Uknown here is my current code:
public class BookTest
{
public static void main(String[] args)
{
Author author1 = new Author("Josanne Oro", "[email protected]", "Male");
author1.print();
Book bk1 = new Book("Java Book", author1, 1499, 10);
bk1.print();
author1.setEmail("[email protected]");
author1.print();
bk1.print();
bk1.getAuthor().setEmail("[email protected]");
bk1.print();
}
}
public class Author
{
private String author;
private String email;
private String gender;
Author(String author, String email, String gender)
{
this.author=author;
this.email=email;
this.gender=gender;
}
String getAuthor()
{
return author;
}
void setAuthor(String author)
{
this.author=author;
}
String getEmail()
{
return email;
}
void setEmail(String email)
{
if
(email=="[email protected]"||email=="[email protected]"||email=="[email protected]")
{
this.email=email;
}
else
System.out.println("Invalid email! Set to unknown");
this.email="Uknown";
}
void print()
{
System.out.println("AUTHOR : " author);
System.out.println("GENDER : " gender);
System.out.println("E-MAIL : " email);
}
}
public class Book
{
private String bookTitle;
private Author author;
private double price;
private int stock=0;
public Book(String bookTitle, Author author, double price, int stock)
{
this.bookTitle=bookTitle;
this.author=author;
this.price=price;
this.stock=stock;
}
public Author getAuthor()
{
return this.author;
}
public void print()
{
System.out.println("Book Title : " bookTitle);
author.print();
}
} ```
CodePudding user response:
The problem looks to be in setEmail:
void setEmail(String email)
{
if
(email=="[email protected]"||email=="[email protected]"||email=="[email protected]")
{
this.email=email;
}
else
System.out.println("Invalid email! Set to unknown");
this.email="Uknown"; //This line is not part of "else" because its not in a block
}
So that this.email line gets called at the end of the function every time, to fix do this:
else {
System.out.println("Invalid email! Set to unknown");
this.email="Uknown";
}