-1

I wrote two classes for Author and Book then included Author in the Book class. There is not any user input, simply a way for me to wrap my head around Java classes. In my driver I'm trying to create book objects to display the details. The details combine and display as expected but I feel there is a better way of doing this. Do you have any suggestions?

3 files:

BookDriver.java

import java.util.Scanner;
import java.util.*;
import java.io.*;

public class BookDriver{
public static void main(String[] args) {

    Scanner userKeyboardInput = new Scanner(System.in);
    System.out.println("");
    System.out.println("===========================");
    System.out.println("     Book Driver Output    ");
    System.out.println("          ******           ");  
    System.out.println("        genericCog         ");   
    System.out.println("===========================");
    System.out.println("");
    Book.CreateBook1();
    Book.ShowBook1();
    Book.CreateBook2();
    Book.ShowBook2();
    System.out.println("");
    System.out.println("===========================");
}//END main
}//END class

Book.java

public class Book {

static String   Title;
static String   Category;
static String   CoverType;
static int      YearPublished;
static int      NumberOfPages;
static String   FirstName;
static String   LastName;
static String   AuthorFullName;

// BOOK 1
static void CreateAuthor1(){
    Author Author1 = new Author();
    FirstName = "J. R. R.";
    LastName = "Tolkien";
    AuthorFullName = Author1.CombineAuthorName(FirstName, LastName);
}
static void CreateBook1() {
    CreateAuthor1();
    Title  = "The Hobbit; or, There and Back Again";
    YearPublished = 1937;
    NumberOfPages = 300;
    Category = "Fantasy";
    CoverType = "Paperback";
}

static void ShowBook1() {
    System.out.println("-----                  -----");
    System.out.println("       Book 1 Details       ");
    System.out.println("-----                  -----");
    System.out.println("Title:      " + Title);
    System.out.println("Author:     " + AuthorFullName);
    System.out.println("Published:  " + YearPublished);
    System.out.println("Pages:      " + NumberOfPages);
    System.out.println("Category:   " + Category);
    System.out.println("Cover Type: " + CoverType);
    System.out.println("");
    System.out.println("############################");
    System.out.println("");

}

// BOOK 2
static void CreateAuthor2(){
    Author Author2 = new Author();
    FirstName = "Christian";
    LastName = "Keur";
    AuthorFullName = Author2.CombineAuthorName(FirstName, LastName);
}

static void CreateBook2() {
    CreateAuthor2();
    Title  = "IOS Programming: The Big Nerd Ranch Guide";
    YearPublished = 2011;
    NumberOfPages = 563;
    Category = "Educational";
    CoverType = "Paperback";
}

static void ShowBook2() {
    System.out.println("-----                  -----");
    System.out.println("       Book 2 Details       ");
    System.out.println("-----                  -----");
    System.out.println("Title:      " + Title);
    System.out.println("Author:     " + AuthorFullName);
    System.out.println("Published:  " + YearPublished);
    System.out.println("Pages:      " + NumberOfPages);
    System.out.println("Category:   " + Category);
    System.out.println("Cover Type: " + CoverType);
    System.out.println("");
    System.out.println("############################");
    System.out.println("");
}
}//END class Book

Author.java

public class Author {
static String   FirstName;
static String   LastName;
static String   AuthorFullName;

    public String CombineAuthorName (String FirstName, String LastName){
        AuthorFullName = FirstName + " " + LastName;
        return AuthorFullName;
}
}//END class Author
  • 9
    This would be better suited to being on http://codereview.stackexchange.com/ – Evan Knowles Apr 24 '15 at 14:45
  • 1
    You probably don't want to be using static variables like that. Read [In laymans terms, what does 'static' mean in Java?](http://stackoverflow.com/questions/2649213/in-laymans-terms-what-does-static-mean-in-java) – PakkuDon Apr 24 '15 at 14:46
  • @GenericCog This question would get better reception on the Code Review site, as someone already suggested. If you like you can delete it from Stack Overflow and post it on CR instead and we'll be happy to help you improve it. – Phrancis Apr 24 '15 at 14:47
  • First dive in some java tutorials. For coding style (which is quite strict in java community as opposed to other languages), and OOP, `static` instead of `new Author()`. With some writing basics you certainly will make nice code designs, as the code itself is looking organized. – Joop Eggen Apr 24 '15 at 14:58

1 Answers1

0

I suggest

  • you use the Java coding conventions in particular use camelcase instead of TitleCase for variables and method names.
  • Don't initialise static fields when you could be using instance fields.
  • Remove fields you don't use. You don't need any fields in Author for example.
  • Use a constructor to initialise instance values rather than a method to reset static variables. Try to avoid mutable static fields.
  • Try not to repeat yourself. ShowBook1 and ShowBook2 are almost exactly the same. You could have set a field which has the book number.
  • Try to avoid classes which initialise themselves too much. All the data you need to set the books could be passed in and the BookDriver class could create many different books without having to change your Book code.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130