我的项目涉及以下内容:
出售某公司的普通股后,其资本收益(或 有时,亏损)是股票的卖出价格与价格之间的差额 最初支付购买。对于单个共享,此规则很容易理解,但是 如果我们卖出长时间购买的多股股票,那么我们 必须确定实际出售的股票。的标准会计原则 在这种情况下,确定出售哪些股票是使用FIFO 协议-出售的股票是持有时间最长的股票(实际上, 这是几个个人理财软件包中内置的默认方法)。 例如,假设我们在第1天以每股20美元的价格购买100股,以20美元的价格购买20股 在第2天,第3天以36美元的价格出售200股,然后在第4天以30美元的价格出售150股 每。然后应用FIFO协议意味着售出150股,即100股 在第1天购买了,在第2天购买了20,在当天购买了30 3.因此,在这种情况下,资本收益为100·10 + 20·6 + 30·(−6), 或940美元。编写一个程序,以该程序的一系列交易作为输入 形式为“以每股$ y的价格购买x股”或“以每股$ y的价格出售x股”, 假设交易发生在连续的几天,并且值x和y 是整数。给定此输入顺序,输出应为总资本收益 (或丢失)整个序列,使用FIFO协议识别份额。
这是我的代码的片段:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Queue;
import java.util.Scanner;
public class CapitalGainLossCalc {
private ArrayList<String> transactions;
private ArrayQueue buyOrders;
private ArrayQueue sellOrders;
public CapitalGainLossCalc() {
transactions = new ArrayList<String>();
}
public void acceptTransactions(){
String transaction;
InputStreamReader reader= new InputStreamReader(System.in);
BufferedReader bufferedReader=new BufferedReader(reader);
while(!(transaction=bufferedReader.readLine()).equals("done")) {
transactions.add(transaction);
}
}
public void processTransactions(){
this.buyOrders=new ArrayQueue(transactions.size());
this.sellOrders=new ArrayQueue(transactions.size());
Iterator<String> iterator=transactions.iterator();
while(iterator.hasNext()) {
String transaction=(String) iterator.next();
Scanner scanner=new Scanner(transaction);
char type = 0;
int price = 0, share = 0;
while(scanner.hasNext()) {
String token=scanner.next();
if(token.equals("buy"))
type='B';
else if (token.equals("sell"))
type='S';
else if(token.matches("\\d+")) {
share=Integer.parseInt(token);
}
else if(token.startsWith("$")){
String val=token.substring(1, token.length());
if (val.matches("\\d+")){
price=Integer.parseInt(val);
}
}
}
Order order = new Order(price,share, type);
if (type=='B')
buyOrders.enqueue(order);
else if(type=='S')
sellOrders.dequeue(order);
scanner.close();
}
}
int calculateCapitalGainLoss(){
int amount = 0;
int totalBuyValue = 0;
int totalBuyQuantity = 0;
int totalSellValue = 0;
int totalSellQuantity = 0;
int sharesToSell = 0;
int balanceSharesToSell = 0;
int buyOrderShare = 0;
int sellOrderShare = 0;
int buyOrderPrice=0;
int sellOrderPrice=0;
Order buyOrder=buyOrders.enqueue();
Order sellOrder=sellOrders.dequeue();
while(!buyOrders.isEmpty()&&!sellOrders.isEmpty()) {
if(buyOrder!=null) {
buyOrderPrice=buyOrder.getUnitPrice();
buyOrderShare=buyOrder.getShare();
totalBuyValue+=buyOrderPrice*buyOrderShare;
totalBuyQuantity+=buyOrderShare;
}
if (sellOrder!=null) {
sellOrderPrice=sellOrder.getUnitPrice();
sellOrderShare=sellOrder.getShare();
if(sellOrderShare > buy OrderShare) {
sharesToSell=buyOrderShare+balanceSharesToSell;
totalSellValue+=sellOrderPrice*sharesToSell;
balanceSharesToSell=(sellOrderShare-buyOrderShare);
totalSellQuantity+=sharesToSell;
}
else {
totalSellValue+=sellOrderPrice*sellOrderShare;
totalSellQuantity+=sellOrderShare;
}
}
}
amount = (sellOrderPrice-buyOrder)*(balanceSharesToSell);
return amount;
}
public static void main(String[] args) throws Exception {
CapitalGainLossCalc calc = new CapitalGainLossCalc();
System.out.println("Enter your transactions one per line.");
System.out.println("Example:\nbuy 100 share(s) at $20 each\n"
+ "sell 150 share(s) at $30 each.");
System.out.println("Be very careful, while entering your transactions.");
System.out.println("They should be in format as shown in example.");
System.out.println("If not so, they won't be processed.");
System.out.println("To stop, enter 'done'.");
System.out.println("Transactions:");
try {
calc.acceptTransactions();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Now processing your transactions...");
calc.processTransactions();
System.out.println("Processing of transactions complete!");
System.out.println("Now calculating capital gain(loss)...");
int amount=calc.calculateCapitalGainLoss();
System.out.println("Your gain(loss) is:"+amount);
}
}
The lines:
> Order sellOrder=sellOrders.dequeue();
and
> int amount=calc.calculateCapitalGainLoss();
have the errors:
cannot convert from void to order and buyOrder cannot be resolved to a variable.
How would I fix my code?
> *Blockquote*