Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- degit
- 함수
- Spring
- 왕초보
- snowpack
- parcel
- java
- 게시판
- java#왕초보
- React
- Spinner
- 쿠키
- 스타일보험
- 시큐어코딩
- git
- 코틀린
- 스프링
- 안드로이드
- 답글
- kotlin
- 미니게임
- FIle
- 버전일치
- 숫자
- sub query
- webpack
- 오버라이드
- 상속
- Android
- SQL
Archives
- Today
- Total
YSHUSH
야구선수 관리 프로그램 본문
야구선수 관리 프로그램을 만들어본다.
기본적인 CRUD와 txt파일로 저장, 타율순으로 정렬과 같은 기능도 넣어보자
Dto
Human
open class Human(var number:Int = 0,
var name:String = "",
var age:Int = 0,
var height:Double = 0.0) {
/*
var number:Int = 0
var name:String = ""
var age:Int = 0
var height:Double = 0.0
constructor(){}
constructor(number: Int, name: String, age: Int, height: Double) {
this.number = number
this.name = name
this.age = age
this.height = height
}*/
override fun toString(): String {
return "$number-$name-$age-$height-"
}
}
Pitcher
class Pitcher : Human {
var win:Int = 0
var lose:Int = 0
var defense:Double = 0.0
constructor(): super(0, "", 0, 0.0){}
constructor(number: Int, name: String, age: Int, height: Double, win:Int, lose:Int, defense:Double) : super(number, name, age, height){
this.win = win
this.lose = lose
this.defense = defense
}
override fun toString(): String {
return super.toString() + "$win-$lose-$defense"
}
}
Batter
class Batter(number: Int, name: String, age: Int, height: Double, var batCount:Int, var hit:Int, var batAvg:Double)
: Human(number, name, age, height) {
override fun toString(): String {
return super.toString() + "$batCount-$hit-$batAvg"
}
}
File
filedata
class FileData {
// java
private var file:File? = null
// kotlin
private var filePath:String? = null
constructor(filename:String){
val dir = "D:\\myfile"
file = File("$dir\\$filename.txt")
filePath = "$dir\\$filename.txt"
}
fun createFile(){
if(file!!.createNewFile()){
println(file!!.name + " 파일을 생성하였습니다")
}else{
println("파일 생성에 실패하였습니다")
}
}
fun fileSave(arrStr: Array<String?>){
/*
val fileWriter = FileWriter(file)
val bw = BufferedWriter(fileWriter)
val pw = PrintWriter(bw)
for(i in arrStr.indices){
pw.println(arrStr[i])
}
pw.close()
*/
File(filePath).printWriter().use { out->
for (str in arrStr)
out.println(str)
}
}
fun fileLoad():List<Human>?{
val list:MutableList<Human> = ArrayList()
/*
val fileReader = FileReader(file)
val br = BufferedReader(fileReader)
var str = br.readLine()
while (str != null && str != ""){ // 1001-홍길동-24-181.1-10-2-0.2
val split = str.split("-".toRegex()).toTypedArray()
val pos = split[0].toInt()
var human = if(pos < 2000){ // 투수
Pitcher(
split[0].toInt(),
split[1],
split[2].toInt(),
split[3].toDouble(),
split[4].toInt(),
split[5].toInt(),
split[6].toDouble())
}
else{ // 타자
Batter(
split[0].toInt(),
split[1],
split[2].toInt(),
split[3].toDouble(),
split[4].toInt(),
split[5].toInt(),
split[6].toDouble())
}
list.add(human)
str = br.readLine()
}
*/
File(filePath).useLines {
lines -> lines.forEach {
// println(it)
val split = it.split("-".toRegex()).toTypedArray()
val pos = split[0].toInt()
var human = if(pos < 2000){ // 투수
Pitcher(
split[0].toInt(),
split[1],
split[2].toInt(),
split[3].toDouble(),
split[4].toInt(),
split[5].toInt(),
split[6].toDouble())
}
else{ // 타자
Batter(
split[0].toInt(),
split[1],
split[2].toInt(),
split[3].toDouble(),
split[4].toInt(),
split[5].toInt(),
split[6].toDouble())
}
list.add(human)
}
}
return list
}
}
Dao
MemberDao
class MemberDao {
// list
private var list : MutableList<Human>? = null
private var fd:FileData? = null
constructor(){
/*
list = ArrayList<Human>()
list?.add(Pitcher(1001, "홍길동", 24, 181.1, 10, 2, 0.2))
list?.add(Pitcher(1002, "성춘향", 16, 157.2, 15, 1, 0.1))
*/
fd = FileData("baseball")
fd!!.createFile()
list = fd!!.fileLoad() as MutableList<Human>
}
fun insert(){
println(">> 선수등록")
print("투수(1)/타자(2) 중 등록하고 싶은 포지션 >> ")
val choice : Int? = readLine()?.toInt()
// 공통 데이터
print("번호:")
var number: Int? = readLine()?.toInt()
print("이름:")
var name: String? = readLine()
print("나이:")
var age: Int? = readLine()?.toInt()
print("신장:")
var height: Double? = readLine()?.toDouble()
var human = if(choice == 1){ // 투수
print("승:")
var win: Int? = readLine()?.toInt()
print("패:")
var lose: Int? = readLine()?.toInt()
print("방어율:")
var defense: Double? = readLine()?.toDouble()
Pitcher(number!!, name!!, age!!, height!!, win!!, lose!!, defense!!)
} else { // 타자
print("타수:")
var batCount: Int? = readLine()?.toInt()
print("안타수:")
var hit: Int? = readLine()?.toInt()
print("타율:")
var hitAvg: Double? = readLine()?.toDouble()
Batter(number!!, name!!, age!!, height!!, batCount!!, hit!!, hitAvg!!)
}
list?.add(human)
}
fun delete(){
print("방출할 선수의 이름을 입력 = ")
val name: String? = readLine()
var findIndex = search(name!!)
if(findIndex == -1){
println("검색된 선수가 없습니다")
return
}
list?.removeAt(findIndex)
println("선택한 선수를 삭제 하였습니다")
}
fun select(){
print("검색할 선수의 이름을 입력 = ")
val name: String? = readLine()
var findIndex = search(name!!)
if(findIndex == -1){
println("검색된 선수가 없습니다")
return
}
// instanceof => is
if(list!![findIndex] is Pitcher){
println("투수입니다")
}else{
println("타자입니다")
}
println(list!![findIndex].toString())
}
fun update(){
print("수정할 선수의 이름을 입력 = ")
val name: String? = readLine()
var findIndex = search(name!!)
if(findIndex == -1){
println("검색된 선수가 없습니다")
return
}
println("수정할 데이터를 입력 >> ")
if(list!![findIndex] is Pitcher){
print("승:")
val win : Int? = readLine()?.toInt()
print("패:")
val lose : Int? = readLine()?.toInt()
print("방어율:")
val defense : Double? = readLine()?.toDouble()
val pitcher = list!![findIndex] as Pitcher // val pitcher = (Pitcher)list!![findIndex]
pitcher.win = win!!
pitcher.lose = lose!!
pitcher.defense = defense!!
}
else{
}
}
fun allPrint(){
for (mem in list!!){
println(mem.toString())
}
}
fun search(name:String):Int{
var findIndex = -1
for(i in list!!.indices){
val h = list!![i]
if(h.name == name){
findIndex = i
break
}
}
return findIndex
}
fun fileSave(){
// list -> array
// 배열을 생성, 크기만을 설정
val strArr = arrayOfNulls<String>(list!!.size)
for (i in list!!.indices){
strArr[i] = list!![i].toString()
}
fd?.fileSave(strArr)
}
fun hitAvgSort(){
// Batter 만으로 리스트를 생성
/*
val sortList:List<Batter>? = list?.filterIsInstance<Batter>()
if (sortList != null) {
for (h in sortList){
println(h.toString())
}
}
// 내림차순 정렬
val sorted = sortList?.sortedByDescending { it->it.batAvg }
for (h in sorted!!){
println(h.toString())
}
*/
// kotlin
val sortList:List<Batter>? = list?.filterIsInstance<Batter>()?.sortedByDescending { it->it.batAvg }
if (sortList != null) {
for (h in sortList){
println(h.toString())
}
}
}
}
Main
fun main(args: Array<String>) {
val memDao = MemberDao()
while (true){
println(" << menu >> ")
println("1.선수등록")
println("2.선수삭제")
println("3.선수검색")
println("4.선수수정")
println("5.선수 모두출력")
println("6.선수명단 저장")
println("7.타율순으로 정렬")
println("8.프로그램 종료")
print("메뉴번호 >>> ")
val choice = readLine()?.toInt()
when(choice){
1 -> memDao.insert()
2 -> memDao.delete()
3 -> memDao.select()
4 -> memDao.update()
5 -> memDao.allPrint()
6 -> memDao.fileSave()
7 -> memDao.hitAvgSort()
8 -> exitProcess(0)
else -> {}
}
}
}