0% found this document useful (0 votes)
251 views

Collection Hashmap PDF

This document describes a Java program that performs operations on a Map containing Book objects. The operations are: 1. Create 5 Book objects with attributes like name, price, author etc and add them to a Map with integer keys. 2. Display all the Map entries using a for-each loop. 3. Read a key from the user and display the corresponding Book object. 4. Reduce the price by 10% for books with a particular publication and display all objects with the updated prices.

Uploaded by

ANIL KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
251 views

Collection Hashmap PDF

This document describes a Java program that performs operations on a Map containing Book objects. The operations are: 1. Create 5 Book objects with attributes like name, price, author etc and add them to a Map with integer keys. 2. Display all the Map entries using a for-each loop. 3. Read a key from the user and display the corresponding Book object. 4. Reduce the price by 10% for books with a particular publication and display all objects with the updated prices.

Uploaded by

ANIL KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

​Write a Java program menu driven to perform the following operations using Map

* Create a class called Book with the following instance variable

Name,price ,author name , isbn no; ,publication

* Create 5 Objects of book class using array of objects

* Add all the object in to the Map using key as integer(book id) and value as Book Object

* Display all the map object using for each

* Read any key from the user and display the particular book object based on key

* Reduce the price by 10% for a particular publication books and display all object with reduced price.

public​ ​class​ Book


{
int​ bookid;
String name;
int​ isbn;
String authorname, publication;
float​ price;
public​ Book(​int​ bookid, String name, ​int​ isbn, String authorname, String publication,
float​ price)
{
this​.bookid = bookid;
this​.name = name;
this​.isbn = isbn;
this​.authorname = authorname;
this​.publication = publication;
this​.price = price;
}
public​ ​void​ display()
{
System.out.println(bookid + name + isbn + authorname + publication + price);
}
public​ String toString() {
return​ ​"Book [bookid="​ + bookid + ​", name="​ + name + ​", isbn="​ + isbn + ​",
authorname="​ + authorname
+ ​", publication="​ + publication + ​", Price"​ +price+ ​"]"​;
}
}
package​ DAY11_bookcollection;
import​ java.util.HashMap;
import​ java.util.Scanner;
import​ java.util.Map.Entry;
public​ ​class​ BookObj
{
@SuppressWarnings(​"unlikely-arg-type"​)
public​ ​static​ ​void​ main(String[] args)
{
boolean​ t = ​true​;
int​ bid, isbn, k, p;
String name,aname,pub;
float​ pri,dis;
HashMap<Integer,Book> hbook = ​new​ HashMap<>();
@SuppressWarnings(​"resource"​)
Scanner s = ​new​ Scanner(System.in);
Book[] b = ​new​ Book[2];
System.out.println(​"Creation of Book Object"​);
for​(​int​ i=1; i<b.length; i++)
{
System.out.println(​"Enter Book ID, Book Name, Book ISBN, Author
Name, Publication and Price of book "​+i);
bid = s.nextInt();
name = s.next();
isbn = s.nextInt();
aname = s.next();
pub = s.next();
pri = s.nextFloat();
b[i] = ​new​ Book(bid, name, isbn, aname, pub, pri);
hbook.put(b[i].bookid, b[i]);
}
while​(t)
{
System.out.println(​"Press 1 for Displaying book object from
HashMap"​);
System.out.println(​"Press 2 to read input key from user and display
that key value"​);
System.out.println(​"Press 3 to give discount of 10% which exceeds
price more than 999 rs"​);
System.out.println(​"Press 4 to EXIT"​);
int​ c = s.nextInt();
switch​(c)
{
case​ 1:System.out.println(​"Displyaing Book Objects using
hashmap key and value"​);
for​( Entry<Integer, Book> m : hbook.entrySet())
{
//System.out.println(m.getKey() + " "+ m.getValue());
System.out.println(m);
}
break​;
case​ 2: System.out.println(​"Enter the any key to fetch the value
if exisits from 0 to 1"​);
k = s.nextInt();
System.out.println(​"The value is:
"​+hbook.get(b[k].bookid));
break​;
case​ 3:System.out.println(​"Enter the which publications you
need discount"​);
p = s.nextInt();
System.out.println(​"The value is:
"​+hbook.get(b[p].publication));
if​(b[p].price > 999)
{
dis = (​float​) (b[p].price * 0.1);
System.out.println(​"Discount: "​+dis);
b[p].price -= dis;
}
System.out.println(​"The final price is:
"​+b[p].price);
for​( Entry<Integer, Book> m : hbook.entrySet())
{
//System.out.println(m.getKey() + " "+
m.getValue());
System.out.println(m);
}
break​;
case​ 4: System.exit(0);
default​:System.out.println(​"Invalid Choice"​);
}
}
}
}

You might also like