Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company.test;
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.List;
- public class Main {
- private static final List<Offer> offers = new ArrayList<>();
- private static final List<User> users = new ArrayList<>();
- public static void main(String[] args) throws IOException {
- ServerSocket serverSocket = new ServerSocket(1234);
- while (true) {
- Socket socket = serverSocket.accept();
- ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
- ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
- String email = objectInputStream.readUTF();
- String password = objectInputStream.readUTF();
- User user = new User()
- // Creating an offer
- Offer offer = new Offer();
- offer.setDestination("Pazardzhik");
- offer.setNameOfHotel("Trakiq");
- // Adding the offer to the offers
- offers.add(offer);
- System.out.println(offer);
- // condition
- double avg = getAverage();
- offer.setRating(avg);
- OfferGetter offerGetter = new OfferGetter(offers, socket);
- offerGetter.start();
- socket.close();
- }
- }
- private static double getAverage() {
- return offers.stream()
- .mapToDouble(Offer::getPrice)
- .average()
- .orElse(0);
- }
- }
- class OfferGetter extends Thread {
- private List<Offer> offers;
- private Socket socket;
- public OfferGetter(List<Offer> offers, Socket socket) {
- this.offers = offers;
- this.socket = socket;
- }
- @Override
- public void run() {
- String destination = null;
- try {
- destination = new ObjectInputStream(socket.getInputStream()).readUTF();
- } catch (IOException e) {
- e.printStackTrace();
- }
- for (Offer offer : offers) {
- if (destination.equals(offer.getDestination())) {
- try {
- socket.getOutputStream().write(offer.toString().getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- class Offer {
- private int offerId;
- private String destination;
- private String nameOfHotel;
- private double price;
- private double rating;
- private List<Integer> evaluation;
- public Offer() {
- evaluation = new ArrayList<>();
- }
- public int getOfferId() {
- return offerId;
- }
- public void setOfferId(int offerId) {
- this.offerId = offerId;
- }
- public String getDestination() {
- return destination;
- }
- public void setDestination(String destination) {
- this.destination = destination;
- }
- public String getNameOfHotel() {
- return nameOfHotel;
- }
- public void setNameOfHotel(String nameOfHotel) {
- this.nameOfHotel = nameOfHotel;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- public double getRating() {
- return rating;
- }
- public void setRating(double rating) {
- this.rating = rating;
- }
- public List<Integer> getEvaluation() {
- return evaluation;
- }
- public void setEvaluation(List<Integer> evaluation) {
- this.evaluation = evaluation;
- }
- }
- class User {
- private String email;
- private String password;
- private List<Grade> grades;
- public User(String email, String password) {
- this();
- this.email = email;
- this.password = password;
- }
- public User() {
- grades = new ArrayList<>();
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public List<Grade> getGrades() {
- return grades;
- }
- public void setGrades(List<Grade> grades) {
- this.grades = grades;
- }
- }
- class BaseGrade {
- private String teacher;
- public BaseGrade(String teacher) {
- this.teacher = teacher;
- }
- public String getTeacher() {
- return teacher;
- }
- public void setTeacher(String teacher) {
- this.teacher = teacher;
- }
- }
- class Grade extends BaseGrade {
- private int offerId;
- private List<Integer> evaluation;
- public Grade(String teacher) {
- super(teacher);
- evaluation = new ArrayList<>();
- }
- public int getOfferId() {
- return offerId;
- }
- public void setOfferId(int offerId) {
- this.offerId = offerId;
- }
- public List<Integer> getEvaluation() {
- return evaluation;
- }
- public void setEvaluation(List<Integer> evaluation) {
- this.evaluation = evaluation;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement