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);
}
}
}
}