Advertisement
Guest User

Untitled

a guest
Feb 8th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.24 KB | None | 0 0
  1. package com.company.test;
  2.  
  3. import java.io.*;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. public class Main {
  10.  
  11.     private static final List<Offer> offers = new ArrayList<>();
  12.     private static final List<User> users = new ArrayList<>();
  13.  
  14.     public static void main(String[] args) throws IOException {
  15.  
  16.         ServerSocket serverSocket = new ServerSocket(1234);
  17.  
  18.         while (true) {
  19.             Socket socket = serverSocket.accept();
  20.  
  21.             ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
  22.  
  23.             ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
  24.  
  25.             String email = objectInputStream.readUTF();
  26.             String password = objectInputStream.readUTF();
  27.  
  28.             User user = new User()
  29.  
  30.  
  31.             // Creating an offer
  32.             Offer offer = new Offer();
  33.             offer.setDestination("Pazardzhik");
  34.             offer.setNameOfHotel("Trakiq");
  35.  
  36.             // Adding the offer to the offers
  37.             offers.add(offer);
  38.  
  39.             System.out.println(offer);
  40.  
  41.             // condition
  42.             double avg = getAverage();
  43.             offer.setRating(avg);
  44.  
  45.             OfferGetter offerGetter = new OfferGetter(offers, socket);
  46.             offerGetter.start();
  47.  
  48.             socket.close();
  49.         }
  50.     }
  51.  
  52.     private static double getAverage() {
  53.         return offers.stream()
  54.                 .mapToDouble(Offer::getPrice)
  55.                 .average()
  56.                 .orElse(0);
  57.     }
  58.  
  59. }
  60.  
  61. class OfferGetter extends Thread {
  62.  
  63.     private List<Offer> offers;
  64.     private Socket socket;
  65.  
  66.     public OfferGetter(List<Offer> offers, Socket socket) {
  67.         this.offers = offers;
  68.         this.socket = socket;
  69.     }
  70.  
  71.     @Override
  72.     public void run() {
  73.         String destination = null;
  74.         try {
  75.             destination = new ObjectInputStream(socket.getInputStream()).readUTF();
  76.         } catch (IOException e) {
  77.             e.printStackTrace();
  78.         }
  79.         for (Offer offer : offers) {
  80.             if (destination.equals(offer.getDestination())) {
  81.                 try {
  82.  
  83.                     socket.getOutputStream().write(offer.toString().getBytes());
  84.  
  85.                 } catch (IOException e) {
  86.                     e.printStackTrace();
  87.                 }
  88.             }
  89.         }
  90.     }
  91. }
  92.  
  93. class Offer {
  94.     private int offerId;
  95.     private String destination;
  96.     private String nameOfHotel;
  97.     private double price;
  98.     private double rating;
  99.     private List<Integer> evaluation;
  100.  
  101.     public Offer() {
  102.         evaluation = new ArrayList<>();
  103.     }
  104.  
  105.  
  106.     public int getOfferId() {
  107.         return offerId;
  108.     }
  109.  
  110.     public void setOfferId(int offerId) {
  111.         this.offerId = offerId;
  112.     }
  113.  
  114.     public String getDestination() {
  115.         return destination;
  116.     }
  117.  
  118.     public void setDestination(String destination) {
  119.         this.destination = destination;
  120.     }
  121.  
  122.     public String getNameOfHotel() {
  123.         return nameOfHotel;
  124.     }
  125.  
  126.     public void setNameOfHotel(String nameOfHotel) {
  127.         this.nameOfHotel = nameOfHotel;
  128.     }
  129.  
  130.     public double getPrice() {
  131.         return price;
  132.     }
  133.  
  134.     public void setPrice(double price) {
  135.         this.price = price;
  136.     }
  137.  
  138.     public double getRating() {
  139.         return rating;
  140.     }
  141.  
  142.     public void setRating(double rating) {
  143.         this.rating = rating;
  144.     }
  145.  
  146.     public List<Integer> getEvaluation() {
  147.         return evaluation;
  148.     }
  149.  
  150.     public void setEvaluation(List<Integer> evaluation) {
  151.         this.evaluation = evaluation;
  152.     }
  153. }
  154.  
  155. class User {
  156.     private String email;
  157.     private String password;
  158.     private List<Grade> grades;
  159.  
  160.  
  161.     public User(String email, String password) {
  162.         this();
  163.         this.email = email;
  164.         this.password = password;
  165.     }
  166.  
  167.     public User() {
  168.         grades = new ArrayList<>();
  169.     }
  170.  
  171.     public String getEmail() {
  172.         return email;
  173.     }
  174.  
  175.     public void setEmail(String email) {
  176.         this.email = email;
  177.     }
  178.  
  179.     public String getPassword() {
  180.         return password;
  181.     }
  182.  
  183.     public void setPassword(String password) {
  184.         this.password = password;
  185.     }
  186.  
  187.     public List<Grade> getGrades() {
  188.         return grades;
  189.     }
  190.  
  191.     public void setGrades(List<Grade> grades) {
  192.         this.grades = grades;
  193.     }
  194. }
  195.  
  196. class BaseGrade {
  197.     private String teacher;
  198.  
  199.     public BaseGrade(String teacher) {
  200.         this.teacher = teacher;
  201.     }
  202.  
  203.     public String getTeacher() {
  204.         return teacher;
  205.     }
  206.  
  207.     public void setTeacher(String teacher) {
  208.         this.teacher = teacher;
  209.     }
  210. }
  211.  
  212. class Grade extends BaseGrade {
  213.  
  214.     private int offerId;
  215.     private List<Integer> evaluation;
  216.  
  217.     public Grade(String teacher) {
  218.         super(teacher);
  219.         evaluation = new ArrayList<>();
  220.     }
  221.  
  222.     public int getOfferId() {
  223.         return offerId;
  224.     }
  225.  
  226.     public void setOfferId(int offerId) {
  227.         this.offerId = offerId;
  228.     }
  229.  
  230.     public List<Integer> getEvaluation() {
  231.         return evaluation;
  232.     }
  233.  
  234.     public void setEvaluation(List<Integer> evaluation) {
  235.         this.evaluation = evaluation;
  236.     }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement