2011-10-18 42 views
1

这是我正在编写的编程任务。它需要一个单一的字符串输入,代表一系列交易并最终打印总收益/损失。运行后编写了Java代码但没有预期输出

我有我的代码编写和认为它应该做我想要的东西...但没有。用指定的输入运行程序后,我没有得到任何输出。

我使用的输入是:

在每个$ 20购买100共享(S);在每个$ 24购买20共享(S);在每个36 $购买200 共享(S);卖150股,每股30美元;购买50股,每股价格 ,每股25美元;卖出200股,每股35美元;

import java.util.*; 
import java.text.*; 

public class Stocks { 

private int shares; 
private int price; 
private int temp; 
private static int total; 
private int finalPrice; 
private int finalShares; 
private Queue<Stocks> StockList = new LinkedList<Stocks>(); 

private static NumberFormat nf = NumberFormat.getCurrencyInstance(); 

public Stocks() 
{ 
    shares  = 0; 
    price  = 0; 

} 

public Stocks(int shares, int price) 
{ 
    this.shares  = shares; 
    this.price  = price; 
} 

public int getShares() 
{ 
    return this.shares; 
} 

public int getPrice() 
{ 
    return this.price; 
} 

public void setShares(int shares) 
{ 
    this.shares = shares; 
} 

public void setPrice(int price) 
{ 
    this.price = price; 
} 

public void sell() { 
    int sharesToSell = this.getShares(); 
    int priceToSell = this.getPrice(); 

    while (!StockList.isEmpty()) { 

     int numShares = StockList.peek().getShares(); 
     int sharePrice = StockList.peek().getPrice(); 

     if (numShares < sharesToSell || numShares == sharesToSell) { 
      temp = sharesToSell - numShares; // remaining shares to sell 
      finalShares = sharesToSell - temp; // # shares selling at price 
      finalPrice = priceToSell - sharePrice; // shares sold at adjusted price 
      total += (finalPrice * finalShares); // Calculates total price 
      StockList.remove(); 
      sharesToSell = temp; // Remaining shares needed to be sold @ price 
     } 

     if (numShares > sharesToSell) { 
      temp = numShares - sharesToSell; // Remaining shares that were bought 
      finalPrice = priceToSell - sharePrice; // Shares sold at adjusted price 
      total += (finalPrice * sharesToSell); // adds to running total 
      StockList.peek().setShares(temp); 
     } 
    } 
} 

public void buy() { 
    int numShares = this.getShares(); 
    int priceToBuy = this.getPrice(); 

    Stocks newStock = new Stocks(numShares,priceToBuy); 
    StockList.add(newStock); // adds stock to list 

    int temptotal = (numShares * priceToBuy); // decreases running total 
    total += (-1 * temptotal); 
} 

public static int getTotal() { // gets total profit (or loss) 
    return total; 
} 

// *****MAIN METHOD***** 
public static void main(String[] args){ 


    Scanner scan = new Scanner(System.in); 

    System.out.println("Enter transaction sequence:"); 

    String input = scan.nextLine().trim(); 
    String[] inputArray = new String[50]; 
    String[] inputArray2 = new String[50]; 
    int numShares, sharePrice; 

    inputArray = input.split(";"); 

    for (String i : inputArray) { 
     if (i.toUpperCase().contains("BUY")) { 
      inputArray2 = i.split(" "); 
      inputArray2[4] = inputArray2[4].substring(1); 

      try { 
       numShares = Integer.parseInt(inputArray2[1]); 
       sharePrice = Integer.parseInt(inputArray2[4]); 

       Stocks newStock = new Stocks(numShares,sharePrice); 
       newStock.buy(); 

      } catch (NumberFormatException e) { 
       System.out.println("Error"); 
       return; 
      } 

     } 

     else if (i.toUpperCase().contains("SELL")) { 
      inputArray2 = input.split(" "); 
      inputArray2[4] = inputArray2[4].substring(1); 

      try { 
       numShares = Integer.parseInt(inputArray2[1]); 
       sharePrice = Integer.parseInt(inputArray2[4]); 

       Stocks newStock = new Stocks(numShares,sharePrice); 
       newStock.sell(); 

      } catch (NumberFormatException e) { 
       System.out.println("Error"); 
       return; 
      } 
     } else { 
      System.out.println("Error - input does not contain buy/sell"); 
     } 
    } System.out.println(nf.format(getTotal())); 
} 

}

+0

请添加作业标签。 –

+0

您能否指定在哪一行输入? – alf

+0

您是否有直觉认为事情出错? –

回答

1

时像解析买入交易方法立即返回。你可能打算把返回声明里面catch块。

+0

正确。感谢您指出了这一点! – TT52

2

您可以通过查看java.util.regex.Matcher和java.util.regex.Pattern来清理您的解析。他们会让你匹配正则表达式的输入。另外,你可以将正则表达式放在正则表达式中以提取某些部分。所以在你的例子中,你只关心三件事:操作(买或卖),数量和价格。

这里有一个小例子

String sentence = "john programs 10 times a day"; 

// here's our regex - each set of parens is a "group" 
Pattern pattern = Pattern.compile("([A-Za-z]+) programs ([0-9]+) times a day"); 
Matcher matcher = pattern.matcher(sentence); 

String person = matcher.group(1); // here we get the first group 
String number = Integers.parseInt(matcher.group(2)); // here we get the second group 

System.out.println("Person: " + person + " Number: " + number); 
+0

我将不得不试试这个。谢谢! – TT52