public void updateTrade(String stockCode, double newPrice, int newQuantity) {
StockTrade current = head;
boolean found = false;
while (current != null) {
if (current.stockCode.equals(stockCode)) {
current.price = newPrice;
current.quantity = newQuantity;
found = true;
break;
}
current = current.next;
}
if (!found) {
throw new IllegalArgumentException("Trade not found for stock: " + stockCode);
}
}
public void removeTrade(String stockCode) {
if (head == null) throw new IllegalStateException("Trade list is empty");
if (head.stockCode.equals(stockCode)) {
head = head.next;
return;
}
StockTrade current = head;
while (current.next != null && !current.next.stockCode.equals(stockCode)) {
current = current.next;
}
if (current.next == null) {
throw new IllegalArgumentException("Trade not found for stock: " + stockCode);
}
current.next = current.next.next;
}
Test 1: Add Trades
Stock: Apple Inc. (AAPL)
Price: 150.0 | Quantity: 100
Stock: Google LLC (GOOGL)
Price: 2800.0 | Quantity: 50
Test 2: Update Trade (AAPL)
Stock: Apple Inc. (AAPL)
Price: 155.0 | Quantity: 120
Stock: Google LLC (GOOGL)
Price: 2800.0 | Quantity: 50
Test 3: Remove Trade (GOOGL)
Stock: Apple Inc. (AAPL)
Price: 155.0 | Quantity: 120
Test 4: Try to Remove Non-existent Trade (AMZN)
Exception: Trade not found for stock: AMZN
Test 5: Try to Update Non-existent Trade (MSFT)
Exception: Trade not found for stock: MSFT
● 在“Test 4: Try to Remove Non-existent Trade (AMZN)”中嘗試刪除一條不存在的交易記錄(Amazon股票的交易記錄)。此時系統拋出了IllegalArgumentException,并顯示了對應的錯誤消息“Trade not found for stock: AMZN”。
● 在“Test 5: Try to Update Non-existent Trade (MSFT)”中嘗試更新一條不存在的交易記錄(Microsoft股票的交易記錄)。系統也拋出了IllegalArgumentException,并顯示了對應的錯誤消息“Trade not found for stock: MSFT”。
首先,在Cursor中創建一個新項目,并設置支持C++開發的環境,確保已安裝VS Code或JetBrains IDE,并已集成Copilot插件。在Cursor中輸入功能需求提示,如Create a Windows desktop application for screen capturing using C++,即可生成代碼框架。
隨后,在Copilot中輸入以下提示:Create a C++ program that captures the entire screen and saves it as a BMP file using Windows API,即可生成項目的初始代碼框架。Copilot生成了以下基礎代碼:
#include <windows.h>
#include <iostream>
#include <string>
// Function to capture the screen and save as BMP file
void CaptureScreen(const std::string& fileName) {
// Get the device context of the screen
HDC hScreenDC = GetDC(NULL);
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
// Get the width and height of the screen
int width = GetDeviceCaps(hScreenDC, HORZRES);
int height = GetDeviceCaps(hScreenDC, VERTRES);
// Create a compatible bitmap from the screen DC
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
// Select the bitmap into the memory DC
HGDIOBJ oldBitmap = SelectObject(hMemoryDC, hBitmap);
// Copy the screen contents into the memory DC
BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
// Save the bitmap to file
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;
memset(&fileHeader, 0, sizeof(fileHeader));
memset(&infoHeader, 0, sizeof(infoHeader));
fileHeader.bfType = 0x4D42;
fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
fileHeader.bfSize = fileHeader.bfOffBits + (width * height * 4);
infoHeader.biSize = sizeof(BITMAPINFOHEADER);
infoHeader.biWidth = width;
infoHeader.biHeight = -height; // Negative to ensure top-down bitmap
infoHeader.biPlanes = 1;
infoHeader.biBitCount = 32;
infoHeader.biCompression = BI_RGB;
FILE* file = fopen(fileName.c_str(), "wb");
fwrite(&fileHeader, sizeof(fileHeader), 1, file);
fwrite(&infoHeader, sizeof(infoHeader), 1, file);
BITMAPINFO bmpInfo;
bmpInfo.bmiHeader = infoHeader;
BYTE* bitmapData = new BYTE[width * height * 4];
GetDIBits(hMemoryDC, hBitmap, 0, height, bitmapData, &bmpInfo, DIB_RGB_COLORS);
fwrite(bitmapData, width * height * 4, 1, file);
delete[] bitmapData;
fclose(file);
// Clean up
SelectObject(hMemoryDC, oldBitmap);
DeleteObject(hBitmap);
DeleteDC(hMemoryDC);
ReleaseDC(NULL, hScreenDC);
}
int main() {
std::string fileName = "screenshot.bmp";
CaptureScreen(fileName);
std::cout << "Screenshot saved as " << fileName << std::endl;
return 0;
}
#include <cassert>
#include <fstream>
// Function to validate if a file exists
bool fileExists(const std::string& fileName) {
std::ifstream file(fileName);
return file.good();
}
// Unit test for CaptureScreen function
void testCaptureScreen() {
std::string testFile = "test_screenshot.bmp";
// Capture screen and save to test file
CaptureScreen(testFile);
// Assert file exists
assert(fileExists(testFile) && "Screenshot file was not created");
// Cleanup
std::remove(testFile.c_str());
std::cout << "Test 1 Passed: Screenshot file created successfully.\n";
}
接下來,我們通過Cursor繼續生成可用于輔助分析的代碼。
輸入提示“Write a test case to validate BMP file integrity”后,Cursor生成了用于檢查文件頭的代碼:
void testFileIntegrity() {
std::string testFile = "test_screenshot.bmp";
// Capture screen and save to test file
CaptureScreen(testFile);
// Open the file and validate the BMP header
std::ifstream file(testFile, std::ios::binary);
assert(file.good() && "Failed to open screenshot file");
char header[2];
file.read(header, 2);
assert(header[0] == 'B' && header[1] == 'M' && "File is not a valid BMP");
file.close();
// Cleanup
std::remove(testFile.c_str());
std::cout << "Test 2 Passed: Screenshot file integrity verified.\n";
}
此外,我們還需要測試在異常情況下,程序是否能夠準確地處理錯誤,例如屏幕設備不可用的情況。我們可以輸入提示“Add exception handling to the CaptureScreen function”,這時Cursor自動生成了捕獲和處理異常的框架代碼,如下所示:
void testErrorHandling() {
try {
// Simulate an error by passing an invalid filename
CaptureScreen("");
} catch (const std::exception& e) {
std::cout << "Test 3 Passed: Exception caught - " << e.what() << "\n";
return;
}
assert(false && "Exception was not thrown for invalid input");
}
在Cursor中輸入提示“Add a logging system to track errors during screen capture”,Cursor可以生成以下代碼:
int main() {
std::cout << "Running tests...\n";
testCaptureScreen();
testFileIntegrity();
testErrorHandling();
std::cout << "All tests passed.\n";
return 0;
}
測試結果如下:
Running tests...
Test 1 Passed: Screenshot file created successfully.
Test 2 Passed: Screenshot file integrity verified.
Test 3 Passed: Exception caught - File name cannot be empty
All tests passed.