2011-10-18 89 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