Patron.class
import java.awt.print.Book;
public class Patron {
public String patron1;
public Book book1, book2, book3;
public Patron (){
book1 = null;
book2 = null;
book3 = null;
}
public Patron(String yourName){
patron1 = yourName;
book1 = null;
book2 = null;
book3 = null;
}
public void setName(String yourName){
patron1 = yourName;
}
public String getName(){
return patron1;
}
public boolean borrowBook(Book book){
if (book1 != null){
book1 = book;
return true;
}
if(book2 != null){
book2 = book;
return true;
}
if(book3 != null){
book3 = book;
return true;
}
return false;
}
public boolean returnBook(String title){
if (book1.equals(title)){
book1 = null;
return true;
}
if (book2.equals(title)){
book2 = null;
return true;
}
if (book3.equals(title)){
book3 = null;
return true;
}
return false;
}
boolean hasBorrowed(String titlex){
return false;
}
public String toString(){
String str;
str = "Name: " + "\n" + patron1 + "\n" +
book1 + "\n" +
book2 + "\n" +
book3 + "\n";
return str;
}
}
Book class
public class Book {
public String title;
public String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle(String title){
return title;
}
public String getAuthor(String author){
return author;
}
public String toString(){
String str;
str = "Author:\t" + author + "\n" + "Title:\t" + title + "\n";
return str;
}
}
tester.java
import java.util.Scanner;
public class Tester {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
//Create 2 Patron objects to use in the test
Patron patron1 = new Patron("John Doe");
Patron patron2 = new Patron();
//Read in and save the info for the first book
System.out.print("Please enter the title for John Doe's first book: ");
String title1 = reader.nextLine();
System.out.print("Please enter the author for John Doe's first book: ");
String author1 = reader.nextLine();
Book book1 = new Book(title1, author1);
boolean result1 = patron1.borrowBook(book1);
if (!result1) System.err.println("Book 1 for John Doe failed.");
//Use the toString method to print the info for Patron John Doe after 1 book
System.out.println("\n" + patron1.toString() + "\n");
//Read in and save the info for the second book
System.out.print("Please enter the title for John Doe's second book: ");
String title2 = reader.nextLine();
System.out.print("Please enter the author for John Doe's second book: ");
String author2 = reader.nextLine();
Book book2 = new Book(title2, author2);
boolean result2 = patron1.borrowBook(book2);
if (!result2) System.err.println("Book 2 for John Doe failed.");
//Use the toString method to print the info for Patron John Doe after 2 books
System.out.println("\n" + patron1.toString() + "\n");
//Read in and save the info for the third book
System.out.print("Please enter the title for John Doe's third book: ");
String title3 = reader.nextLine();
System.out.print("Please enter the author for John Doe's third book: ");
String author3 = reader.nextLine();
Book book3 = new Book(title3, author3);
boolean result3 = patron1.borrowBook(book3);
if (!result3) System.err.println("Book 3 for John Doe failed.");
//Use the toString method to print the info for Patron John Doe
System.out.println("\n" + patron1.toString() + "\n");
//Read in and attempt to save the info for a fourth book
System.out.print("Please enter the title for John Doe's fourth book: ");
String title4 = reader.nextLine();
System.out.print("Please enter the author for John Doe's fourth book: ");
String author4 = reader.nextLine();
Book book4 = new Book(title4, author4);
//This attempt to borrow a fourth book should fail
boolean result4 = patron1.borrowBook(book4);
if (!result4) System.out.println("Book 4 for John Doe failed.\n" +
"This message should print since 3" +
" books max can be borrowed.\n");
//Read for a book title that is already borrowed
System.out.print("Please enter the title to return from John Doe's borrowed list: ");
String titlex = reader.nextLine();
//Check to see if this title has already been borrowed
if (patron1.hasBorrowed(titlex)){
//Try to return this title
boolean resultx = patron1.returnBook(titlex);
//Check to see if this title was successfully returned
if (resultx){
System.out.println(titlex + " has been returned.");
//Now there should be room for the fourth book
boolean result4x = patron1.borrowBook(book4);
if (result4x)
System.out.println("John Doe's fourth book successfully checked out.");
else
System.err.println("Book 4 for John Doe failed again.");
}
else
System.err.println(titlex + " failed to be returned.");
}
else {
System.err.println("Title not found in John Doe's checked out list.");
}
//Use the toString method to print the info for Patron John Doe
System.out.println("\n" + patron1.toString() + "\n");
//Read in your name and save it in patron2
System.out.print("Please enter your name: ");
String yourName = reader.nextLine();
patron2.setName(yourName);
//Use the toString method to print the info for Patron with your name
System.out.println("\n" + patron2.toString() + "\n");
}
}
The assignment was to create the book and patron class, the tester.java file was provided. So I am not sure were I have gone wrong but any suggestions would be appreciated.
Attached File(s)
-
CINS 136 Project 5 Summer 2012.pdf (90.41K)
Number of downloads: 28

New Topic/Question
Reply



MultiQuote




|