Last active
December 24, 2024 08:05
-
-
Save junsuk5/343c269c784f1f8d8c2a0739ca4bac54 to your computer and use it in GitHub Desktop.
연습용 주식 데이터
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 주식 가격 모니터링 시스템을 구현하시오. | |
| * | |
| * 요구사항: | |
| * 1. 여러 주식의 가격을 동시에 모니터링 (병렬처리) | |
| * 2. 가격이 특정 임계값을 넘으면 알림 발생 | |
| * 3. 1초마다 가격 업데이트 | |
| * 4. 거래시간(9:00-15:30)에만 동작 | |
| * 5. 에러 발생시 재시도 (최대 3회) | |
| */ | |
| interface StockMonitor { | |
| fun monitorStocks(symbols: List<String>): Flow<StockUpdate> | |
| fun setThreshold(symbol: String, threshold: Double) | |
| suspend fun stopMonitoring(symbol: String) | |
| } | |
| data class StockUpdate( | |
| val symbol: String, | |
| val price: Double, | |
| val timestamp: LocalDateTime | |
| ) | |
| // 테스트용 주식 데이터 생성기 | |
| object StockDataGenerator { | |
| private val stocks = mapOf( | |
| "AAPL" to 150.0, | |
| "GOOGL" to 2800.0, | |
| "MSFT" to 300.0, | |
| "AMZN" to 3300.0, | |
| "TSLA" to 900.0 | |
| ) | |
| fun generateStockFlow(symbol: String): Flow<StockUpdate> = flow { | |
| val basePrice = stocks[symbol] ?: 100.0 | |
| while (true) { | |
| val change = (-5..5).random() / 100.0 // -5% to +5% 변동 | |
| val newPrice = basePrice * (1 + change) | |
| emit( | |
| StockUpdate( | |
| symbol = symbol, | |
| price = "%.2f".format(newPrice).toDouble(), | |
| timestamp = LocalDateTime.now() | |
| ) | |
| ) | |
| delay(1000) // 1초마다 업데이트 | |
| } | |
| } | |
| // 테스트용 데이터셋 | |
| val sampleData = listOf( | |
| StockUpdate("AAPL", 150.25, LocalDateTime.now()), | |
| StockUpdate("AAPL", 151.30, LocalDateTime.now().plusSeconds(1)), | |
| StockUpdate("GOOGL", 2805.50, LocalDateTime.now()), | |
| StockUpdate("GOOGL", 2795.25, LocalDateTime.now().plusSeconds(1)) | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment