📄 new text document (3).txt
字号:
n=i;
//System.out.println(i);
n1=company.get(i).getprice();
}
}
// System.out.println("n"+"="+n);
for(int i=0;i<playersell.size();i++){
if(playersell.get(i).getstockName().equals(StockName)&&playersell.get(i).getsellprice()<m1&&!(playersell.get(i).PlayerCallback()==g)){
m=i;
m1=playersell.get(i).getsellprice();
}
}
// System.out.println(m1);
if((buyprice<n1&&buyprice<m1)||(n1<=m1&&m1==100000000&&mm==0)){
playerbuy.add(new PlayerbuyInformation(player,StockName,buyprice,StockNumber,g));
break;
}
if(n1<=m1&&buyprice>n1&&mm==1){ // to choose the company
CompanyInformation targetcom=company.get(n);
//System.out.println("n"+n);
CompanyCallBackInterface targetcompany = (CompanyCallBackInterface)targetcom.Companycallback();
averageprice=(buyprice*StockNumber+targetcom.getprice()*targetcom.getStockNumber())/(targetcom.getStockNumber()+StockNumber);
averageprice=(double)Math.round(averageprice*100)/100;
if(StockNumber<=targetcom.getStockNumber()){
try{
targetcompany.SellStock(StockNumber,buyprice);
g.BuyStock(StockName, StockNumber,averageprice);
callback(player,targetcom.getcompanyName(),StockNumber,StockName);
// System.out.println("tr");
}
catch(Exception e){
} // end try
//targetcompany.SellStock(StockNumber);
finally{
StockNumber=0;
}
}
else{
try{
StockNumber-=targetcom.getStockNumber();
targetcompany.SellStock(targetcom.getStockNumber(),buyprice);
g.BuyStock(StockName,targetcom.getStockNumber(), averageprice);
callback(player,targetcom.getcompanyName(),targetcom.getStockNumber(),StockName);
continue;
}
catch(Exception e){
}
}
}
else if((m1<n1&&buyprice>m1)||(n1<=m1&&buyprice>n1&&mm==0)){ // to choose the player
PlayerSellInformation targetpla=playersell.get(m);
PlayerCallBackInterface targetplayer=targetpla.PlayerCallback();
averageprice=(buyprice*StockNumber+targetpla.getsellprice()*targetpla.getStockNumber())/(targetpla.getStockNumber()+StockNumber);
averageprice=(double)Math.round(averageprice*100)/100;
if(StockNumber<targetpla.getStockNumber()){
try{
g.BuyStock(StockName, StockNumber, averageprice);
targetplayer.SellStock(StockName, StockNumber, averageprice);
callback(player,targetpla.getplayerName(),StockNumber,StockName);
targetpla.SetStockNumber(targetpla.getStockNumber()-StockNumber);
}
catch(Exception e){
}
finally{
StockNumber=0;
}
}
else if(StockNumber>=targetpla.getStockNumber()){
try{
StockNumber-=targetpla.getStockNumber();
g.BuyStock(StockName,targetpla.getStockNumber() ,averageprice);
targetplayer.SellStock(StockName,targetpla.getStockNumber(),averageprice);
callback(player,targetpla.getplayerName(),targetpla.getStockNumber(),StockName);
playersell.remove(m);
continue;
}
catch(Exception e){
}
}
}
}
}//end find2
public void UpdateCompanyInformation(String companyName,double price,int StockNumber){
for(CompanyInformation s:company){
if(s.getcompanyName().equals(companyName)){
s.Setprice(price);
s.SetStockNumber(StockNumber);
}
}
}// end UpdateCompanyInformation
public static void main(String args[]){
try{
StockExchange obj = new StockExchange();
startRegistry(1099);
Registry registry = LocateRegistry.getRegistry();
registry.rebind("Stock Exchange", obj);
System.out.println("Stock Exchange " + obj + " registered");
Timer timer=new Timer(); // the game time is 7 minutes, the sever will find who wins.
timer.scheduleAtFixedRate(new TimerTask(){
public void run() {
if(endstatus==1){
double m=0;
double n=0;
String name=null;
try{
for(int i=0;i<callbackObjects.size();i++){
endstatus=0;
PlayerCallBackInterface player=(PlayerCallBackInterface)callbackObjects.elementAt(i);
if((player.getBalance()+player.checkaccount(player.getName()))>=m){
m=(player.getBalance()+player.checkaccount(player.getName()));
n=i;
name=player.getName();
}
}
//endstatus=0;
for(int i=0;i<callbackObjects.size();i++){
PlayerCallBackInterface player=(PlayerCallBackInterface)callbackObjects.elementAt(i);
player.showResult(name);
}
}
catch(Exception e){
}
}
}
}, new Date(),350000); // 7 minutes
}
catch(Exception ex){
ex.printStackTrace();
} // end timer
Timer timer1=new Timer(); // to check whether some players has been crashed,if so remove it,
// and tells others
timer1.scheduleAtFixedRate(new TimerTask(){
public void run() {
for(int i=0;i<callbackObjects.size();i++){
try{
PlayerCallBackInterface player=(PlayerCallBackInterface)callbackObjects.elementAt(i);
if(player.Check()==1){
System.out.println("connection"+" "+player.getName());
continue;
}}
catch(Exception e){
String playname=(String)playername.elementAt(i);
for(int k=0;k<playersell.size();k++){ // to remove the information of this player
if(playersell.get(k).getplayerName().equals(playname)){
playersell.remove(k);
k--;
continue;
}
else continue;
}
for(int k=0;k<playerbuy.size();k++){ // to remove the information of this player
if(playerbuy.get(k).getplayerName().equals(playname)){
playerbuy.remove(k);
k--;
continue;
}
else continue;
}
playername.removeElementAt(i);
callbackObjects.removeElementAt(i);
for(int j=0;j<callbackObjects.size();j++){
try{
PlayerCallBackInterface play=(PlayerCallBackInterface)callbackObjects.elementAt(j);
play.show55(playname);}
catch(Exception ex){
}
}
continue;
}}
}
}, new Date(),
5000); // the period is one minute and end timer1
} // end main
} //end StockExchange
class CompanyInformation{ //for Stock Exchange to Store the information of the company;
String company;
String StockName; // record it as company name
double price; // current price of a stock in a company
int StockNumber;
CompanyCallBackInterface g;
CompanyInformation(String company,String StockName,double price,int StockNumber,CompanyCallBackInterface g){
this.company=company;
this.StockName=StockName;
this.price=price;
this.StockNumber=StockNumber;
this.g=g;
}
public String getcompanyName(){
return company;
}
public String getName(){
return StockName;
}
public double getprice(){
return price;
}
public int getStockNumber(){
return StockNumber;
}
public CompanyCallBackInterface Companycallback(){
return g;
}
public void Setprice(double price){
this.price=price;
}
public void SetStockNumber(int number){
this.StockNumber=number;
}
} // end CompanyInformation
class PlayerSellInformation{ // record the information of the selling of the player
String player;
String stock;
double sellprice;
int StockNumber;
PlayerCallBackInterface k;
PlayerSellInformation(String player,String stock,double sellprice,int StockNumber,PlayerCallBackInterface g){
this.player=player;
this.stock=stock;
this.sellprice=sellprice;
this.StockNumber=StockNumber;
this.k=g;
}
public String getplayerName(){
return player;
}
public double getsellprice(){
return sellprice;
}
public String getstockName(){
return stock;
}
public int getStockNumber(){
return StockNumber;
}
public PlayerCallBackInterface PlayerCallback(){
return k;
}
public void Setsellprice(double sellprice){
this.sellprice=sellprice;
}
public void SetStockNumber(int number){
this.StockNumber=number;
}
} //end PlayerSellInformation
class PlayerbuyInformation{ // record the information of the buying of the player
String player;
String stock;
double buyprice;
int StockNumber;
PlayerCallBackInterface k;
PlayerbuyInformation(String player,String stock,double buyprice,int StockNumber,PlayerCallBackInterface g){
this.player=player;
this.stock=stock;
this.buyprice=buyprice;
this.StockNumber=StockNumber;
this.k=g;
}
public String getplayerName(){
return player;
}
public double getbuyprice(){
return buyprice;
}
public String getstockName(){
return stock;
}
public int getStockNumber(){
return StockNumber;
}
public PlayerCallBackInterface PlayerCallback(){
return k;
}
public void Setbuyprice(double buyprice){
this.buyprice=buyprice;
}
public void SetStockNumber(int number){
this.StockNumber=number;
}
} // end PlayerbuyInformation
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -