You are on page 1of 8

National Aviation University

FCCSE

Department of Software Engineering

Homework
discipline

"Software Development Studio"

Variant #6

Performed: Student of group


SE-227B
Frynas Vadym
Accepted by: Tkachenko O.A.
Purpose: To study the mechanism of object serialization when using Java I / O
streams and to learn how
to use I / O streams in multi-threaded applications.
Task: Write a console application in Java that:
1) creates a thread to write class objects to a certain file according to the task
variant;
2) creates a stream to read from the object file and output them in tabular form;
3) provides for synchronization between threads (ie write or read are atomic
operations) and
implements it in two ways:
- using the wait / notify mechanism;
- using the classes and interfaces of java.util.concurrent.locks;
My variant’s Task:
Apartment:
The number of apartments, Area, Floor, Number of rooms, Type of construction,
Period of operation
Guidelines for homework
It is necessary to provide an interactive mode of operation of the application, ie:
- enter the file name for processing from the keyboard;
- enter object data from the keyboard;
- all actions must be recorded by outputting appropriate messages.
To ensure the stability of the application, provide validation of the data input and
propose solutions for
incorrect input to continue the application.
Standard formatting tools, such as the System.out.printf () method, must be used to
output object
information in tabular form.
Class diagram with all classes, methods and fields in my program.

Fig.1 Class Diagram

2
User's Manual for working with the program
First of all, user should enter a path and name of file where he wants to save and
read data

Fig.2 Path and name of the file


Above the red line you can see the path to the future file and above green line
-name of this file
Also, on this stage you can see important explanation in console

Fig.3 Program menu


On the next step of program execution, you will see a menu of this program and
you should press one of available numbers.
In the case of 1 or 2, you will see the list of existing apartments, but this action
perform by 2 different ways.

Fig.4 Result of deserialization


3
If you choose case 3, you will see a menu for adding a new apartment to the list
and two option for saving your data

Fig.4 New apartment menu


Apatrment.java
package com.company;
import java.io.Serializable;

public class Apartment implements Serializable {


private int apartmentNumber;
private int floor;
private int roomsNumber;
private int operationPeriod;
private double area;
private String constructionType;

public Apartment(int apartmentNumber, int floor, int roomsNumber, int


operationPeriod,
double area, String constructionType)
{
this.apartmentNumber = apartmentNumber;
this.area=area;
this.constructionType=constructionType;
this.floor=floor;
this.operationPeriod=operationPeriod;
this.roomsNumber=roomsNumber;
}

@Override
public String toString() {
return "APARTMENT:\nApartment's number: "+ apartmentNumber+
"\nArea: "+area+
"\nFloor number: "+floor+
"\nRoom number: "+roomsNumber+

4
"\nConstruction period: "+operationPeriod+
"\nConstruction type: "+constructionType;
}
}

LockRead.java
package com.company;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockRead implements Runnable{


ArrayList<Apartment> apartmentArrayList;
String Path;
public LockRead(String Path)
{
this.Path=Path;
}
@Override
public void run() {
Lock lock = new ReentrantLock();
System.out.println("Deserialization.");
try {
lock.lock();
FileInputStream fis = new FileInputStream(Path);
ObjectInputStream ois = new ObjectInputStream(fis);
apartmentArrayList =(ArrayList<Apartment>) ois.readObject();
ois.close();

} catch (IOException | ClassNotFoundException e) {


e.printStackTrace();
}
finally {
lock.unlock();
}
}

public ArrayList<Apartment> getApartmentArrayList() {


return apartmentArrayList;
}
}
LockWrite.java
package com.company;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockWrite implements Runnable{


ArrayList<Apartment> apartmentArrayList;
String Path;
public LockWrite(ArrayList<Apartment> apartmentArrayList,String Path) {
this.apartmentArrayList = apartmentArrayList;
this.Path=Path;
}

@Override

5
public void run() {
Lock lock = new ReentrantLock();
try {
lock.lock();
FileOutputStream fos = new FileOutputStream(Path);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(apartmentArrayList);
oos.close();
System.out.println("Serialized with lock");
} catch (IOException e) {
e.printStackTrace();
}
finally {
lock.unlock();
}
}
}
Main.java
package com.company;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

public static void main(String[] args) throws InterruptedException,


IOException {

Scanner sc = new Scanner(System.in);


BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Write a name of the file where program should
save/take a data" +
"\n(You must write path to the file and Name of this file" +
"\nExtension to the file will be automatically added " +
"\nExample: C:\\Users\\user\\source\\Java\\Новая
папка\\Майстерня\\homework\\Apartment ): ");

String path=br.readLine();
path += ".bin";
ArrayList<Apartment> apartmentArrayList = new ArrayList();
apartmentArrayList.add(new Apartment(32,2,4,17,110,"cottage"));
apartmentArrayList.add(new Apartment(312,1,3,17,100,"cottage"));
apartmentArrayList.add(new Apartment(12,2,5,17,160,"cottage"));
apartmentArrayList.add(new Apartment(2,3,7,27,200,"cottage"));
apartmentArrayList.add(new Apartment(2222222,3,7,27,200,"cottage"));

System.out.println("What do you want to do: \n\t1. Serialize and


deserialize existing list of apartments USING WAIT/NOTIFY " +
"\n\t2.Deserialize existing list of apartments USING
LOCK\n\t3.Add a new apartment to the list\n\t4.Close program");
int option=sc.nextInt();
while (option<4)
{
switch (option) {
case 1: {
WaitThread(apartmentArrayList,path);
break;
}
case 2: {
LockThread(apartmentArrayList,path);
break;
6
}
case 3: {
System.out.println("Enter apartment's number: ");
int apartmentNumber = sc.nextInt();
System.out.println("Enter apartment's area: ");
double area = sc.nextDouble();
System.out.println("Enter amount of apartment's floors:
");
int floor = sc.nextInt();
System.out.println("Enter amount of apartment's rooms:
");
int roomsNumber = sc.nextInt();
System.out.println("Enter amount of months for
apartment's construction: ");
int operationPeriod = sc.nextInt();
System.out.println("Enter specific construction type(such
as cottage, wood apartment): ");
String constructionType = br.readLine();
apartmentArrayList.add(new
Apartment(apartmentNumber,floor,roomsNumber,operationPeriod,area,construction
Type));
System.out.println("Apartment was successfully added. How
do you want to serialize list:\n\t1.Wait/Notify method" +
"\n\t2.Lock method ");
int opt=sc.nextInt();
if(opt==1) WaitThread(apartmentArrayList,path);
else if(opt==2) LockThread(apartmentArrayList,path);
break;
}

case 4: {

break;
}

}
System.out.println("What do you want to do: \n\t1. Serialize and
deserialize existing list of apartments USING WAIT/NOTIFY " +
"\n\t2.Deserialize existing list of apartments USING
LOCK\n\t3.Add a new apartment to the list\n\t4.Close program");

option=sc.nextInt();
}

}
private static void WaitThread(ArrayList<Apartment>
apartmentArrayList,String path) throws InterruptedException {
Object sync = new Object();
new Thread(() -> {
synchronized (sync) {
try {
FileOutputStream fos = new FileOutputStream(path);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(apartmentArrayList);
oos.close();
System.out.println("Object was serialized.");
sync.wait();
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
7
e.printStackTrace();
}
}
}).start();

Thread.sleep(500);

new Thread(() -> {


synchronized (sync) {
System.out.println("Deserialization.");
try {
FileInputStream fis = new FileInputStream(path);
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<Apartment> list =(ArrayList<Apartment>)
ois.readObject();
ois.close();
System.out.println("Result:");
for (Apartment temp:
list) {
System.out.println(temp.toString());
}

} catch (IOException | ClassNotFoundException e) {


e.printStackTrace();
}
sync.notify();
}
}).start();
Thread.sleep(600);

}
private static void LockThread(ArrayList<Apartment>
apartmentArrayList,String path)
{
LockWrite lockWrite = new LockWrite(apartmentArrayList,path);
lockWrite.run();

LockRead lockRead = new LockRead(path);


lockRead.run();

ArrayList<Apartment> apartments = lockRead.getApartmentArrayList();


System.out.println("Result:");
for (Apartment temp: apartments) {
System.out.println(temp.toString());
}
}
}

You might also like