2 Replies - 21467 Views - Last Post: 04 January 2011 - 05:05 AM Rate Topic: -----

#1 Dark Samus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-January 11

Java library system implementing arrays for books and library class

Posted 04 January 2011 - 03:41 AM

Greetings!

I am currently working on a Java programme that will contain Books and Journals. It will also allow access for members of staff and students to take out books and journals.

So far I think I have implemented nearly all the classes/objects, however I am stuck on implementing arrays for the Library class. I keep getting compiler errors for adding a book/journal in the Library class.

Am I right in assuming that the BookInformation class is where the array is stored for different books.
I have tried multiple solutions (e.g. changing around field names). I am assuming I keep getting compiler errors, because I haven’t inserted any book information.

//Cannot find symbol – constructor BookInformation(java.lang.String,java.lang.String,java.lang.String,java.lang.String)

As you can see I am using an array list, however I would be willing to use normal arrays if there is a simple expl
aination with a working example as I am having difficulty in understanding the theory and work better with examples.

Please note that this is my very first Java programme.

This is what I have done so far.

Book Information

import java.util.*;
class Books{
public String title;
public String author;
public int year;
public double price;
final static int ARRAY_SIZE =10;

public Books(){}
public Books(String book,String author, int year,double price) {
super();
this.title=title;
this.author= author;
this.year = year;
this.price=price;
}
public String gettitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getYear() {
return year;
}
public double getPrice() {
return price;
}
}
public class BookInformation {
public static void main(String[] args) throws Exception {
float[] array = new float [ARRAY_SIZE];


List<Books> list = new ArrayList<Books>();
list.add(new Books("Gitanjali","Rabindra Nath Tagore",1912,200));
list.add(new Books("Godan","Munshi Premchand",1936,300));
list.add(new Books("My Experiments With Truth","Mahatma Gandhi",1927,150));
list.add(new Books("Macbeth","William Shakespeare",1623,400));
list.add(new Books("My Truth","Indira Gandhi",1980,145));
Scanner input=new Scanner(System.in);
System.out.println("Enter book name:");
String st=input.nextLine();

for (Books s : list){
if(st.equals(s.gettitle())){
System.out.println(s.gettitle()+" " +s.getAuthor()+" " +s.getYear()+" "+s.getPrice());
}
}
}
}



Library

import java.util.Scanner;

public class Library
{
	private String libraryName;
	private Borrower[] libraryUsers = new Borrower[20];
	private BookInformation [] libraryItems = new BookInformation[2];
	Scanner input = new Scanner(System.in);
	int x=0;
	int i=0;
	
	public Library(String libraryName)
	{
		this.libraryName=libraryName;
	}
	
	private String inputString(String prompt) { 
			
			System.out.println(prompt);	 
			return input.next();
		}
	
	private int inputInt(String prompt) {	 
	
		int val = -1;	 
			for(;;)/> 
			{		  
			System.out.println(prompt);		  
				try {			   
					val = Integer.parseInt(input.next());			   
					break;		  
					} 
				catch (Exception ex) {			   
					System.out.println("Invalid value, please try again.");		  
					}	 
			}	 
		return val;
		}
	
	private double inputDouble(String prompt) {	 
	
		double val = -1.00;	 
			for(;;)/> 
			{		  
			System.out.println(prompt);		  
				try {			   
					val = Double.parseDouble(input.next());			   
					break;		  
					} 
				catch (Exception ex) {			   
					System.out.println("Invalid value, please try again.");		  
					}	 
			}	 
		return val;
		}
		
	public void addUser(int choice)
	{
		
		if(choice==1)
		{
			String firstName = inputString("Please enter your first name ");
			String lastName = inputString("Please enter your last name ");
			String title = inputString("Please enter your title ");
			String dateOfBirth = inputString("Please enter your date of birth ");
			String homeAddress = inputString("Please enter your home address ");
			String phoneNumber = inputString("Please enter your phone number ");
			int barCode = inputInt("Please enter your library card barcode");
			String studentId = inputString("Please enter your Student ID ");
					
			StudentBorrower studentBor = new StudentBorrower(firstName, lastName, title, dateOfBirth, homeAddress, phoneNumber, barCode, studentId);
			libraryUsers[x]=studentBor;
			x++;
		}

		
		else if(choice==2)
		{

			String firstName = inputString("Please enter your first name ");
			String lastName = inputString("Please enter your last name ");
			String title = inputString("Please enter your title ");
			String dateOfBirth = inputString("Please enter your date of birth ");
			String homeAddress = inputString("Please enter your home address ");
			String phoneNumber = inputString("Please enter your phone number ");
			int barCode = inputInt("Please enter your library card barcode");
			String studentId = inputString("Please enter your Student ID ");
			
			StaffBorrower staffBor = new StaffBorrower(firstName, lastName, title, dateOfBirth, homeAddress, phoneNumber, barCode, studentId);
			libraryUsers[x]=staffBor;
			x++;
		}
	
	}
	
	public void addItem(int choice)
	{
		if(choice==1)
		{

			String title = inputString("Please enter your item code ");
			String author = inputString("Please enter the items due date ");
			String year = inputString("Please enter your items title ");
			String price = inputString("Please enter your books title ");
			
			//Cannot find symbol – constructor BookInformation(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
			BookInformation book = new BookInformation(title,author,year,price);
			libraryItems[i] = book;
			i++;
		
		}
		
		else if(choice==2)
		{
			String title = inputString("Please enter your item code ");
			String author = inputString("Please enter the items due date ");
			String pages = inputString("Please enter your items title ");
			String refNumber = inputString("Please enter your journals title ");	
			
			Journal jnal = new Journal(title,author,pages,refNumber);
			libraryItems[x] = dvd;
			i++;
		}

   }
	
	public void getBorrowers()
	{
		for(int x=0; x<libraryUsers.length; x++)
		{
			if (libraryUsers[x] == null)   
			{	 
				break;
			}	
			else	
			{
				System.out.println(libraryUsers[x].toString());
			}
		}
	}
	
	public void getItems()
	{
		for(int x=0; x<libraryItems.length; x++)
		{
		 System.out.println(libraryItems[x].toString());
		}
	}
	
	public Person[] searchUsers()
	{
		System.out.println("Please enter the surname of the user you wish to find");
		return null;
	}
	
	public BookInformation[] searchItems()
	{
		return null;
	}
	
	public int textMenu()
	{
		
		System.out.println("WELCOME TO " + libraryName + " LIBRARY" + "\n");
		System.out.println("To add a User, press 1" + "\n");
		System.out.println("To add an Item, press 2" + "\n");
		System.out.println("To list the users of this library, press 3" + "\n");
		System.out.println("To list the items in this library, press 4" + "\n");
		System.out.println("To search for a user, press 5" + "\n");
		System.out.println("To search for an Item, press 6" + "\n");
		System.out.println("To exit the system, press 7" + "\n");
		
		int choice = input.nextInt();
		return choice;
	}

}


Person

public abstract class Borrower extends Person{
	
  protected BookInformation [] itemsBorrowed;
  private double currentFine;
  private int barCode;
 
  public Borrower(String FirstName, String LastName,
 				  String homeAddress){
 					  
	  super(FirstName, LastName, homeAddress);
	  this.stuNumber=stuNumber;
  }
  
  public BookInformation [] getItemsBorrowed()
  {
	  return itemsBorrowed;
  }

  public double getCurrentFine()
  {
	  return currentFine;
  }
	
  public int getBarCode()
  {
	  return barCode;
  }

  public void incrementFine(double addFine)
  {
	  currentFine = currentFine+addFine;
  }
  
  public void decrementFine(double reduceFine)
  {
	  currentFine = currentFine-reduceFine;
  }
  
  public abstract void addItem(int choice);
  
  public void removeItem(BookInformation[] item, int index)
  {
	for(int i =0; i <item.length; i ++)
	{
	item[index]= null;
	}
  }
  
  public String toString()
  {
	  
		String borDetails =   super.toString() +
							 "Bar code: " + barCode +"\n" +
							 "Items borrowed: " + itemsBorrowed +"\n";
		return borDetails;
  }
}



Student Borrower

import java.util.Scanner;

public class StudentBorrower extends Borrower
{
	private final int maxItems = 7;
	private final double maxFine = 5.00;
	private String studentId;
	Scanner input = new Scanner(System.in);
	int x=0;
	int i=0;
	
	public StudentBorrower(String FirstName, String LastName, String title,
 				 		   String dateOfBirth, String homeAddress, String phoneNumber, 
 						   int barCode, String studentId){
			  
			  super(FirstName,  LastName, homeAddress
			  );
			  this.studentId=studentId;
	}
	
	public void setId(String studentId)
	{
		this.studentId=studentId;
	}
	
	public int getMaxItems()
	{
		return maxItems;
	}
	
	public double getMaxFine()
	{
		return maxFine;
	}
	
	public String getID()
	{
		return studentId;
	}
	

	
	public void addItem(int choice)
	{
		if(choice==1 || choice==2 && maxItems>7)
		{
			System.out.println("The maximum number of items allowed is 7" + "\n" + "You currently have " + maxItems +
							   "out at the moment");
		}
		
		else if(choice==1 || choice==2 && maxFine>5.00)
		{
			System.out.println("The maximum fine allowed is 5.00" + "\n" + "You currently have " + maxFine +
							   "worth of fines");
		}
		
		else if(choice==1 && maxItems<7 && maxFine<5.00)
		{
			System.out.println("Please enter the title of the book you wish to take out ");
			String title = input.next();
			
			
			/*
			Book book = new Book(itemCode, dueDate, homeLibrary, title, bookTitle, bookAuthor, bookPublisher, bookId);
			itemsBorrowed[i] = book;
			i++;*/
		}		
		
		else if(choice==2 && maxItems<7 && maxFine<5.00)
		{
				
			/*
			Dvd dvd = new Dvd(itemCode, dueDate, homeLibrary, title, dvdTitle, dvdDirector, dvdLength, dvdId);
			itemsBorrowed[x] = dvd;
			x++;*/
		}
	}
}


Borrower
public abstract class Borrower extends Person{
	
  protected BookInformation [] itemsBorrowed;
  private double currentFine;
  private int barCode;
 
  public Borrower(String FirstName, String LastName,
 				  String homeAddress){
 					  
	  super(FirstName, LastName, homeAddress);
	  this.stuNumber=stuNumber;
  }
  
  public BookInformation [] getItemsBorrowed()
  {
	  return itemsBorrowed;
  }

  public double getCurrentFine()
  {
	  return currentFine;
  }
	
  public int getBarCode()
  {
	  return barCode;
  }

  public void incrementFine(double addFine)
  {
	  currentFine = currentFine+addFine;
  }
  
  public void decrementFine(double reduceFine)
  {
	  currentFine = currentFine-reduceFine;
  }
  
  public abstract void addItem(int choice);
  
  public void removeItem(BookInformation[] item, int index)
  {
	for(int i =0; i <item.length; i ++)
	{
	item[index]= null;
	}
  }
  
  public String toString()
  {
	  
		String borDetails =   super.toString() +
							 "Bar code: " + barCode +"\n" +
							 "Items borrowed: " + itemsBorrowed +"\n";
		return borDetails;
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java library system implementing arrays for books and library class

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Java library system implementing arrays for books and library class

Posted 04 January 2011 - 04:04 AM

in class Library you are calling a BookInformation constructor which takes four string parameters
 BookInformation book = new BookInformation(title,author,year,price);

you don't have such a constructor in class BookInformation

however, you do have such a constructor in class Books
public Books(String book,String author, int year,double price) {


should you be that contructor?
Was This Post Helpful? 1
  • +
  • -

#3 Dark Samus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-January 11

Re: Java library system implementing arrays for books and library class

Posted 04 January 2011 - 05:05 AM

View Posthorace, on 04 January 2011 - 03:04 AM, said:

in class Library you are calling a BookInformation constructor which takes four string parameters
 BookInformation book = new BookInformation(title,author,year,price);

you don't have such a constructor in class BookInformation

however, you do have such a constructor in class Books
public Books(String book,String author, int year,double price) {


should you be that contructor?


I am happy to confirm the Library programe now works with no compiler errors now. It works (I think)

I removed the BookInformation class name to allow the class to be called Books. I did the same with the LibraryItem arrays in the Library class.

Thank you very much for taking the time to help me with my fustrating problem. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1