From cde66c0a8c430eb071721d9c2b35036b463a0744 Mon Sep 17 00:00:00 2001 From: deyanpeev Date: Wed, 25 Jan 2023 10:49:14 +0200 Subject: [PATCH] Socket Server imp --- Seminar7/code/Server/.idea/.gitignore | 3 + Seminar7/code/Server/.idea/misc.xml | 6 ++ Seminar7/code/Server/.idea/modules.xml | 8 ++ Seminar7/code/Server/.idea/vcs.xml | 6 ++ .../production/Server/ClientResolver.class | Bin 0 -> 1643 bytes .../out/production/Server/Computer.class | Bin 0 -> 2464 bytes .../production/Server/ComputerBuilder.class | Bin 0 -> 1237 bytes .../Server/out/production/Server/Main.class | Bin 0 -> 1990 bytes .../out/production/Server/ServerHandler.class | Bin 0 -> 1856 bytes .../Server/out/production/Server/Store.class | Bin 0 -> 3340 bytes .../out/production/Server/StoreSeeder.class | Bin 0 -> 904 bytes .../Server/StreamCommunicator.class | Bin 0 -> 2869 bytes Seminar7/code/Server/src/ClientResolver.java | 34 ++++++++ Seminar7/code/Server/src/Computer.java | 76 ++++++++++++++++++ Seminar7/code/Server/src/ComputerBuilder.java | 14 ++++ Seminar7/code/Server/src/Main.java | 29 +++++++ Seminar7/code/Server/src/ServerHandler.java | 45 +++++++++++ Seminar7/code/Server/src/Store.java | 43 ++++++++++ Seminar7/code/Server/src/StoreSeeder.java | 17 ++++ .../code/Server/src/StreamCommunicator.java | 32 ++++++++ 20 files changed, 313 insertions(+) create mode 100644 Seminar7/code/Server/.idea/.gitignore create mode 100644 Seminar7/code/Server/.idea/misc.xml create mode 100644 Seminar7/code/Server/.idea/modules.xml create mode 100644 Seminar7/code/Server/.idea/vcs.xml create mode 100644 Seminar7/code/Server/out/production/Server/ClientResolver.class create mode 100644 Seminar7/code/Server/out/production/Server/Computer.class create mode 100644 Seminar7/code/Server/out/production/Server/ComputerBuilder.class create mode 100644 Seminar7/code/Server/out/production/Server/Main.class create mode 100644 Seminar7/code/Server/out/production/Server/ServerHandler.class create mode 100644 Seminar7/code/Server/out/production/Server/Store.class create mode 100644 Seminar7/code/Server/out/production/Server/StoreSeeder.class create mode 100644 Seminar7/code/Server/out/production/Server/StreamCommunicator.class create mode 100644 Seminar7/code/Server/src/ClientResolver.java create mode 100644 Seminar7/code/Server/src/Computer.java create mode 100644 Seminar7/code/Server/src/ComputerBuilder.java create mode 100644 Seminar7/code/Server/src/Main.java create mode 100644 Seminar7/code/Server/src/ServerHandler.java create mode 100644 Seminar7/code/Server/src/Store.java create mode 100644 Seminar7/code/Server/src/StoreSeeder.java create mode 100644 Seminar7/code/Server/src/StreamCommunicator.java diff --git a/Seminar7/code/Server/.idea/.gitignore b/Seminar7/code/Server/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/Seminar7/code/Server/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Seminar7/code/Server/.idea/misc.xml b/Seminar7/code/Server/.idea/misc.xml new file mode 100644 index 0000000..03f397c --- /dev/null +++ b/Seminar7/code/Server/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Seminar7/code/Server/.idea/modules.xml b/Seminar7/code/Server/.idea/modules.xml new file mode 100644 index 0000000..7ec00e1 --- /dev/null +++ b/Seminar7/code/Server/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Seminar7/code/Server/.idea/vcs.xml b/Seminar7/code/Server/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/Seminar7/code/Server/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Seminar7/code/Server/out/production/Server/ClientResolver.class b/Seminar7/code/Server/out/production/Server/ClientResolver.class new file mode 100644 index 0000000000000000000000000000000000000000..c5a5a53b80a78085af55713ef6060cbf3a9e3ecb GIT binary patch literal 1643 zcmZuyYgZdp6x}xu2GZe`Kue%fTCL^}V}=XFn{FJRhQ(DLl(lD^YT z9nY0Pbs(y%023xAF(r`h1)eVjjAl!1>ISlO(H>#obgM1fa$V_XFoRhGS4~{QoWSJ& zLNbd389Z^1!=M%T(mE2jRwxdD9j_Ww)H9gIysC6TV6JefNwIC<4S}2nq5D(caih2@ zj~gbIaFa<8ax)BKm~%1AxQ3wD@umWOOCWy%I%K0z-xkOY3TSvoN1^N37Fo&QHVVqS zC{T{P7TOM2Pepm5W!m=ZdU2Rz2!Nv8_zsHSwMr ziEJ;h{NTdQ@m$5@9TOiYb=vNFJ*kSkW8kB6=JW(T_{EmT0qw`|32FxFChp!fB_43Fj#c-% zE+a?O#Vw2NQ9NRO4Ubc1t2~!x;y1(Z6^t>^1jeyW$G$ZH zeuutjXKI{jXZiv9qMyid`ak<1b+dW1_vYlDbARXY{`2O~zX9AqJ&7LlCJ@sRN1wvT ziTR_ME1T8h+~eYjRdN*iR_vMjohUr)e7yNr1yJF=EwUSvDxqsE9j`uL7FhJEOEwk#_&QD@_b78lim(J+8 zh)W7RHt$oLBAV548CMkIMbC{?rx$YUeH|aT$1380NYXkMkYS|krpK=iM0{1pH7qiw zl@9VT&vXb}ls?q)k#K20_{<4>tS}Ld=UU4yA6kthme8x=Cfy7&v20us>8CnwVVO*3 zy>3ZTu0NkP6E3+_T;)zH_Y#zA-7ZWide(GLNX{O@7}Lnhjfi`@)#>!fq0x z5LnAAtVU$EBQN=b$dY3>Fu`mQNpMWC*b>d6!eobqpfnT)w;i)|x@FdVsN{C7)hJo_ zZNVN4$QC7-m}M+_IBNR%Wy5jTBVFdMY$JDN^SCR!#a-C|T!%q2>M4}?E5}uFjb~m# z{q7z+TJU+^o+Kl9Q)u7)UQvTYE5qBuCXH?&Q8z&!&GLt-Fp1k0BGnb=#21fL>Q7?#0}l zI2b1z+l$|K-TMR6zw%HI&fFW1bkD|=ue{3BZj3WiFEM@Qgv@pjInGHaz~kPjx4?aF zMkKE6&j=9Xw-CroBnV%E4v_3i7-0~DAhTUSq$C7{T^|857Y3P&1mV*X1u_=~nI}lP z3y366K-jzyAoJnWWFtZNoOOUOHS=MRMS|qIfSj;y1cXy90wf!b$WkN-AI}aDrY0K( zxj~SdT|lI41%zWR0wfoX$gM~aKE)j%j7TmFvO86>4ixuW_2- LbD&UlJFfO0dcBy> literal 0 HcmV?d00001 diff --git a/Seminar7/code/Server/out/production/Server/ComputerBuilder.class b/Seminar7/code/Server/out/production/Server/ComputerBuilder.class new file mode 100644 index 0000000000000000000000000000000000000000..bbb31eaaddda060713ae322e5a7600e2d40bc26b GIT binary patch literal 1237 zcmZ`&Ygf}i5WU+L5<(Ox55Z>j(IwJkB;WP{4fH&D@>2b7yDv&)@IA0X)HL4G}~Y#B_9_o1uS~f99Dww`-a8@~)_O z4Bd;SZF)-#(PU~vfy$uf9c$0`gsVZ3!Mz=ECC@eOnu>D_`if0niL%S>>Vk$o^eY(9 z5yv1yocsj0h|2P9v%)>cC9o2jq9_^-UK0#c$q&Yntbjh7It@uR!#YNAfh0DBmy>B3 zhLfjVQyVJALVcD~6?M|5;F6BZ?eL;RRa|A52+F8(Pb8A2o!E9|9I+-4X!y4IZI)CITc zPFfm6^UT}Zl7jB)xQF`;3JH2oxMW$2}BZE~5hAZIei(AS>HoNv~vk~Ws72GW6H+G5SO%EEoe z%XLEHt&VJP*Oc}l81r__2I(_S@MNiLWl719NE)YSmO9psAXoipe&sk1ofSX@lUM8g zFlfi4wUXnz6|rhcl|yaX!-8gIuP}~h^kC%|0VzW_t$DICv@_bf(g$E)TgS_^%3~mk z6DVu5o6)#lYT|qoLm@C402f2xiUhvl zS`#-zEEQnWP235wxd40E#N+8Nlq&+MZ<2J7XsD!MjJlkrzd6F@X;Z0wEw^zSE|pC8?c+xDDwCI86#J6gM_d>^h}sn+ki2g@xowGA;k4 z|DbP~X&)dHX4>~Y_W$(btmJ@5n2tu;)!loq&OPVu>d$|k{|?{*4s>*(TSLM?50V0x z_RIq_Yn#q)c4KEx)&ha#J&vwJss5)kL(Z>H|faY9S!dq zxPy6;7y>)bgMijjtk@2Tx+rT^!+Qjuw=L-eThe!Jim2l*K2V~4C~&zG6-pc_)6qY} z*u`_gR2t;Iu44iBRP;xzG5*c1w&R#Pw#=z6Sil1VAFKZCVc8yAW|*k zt0ExiH}FD_s%f`nH;{VD4$ixE$!;oIj$Ci=??|tratn+U-I{4vP0v#A9b+PBSX47v zR2w13=zWd?v)#(QszJFk5;?vhR0n%Wuf~;*1b53S9E?w(0xQ*TBQ~+AAerREY+jf>-|ERdr!l zHm+ibzXyQ*g3s`fW07xNDDkFJNb~3aE|@xVt@f>lp7Z zCQh)hJ(V~{?g;mP`S-8&6Xdt&j?u7|>8_X;>;~AwU_1e2-m@y$ovbcF^LR&e;#} H(ZA literal 0 HcmV?d00001 diff --git a/Seminar7/code/Server/out/production/Server/ServerHandler.class b/Seminar7/code/Server/out/production/Server/ServerHandler.class new file mode 100644 index 0000000000000000000000000000000000000000..776302fe6f78e853b140f8564b8821268d2ad26e GIT binary patch literal 1856 zcmZux?N%FA6x}z3OqdR(1Sl3LonmYAL4nqOsG(4xR3d>^Q-ic>nIu=3HkrZ61j-Zf z!yhi!va!px^Z|SpAHikacbJeMtXawZI_KfhM+99Oq6WGV6S(lo z+Owvsma{$mr1VObeSz4l?b!aDKvyzV>_Hqo8gv8aUK$YjRBGz* zx`B_>;XudXQiFokq!&k$;;*|tb2r^x-*%li(gNl(@s(tzWjb=-r0Z^jJ7z`tmR+q+ zDQPAR+{BcCZdEGBsh>%*sAC4VG~71uG42TTErszG8+Ns#((Ep&8G(~$l{%GgIb?O* z!+i~(82A*Q(J!dCz&Ubg*)XGI+aXnqW0rMVY#bcr)5?W8+{2u~=6y(Qx23z_pDXyS&p@X5Vd8E9QaQFst@!Y5J~NY8)t;9;xbTbI>p2WEFNmrE5j3WJO$GY$C+=pJ)^*?Vd=d_C zD4T}2yS(GtWyTUYb)9{y{CdsW4MW9Snv-$6G6E6#hSGO*^>LDf(OXVA_C@^1hUNIS zf582M+wjWru^li!L2gRPg{y4(2w$ZLKfW%(jIRX8DA+N0%KtC9+Eh;#$G-G$5b4ZY zXz9tf==~*d_=^7tI)K5V=;5d1Ji`^ZV;;HT8NTL<0Rk8i%oI?B07b+?n4bx$=sZpT z3N88v`Zv201BE7r-eGih^d}VnDgKm=iZL3_C(=z^J;W%7Ylj%@@V=qEHx$nKQDvKe^VVJFQDd2tah)2kK5VP38H^khecMqcfU`E7ZiasBckot=#>uGkr1|@=g;wi@gwS83UU6XNJ{K2 z6+U((Qns)eRVYn7C~S5o7J{tRc@s-YJ2egUTn_bopzKG-n)v(>!|QE>n@y~Rq6uF3 z6<(p$P`oq?t0Wv?)?*w^Tt|k?=8>dbhIT3Bk;Y3*2HB*^Gf(Sp@mHu@zB4z~Ef0J>UB>(^b literal 0 HcmV?d00001 diff --git a/Seminar7/code/Server/out/production/Server/Store.class b/Seminar7/code/Server/out/production/Server/Store.class new file mode 100644 index 0000000000000000000000000000000000000000..d80240d333c294841ac249f4aa0cf0e431eb09a2 GIT binary patch literal 3340 zcmcImYjYb{8Geo|X=Qn1J5H^{O`IyhPAogI3!x>oLR!a7s6cko+O>&Wplj*a-gvbu zc2{v!?v!$AxI>{S1ubY1c@GyRtMhqPq;yMyY3iMtwE*le;VXsb{U%n(uzCiMnX`B9OfsT>ff`*j9 zol*c<>G^4B=t!drx`tgky3r%h-_)CSEQ{Vu$MzV+tJ|eD*RjoKWmy>u z93Ck)m6a%;+b-zDZuDu`qvIa*3nb=z$CUzo1Z-NyvL&aTO10)oS4G%v+=ac^r(r3A>LEGgd!GZko$7D}1NUOb{>3TGJ2^Id6F1olU`g5C{MuQ5|P-j%oN(HO(Q`p{39(H9P3Ai3_eQn*n-bH$?5xX)Vi24b z#)?tmYORx&O6$|#TX$T;C=BqJVV5oG4HX?{y;jZNRk&73o8df(``=!MA>ZD%v4aAd z8)2sn%bMfIBj1Z*vhuw&KbEiP_%goIiyv%j+$Ac!n#PatV+}u1bNW+(q2_XFcnrrr zTp;Hn;$HgyQ`J~EPqmO8ogK)wo~6|_>I89w^!vF44VvxSh*O#b%IwObol#GKE>d2HFE>0fQ zF?tKy#T)4S6ViV~@9Tkq&#OA!fr@ed)p)0eCb*0ZJ_%ewz2WXjYAW4ReUO zF3$cnMEEdICO0t+Y~g(|US}Fevu?6Uj$4yzmy1y=k?RLbfqOC#vVVSz`O%SE0qPBi`{EC1I^q ztAmLtuwjFB2O#*!)V~@umCgHvl_$q$2@EMbbbDX$JGyJ#i~t*Y8wb zH;;KMWJo{ud@tN)Q0!tug~pJoha%uQR46*KFjV9WS5sdo$CLQqnSLlVna5%YTz=~s6n^go=RuP z+0N9V#x$%mBzFhHfre`g>Jj&wLcEvF*anIyF(kxWh9$dJtIbxe;RcO&x8Lt_4P`3# z(rrnO)i;44?mf|_SlhWyc;S#keem#eG=^UKM3I;!PYgs2 zjra^&&Xi61_i&#uT|UPN+ehM-Z2P5Cn1_~SEhuB0)ke7Z1uJpvigh(vP=AT$+Kht literal 0 HcmV?d00001 diff --git a/Seminar7/code/Server/out/production/Server/StreamCommunicator.class b/Seminar7/code/Server/out/production/Server/StreamCommunicator.class new file mode 100644 index 0000000000000000000000000000000000000000..0b1e9809e1c54d7c1d0d224dbf614d0f09fdea40 GIT binary patch literal 2869 zcmcImYjfK~6g_Jvk)tSS>Lw|DH^rriQ|cBd6dd|WNgHtbz#*kA1va*tsF5Wj$)(|4 z-j6@P&wv@)(l9W5X827EXC*n7V^4=Kcs$Z-wfCHR&%Jk7fBm!m2Y^%fK}QN{4Lt^W z(I+r`Ti%fcTRO{yOG~%SN+8fTZ8=tOQXoAsx!8{k`ZeeV24Dze%YkRg+KgMPH5{uV z1J@HsS&qQAQi#+DEW1#yNXId~qB0&bum!^ea~s4SjaFH1;j(8rL4;P+kR?FN{*33z zwUXrrI<_LMP(}@mVY@)4X8OKdHhp@YAXICM1K5F`8g?1jjXeTGEj;y_9>{8y9!x}b ziI9tv*RvF7pMm`t7wDDsy1gcFU}7>sajW4}0?Ty@b51ZDk3-5Q4dif8KnvW^Z-L`6 zMW{tryU81!PG~qp5t1*cj^!*Db;dtwAde~PU)fOJoR6dsI5g44D)v1LGX_!uJ#FBK z%6o6cHl?TID2{1(#=x_9u2~GB+hHhFxz7(1E^wrcB&wBSQlMyIfm|$*Vg}C(APa#L zO4b(z4tAIGtmQ~=?NZ(JR54M}BBnLGY~UnL32bjAu??S&d1kdyF$LzjB(z)JHW4ND ziKEeQrl~#zZ@TEPlOejuHI`8ttQBc@N( zTGOGBFiH8#N7%0T*@dRIQ*b z=Z?E-7D{0-w2-%Cg?+{CY@n8W`a4-Cu~9^DK{{32^m8TGU2W8hoq{H2+6))g>Sl8J ze}5TKKHfIjuQBqBwC%DLn8ji66dI-E3D@GDrdm8jGH4j6pgOGncQs|t5dRY$pW-tO_f&I!&fe+BMwI=iUuT>7`0@YGKwNdn)1iIi4*O)Yni+h_ zGhGM)V@@68Gq&`7)7S6~H}Q_xx)qIv?*#Tfekt2BsxXXG&lJz!G_U-~;3%a&8Nxdc zeI6mNhvPE(IPS#)uWxW>g%)Yvcjg~L$-N(q!q1_^{W3&Tw z^pK&S977o7VK{`VoZTOx;7wfP6Y)YXczSW9{78}SI?l(e=lHC=HS)h;Wa<%~Na1RW zM}1v^A1wns`V{t9v~~mAzzw`bB*Ei+K1RG5Mh=YU734bh0*`PYg+KAs15A|E>dPUfx=lMJS#IiA6tc(H@q*%dDNU}^ z^CTKP`~YWp*B;=)X0}}z!5)5E?M;Z5+XyU&w{eIpBNXf%@}`D$)gdwOH2qe>Wb#w% zxCpecRZ2nBRsCmg+xHGG7RY4ZiX;tHLkukkIu$BusiHT>ZO literal 0 HcmV?d00001 diff --git a/Seminar7/code/Server/src/ClientResolver.java b/Seminar7/code/Server/src/ClientResolver.java new file mode 100644 index 0000000..57eda9f --- /dev/null +++ b/Seminar7/code/Server/src/ClientResolver.java @@ -0,0 +1,34 @@ +import java.io.PrintStream; +import java.net.Socket; +import java.util.Scanner; + +public class ClientResolver implements Runnable { + private Socket clientSocket; + private Store store; + + public ClientResolver(Socket socket, Store store) + { + this.clientSocket = socket; + this.store = store; + } + + public void run() + { + Scanner in = null; + PrintStream out = null; + try { + in = new Scanner(clientSocket.getInputStream()); + out = new PrintStream(clientSocket.getOutputStream()); + StreamCommunicator communicator = new StreamCommunicator(in, out); + + var server = new ServerHandler(communicator, this.store); + server.startCommunication(); + } catch (Exception e) { + System.out.println(e.getMessage()); + System.out.println("Connection closed."); + } finally { + in.close(); + out.close(); + } + } +} diff --git a/Seminar7/code/Server/src/Computer.java b/Seminar7/code/Server/src/Computer.java new file mode 100644 index 0000000..c592ebc --- /dev/null +++ b/Seminar7/code/Server/src/Computer.java @@ -0,0 +1,76 @@ +import java.time.LocalDate; + +public class Computer { + private int id; + private String brand; + private String model; + private boolean isUsed; + private LocalDate manufacturedDate; + private int quantity; + + public Computer() { } + + public Computer(String brand, String model, boolean isUsed, LocalDate manufacturedDate, int quantity) { + this.setBrand(brand); + this.setModel(model); + this.setUsed(isUsed); + this.setManufacturedDate(manufacturedDate); + this.setQuantity(quantity); + } + + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + public String getBrand() { + return brand; + } + public void setBrand(String brand) { + this.brand = brand; + } + + public String getModel() { + return model; + } + public void setModel(String model) { + this.model = model; + } + + public boolean isUsed() { + return isUsed; + } + public void setUsed(boolean used) { + isUsed = used; + } + + public LocalDate getManufacturedDate() { + return manufacturedDate; + } + public void setManufacturedDate(LocalDate manufacturedDate) { + this.manufacturedDate = manufacturedDate; + } + + public int getQuantity() { + return quantity; + } + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("Computer: #").append(this.getId()).append("\t") + .append(this.getBrand()).append(" ").append(this.getModel()).append("\t") + .append("Year of manufacturing: ").append(this.getManufacturedDate().toString()).append("\t") + .append("Quantity: ").append(this.getQuantity()).append("\t"); + if(this.isUsed()) { + sb.append("Computer is used").append("\t"); + } + + return sb.toString(); + } +} diff --git a/Seminar7/code/Server/src/ComputerBuilder.java b/Seminar7/code/Server/src/ComputerBuilder.java new file mode 100644 index 0000000..2c161ad --- /dev/null +++ b/Seminar7/code/Server/src/ComputerBuilder.java @@ -0,0 +1,14 @@ +import java.time.LocalDate; + +public class ComputerBuilder { + public static Computer build(StreamCommunicator communicator) { + Computer computer = new Computer(); + computer.setBrand(communicator.communicateMessage("Enter brand:")); + computer.setModel(communicator.communicateMessage("Enter model:")); + computer.setManufacturedDate(LocalDate.parse( + communicator.communicateMessage("Enter date (in format: yyyy-mm-dd):"))); + computer.setUsed(Boolean.parseBoolean(communicator.communicateMessage("Is used:"))); + computer.setQuantity(Integer.parseInt(communicator.communicateMessage("Quantity:"))); + return computer; + } +} diff --git a/Seminar7/code/Server/src/Main.java b/Seminar7/code/Server/src/Main.java new file mode 100644 index 0000000..99c652c --- /dev/null +++ b/Seminar7/code/Server/src/Main.java @@ -0,0 +1,29 @@ +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketException; + +public class Main { + private static final int SOCKET_PORT = 5038; //Any random free port + + public static void main(String[] args) { + ServerSocket server = null; + Store store = StoreSeeder.getSeededStore(); + + try { + server = new ServerSocket(SOCKET_PORT); + + while (true) { + Socket client = server.accept(); + System.out.println("Connection established: " + client.getInetAddress().getHostAddress()); + + ClientResolver resolver = new ClientResolver(client, store); + new Thread(resolver).start(); + } + } catch (SocketException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/Seminar7/code/Server/src/ServerHandler.java b/Seminar7/code/Server/src/ServerHandler.java new file mode 100644 index 0000000..dee0e11 --- /dev/null +++ b/Seminar7/code/Server/src/ServerHandler.java @@ -0,0 +1,45 @@ +import java.io.PrintStream; +import java.util.Scanner; + +public class ServerHandler { + private StreamCommunicator communicator; + private Store store; + + public ServerHandler(StreamCommunicator communicator, Store store) { + this.communicator = communicator; + this.store = store; + } + + public void startCommunication() { + int choice = Integer.parseInt(communicator.communicateMessage("Write 1 for administrator and 2 for client")); + switch(choice) { + case 1: + this.communicateAdmin(); + break; + case 2: + this.communicateCustomer(); + break; + default: + communicator.communicateMessage("Not an option"); + } + } + + private void communicateAdmin() { + communicator.addMessage("Entering a new computer details."); + Computer computer = ComputerBuilder.build(communicator); + this.store.addComputer(computer); + } + + private void communicateCustomer() { + communicator.communicateMessage("Buying a computer.", + this.store.getAvailableComputers(), + "Which computer would you like to buy"); + int computerId = Integer.parseInt(communicator.communicateMessage()); + int quantity = Integer.parseInt(communicator.communicateMessage("How many?")); + try { + store.sellComputers(computerId, quantity); + } catch (Exception ex) { + communicator.communicateMessage(ex.getMessage()); + } + } +} diff --git a/Seminar7/code/Server/src/Store.java b/Seminar7/code/Server/src/Store.java new file mode 100644 index 0000000..c04f25d --- /dev/null +++ b/Seminar7/code/Server/src/Store.java @@ -0,0 +1,43 @@ +import java.util.*; + +public class Store { + private List availableComputers = Collections.synchronizedList(new ArrayList<>()); + + public void addComputer(Computer computer) { + computer.setId(this.availableComputers.size()); + this.availableComputers.add(computer); + } + + public String getAvailableComputers() { + StringBuffer sb = new StringBuffer(); + for (var computer : this.availableComputers) { + sb.append(computer); + } + + return sb.toString(); + } + + public void sellComputers(int computerId, int desiredQuantity) { + synchronized (this.availableComputers) { + var computerInStock = this.availableComputers.stream() + .filter(c -> c.getId() == computerId).findFirst(); + + if (computerInStock.isEmpty()) { + throw new IllegalArgumentException("Computer with id " + computerId + " doesn't exist."); + } + + Computer computer = computerInStock.get(); + int newQuantity = computer.getQuantity() - desiredQuantity; + if (newQuantity < 0) { + throw new IllegalArgumentException("There are only " + computer.getQuantity() + " computers left." + + " We cannot sell you " + desiredQuantity); + } + + if(newQuantity == 0) { + this.availableComputers.remove(computer); + } else { + computer.setQuantity(newQuantity); + } + } + } +} diff --git a/Seminar7/code/Server/src/StoreSeeder.java b/Seminar7/code/Server/src/StoreSeeder.java new file mode 100644 index 0000000..dc8f300 --- /dev/null +++ b/Seminar7/code/Server/src/StoreSeeder.java @@ -0,0 +1,17 @@ +import java.time.LocalDate; + +public class StoreSeeder { + public static Store getSeededStore() { + Store store = new Store(); + populate(store); + return store; + } + + private static void populate(Store store) { + store.addComputer(new Computer("Lenovo", "T15", false, LocalDate.now(), 5)); + store.addComputer(new Computer("Asus", "Zenbook", + true, LocalDate.of(2020, 10, 1), 2)); + store.addComputer(new Computer("Apple", "Macbook Pro", + false, LocalDate.of(2021, 10, 31), 30)); + } +} diff --git a/Seminar7/code/Server/src/StreamCommunicator.java b/Seminar7/code/Server/src/StreamCommunicator.java new file mode 100644 index 0000000..c773bba --- /dev/null +++ b/Seminar7/code/Server/src/StreamCommunicator.java @@ -0,0 +1,32 @@ +import java.io.PrintStream; +import java.lang.invoke.StringConcatException; +import java.util.*; + +public class StreamCommunicator { + private Scanner in; + private PrintStream out; + private List messages; + + public StreamCommunicator(Scanner in, PrintStream out) { + this.in = in; + this.out = out; + this.messages = new ArrayList<>(); + } + + public void addMessage(String message) { + this.messages.add(message); + } + + public String communicateMessage() { + var result = this.communicateMessage(this.messages.toArray(String[]::new)); + this.messages.clear(); + return result; + } + + public String communicateMessage(String ...messsages) { + String messageToSend = Arrays.stream(messsages).reduce("", + (String total, String current) -> total + current); + out.println(messageToSend); + return in.nextLine(); + } +}