我为Author和Book编写了两个类,然后在Book类中包含了Author。没有任何用户输入,只是让我围绕Java类的方式。在我的驱动程序中,我正在尝试创建书籍对象以显示详细信息。细节结合并按预期显示,但我觉得有更好的方法。你有什么建议吗?
3个文件:
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
答案 0 :(得分:0)
我建议
camelcase
代替TitleCase
来获取变量和方法名称。Author
中不需要任何字段。ShowBook1
和ShowBook2
几乎完全相同。您可以设置一个具有图书编号的字段。