Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners arraylistexample
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


arvind
arraylistexample
1 Answer(s)      3 years and 9 months ago
Posted in : Java Beginners


write a java code to store n(say 10) book details like-title,price,bookid,author,publsher in a arraylist. also write the code to display the details of a particular bookname or bookid and there must be a mechanism for deleting a particular book details from the list and display the arraylist.


Advertisement
View Answers

April 5, 2011 at 4:53 PM


import java.util.*;

 class Book{
    int bookid;
    String title;
    String author;
    double price;
    String publisher;

    Book(int bookid,String title,String author,double price,String publisher){
        this.bookid=bookid;
        this.title=title;
        this.author=author;
        this.price=price;
        this.publisher=publisher;
    }

    public void setBookId(int bookid){
        this.bookid=bookid;
    }
    public int getBookId(){
        return bookid;
    }
    public void setTitle(String title){
        this.title=title;
    }
    public String getTitle(){
        return title;
    }
    public void setAuthor(String author){
        this.author=author;
    }
    public String getAuthor(){
        return author;
    }
    public void setPrice(double price){
        this.price=price;
    }
    public double getPrice(){
        return price;
    }
    public void setPublisher(String publisher){
        this.publisher=publisher;
    }
    public String getPublisher(){
        return publisher;
    }
}
public class BookInformation{
    public static void main(String[]args){
        ArrayList<Book> list=new ArrayList<Book>();
        Scanner input=new Scanner(System.in);
        for(int i=0;i<10;i++){
        System.out.print("Book ID: ");
        int id=input.nextInt();
        System.out.print("Title: ");
        String  title=input.next();
        System.out.print("Author: ");
        String  author=input.next();
        System.out.print("Price: ");
        double  price=input.nextDouble();
        System.out.print("Publisher: ");
        String  pub=input.next();
        list.add(new Book(id,title,author,price,pub));
        }
        System.out.print("Enter bookid whose details is to be displayed: ");
        int ide=input.nextInt();
         for(Book data: list){
         if(data.getBookId()==ide){
        System.out.println(data.getBookId()+"\t "+data.getTitle()+"\t "+data.getAuthor()+"\t "+data.getPrice()+"\t "+data.getPublisher());
         }
         }
        System.out.println();
        System.out.println("----Book Information----");
        System.out.println();
        for(Book data: list){
        System.out.println(data.getBookId()+"\t "+data.getTitle()+"\t "+data.getAuthor()+"\t "+data.getPrice()+"\t "+data.getPublisher());
        }
    }   
}


Related Tutorials/Questions & Answers:
how to do combinations in java - Java Beginners
: import java.util.*; class ArrayListExample { public static void main
Advertisements
Collection Framework - Java Interview Questions
ArrayListExample{ public static void main(String[] args){ List list = new ArrayList
Java ArrayList, Java Array List examples
java.util.ArrayList; import java.util.Iterator; public class ArrayListExample
Java ArrayList Example
Java ArrayList Example  How can we use array list in java program ?   import java.util.ArrayList; public class ArrayListExample { public static void main(String [] args){ ArrayList<String> array = new
Real Time code for Array list - Development process
ArrayListExample{ public static void main(String[] args){ ArrayList
java compilation - UML
getMarks(){ return marks; } } public class ArrayListExample
ArrayJava
java.util.*; class ArrayListExample{ public static void main(String[] args
marks - Java Beginners
; } } public class ArrayListExample{ public static void main
Collection : ArrayList Example
collection; import java.util.ArrayList; class ArrayListExample { public
Advertisements
 

 

 

DMCA.com