package com.
sunbeam;
import java.time.LocalDate;
import java.util.*;
enum Category {
FICTION, NONFICTION, SCIENCE, HISTORY, BIOGRAPHY
}
class Book implements Comparable<Book> {
private String isbn;
private Category category;
private double price;
private LocalDate publishDate;
private String authorName;
private int quantity;
public Book(String isbn, Category category, double price, LocalDate
publishDate, String authorName, int quantity) {
this.isbn = isbn;
this.category = category;
this.price = price;
this.publishDate = publishDate;
this.authorName = authorName;
this.quantity = quantity;
}
public String getIsbn() {
return isbn;
}
public LocalDate getPublishDate() {
return publishDate;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
Book other = (Book) obj;
return Objects.equals(isbn, other.isbn);
}
@Override
public int hashCode() {
return Objects.hash(isbn);
}
@Override
public String toString() {
return String.format("ISBN: %s | Category: %s | Price: %.2f | Date: %s |
Author: %s | Qty: %d",
isbn, category, price, publishDate, authorName, quantity);
}
@Override
public int compareTo(Book other) {
return this.publishDate.compareTo(other.publishDate);
}
}
public class BookLibrary {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Set<Book> bookHashSet = new HashSet<>();
System.out.println("Enter 5 book details:");
for (int i = 0; i < 5; i++) {
System.out.print("ISBN: ");
String isbn = sc.next();
System.out.print("Category
(FICTION/NONFICTION/SCIENCE/HISTORY/BIOGRAPHY): ");
Category category = Category.valueOf(sc.next().toUpperCase());
System.out.print("Price: ");
double price = sc.nextDouble();
System.out.print("Publish Date (yyyy-mm-dd): ");
LocalDate date = LocalDate.parse(sc.next());
sc.nextLine(); // clear buffer
System.out.print("Author Name: ");
String author = sc.nextLine();
System.out.print("Quantity: ");
int qty = sc.nextInt();
bookHashSet.add(new Book(isbn, category, price, date, author, qty));
System.out.println("-- Book Added --\n");
}
System.out.println("\n--- HashSet (No Order) ---");
for (Book book : bookHashSet) {
System.out.println(book);
}
System.out.println("\n--- LinkedHashSet (Insertion Order) ---");
Set<Book> bookLinkedHashSet = new LinkedHashSet<>(bookHashSet);
for (Book book : bookLinkedHashSet) {
System.out.println(book);
}
System.out.println("\n--- Sorted by Publish Date ---");
List<Book> bookList = new ArrayList<>(bookHashSet);
Collections.sort(bookList);
for (Book book : bookList) {
System.out.println(book);
}
sc.close();
}
}
4----------------------------
package com.sunbeam;
import java.util.*;
public class ListMaxFinder {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> arrayList = new ArrayList<>();
List<Double> linkedList = new LinkedList<>();
List<Float> vector = new Vector<>();
System.out.println("Enter 5 integers for ArrayList:");
for (int i = 0; i < 5; i++) {
arrayList.add(sc.nextInt());
}
System.out.println("Enter 5 doubles for LinkedList:");
for (int i = 0; i < 5; i++) {
linkedList.add(sc.nextDouble());
}
System.out.println("Enter 5 floats for Vector:");
for (int i = 0; i < 5; i++) {
vector.add(sc.nextFloat());
}
System.out.println("\nArrayList: " + arrayList);
System.out.println("Max in ArrayList: " + findMax(arrayList));
System.out.println("LinkedList: " + linkedList);
System.out.println("Max in LinkedList: " + findMax(linkedList));
System.out.println("Vector: " + vector);
System.out.println("Max in Vector: " + findMax(vector));
sc.close();
}
public static <T extends Comparable<T>> T findMax(List<T> list) {
if (list == null || list.isEmpty())
throw new IllegalArgumentException("List is null or empty");
T max = list.get(0);
for (T item : list) {
if (item.compareTo(max) > 0) {
max = item;
}
}
return max;
}
}
5------------------------------------------
package com.sunbeam;
import java.util.*;
public class ListMaxFinder {
public static void main(String[] args) {
// Creating a TreeSet of colors
TreeSet<String> colorSet = new TreeSet<>();
// Adding colors to the TreeSet
colorSet.add("Red");
colorSet.add("Blue");
colorSet.add("Green");
colorSet.add("Yellow");
colorSet.add("Purple");
colorSet.add("Orange");
// Printing the TreeSet
System.out.println("TreeSet of Colors:");
for (String color : colorSet) {
System.out.println(color);
}
}
}