-
Help with building a library system
Hi all..
I'm trying to build a library system that will print the following:
Title: colour of Magic
Author: Terry Pratchett
Year published: 1983
I have written the following:
Code:
public class Library
{
public static void main(String args[])
{
Catalogue usersCatalogue = new Catalogue();
String title = "Colour of Magic";
String author = "Terry Pratchett";
int year = 1983;
usersCatalogue.set(title, author, year);
System.out.println(usersCatalogue);
}
}
-------------------------------------------------------------------
Code:
public class Catalogue
{
private String title;
private String author;
private int year;
public String toString()
{
return ("Title: " + title + "\nAuthor: " + author + "\nYear published: " + year);
}
public void set(String newTitle, String newAuthor, int newYear)
{
title = newTitle;
if (newYear <0)
{
System.out.println("Error: Incorrect year or author");
System.exit(0);
}
else
{
author = newAuthor;
year = newYear;
}
}
public Catalogue(String initialTitle, String initialAuthor)
{
title = initialTitle;
author = initialAuthor;
year = 0;
}
public void setTitle(String newTitle)
{
title = newTitle;
}
public Catalogue(String initialAuthor)
{
title = "No title yet.";
year = 0;
}
public Catalogue(int initialYear)
{
title = "No title yet";
author = "No author yet";
if (initialYear < 0)
{
System.out.println("Error: Negative year.");
System.exit(0);
}
else
year = initialYear;
}
public void setYear(int newYear)
{
if (newYear <0)
{
System.out.println("Error: negative year");
System.exit(0);
}
else
year = newYear;
}
public Catalogue()
{
title = "No title yet.";
author = "No author yet.";
year = 0;
}
public String getTitle()
{
return title;
}
public String getAuthor()
{
return author;
}
public int getYear()
{
return year;
}
}
It works fine as far as I'm concerned.. The only thing I need is to be able to print more books using arrays..
Could someone please let me know of the best way of doing this?
Thanks,
Pitr.
-
Wouldn't a real card catalogue be a collection of book data? So why won't your catalogue contain Book (or Card) objects?
If you are ready for it, this would be a great project for using a map or a hash table or a skip list - the key being something derived from the title, (or the author), the entry being your Book (or Card) object for that book.
-
What I was thinking was to have a class called book.java
In it, define variables title author and year, and call them from the driver library..
I can't think on how to do this..
Any ideas?
I'm thinking
Code:
Book[] author = new Book[2];
author[1] = "Terry Pratchett";
author[2] = "Jasper Fforde";
author[3] = "Michael Marshall Smith"
Would that work?
To do a title, should I just do the same, only instead of author do title?
-
How about a Book which has a title, an author, and a year published; the class would include getters and setters for each field. Perhaps a compareTo() method to make the class Comparable, and a toString() method.
The Catalogue can have a field which is an ArrayList<Book> or Book[] to hold the book objects, and methods to add a book to its data storage, sort the data, print out the data, etc.
-
I like nspils' first idea of using a HashMap, but if retrieving a specific book isn't that important and maybe only bringing up more general search results then the array in a Catalog idea sounds simple enough.
-
the nice thing with a map is that you can store the books in some structure, then when someone wants to choose by way of authors, you use a hash of the author's name to be the key and store the book, and if by title you hash the title and have a different structure. A problem with using a hash is that you do away with searching keys with only a partial title or partial name of the author. Could do a "look up" (author table, title table) which can be searched using a partial result, then click to choose the corresponding complete name/hash key to return the book.
-
How's this?
Hi All..
I've re-written the program.. It now looks like this:
Code:
public class Library
{
public static void main(String args[])
{
Book[] booksArray = new Book[3];
Catalogue catalogue0 = new Catalogue("The Colour of Magic", "Terry Pratchett", 1982);
Book book0 = new Book(catalogue0);
booksArray[0] = book0;
Catalogue catalogue1 = new Catalogue("The Light Fantastic", "Terry Pratchett", 1986);
Book book1 = new Book(catalogue1);
booksArray[1] = book1;
Catalogue catalogue2 = new Catalogue("Equality Rights", "Terry Pratchett", 1987);
Book book2 = new Book(catalogue2);
booksArray[2] = book2;
for (int i=0; i<booksArray.length; i++)
{
System.out.println(booksArray[i]);
}
}
}
Code:
public class Catalogue
{
private String bookTitle;
private String bookAuthor;
private int bookYear;
public Catalogue(String bookTitle, String bookAuthor, int bookYear)
{
this.bookTitle = bookTitle;
this.bookAuthor = bookAuthor;
this.bookYear = bookYear;
}
public String toString()
{
return "Title: " + bookTitle + "\nAuthor: " + bookAuthor + "\nYear: " + bookYear;
}
}
Code:
public class Book
{
private Catalogue catalogue;
public Book(Catalogue catalogue)
{
this.catalogue = catalogue;
}
public Catalogue getCatalogue()
{
return this.catalogue;
}
public String toString()
{
return catalogue.toString() + "\n";
}
}
What I am trying to do and can't figure out why it doesn't work, is something like this:
In the catalogue.java
Code:
public String fillData()
{
return printWelcome;
}
and on the Library.java
Code:
Catalogue welcomeString = new Catalogue();
welcomeString.fillData();
Should that work, or does it not work like that?
-
I think this is more direct and more simple:
Code:
public class Book
{
private String bookTitle;
private String bookAuthor;
private int bookYear;
public Book (String bookTitle, String bookAuthor, int bookYear)
{
this.bookTitle = bookTitle;
this.bookAuthor = bookAuthor;
this.bookYear = bookYear;
}
public String getTitle()
{
return bookTitle;
}
public String getAuthor()
{
return bookAuthor;
}
public int getYear()
{
return bookYear;
}
public String toString()
{
return "Title: " + bookTitle + "\nAuthor: " + bookAuthor + "\nYear: " + bookYear;
}
}
and
Code:
public class Catalogue
{
private Book[] bookArray = new Book[3];
public static void main(String args[])
{
Book book0 = new Book("The Colour of Magic", "Terry Pratchett", 1982);
booksArray[0] = book0;
Book book1 = new Book ("The Light Fantastic", "Terry Pratchett", 1986);
booksArray[1] = book1;
Book book2 = new Catalogue("Equality Rights", "Terry Pratchett", 1987);
booksArray[2] = book2;
}
public void printCatalogue()
{
for( Book b : bookArray)
{
System.out.println(b.toString());
}
}
}
What are you trying to do with the "printWelcome" method?
Last edited by nspils; 10-29-2006 at 11:20 PM.
Similar Threads
-
Replies: 0
Last Post: 03-10-2006, 09:08 AM
-
By AM003295 in forum VB Classic
Replies: 4
Last Post: 08-12-2005, 09:13 PM
-
Replies: 0
Last Post: 05-17-2001, 01:19 PM
-
By Saiful in forum VB Classic
Replies: 6
Last Post: 10-15-2000, 03:18 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks