Vous êtes sur la page 1sur 7

FinalProject:StockMarket

JingjingWang
Theultimategoalofthisprojectistousedistributedobjectstoimplementsastock
marketgameforseveralhumanplayers.Theimplementationhasbeendonebytheuse
ofJavaRMIandCallbacktechnology.Andalsointhisfinalproject,Ihaveusedthe
centralserveralgorithmforthemutualexclusion.Andtheserverdesignedinthis
projectalsohasthefunctionofthefaulttolerate.Thedetailedinformationwillbe
describedasbelow.
1Thedesignandthearchitectureofthisgame
Inthisdesign,therearefourmainclasseswhichinteroperatewitheachother.
ThefirstclassisBank.Aplayercanrequestanewaccountbyuseofhisname,and
depositandwithdrawmoneyfromtheaccount.Inthisgame,oneplayerisallowedto
haveatmostoneaccountinthebank.Iftheplayerhasdepositedthemoneyinthis
account,thenonceevery5minutesduringthegame,themoneyintheaccount
accumulates3%interest.Accountclassisusedtomanagethefinancialactionofdeposit
andwithdrawforeachaccount.Inthisclass,Iusethelocktechnologytodomutual
exclusionfortheactionofdepositandwithdraw.Whenthebalanceoftheaccountis
lessthanthenumberofthewithdraw,thenthewithdrawactionneedstowaituntil
thereisenoughmoneyinthisaccount.
ThesecondoneclassisCompany.Acompanyhasanameandissuesstock.Oncea
minute,thecompanyspriceofthestockisadjustedbasedonarandomprocess.Inthis
case,Idefinedarandomvariableforthistwoactions.Thatisifthisvariableequalswith
zero,thenthepriceofthestockwillincreaseby1%to3%,andifitequalsone,thenthe
pricewilldecreaseby1%to3%.Alsotheadjustofthepriceofthecompanyisalso
basedonthelawofsupplyanddemand,thatisifaplayerbuysthestocksfromthe
company,thenthepriceofthestockwillbeup,andviceversa.Thefinallypriceafter
eachtransactionisbasedonthefollowingrule:
Theprice=(thebidpostedbytheplayerXthestocknumberpostedbytheplayer+the
stockpriceofthecompanybeforethetransactionXthestocknumberofthecompany)
/(thestocknumberpostedbytheplayer+thestocknumberofthecompany).
Whenthepriceofthestockofthecompanyorthenumberofthestockischanged,
thentheupdatemethodwillbeincurredandthecurrentinformationwillbeputinto
theStockExchangewhichtheplayersareabletonotice.Sointhiscase,atthebeginning
ofthegame,thecompanywillneedtoregisterintotheStockExchange.Acompany
buysorsellssharestoaplayerbyacceptingorrejectingtheplayersbid.Theprocessof
decidingtoacceptorrejectisrandom.Initiallyeachcompanyhas1000sharesofthe
stockwhichstartsat$30ashare.Therearethreecompaniesforthisgameandeach
companyhasonekindofstock.
Thethirdmainclassistheplayer.Inthisgame,wecouldallowseveralplayerstotake
partin(ofcourse,themorethebetter).Aplayerstartswith$2000and20sharesfor
eachstock(becauseIthinkitcanmakethegamebemoreinteresting)andbuysandsells
stockfromthecompaniesorotherplayersbytradingthroughtheStockExchange.The
goalofaplayeristomakemoreandmoremoney.Aplayerbuysorsellssharesfrom
anotherplayeroracompanybypostingbidsontheStockExchange.Sincetheplayer
cangettheupdatedinformationoftheStockExchangeandenterthebank.Soatfirst,
theplayershouldregisteritintobothserverandthebank.Thereareseveralfunctionsin
theplayer,IwilldescribetheminthePlayerGraphicInterface.
ThefinallymainclassiscalledStockExchange.Playersandcompaniesmustregister
withthestockexchange.Alltradingofthestocksmustgothroughthecentralizedstock
exchange.IntheStockExchange,IusethreeArrayListtorecordthestockinformation.
Theyrespectivelyincludesthecurrentinformationofthecompany,sellersandbuyers.
Forthecompany,oneArrayListrecordsthenameofthecompany,thenameofthe
stock,thepriceofthestock,thenumberofthestockandthecallbackobjectofthe
companywhichisusedtoupdatetheinformationofthecompanyandremotelyincurs
somemethodsinthecompanyobject.Fortheplayers,theothertwoArrayLists
respectivelyrecordtheinformationofthesellersandbuyers.Thatmeansifonewants
tosell,thenthestockExchangewillrecordtheinformationincludingthenameofthe
player,thenameofthestockthattheguywantstosell,thesellprice(bid),thenumber
ofthestockaswellasthecallbackobjectoftheplayer.Therulesorthestrategyofthe
transactionwillbedescribeddetailedasbelow.Finally,Idesignthetotaltimeofthe
gameis21minutes.Oncethetimeisout,theStockExchangewillcallbacktheplayers
listedinthecallbackobjectswhoisthewinner.Thedecisionofthewinneristheplayer
withthehighesttotalvalueincashanddepositinthebankaswellasstockvalue.
Meanwhile,theStockexchangeremindstheplayersthetimeofthegameonevery7
minutes.Bothofthemareimplementedbythetimersandcallbacktechnology.
2Thestrategyortherulesofthegame
Thereareseveralrulesorthestrategyinofthegametomakethegamemore
interestingandrealistic.
1)Aplayercannotbuyorsellsharesofstockthatdonotexistandheorshemusthave
enoughcashtopurchasethedesiredshares.
2)Ifaplayerwantstosellonekindofthestock,onlywhenheorshehasenough
numberofthestocktosell.
3)Thefinallypricebetweenabuyerandsellerfollowsthefollowingrule:
Theprice=(thebidpostedbytheplayer1Xthestocknumberpostedbytheplayer1
+thebidpostedbytheplayer2Xthestocknumberoftheplayer2)/(thestocknumber
postedbytheplayer1+thestocknumberoftheplayer2).
4)Whentheplayerwantstobuy(sell)thestock,theStockExchangeserverwill
automaticallyfindthepostinformationstoredinit,andfindonewhohaspostedthe
lowestsellprice(highestbuyprice).Ofcourse,thebuypricemustbegreaterthanthe
sellprice.Ifitcannotbesatisfied,thennotransactioncanoccurandthebuy/sell
informationwillstoreintothestockexchange.
5)Sincethecompanyisdesignedtobeabletoacceptthebidfromtheplayers
randomly,soifthecompanyacceptsthebidoftheplayer,thenitalsoneedtofollow
therule(4).However,ifitrejects,thenthestockexchangewillfindthesecondmost
availablechoiceforthebuyerortheseller.
6)Ifthebuyer(theseller)poststhenumberofthestockthatislargerthantheone
sellinformation(buyinformation),thenthestockexchangewillfindseveralplayersthat
storesthesellinformation(buyinformation)forthebuyer(theseller)tostratifythe
buyer(seller)sreuqestasmuchaspossible.However,ifthebuyers(thesellers)request
forthenumberofthestockislargerthanthetotalstocknumberofthe
sellinformation(buyinformation)inthestockexchange.Thenthestockexchangewillput
thereststocknumberofthebuyer(theseller)intothebuyinformation(sellinformation).
7)Inthisgame,therewillbethefollowingcasethatwemustprevent.Ifinthe
timestampt1,onebuyercalledplayer1poststhebuyinformationcalledT1intothe
stockexchange.Andinthetimestampt2,thisbuyerspendsalotofmoneyonanother
transactioncalledT2.However,intimestampt3,anotherplayercalledplayer2wantsto
sellsomestockthatsatisfyT1,butatthismomentthestockexchangechecksthe
player1doesnothaveenoughmoneytobuy.Thenthestockexchangewillconsiderthis
transactioncannotbefinished,andthenitremovesthebuyinformaionoftheplayer1
init,andrecordsthesellinformaitonoftheplayer2.Inthecase,therelationshipofthe
timestampisasbelow:t1<t2<t3.
3TheGraphicUserInterfaceofthegame
ThefollowingistheGraphicUserInterfaceforStockExchange.

Transactioninformationshowstheinformationofeachfinishedtransaction.And
FinancialInformationshowstheinformationincludingcurrentpriceofthestockinthe
companiesandthepostedinformationfromtheplayerswhichareverybeneficialand
convenientfortheplayertobefamiliarwiththecurrentsituationinthemarket.When
theplayerseethisgraphics,heorsheneedtopressStartGamebuttontostartthe
game.Therearethreedifferentkindsofthestockfortheplayertochoosestock1,
stock2aswellasstock3.Thebuttoncalledrequestplayerinformationisusefulwhen
theplayerwantstoknowwhetherthereissomeotherplayershavepostedsomeproper
buyinformationorsellinformationinthestockexchange.Thebuttonnamedrequest
companyinformationisconvenienttochecktheinformationofthecompany.The
buttoncalledshowmyinformationisforcheckingtheinformationofhisorhersuch
asthecash,theholdofthestocksandthedeposit.
Iftheplayerwantstodosomeactionsthathassomethingtodowithbank,thenheor
sheneedstopressthebuttoncalledEnterthebank,andheorshewillseeanother
graphicinterface.Seebelow:

Beforedoingsomeactions,theplayershouldfirstpressCreateAccountastoldinthe
suggestion.
4TheAlgorithmdesignedinthisgame
Sinceacollectionoftheplayersshareresources,mutualexclusionisneededtoprevent
theinterfaceandensureconsistency.Forthisreason,IusedtheCentralServer
Algorithmformanagingamutualexclusiontokenforasetofprocessestoenterthe
stockexchange.Thealgorithmisasbelow:
Aprocesssendsarequestmessagetoserverandawaitsareplyfromit.
Ifareplyconstitutesatokensignifyingthepermissiontoenterthestockexchange.
Ifnootherprocesseshasthetokenatthetimeoftherequest,thentheserverreplied
immediatelywiththetoken.
Ifthetokeniscurrentlyheldbyotherprocesses,theserverdosenotreplybutqueues
therequest.
Clientonexitingthestockexchange,amessageissenttothestockexchangeserver,
givingitbackthetoken.
5Thefaulttoleratedesignedinthisgame
Inthisgame,thestockexchangeserverisabletodetectwhethersomecertainplayer
failstoconnectduringthegame.Oneveryoneminute,thestockexchangewilldetect
eachplayerbysendingtherequest.Ifitfindssuchplayers,thenitwillsendthe
informationtootherplayersandremovetheinformationofthisplayerstoredinthe
stockexchange.
6Thesimulationofthisgameandhowtoimplement
Atfirst,starttheStockExchange.java.ThenstartBank.java.Afterthat,start
Company.javaaswellasPlayer.java.
Thefollowingisthepartsimulationofthegame:

Vous aimerez peut-être aussi