图和我自己写的代码如下,大部分都写完了,只有一些问题希望大神帮忙修改一下就好,谢谢!
public class MyBook {
//class Book, which is the parent class of Paperback and Hardcover
class Book{
private String isbn;
private int publicationYear;
private int pages;
public Book(String isbn, int pages) {
this.isbn=isbn;
this.pages=pages;
}
public Book() {
}
//private variables need getter and setter methods
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public int getPublicationYear() {
return publicationYear;
}
public void setPublicationYear(int publicationYear) {
this.publicationYear = publicationYear;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
}
//abstract class Paperback, which is the parent class of Nonfiction
//and Fiction
abstract class Paperback extends Book{
public Paperback() {
super();
}
public abstract String coverArt();
}
//class Fiction, which is the parent class of Novel and Anthology
//and the child class of Paperback
class Fiction extends Paperback{
public String coverArt() {
return "Method coverArt called from Fiction";
}
public Fiction() {
super();
}
public String genre() {
return "Method genre called from Fiction";
}
}
//class Nonfiction, which is the child class of the Paperback,
//and has aggregation with Category
class Nonfiction extends Paperback{
private Category deweyClassification;
public Nonfiction() {
super();
}
//private variables need getter and setter methods
public Category getDeweyClassification() {
return deweyClassification;
}
public void setDeweyClassification(Category deweyClassification) {
this.deweyClassification = deweyClassification;
}
public String topic() {
return "Method topic called from Nonfiction";
}
//coverArt is an override method
@Override
public String coverArt() {
return null;
}
}
//class Category, which has aggregation with Nonfiction,
//and it also has reflexive association
class Category{
// reflexive association
private Category subCategory;
private Category superCategory;
private String category;
public String sort() {
return "Method sort called from Category";
}
//private variables need getter and setter methods
public Category getSubCategory() {
return subCategory;
}
public void setSubCategory(Category subCategory) {
this.subCategory = subCategory;
}
public Category getSuperCategory() {
return superCategory;
}
public void setSuperCategory(Category superCategory) {
this.superCategory = superCategory;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
//class Hardcover, which is the child class of Book
class Hardcover extends Book{
public Hardcover() {
super();
}
//binding is a final method
final public void binding() {
}
}
//class Classic, which is a child class of Hardcover
class Classic extends Hardcover{
private int origPubYear=1860;
private Author theAuthor;
private Publisher[] bookOublisher;
public Classic() {
super();
}
public String createNotes() {
return "Method createNotes called from Classis";
}
//private variables need getter and setter methods
public int getOrigPubYear() {
return origPubYear;
}
public void setOrigPubYear(int origPubYear) {
this.origPubYear = origPubYear;
}
public Author getTheAuthor() {
return theAuthor;
}
public void setTheAuthor(Author theAuthor) {
this.theAuthor = theAuthor;
}
public Publisher[] getBookOublisher() {
return bookOublisher;
}
public void setBookOublisher(Publisher[] bookOublisher) {
this.bookOublisher = bookOublisher;
}
}
//class Publisher, which has association with Classic and Author
//One publisher can have 0-10 classics and 1-10 authors
class Publisher{
private String name;
private String address;
private Classic[] classicsCatalog;
public Publisher(String name, String address) {
this.name=name;
this.address=address;
}
public String printLetterhead() {
return "Method printLetterhead called from Publisher";
}
//private variables need getter and setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Classic[] getClassicsCatalog() {
return classicsCatalog;
}
public void setClassicsCatalog(Classic[] classicsCatalog) {
this.classicsCatalog = classicsCatalog;
}
}
//class Contract, which has association with Publisher and Author
//Publisher and author are combined because of the contract
class Contract{
private String date;
private Publisher publisherInfo;
private Author authorInfo;
public Contract(String date, Publisher publisherInfo, Author authorInfo) {
this.date=date;
this.publisherInfo=publisherInfo;
this.authorInfo=authorInfo;
}
//private variables need getter and setter methods
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Publisher getPublisherInfo() {
return publisherInfo;
}
public void setPublisherInfo(Publisher publisherInfo) {
this.publisherInfo = publisherInfo;
}
public Author getAuthorInfo() {
return authorInfo;
}
public void setAuthorInfo(Author authorInfo) {
this.authorInfo = authorInfo;
}
}
//class Author, which has association with Publisher and Contract,
//and has aggregation with Novel, Classic and Story respectively
//One Author can have 1-10 stories, and 1-10 novels, and 0-10 classics
class Author{
private String name="Unknown";
private String address;
private int age;
public Author(String name, String address, int age) {
this.name=name;
this.address=address;
this.age=age;
}
public Author() {
}
public String write() {
return "Method write called from Author";
}
//private variables need getter and setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//class Novel, which is the child class of Fiction, and
//has aggregation with Author, composition with Series
//There are many authors who has many novels, and there
//are many series composite many novels
class Novel extends Fiction{
private Author theAuthor;
private Series mySeries;
public Novel() {
super();
}
public String theme() {
return "Method theme called from Novel";
}
//private variables need getter and setter methods
public Author getTheAuthor() {
return theAuthor;
}
public void setTheAuthor(Author theAuthor) {
this.theAuthor = theAuthor;
}
public Series getMySeries() {
return mySeries;
}
public void setMySeries(Series mySeries) {
this.mySeries = mySeries;
}
}
//class Series, which has composition with Novel
class Series{
private String seriesName;
public String theme() {
return "Method theme called from Series";
}
public String getSeriesName() {
return seriesName;
}
public void setSeriesName(String seriesName) {
this.seriesName = seriesName;
}
}
//class Story,which has aggregation with Author, and
//has generation with Anthology. There are 1-10 author who
//have 1-10 stories, and one anthology must have 5-10 stories
class Story{
private Author theAuthor;
public String plot() {
return "Method plot called from Story";
}
//private variables need getter and setter methods
public Author getTheAuthor() {
return theAuthor;
}
public void setTheAuthor(Author theAuthor) {
this.theAuthor = theAuthor;
}
}
//class Anthology, which is the child class of Fiction
//and has generation with Story
class Anthology extends Fiction{
//the lower limit of Story in Anthology is 5
private Story[] story=new Story[5];
public Anthology() {
super();
}
public String storyOrder() {
return "Method storyOrder called from Anthology";
}
//private variables need getter and setter methods
public Story[] getStory() {
return story;
}
public void setStory(Story[] story) {
this.story = story;
}
public void test(Story[] story) throws Exception {
this.story = story;
for(int i=0;i<story.length;i++) {
if(story[i]==null)
throw new Exception("The number of stories should be larger than 5");
}
}
}
//main function for test
public static void main(String[] args) {
MyBook book=new MyBook();
// Nonfiction, Category, Paperback
var history = book.new Nonfiction();
var deweyOne = book.new Category();
deweyOne.setCategory("History");
var deweyTwo = book.new Category();
deweyTwo.setCategory("French");
deweyTwo.setSuperCategory(deweyOne);
deweyOne.setSubCategory(deweyTwo);
var deweyThree = book.new Category();
deweyThree.setCategory("Revolution");
deweyThree.setSuperCategory(deweyTwo);
deweyTwo.setSubCategory(deweyThree);
System.out.println(deweyThree.sort());
history.setDeweyClassification(deweyThree);
System.out.println(history.topic());
System.out.println(history.coverArt());
// Series, Novel
var anne = book.new Author("Anne Rice", "USA", 60);
System.out.println(anne.write());
var interviewWithVampire = book.new Novel();
interviewWithVampire.setTheAuthor(anne);
System.out.println(interviewWithVampire.coverArt());
System.out.println(interviewWithVampire.genre());
// Anthology, Story
var vampireInParis = book.new Story();
vampireInParis.setTheAuthor(anne);
System.out.println(vampireInParis.plot());
var vampiresEverywhere = book.new Anthology();
System.out.println(vampiresEverywhere.storyOrder());
}
}