Collection and Interface ASsignment
(Submitted by- 2204920100022)
Solution 021: Article count
import java.util.Scanner;
// Class Article extending Thread
class Article extends Thread {
private String line;
private int count;
// Parameterized Constructor
public Article(String line) {
this.line = line;
this.count = 0;
}
// Getter for count
public int getCount() {
return count;
}
// Method to count articles in the line
@Override
public void run() {
String[] words = line.split("\\s+");
for (String word : words) {
if (word.equalsIgnoreCase("a") || word.equalsIgnoreCase("an") ||
word.equalsIgnoreCase("the")) {
count++;
}
}
}
}
// Main class
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get the number of lines from the user
System.out.println("Enter the number of lines");
int n = Integer.parseInt(scanner.nextLine());
Article[] articles = new Article[n];
// Get each line from the user and create threads
for (int i = 0; i < n; i++) {
System.out.println("Enter line " + (i + 1));
String line = scanner.nextLine();
articles[i] = new Article(line);
articles[i].start();
}
// Wait for all threads to complete and calculate total count
int totalCount = 0;
for (Article article : articles) {
try {
article.join();
totalCount += article.getCount();
} catch (InterruptedException e) {
System.out.println("Thread interrupted: " + e.getMessage());
}
}
// Output the total count of articles
System.out.println("There are " + totalCount + " articles in the given input");
}
Solution 022: User notification
import java.util.*;
// Class User
class User {
private String username;
private String mobileNumber;
// Parameterized Constructor
public User(String username, String mobileNumber) {
this.username = username;
this.mobileNumber = mobileNumber;
}
// Getters and Setters
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
}
// Class UserBO extending Thread
class UserBO extends Thread {
public List<User> userList;
public static List<String> message = new ArrayList<>();
// Constructor
public UserBO(List<User> userList) {
this.userList = userList;
}
// Overriding run method
@Override
public void run() {
synchronized (message) {
for (User user : userList) {
String notification = "The message is sent to the user \"" + user.getUsername() +
"\" at the mobile number \"" + user.getMobileNumber() + "\"";
message.add(notification);
}
}
}
}
// Main class
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input: Number of users
System.out.println("Enter the number of users:");
int n = Integer.parseInt(scanner.nextLine());
// Input: Number of users per thread
System.out.println("Enter the number of users per thread:");
int usersPerThread = Integer.parseInt(scanner.nextLine());
// Input: User details
System.out.println("Enter the user details");
List<User> users = new ArrayList<>();
for (int i = 0; i < n; i++) {
String[] details = scanner.nextLine().split(",");
users.add(new User(details[0], details[1]));
}
// Create threads and distribute users
List<UserBO> threads = new ArrayList<>();
for (int i = 0; i < n; i += usersPerThread) {
List<User> subList = users.subList(i, Math.min(i + usersPerThread, n));
UserBO thread = new UserBO(subList);
threads.add(thread);
thread.start();
}
// Wait for all threads to complete
for (UserBO thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
System.out.println("Thread interrupted: " + e.getMessage());
}
}
// Output notifications
for (String msg : UserBO.message) {
System.out.println(msg);
}
}
}
Solution 023: Stall revenue
import java.util.*;
// Define the Stall class
class Stall implements Runnable {
private String stallName;
private String details;
private double stallArea;
private String owner;
private double stallCost;
// Constructor
public Stall(String stallName, String details, double stallArea, String owner) {
this.stallName = stallName;
this.details = details;
this.stallArea = stallArea;
this.owner = owner;
}
// Getters and setters
public String getStallName() {
return stallName;
}
public void setStallName(String stallName) {
this.stallName = stallName;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public double getStallArea() {
return stallArea;
}
public void setStallArea(double stallArea) {
this.stallArea = stallArea;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public double getStallCost() {
return stallCost;
}
public void setStallCost(double stallCost) {
this.stallCost = stallCost;
}
@Override
public void run() {
this.stallCost = this.stallArea * 150.0;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of stalls:");
int n = scanner.nextInt();
scanner.nextLine(); // Consume newline
if (n <= 0) {
System.out.println("Invalid number of stalls.");
return;
}
List<Stall> stalls = new ArrayList<>();
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < n; i++) {
System.out.println("Enter the details of stall " + (i + 1) + "
(stallName,details,stallArea,owner):");
String[] details = scanner.nextLine().split(",");
String stallName = details[0].trim();
String stallDetails = details[1].trim();
double stallArea = Double.parseDouble(details[2].trim());
String owner = details[3].trim();
Stall stall = new Stall(stallName, stallDetails, stallArea, owner);
stalls.add(stall);
Thread thread = new Thread(stall);
threads.add(thread);
thread.start();
}
double totalRevenue = 0.0;
for (int i = 0; i < n; i++) {
try {
threads.get(i).join();
totalRevenue += stalls.get(i).getStallCost();
} catch (InterruptedException e) {
System.out.println("Thread interrupted: " + e.getMessage());
}
}
System.out.printf("The total revenue is %.1f\n", totalRevenue);
scanner.close();
}
}
Solution 024: Search supplier using ArrayList
// Supplier.java
public class Supplier {
private Integer supplier_Id;
private String supplier_Name;
private String supplier_Address;
private String email;
private String phoneNO;
public Supplier(Integer supplier_Id, String supplier_Name, String supplier_Address, String
email, String phoneNO) {
this.supplier_Id = supplier_Id;
this.supplier_Name = supplier_Name;
this.supplier_Address = supplier_Address;
this.email = email;
this.phoneNO = phoneNO;
}
public Integer getSupplier_Id() {
return supplier_Id;
}
public void setSupplier_Id(Integer supplier_Id) {
this.supplier_Id = supplier_Id;
}
public String getSupplier_Name() {
return supplier_Name;
}
public void setSupplier_Name(String supplier_Name) {
this.supplier_Name = supplier_Name;
}
public String getSupplier_Address() {
return supplier_Address;
}
public void setSupplier_Address(String supplier_Address) {
this.supplier_Address = supplier_Address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNO() {
return phoneNO;
}
public void setPhoneNO(String phoneNO) {
this.phoneNO = phoneNO;
}
}
// SupplierBO.java
import java.util.*;
public class SupplierBO {
public void createSupplier(String supplierDetails, List<Supplier> supplierList) {
String[] details = supplierDetails.split(",");
Supplier supplier = new Supplier(
Integer.parseInt(details[0]),
details[1],
details[2],
details[3],
details[4]
);
supplierList.add(supplier);
}
public void display(List<Supplier> supplierList) {
System.out.format("%s %20s %30s %20s %10s\n",
"supplier_Id", "supplier_Name", "supplier FullAddressDeatails", "supplier_Email",
"PhoneNO");
for (Supplier supplier : supplierList) {
System.out.format("%10d %20s %30s %20s %10s\n",
supplier.getSupplier_Id(),
supplier.getSupplier_Name(),
supplier.getSupplier_Address(),
supplier.getEmail(),
supplier.getPhoneNO());
}
}
public Supplier searchSupplierFromList(String email, List<Supplier> supplierList) {
for (Supplier supplier : supplierList) {
if (supplier.getEmail().equals(email)) {
return supplier;
}
}
return null;
}
}
// Main.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Supplier> supplierList = new ArrayList<>();
SupplierBO supplierBO = new SupplierBO();
boolean continueLoop = true;
while (continueLoop) {
System.out.println("Menu\n1.Add Supplier\n2.Display Supplier\n3.Search Supplier\
nEnter your choice");
int choice = Integer.parseInt(scanner.nextLine());
switch (choice) {
case 1:
System.out.println("Enter the Supplier details in CSV format");
String supplierDetails = scanner.nextLine();
supplierBO.createSupplier(supplierDetails, supplierList);
System.out.println("Supplier created successfully");
break;
case 2:
if (supplierList.isEmpty()) {
System.out.println("No suppliers to display");
} else {
System.out.println("Supplier Details");
supplierBO.display(supplierList);
}
break;
case 3:
System.out.println("Enter the Supplier email to search :");
String email = scanner.nextLine();
Supplier supplier = supplierBO.searchSupplierFromList(email, supplierList);
if (supplier != null) {
System.out.println("Supplier Detail");
System.out.format("%s %20s %30s %20s %10s\n",
"supplier_Id", "supplier_Name", "supplier FullAddressDeatails",
"supplier_Email", "PhoneNO");
System.out.format("%10d %20s %30s %20s %10s\n",
supplier.getSupplier_Id(),
supplier.getSupplier_Name(),
supplier.getSupplier_Address(),
supplier.getEmail(),
supplier.getPhoneNO());
} else {
System.out.println("Supplier not found");
}
break;
default:
System.out.println("Invalid choice");
break;
}
System.out.println("Do you want to continue(y/n)?:");
String continueChoice = scanner.nextLine();
if (!continueChoice.equalsIgnoreCase("y")) {
continueLoop = false;
}
}
scanner.close();
}
}
Solution 026: Replica of a List
import java.util.*;
// Define the User class
class User {
private String username;
private String password;
// Default constructor
public User() {}
// Parameterized constructor
public User(String username, String password) {
this.username = username;
this.password = password;
}
// Getter for username
public String getUsername() {
return username;
}
// Setter for username
public void setUsername(String username) {
this.username = username;
}
// Getter for password
public String getPassword() {
return password;
}
// Setter for password
public void setPassword(String password) {
this.password = password;
}
}
public class Main {
// Method to create a backup of the user list
public static List<User> backUp(List<User> dest, List<User> source) {
for (int i = 0; i < source.size(); i++) {
dest.set(i, source.get(i)); // Copy each user to the destination list
}
return dest;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input the number of users
System.out.println("Enter number of users");
int n = sc.nextInt();
sc.nextLine(); // Consume the newline
if (n <= 0) {
System.out.println("Invalid Input");
return;
}
List<User> sourceList = new ArrayList<>();
System.out.println("Enter the user details in CSV(Username,password)");
// Input user details and populate the source list
for (int i = 0; i < n; i++) {
String[] userDetails = sc.nextLine().split(",");
sourceList.add(new User(userDetails[0], userDetails[1]));
}
// Create a destination list with the same size as the source list
List<User> destList = new ArrayList<>(Collections.nCopies(n, null));
// Backup the user list
destList = backUp(destList, sourceList);
// Display the backup list
System.out.println("Copy of user list:");
System.out.format("%-20s %s\n", "Username", "Password");
for (User user : destList) {
System.out.format("%-20s %s\n", user.getUsername(), user.getPassword());
}
sc.close();
}
}
Solution 027: Minimum and Maximum spenders
import java.util.*;
// Define the TicketBooking class implementing Comparable
class TicketBooking implements Comparable<TicketBooking> {
private String customerName;
private Integer price;
// Parameterized constructor
public TicketBooking(String customerName, Integer price) {
this.customerName = customerName;
this.price = price;
}
// Getter for customerName
public String getCustomerName() {
return customerName;
}
// Setter for customerName
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
// Getter for price
public Integer getPrice() {
return price;
}
// Setter for price
public void setPrice(Integer price) {
this.price = price;
}
// Override compareTo method
@Override
public int compareTo(TicketBooking other) {
return this.price.compareTo(other.price);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input the number of customers
System.out.println("Enter the number of customers");
int n = sc.nextInt();
sc.nextLine(); // Consume the newline
if (n <= 0) {
System.out.println("Invalid Input");
return;
}
List<TicketBooking> bookings = new ArrayList<>();
System.out.println("Enter the booking price accordingly with customer name in
CSV(Customer Name,Price)");
// Input customer details and populate the list
for (int i = 0; i < n; i++) {
String[] details = sc.nextLine().split(",");
String customerName = details[0];
Integer price = Integer.parseInt(details[1]);
bookings.add(new TicketBooking(customerName, price));
}
// Find the minimum and maximum spenders
TicketBooking minSpender = Collections.min(bookings);
TicketBooking maxSpender = Collections.max(bookings);
// Display the results
System.out.println(minSpender.getCustomerName() + " spends minimum amount of
Rs." + minSpender.getPrice());
System.out.println(maxSpender.getCustomerName() + " spends maximum amount of
Rs." + maxSpender.getPrice());
sc.close();
}
}
Solution 028: Product cost filter using file
import java.io.*;
import java.util.*;
// Define the Product class
class Product {
private int productId;
private String productName;
private String description;
private double price;
// Constructor
public Product(int productId, String productName, String description, double price) {
this.productId = productId;
this.productName = productName;
this.description = description;
this.price = price;
}
// Getters and setters
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return productId + "," + productName + "," + description + "," + price;
}
}
// Define the ProductBO class
class ProductBO {
public static List<Product> addProduct(String details, List<Product> productList) {
String[] parts = details.split(",");
int productId = Integer.parseInt(parts[0].trim());
String productName = parts[1].trim();
String description = parts[2].trim();
double price = Double.parseDouble(parts[3].trim());
productList.add(new Product(productId, productName, description, price));
return productList;
}
public static void display(List<Product> list) {
System.out.println("Products Filtered on price greater than 15000");
boolean found = false;
for (Product product : list) {
if (product.getPrice() > 15000) {
System.out.println("ProductId: " + product.getProductId());
System.out.println("Product Name: " + product.getProductName());
System.out.println("Description: " + product.getDescription());
System.out.println("Price: " + product.getPrice());
found = true;
}
}
if (!found) {
System.out.println("Product having price greater than Rs.15000 is not found...");
}
}
}
public class Main {
public static void main(String[] args) {
List<Product> productList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("ProductDetails.txt"))) {
String line;
while ((line = br.readLine()) != null) {
ProductBO.addProduct(line, productList);
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
return;
}
System.out.println("Complete Product Catalog");
for (Product product : productList) {
System.out.println(product);
}
System.out.println();
ProductBO.display(productList);
}
}
Solution 029: Search product count using File IO
import java.io.*;
import java.util.*;
// Define the Product class
class Product {
private int productId;
private String productName;
private String description;
private double price;
// Constructor
public Product(int productId, String productName, String description, double price) {
this.productId = productId;
this.productName = productName;
this.description = description;
this.price = price;
}
// Getters and setters
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return productId + "," + productName + "," + description + "," + price;
}
}
// Define the ProductBO class
class ProductBO {
public static List<Product> addProduct(String details, List<Product> productList) {
String[] parts = details.split(",");
int productId = Integer.parseInt(parts[0].trim());
String productName = parts[1].trim();
String description = parts[2].trim();
double price = Double.parseDouble(parts[3].trim());
productList.add(new Product(productId, productName, description, price));
return productList;
}
public static void display(List<Product> list) {
System.out.println("Products Filtered on price greater than 15000");
boolean found = false;
for (Product product : list) {
if (product.getPrice() > 15000) {
System.out.println("ProductId: " + product.getProductId());
System.out.println("Product Name: " + product.getProductName());
System.out.println("Description: " + product.getDescription());
System.out.println("Price: " + product.getPrice());
found = true;
}
}
if (!found) {
System.out.println("Product having price greater than Rs.15000 is not found...");
}
}
}
public class Main {
public static void main(String[] args) {
List<Product> productList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("ProductDetails.txt"))) {
String line;
while ((line = br.readLine()) != null) {
ProductBO.addProduct(line, productList);
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
return;
}
System.out.println("Complete Product Catalog");
for (Product product : productList) {
System.out.println(product);
}
System.out.println();
ProductBO.display(productList);
}
}
Solution 030: User Objects from CSV file.
import java.io.*;
import java.util.*;
// Define the Customer class
class Customer {
private int id;
private String firstName;
private String lastName;
private String city;
private String email;
// Constructor
public Customer(int id, String firstName, String lastName, String city, String email) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.city = city;
this.email = email;
}
// Getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return String.format("%-15d %-20s %-15s %-10s %s", id, firstName, lastName, city,
email);
}
}
// Define the CustomerBO class
class CustomerBO {
public static List<Customer> readFromFile(BufferedReader br) throws IOException {
List<Customer> customerList = new ArrayList<>();
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
int id = Integer.parseInt(parts[0].trim());
String firstName = parts[1].trim();
String lastName = parts[2].trim();
String city = parts[3].trim();
String email = parts[4].trim();
customerList.add(new Customer(id, firstName, lastName, city, email));
}
return customerList;
}
}
public class Main {
public static void main(String[] args) {
List<Customer> customerList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("input.csv"))) {
customerList = CustomerBO.readFromFile(br);
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
return;
}
if (customerList.isEmpty()) {
System.out.println("The list is empty");
} else {
System.out.printf("%-15s %-20s %-15s %-10s %s\n", "Id", "FirstName", "LastName",
"City", "Email");
for (Customer customer : customerList) {
System.out.println(customer);
}
}
}
}