/**
*<p>ShareCaculator</p>
*<p>实现股票利润计算</p>
*版本:0.0.1<br />
*<p>创建日期:2007.3.27 8:49:36</p>
*实现:<br />
*功能综述:实现股票利润的简单计算<br />
*功能1:实现单股股票多次买入,多次卖出的利润计算<br />
*功能2:可以选择市场,然后按照市场的费用进行计算<br />
* 沪市利润计算公式:
* (买出股票后资金-(投入本金+印花税+佣金+过户费))/投入本金
* 深市利润计算公式:
* (买出股票后资金-(投入本金+印花税+佣金))/投入本金
* 其中:
* 投入本金=SUM(一次买入股票数*一次买入股票单价)
* 印花税=投入本金*0.001
* 佣金=投入本金*0.015>5?投入本金*0.015:5
* 过户费=买卖股票数*0.001>1?买卖股票数*0.001:1
*
* 最后的结果将以绝对现金数量和百分比方式显示
*<p></p>
*版本:0.0.1 beta 1 Build 070<br />
*完成:
*<br />
*<br />
*<br />
*<br />
*<br />
*<br />
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShareCaculator extends JApplet{
JTextField[] txtPriceIn=new JTextField[3];//三个买入股票价格的文本框
JTextField[] txtNumIn=new JTextField[3];//三个买入股票数量的文本框
JTextField[] txtPriceOut=new JTextField[3];//三个卖出股票价格的文本框
JTextField[] txtNumOut=new JTextField[3];//三个卖出股票数量的文本框
JComboBox cmbShare=null;//沪A和深A的下拉列表
JButton btnCal=null;//计算按钮
JLabel lblSum=null;//显示结果的标签
//初始化
public void init(){
//初化计算控件
for(int i=0;i<3;i++){
txtPriceIn[i]=new JTextField(6);
txtPriceOut[i]=new JTextField(6);
txtNumIn[i]=new JTextField(6);
txtNumOut[i]=new JTextField(6);
}
btnCal=new JButton("计算");
String[] str={"沪A","深A"};
cmbShare=new JComboBox(str);
this.setLayout(new GridLayout(9,1));
//初始化界面
JPanel[] p=new JPanel[9];
p[0]=new JPanel(new FlowLayout(FlowLayout.CENTER));
JLabel lblSaleIn=new JLabel("买入");
p[0].add(lblSaleIn);
add(p[0]);
JLabel[] lblPriceIn=new JLabel[3];
JLabel[] lblNumIn=new JLabel[3];
JLabel[] lblPriceOut=new JLabel[3];
JLabel[] lblNumOut=new JLabel[3];
for(int i=0;i<3;i++){
lblNumIn[i]=new JLabel("第"+(i+1)+"次买入数量:");
lblPriceIn[i]=new JLabel("单价(元):");
lblNumOut[i]=new JLabel("第"+(i+1)+"次卖出数量:");
lblPriceOut[i]=new JLabel("单价(元):");
//买入的界面
p[i+1]=new JPanel(new FlowLayout(FlowLayout.LEFT));
p[i+1].add(lblNumIn[i]);
p[i+1].add(txtNumIn[i]);
p[i+1].add(lblPriceIn[i]);
p[i+1].add(txtPriceIn[i]);
add(p[i+1]);
}
JLabel lblSaleOut=new JLabel("卖出");
p[5]=new JPanel();
p[5].add(lblSaleOut);
add(p[5]);
for(int i=0;i<3;i++){
//卖出的界面
p[i+5]=new JPanel(new FlowLayout(FlowLayout.LEFT));
p[i+5].add(lblNumOut[i]);
p[i+5].add(txtNumOut[i]);
p[i+5].add(lblPriceOut[i]);
p[i+5].add(txtPriceOut[i]);
add(p[i+5]);
}
p[1].add(cmbShare);
p[8]=new JPanel(new FlowLayout(FlowLayout.LEFT));
btnCal=new JButton("计算");
btnCal.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
lblSum.setText("利润:");
if(cmbShare.getSelectedIndex()==0)//判断是沪A还是深A
lblSum.setText("沪A:"+lblSum.getText()+getSumForShanghai());
else
lblSum.setText("深A:"+lblSum.getText()+getSumForShenzhen());
}
});
lblSum=new JLabel("利润:");
p[8].add(btnCal);
p[8].add(lblSum);
add(p[8]);
}
//计算沪A的利润
private String getSumForShanghai(){
double gain=0,corpus=0,income=0;//利润,本金,收入
int countIn=0,countOut=0;//计算交易(买和卖)的股票总数
for(int i=0;i<3;i++){
if(!txtPriceIn[i].getText().trim().equals("")&&
!txtNumIn[i].getText().trim().equals("")){
corpus+=Double.parseDouble(txtPriceIn[i].getText())*
Double.parseDouble(txtNumIn[i].getText());
countIn+=Integer.parseInt(txtNumIn[i].getText());
}
if(!txtPriceOut[i].getText().trim().equals("") &&
!txtNumOut[i].getText().trim().equals("")){
income+=Double.parseDouble(txtPriceOut[i].getText())*
Double.parseDouble(txtNumOut[i].getText());
countOut+=Integer.parseInt(txtNumIn[i].getText());
}
}
gain=(income-(corpus+corpus*0.001+income*0.001+
(corpus*0.002>5?corpus*0.002:5)+(income*0.002>5?income*0.015:5)+
(countIn*0.001>1?countIn*0.001:1)+(countOut*0.001>1?countOut*0.001:1)));
return ""+(long)(gain*100)/100.0+" 盈利率:"+((int)((gain/corpus)*10000)/100.0)+"%";
}
//计算深A的利润
private String getSumForShenzhen(){
double gain=0,corpus=0,income=0;//利润,本金,收入
for(int i=0;i<3;i++){
if(!txtPriceIn[i].getText().trim().equals("")&&
!txtNumIn[i].getText().trim().equals("")){
corpus+=Double.parseDouble(txtPriceIn[i].getText())*
Double.parseDouble(txtNumIn[i].getText());
}
if(!txtPriceOut[i].getText().trim().equals("") &&
!txtNumOut[i].getText().trim().equals("")){
income+=Double.parseDouble(txtPriceOut[i].getText())*
Double.parseDouble(txtNumOut[i].getText());
}
}
gain=(income-(corpus+corpus*0.001+income*0.001+
(corpus*0.002>5?corpus*0.002:5)+(income*0.002>5?income*0.015:5)));
return ""+(long)(gain*100)/100.0+" 盈利率:"+((int)((gain/corpus)*10000)/100.0)+"%";
}
}