Skip to content

Instantly share code, notes, and snippets.

@JorgeOlvera
Created April 3, 2014 05:25
Show Gist options
  • Save JorgeOlvera/9948709 to your computer and use it in GitHub Desktop.
Save JorgeOlvera/9948709 to your computer and use it in GitHub Desktop.
Author and Book classes, with their respective testing programs
//COMPILES!!!!!
/*public getters/setters: getName(), getEmail(), setEmail(), and getGender();
(There are no setters for name and gender, as these attributes cannot be changed.)
A toString() method that returns "author-name (gender) at email", e.g., "Tan Ah Teck (m) at [email protected]".*/
public class Author{
private String name;
private String email;
private char gender;
public Author (String name, String email, char gender){
this.name = name;
this.email = email;
this.gender = gender;
}
public String getName(){
return this.name;
}
public String getEmail(){
return this.email;
}
public void setEmail(String email){
this.email = email;
}
public char getGender() {
return this.gender;
}
public String toString(){
String completeAuthor = (this.name + " (" + this.gender + ") " + "@" + this.email);
return completeAuthor;
}
}
public class Book {
private String name;
private Author author;
private double price;
private int qtyInStock = 0;
public Book(String name, Author author, double price){
this.name = name;
this.author = author;
this.price = price;
}
public Book(String name, Author author, double price, int qtyInStock){
this.name = name;
this.author = author;
this.price = price;
this.qtyInStock = qtyInStock;
}
public String getName(){
return this.name;
}
public Author getAuthor(){
return this.author;
}
public void setPrice(double price){
this.price = price;
}
public int getQtyInStock(){
return this.getQtyInStock;
}
public void setQtyInStock(int qtyInStock){
this.qtyInStock = qtyInStock;
}
public String toString(){
String title = (name + "by" + author.toString());
return title;
}
public String getAuthorName(Book oneBook){
String authorName = oneBook.getAuthor().getName();
return authorName;
}
public String getAuthorEmail(Book oneBook){
String authorEmail = oneBook.getAuthor().getEmail();
return authorEmail;
}
public char getAuthorGender(Book oneBook){
char authorGender = oneBook.getAuthor().getGender();
return authorGender;
}
//dsda
}
public class TestAuthor{
public static void main (String[] args){
Author authorOG = new author("Jorge Olvera", "[email protected]", 'm');
System.out.println(authorOG.getName());
System.out.println(authorOG.getEmail());
authorOG.setEmail("eeelolvera.com");
System.out.println("New email is " + authorOG.getEmail());
System.out.println("Gender is" + authorOG.getGender());
System.out.println("Author info is" + authorOG.toString());
}
}
public class testBook{
public static void main (String [] args){
Author authorOG = new author("Jorge Olvera", "[email protected]", 'm');
Book oneBook = new Book(oneBook.getAuthor().getName(), oneBook.getAuthor.getEmail());
System.out.println("Author: " + oneBook.getAuthorName(oneBook));
System.out.println("Author's E-mail: " + oneBook.getAuthorEmail(oneBook));
System.out.println("Gender: " + oneBook.getAuthorGender(oneBook));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment