In: Computer Science
2. A cook in canteen prepares parotta and stacks it up in a container, and the server takes parotta from the container and serves to his customer. The max capacity of the container is 15. If parotta in the container is empty, server waits for the cook to prepare new parotta. Write a Java program to illustrate the given scenario using multithreading.
b. Write a Java program to define a class ‘Covid19’ to store the below mentioned details of a Covid patients for CMC hospital. Name, age, address, mobile number, blood group, date of Covid checking. symptoms. Create ‘n’ objects of this class for all the Covid patients at india. Write these objects to a file. Read these objects from the file and display only those Covid patient details whose symptoms is ‘fever’ and seven days completed from the date of Covid checking.
please code in java only and also provide the screenshot of output along with the code
Answer 1:
import java.util.LinkedList;
public class Threadexample {
public static void main(String[] args)
throws InterruptedException
{
final PC pc = new PC();
Thread t1 = new Thread(new Runnable() {
@Override
public void run()
{
try {
pc.produce();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run()
{
try {
pc.consume();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
public static class PC {
LinkedList<Integer> list = new LinkedList<>();
int capacity =15;
public void produce() throws InterruptedException
{
int value = 0;
while (true) {
synchronized (this)
{
while (list.size() == capacity)
wait();
System.out.println("Producer produced-"
+ value);
list.add(value++);
notify();
Thread.sleep(1000);
}
}
}
public void consume() throws InterruptedException
{
while (true) {
synchronized (this)
{
while (list.size() == 0)
wait();
int val = list.removeFirst();
System.out.println("Consumer consumed-"
+ val);
notify();
Thread.sleep(1000);
}
}
}
}
}
Answer 2:
package com.kontrolscan.services.impl;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Scanner;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Covid19 {
public static void main(String[] args) throws ParseException, IOException {
Scanner s = new Scanner(System.in);
int noOfPatients= 0;
File file = new File("/home/amity/Desktop/sample.txt");
if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
} else {
System.out.println("File already exists.");
}
FileWriter fileWriter = new FileWriter(file);
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
System.out.println("Enter number of patients:");
noOfPatients = s.nextInt();
for(int i =0 ;i< noOfPatients ; i++) {
CovidPatient currentPatient = new CovidPatient("Patient"+i+1,20,"New York","+123345667","AB+",new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse("02/10/2020 10:34:33"),"fever");
String jsonString = mapper.writeValueAsString(currentPatient);
fileWriter.write(jsonString);
}
fileWriter.close();
File myObj = new File("/home/amity/Desktop/sample.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
Date currentDate1 = new Date();
CovidPatient patient = mapper.readValue(data, CovidPatient.class);
String admittedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(patient.getCovidCheckingDate());
String currentDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currentDate1);
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDate from = LocalDate.parse(admittedDate,format);
LocalDate to = LocalDate.parse(currentDate,format);
long days = ChronoUnit.DAYS.between(from, to);
if(days > 7 && patient.getSymptoms().equals("fever")) {
System.out.println(patient);
}
}
myReader.close();
}
}
@JsonInclude(Include.NON_DEFAULT)
class CovidPatient {
String name;
int age;
String address;
String mobileNumber;
String bloodGroup;
Date covidCheckingDate;
String symptoms;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getBloodGroup() {
return bloodGroup;
}
public void setBloodGroup(String bloodGroup) {
this.bloodGroup = bloodGroup;
}
public Date getCovidCheckingDate() {
return covidCheckingDate;
}
public void setCovidCheckingDate(Date covidCheckingDate) {
this.covidCheckingDate = covidCheckingDate;
}
public CovidPatient(String name, int age, String address, String mobileNumber, String bloodGroup,
Date covidCheckingDate,String symptoms) {
super();
this.name = name;
this.age = age;
this.address = address;
this.mobileNumber = mobileNumber;
this.bloodGroup = bloodGroup;
this.covidCheckingDate = covidCheckingDate;
this.symptoms = symptoms;
}
public String getSymptoms() {
return symptoms;
}
public void setSymptoms(String symptoms) {
this.symptoms = symptoms;
}
public CovidPatient() {
}
@Override
public String toString() {
return "CovidPatient [name=" + name + ", age=" + age + ", address=" + address + ", mobileNumber=" + mobileNumber
+ ", bloodGroup=" + bloodGroup + ", covidCheckingDate=" + covidCheckingDate + "]";
}
}