Share on Google+Share on Google+

arvind
arraylistexample
1 Answer(s)      6 years 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.

Ads

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
Tutorials   
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized

Ads

 
Advertisement null

Ads