要求:写一个有关股票的java小程序,写出相关属性,实现简单的几个方法,作为java初学者,简单明了的感受一下项目--
具体程序为:
package com.dr.stock;
public class Stock {
private String name;
private int price;
private int id;
private int flag;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the price
*/
public int getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(int price) {
this.price = price;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the flag
*/
public int getFlag() {
return flag;
}
/**
* @param flag the flag to set
*/
public void setFlag(int flag) {
this.flag = flag;
}
}
这个类列出了股票的几个属性,下面是股票server和client端的类代码,在client端调用服务器端的方法,从而得到一些信息。
package com.dr.stock;
import java.util.Random;
public class StockServer {
public static Stock[] getStock(){
Stock[] stock = new Stock[3];
Random rd = new Random();
//##############################
Stock sk1 = new Stock();
sk1.setName("ibm");
sk1.setPrice(rd.nextInt(100));
sk1.setId(1);
sk1.setFlag(1);
stock[0] = sk1;
//###############################
Stock sk2 = new Stock();
sk2.setName("sun");
sk2.setPrice(rd.nextInt(200));
sk2.setId(2);
sk2.setFlag(2);
stock[1] = sk2;
//##################################
Stock sk3 = new Stock();
sk3.setName("sico");
sk3.setPrice(rd.nextInt(300));
sk3.setId(3);
sk3.setFlag(3);
stock[2] = sk3;
return stock;
}
}
package com.dr.stock;
import java.util.Random;
public class StockClient {
public static void main(String[] args) {
while(true){
Stock[] sk = StockServer.getStock();
for(int i = 0;i<sk.length;i++){
if(sk[i] != null)
System.out.println("股票名字:"+sk[i].getName()+" 股票价格:"+sk[i].getPrice()+" 股票ID:"+sk[i].getId()+" 股票类型"+sk[i].getFlag());
}
try{
Thread.sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
break;
}
}
}
}
posted on 2010-10-08 21:51
迷人笑笑 阅读(405)
评论(0) 编辑 收藏