Hello


below is the progra... i cann access araylist in the main class function.... like
"LoanBook" see that in the code.... plz recity meeee folks... thanking uuuu
=--------------------------------------------------------------------------=

import java.util.ArrayList;
import java.util.Scanner;

// To take customer detais by using Constructor and Customer deatils
class Customer {
	
	int cusId;
	String fName;
	String lName;
	long phNo;
	int count;
	int cbid;
	public Object setcbid;

Customer(int id,String fn,String ln,long no,int count){
	this.cusId = id;
	this.fName = fn;
	this.lName = ln;
	this.phNo = no;	
	this.count=count;
}
public int getcusId(){
	return this.cusId;
	
}
public String getfname(){
	return this.fName;

}
public String getlname(){
	return this.lName;
}
public long getphNo(){
	return this.phNo;
}
public void setcbid(int i){
	this.cbid=cbid;
}
public int getcbid(){
	return this.cbid;
}
public int getcount(){
	return this.count;
	
}
//to take book details by using Constructor and get methods
class Book 
{
	int bookId;
	String title;
	String status;
	String bookType;
	public String setstatus;
	public Object bid;
Book (int bid,String title,String status,String booktype){
	this.bookId=bid;
	this.title=title;
	this.status=status;
	this.bookType=booktype;
}
public void setStatus(String s){
	this.status=status;
}
	
public int getBookId(){
	return this.bookId;
}
public String getTitle(){
	return this.title;

}
public String getStatus(){
	return this.status;
}
public String getBookType(){
	return this.bookType;
}

}
// To create a main class ti allow user to do his operations
public class Lib {
	
	public  void main(String args[]){
		
// creating an Arraylist for Customer & book
		ArrayList<Customer> cl=new ArrayList<Customer>();
		ArrayList<Book> bl=new ArrayList<Book>();
//Adding Details to class Customer		
		cl.add(new Customer(519340,"Pallavi","priya",790074455,3));
		cl.add(new Customer(519339,"Sravani","tuppu",564128374,2));
		cl.add(new Customer(519343,"Pallavi1","priya1",790074456,1));
		cl.add(new Customer(519346,"Pallavi2","priya2",790074458,0));
		cl.add(new Customer(519335,"Pallavi3","priya3",790074425,5));
		
// Adding Details to class Book		
		bl.add(new Book (51934 , "Java","Available","Technical"));
		bl.add(new Book (51933 , "WebTechnologies","Available","Technical"));
		bl.add(new Book (51934 , "Managementskills","loaned","Management"));
		bl.add(new Book (51936 , "Fiction1","Available","Fiction"));
		
		System.out.println("1. LoanBook");
		System.out.println("2. returnBook");
		System.out.println("3. getCustomerWhoLoanedBook");
		System.out.println("4. getTotalIssuedBookCount");
		
		Lib o=new Lib();
		System.out.println("Enter Your choice");
		Scanner s=new Scanner(System.in);
		int n=s.nextInt();
		switch(n)
		{
		case 1:
			
			System.out.println("Enter the Customer Id");
			int id1=s.nextInt();
			System.out.println("Enter the Book Id");
			int bid1=s.nextInt();
			boolean b1 = o.LoanBook (id1,bid1);
			if(b1)
				System.out.println("Book is Loaned Successfully");
			else
				System.out.println("Book is not loaned");
			break;
			
		case 2:
			System.out.println("Enter the Customer Id");
			int id2=s.nextInt();
			System.out.println("Enter the Book Id");
			int bid2=s.nextInt();
			Boolean b = o.ReturnBook(id2,bid2);
			if(b)
				System.out.println("Book Returned Successfully");
			else
				System.out.println("Book not returned");
			break;
			
		case 3:
			System.out.println("Enter the Book Id");
			int bid3=s.nextInt();
			String st = o.getCustomerWhoLoanedBook(bid3);
			System.out.println("The Full name of customer who owned the book is"+st);
			break;
		case 4:
			o.getTotalIssuedBookCount();
			break;
		}
	
	}
// To loan a book by giving Bookid & Customer id
 Boolean LoanBook(int cid,int bid){
		for(Customer c: cl){
	
			if(cid == cl.getcusId()){
			for(Book b : bl){
				
				if(bid == b.getBookId() && b.getStatus() == "Available"){
				
					if(c.count<5){
						b.setstatus="loaned";
						c.count++;
						c.setcbid=b.bid;
					return true;
				
					}
					return false;
				}
				return false;
			}
			
		}
	return false;				
	}

}
 // To Return a Book by giving Customer id & Book Id 
 boolean ReturnBook(int id2,int bid2){

	for(Customer c1: cl){
		
		if(cid == c1.getcusId()){
		for(Book b1 : bl){
			if(b1.getBookId()==c1.getcbid()){
				b1.setstatus="Available";
				c1.setcbid=0;
				return true;
			}
			return false;
		}
}
return false;
}
}
 // To get the details of the person who owned the book
 String getCustomerWhoLoanedBook(int bid){
	for(Book b2:bl){
		for(Customer c2: cl){
			if(c2.cbid == b2.getBookId()){
				String s=(c2.getfname()).concat(c2.getlname());
				return s;
			}
	}
	}
	}
 // to get the Count of total Issued Books
  void getTotalIssuedBookCount(){
	for(Customer c3: cl)
	{
		System.out.println("total Issued Book count is:" +c3.cbid);
	}
}
}
}

Recommended Answers

All 15 Replies

You declare

ArrayList<Customer> cl=new ArrayList<Customer>();
ArrayList<Book> bl=new ArrayList<Book>();

inside the main method, so they are local to that method. If you want to access them from all the methods of the class they need to be declared outside any one method - like the variables in your Customer and Book classes.
ps Please write your posts in normal English, especially for the benefit of the majority of DaniWeb users for whom English is a foreign language.
pps: having variables called c1 and cl, b1 and bl is probably the most ingenious stupid idea I have seen in a long time!

yaa i will do it... sorry.....

boolean ReturnBook(int id2,int bid2){

 for(Customer c1: cl){
 /* getting error here that

 2 errors found:
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 158]
Error: C:\j2sdk1.4.2\bin\Lib.java:158: cannot find symbol
symbol  : method getcusId()
location: class java.util.ArrayList<Customer>
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 185]
Error: C:\j2sdk1.4.2\bin\Lib.java:185: cannot find symbol
symbol  : variable cid
location: class Customer.Lib

*/
 
  if(cid == c1.getcusId()){
  for(Book b1 : bl){
   if(b1.getBookId()==c1.getcbid()){
    b1.setstatus="Available";
    c1.setcbid=0;
    return true;
   }
   return false;
  }
}
return false;
}
}

iam getting error in this

You haven't defined a variable called cid, so that's why it can't be found. Fix that first.
You'll have fewer errors like this if you use good descriptive names for everything, and have a consitent sty;e for capitalisation etc. A parameter list like
(int id2,int bid2)
tells you nothing - but maybe one of these is a customer ID???

import java.util.ArrayList;
import java.util.Scanner;

// To take customer detais by using Constructor and Customer deatils
class Customer {
 
 int cusId;
 String fName;
 String lName;
 long phNo;
 int count;
 int cbid;
 public Object setcbid;

Customer(int id,String fn,String ln,long no,int count){
 this.cusId = id;
 this.fName = fn;
 this.lName = ln;
 this.phNo = no; 
 this.count=count;
}
public int getcusId(){
 return this.cusId;
 
}
public String getfname(){
 return this.fName;

}
public String getlname(){
 return this.lName;
}
public long getphNo(){
 return this.phNo;
}
public void setcbid(int i){
 this.cbid=cbid;
}
public int getcbid(){
 return this.cbid;
}
public int getcount(){
 return this.count;
 
}
//to take book details by using Constructor and get methods
class Book 
{
 int bookId;
 String title;
 String status;
 String bookType;
 public String setstatus;
 public Object bid;
Book (int bid,String title,String status,String booktype){
 this.bookId=bid;
 this.title=title;
 this.status=status;
 this.bookType=booktype;
}
public void setStatus(String s){
 this.status=status;
}
 
public int getBookId(){
 return this.bookId;
}
public String getTitle(){
 return this.title;

}
public String getStatus(){
 return this.status;
}
public String getBookType(){
 return this.bookType;
}

}
// To create a main class ti allow user to do his operations
class Lib {
  // creating an Arraylist for Customer & book
 ArrayList<Customer> cl=new ArrayList<Customer>();
  ArrayList<Book> bl=new ArrayList<Book>();
 public static void main(String args[]){
  

  /*

getting these errors in below statements 

15 errors found:
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 85]
Error: C:\j2sdk1.4.2\bin\Lib.java:85: inner classes cannot have static declarations
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 90]
Error: C:\j2sdk1.4.2\bin\Lib.java:90: non-static variable cl cannot be referenced from a static context
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 91]
Error: C:\j2sdk1.4.2\bin\Lib.java:91: non-static variable cl cannot be referenced from a static context
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 92]
Error: C:\j2sdk1.4.2\bin\Lib.java:92: non-static variable cl cannot be referenced from a static context
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 93]
Error: C:\j2sdk1.4.2\bin\Lib.java:93: non-static variable cl cannot be referenced from a static context
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 94]
Error: C:\j2sdk1.4.2\bin\Lib.java:94: non-static variable cl cannot be referenced from a static context
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 97]
Error: C:\j2sdk1.4.2\bin\Lib.java:97: non-static variable bl cannot be referenced from a static context
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 97]
Error: C:\j2sdk1.4.2\bin\Lib.java:97: non-static variable this cannot be referenced from a static context
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 98]
Error: C:\j2sdk1.4.2\bin\Lib.java:98: non-static variable bl cannot be referenced from a static context
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 98]
Error: C:\j2sdk1.4.2\bin\Lib.java:98: non-static variable this cannot be referenced from a static context
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 99]
Error: C:\j2sdk1.4.2\bin\Lib.java:99: non-static variable bl cannot be referenced from a static context
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 99]
Error: C:\j2sdk1.4.2\bin\Lib.java:99: non-static variable this cannot be referenced from a static context
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 100]
Error: C:\j2sdk1.4.2\bin\Lib.java:100: non-static variable bl cannot be referenced from a static context
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 100]
Error: C:\j2sdk1.4.2\bin\Lib.java:100: non-static variable this cannot be referenced from a static context
File: C:\j2sdk1.4.2\bin\Lib.java  [line: 107]
Error: C:\j2sdk1.4.2\bin\Lib.java:107: non-static variable this cannot be referenced from a static context


*/
//Adding Details to class Customer  
  cl.add(new Customer(519340,"Pallavi","priya",790074455,3));
  cl.add(new Customer(519339,"Sravani","tuppu",564128374,2));
  cl.add(new Customer(519343,"Pallavi1","priya1",790074456,1));
  cl.add(new Customer(519346,"Pallavi2","priya2",790074458,0));
  cl.add(new Customer(519335,"Pallavi3","priya3",790074425,5));
  
// Adding Details to class Book  
  bl.add(new Book (51934 , "Java","Available","Technical"));
  bl.add(new Book (51933 , "WebTechnologies","Available","Technical"));
  bl.add(new Book (51934 , "Managementskills","loaned","Management"));
  bl.add(new Book (51936 , "Fiction1","Available","Fiction"));
  
  System.out.println("1. LoanBook");
  System.out.println("2. returnBook");
  System.out.println("3. getCustomerWhoLoanedBook");
  System.out.println("4. getTotalIssuedBookCount");
  
  Lib o=new Lib();
  System.out.println("Enter Your choice");
  Scanner s=new Scanner(System.in);
  int n=s.nextInt();
  switch(n)
  {
  case 1:
   
   System.out.println("Enter the Customer Id");
   int id1=s.nextInt();
   System.out.println("Enter the Book Id");
   int bid1=s.nextInt();
   boolean b1 = o.LoanBook (id1,bid1);
   if(b1)
    System.out.println("Book is Loaned Successfully");
   else
    System.out.println("Book is not loaned");
   break;
   
  case 2:
   System.out.println("Enter the Customer Id");
   int id2=s.nextInt();
   System.out.println("Enter the Book Id");
   int bid2=s.nextInt();
   Boolean b = o.ReturnBook(id2,bid2);
   if(b)
    System.out.println("Book Returned Successfully");
   else
    System.out.println("Book not returned");
   break;
   
  case 3:
   System.out.println("Enter the Book Id");
   int bid3=s.nextInt();
   String st = o.getCustomerWhoLoanedBook(bid3);
   System.out.println("The Full name of customer who owned the book is"+st);
   break;
  case 4:
   o.getTotalIssuedBookCount();
   break;
  }
 
 }
// To loan a book by giving Bookid & Customer id
 Boolean LoanBook(int cid,int bid){
  for(Customer c: cl){
 
   if(cid==c.getcusId()){
   for(Book b : bl){
    
    if(bid == b.getBookId() && b.getStatus() == "Available"){
    
     if(c.count<5){
      b.setstatus="loaned";
      c.count++;
      c.setcbid=b.bid;
     return true;
    
     }
        }
    return false;
   }
    
  }
return false;  
 }
  return false;
 }
 // To Return a Book by giving Customer id & Book Id 
 boolean ReturnBook(int id2 , int bid2){

 for(Customer c1: cl){
  
  if(id2 == c1.getcusId()){
  for(Book b1 : bl){
   if(b1.getBookId()==c1.getcbid()){
    b1.setstatus="Available";
    c1.setcbid=0;
    return true;
   }
   
  }
  return false;
}

}
return false;
 }
 // To get the details of the person who owned the book
 String getCustomerWhoLoanedBook(int bid){
 for(Book b2:bl){
  for(Customer c2: cl){
   if(c2.cbid == b2.getBookId()){
    String s=(c2.getfname()).concat(c2.getlname());
    return s;
   }
 return "NILL";
  }
 }
 return "NILL";
 }
 // to get the Count of total Issued Books
  void getTotalIssuedBookCount(){
 for(Customer c3: cl)
 {
  System.out.println("total Issued Book count is:" +c3.cbid);
 }
}
}
}

Yes. Your variables are all instance variables, but main is (must be) a static method, so it doesn't have an instance to hold the values of those variables (Google that if it's not clear).
A good solution is (1) to create a new instance of Lib and (2) take all that code out of main and put it in an ordinary method called, eg, runUserInterface().
So now you structure for Lib looks like this:

class Lib...
   public static void main...
      Lib myLibrary = new Lib();
      myLibrary.runUserInterface();
   }

   void runUserInterface() {
      // all the code that's currently in main
   }

Can you plz compile all the code and paste it here.... iam little bit confused..... thanking u so much

No way. This is your program. I'll advise you, but you have to do the work.

i did what u told but same problem persists at main statement and at Lib object.....

exaxtly whts the prblm????

The error message says "inner class" - that means a class that's declared inside another class. I don't think you intended that. That happens because you have the { and } matched up wrong. Your class Customer looks like its missing a closing }, so everything after that is still inside the Customer class rather than being at the same level as it.
To avoid this problem indent your code properly - then you can see how your {} are matched up.

ps: Hi @115harman. Please feel free to contribute here, but don't forget that we have to help maroom learn to fix his own problems, and not just tell him the answer.

hey iam using drjava... to compile my program..... rectified all errors well worked....

but when i clicked run in drjava it showed me error " Static Error: This class does not have a static void main method accepting String[].
"

i ignored it and typed java Lib
it worked..... and got the output

plz help to rectify this run time error..

Sorry, I use Eclipse and don't know DrJava - presumably there's some kind of project setup where you identify the main class?

thank U... vey kind of u... for the suggestion....

i too used... eclipse.... it get succesfully run..... aby way thanks so much to this forum and U.....

OK, glad to help. Mark this "solved" now?
I'm going to dinner now. Bye.
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.