Groups
Groups
Sign in
Groups
Groups
Good Ideas
Conversations
About
Send feedback
Help
answer for 維克諾
7 views
Skip to first unread message
Steven_Tian
unread,
Dec 1, 2010, 8:48:15 PM
12/1/10
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Good Ideas
import java.text.DecimalFormat;
import java.util.*;
public class StockTransaction {
public static void main(String[] args) {
LinkedList ll = new LinkedList();
TransactionRecord tr1 = new TransactionRecord();
tr1.setbOrs("B");
tr1.setUnit(10);
tr1.setPrice(12.5f);
TransactionRecord tr2 = new TransactionRecord();
tr2.setbOrs("B");
tr2.setUnit(80);
tr2.setPrice(11f);
TransactionRecord tr3 = new TransactionRecord();
tr3.setbOrs("S");
tr3.setUnit(20);
tr3.setPrice(15f);
TransactionRecord tr4 = new TransactionRecord();
tr4.setbOrs("B");
tr4.setUnit(30);
tr4.setPrice(9f);
TransactionRecord tr5 = new TransactionRecord();
tr5.setbOrs("S");
tr5.setUnit(70);
tr5.setPrice(18f);
ll.add(tr1);
ll.add(tr2);
ll.add(tr3);
ll.add(tr4);
ll.add(tr5);
float cost = 0f, sum = 0f;
LinkedList lb = new LinkedList();
LinkedList ls = new LinkedList();
for (int i = 0; i < ll.size(); i++) {
TransactionRecord tr = (TransactionRecord) ll.get(i);
if (tr.getbOrs() == "B") {
cost += tr.getPrice() * tr.getUnit();
lb.add(tr);
} else if (tr.getbOrs() == "S") {
ls.add(tr);
}
}
for (int i = 0; i < ls.size(); i++) {
TransactionRecord tr = (TransactionRecord) ls.get(i);
for (int j = 0; j < lb.size(); j++) {
TransactionRecord trForS = ((TransactionRecord) ll.get(j));
if (tr.getUnit() > trForS.getUnit()) {
sum += trForS.getUnit()
* (tr.getPrice() - trForS.getPrice());
tr.setUnit(tr.getUnit() - trForS.getUnit());
trForS.setUnit(0);
} else if (tr.getUnit() < trForS.getUnit()) {
sum += tr.getUnit() * (tr.getPrice() - trForS.getPrice());
trForS.setUnit(trForS.getUnit() - tr.getUnit());
tr.setUnit(0);
} else {
sum += tr.getUnit() * (tr.getPrice() - trForS.getPrice());
}
}
}
DecimalFormat twoDForm = new DecimalFormat("#.##");
System.out.print("報酬率 = "
+ String.valueOf(twoDForm.format((sum / cost) * 100)) + "%");
}
}
class TransactionRecord {
String bOrs;
int unit;
float price;
public String getbOrs() {
return bOrs;
}
public void setbOrs(String bOrs) {
this.bOrs = bOrs;
}
public int getUnit() {
return unit;
}
public void setUnit(int unit) {
this.unit = unit;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
Reply all
Reply to author
Forward
0 new messages