본문 바로가기

Java/java 기초

HashMap , TreeMap 상속 을 활용하여 야구선수 관리 시스템 만들기

부모 클래스 (휴먼)

package human;

public class Human {
	private int number;
	private String name;
	private int age;
	private double height;

	public Human() {
		// TODO Auto-generated constructor stub
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public String getName() {
		return name;
	}

	public void setName(String string) {
		this.name = string;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}

	@Override
	public String toString() {
		return "Human [number=" + number + ", name=" + name + ", age=" + age + ", height=" + height + "]";
	}

	public Human(int number, String name, int age, double height) {
		super();
		this.number = number;
		this.name = name;
		this.age = age;
		this.height = height;
	}

}

투수(자식클래스) 

 

package human;

public class Pitcher extends Human{
	int win;
	int lose;
	double def;
	
	public Pitcher() {
		// TODO Auto-generated constructor stub
	}
	public Pitcher(int number, String name, int age, double height, int win, int lose, double def) {
		super(number, name, age, height);
		this.win = win;
		this.lose = lose;
		this.def = def;
	}
	
	
	public int getWin() {
		return win;
	}
	public void setWin(int win) {
		this.win = win;
	}
	public int getLose() {
		return lose;
	}
	public void setLose(int lose) {
		this.lose = lose;
	}
	public double getDef() {
		return def;
	}
	public void setDef(double def) {
		this.def = def;
	}
	@Override
	public String toString() {
		return "투수 [번호: " + getNumber()
		+ ", 이름: " + getName() + ", 나이: " + getAge() + ", 키: " + getHeight()+
		", 이긴 횟수: "+ win + ", 진 횟수: " + lose + ", 방어율: " + def + "]";
	
}

 

 

타자(자식클래스)

package human;

public class Batter extends Human{
	int bat;
	int hit;
	double batAvg;
	
	public Batter() {
		// TODO Auto-generated constructor stub
	}
	
	public Batter(int number, String name, int age, double height, int bat, int hit, double batAvg) {
		super(number, name, age, height);
		this.bat = bat;
		this.hit = hit;
		this.batAvg = batAvg;
	}
	
	public int getBat() {
		return bat;
	}
	
	public void setBat(int bat) {
		this.bat = bat;
	}
	
	public int getHit() {
		return hit;
	}
	
	public void setHit(int hit) {
		this.hit = hit;
	}
	
	public double getBatAvg() {
		return batAvg;
	}
	
	public void setBatAvg(double batAvg) {
		this.batAvg = batAvg;
	}

	@Override
	public String toString() {
		return "타자 [번호: " + getNumber()
		+ ", 이름: " + getName() + ", 나이: " + getAge() + ", 키: " + getHeight()+
		", 친 횟수: "+ bat + ", 유효타: " + hit + ", 타율: " + batAvg + "]";
	}
	
}

 

 

DAO( 컨트롤러 )

HashMap 과 TreeMap을 활용하여 방어율/ 타율에 따라 정렬하였다

package dao;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeMap;

import human.Batter;
import human.Human;
import human.Pitcher;

public class HumanDao {
	HashMap<Integer, Human> member = new HashMap<Integer, Human>();
	Scanner scanner = new Scanner(System.in);

	public HumanDao() {
		Human human1 = new Pitcher(3, "온유", 27, 178.8, 50, 40, 15);
		Human human3 = new Pitcher(1, "진형", 29, 188.8, 60, 30, 25);
		Human human2 = new Batter(1001, "규성", 27, 176.4, 40, 20, 10);
		Human human4 = new Pitcher(2, "스윙스", 35, 174.8, 30, 80, 35);
		Human human5 = new Batter(1002, "피카츄", 15, 120.4, 20, 20, 10);
		Human human6 = new Batter(1003, "돈까스", 60, 140.4, 90, 10, 40);
		member.put(human1.getNumber(), human1);
		member.put(human2.getNumber(), human2);
		member.put(human3.getNumber(), human3);
		member.put(human4.getNumber(), human4);
		member.put(human5.getNumber(), human5);
		member.put(human6.getNumber(), human6);
		printAll();
	}
	
	public void insert() {
		System.out.println("입력할 선수의 정보를 입력하세요");
		System.out.println("1. 투수 2. 타자");
		int choice = scanner.nextInt();
		int pitcherNumber = 3;
		int batterNumber = 1003;
		
		Human human = null;
		if(choice == 1) {
			pitcherNumber = pitcherNumber + 1;
			System.out.println(pitcherNumber);
			human = new Pitcher();
			human.setNumber(pitcherNumber);
			System.out.print("이름: ");
			human.setName(scanner.next());
			System.out.print("나이: ");
			human.setAge(scanner.nextInt());
			System.out.print("키: ");
			human.setHeight(scanner.nextDouble());
			System.out.print("이긴 횟수: ");
			((Pitcher)human).setWin(scanner.nextInt());
			System.out.print("진 횟수: ");
			((Pitcher)human).setLose(scanner.nextInt());
			System.out.print("방어율: ");
			((Pitcher)human).setDef(scanner.nextDouble());
			member.put(pitcherNumber , human);
			
		} else if (choice == 2) {
			batterNumber = batterNumber + 1;
			human = new Batter();
			human.setNumber(batterNumber);
			System.out.print("이름: ");
			human.setName(scanner.next());
			System.out.print("나이: ");
			human.setAge(scanner.nextInt());
			System.out.print("키: ");
			human.setHeight(scanner.nextDouble());
			System.out.print("친 횟수: ");
			((Batter)human).setBat(scanner.nextInt());
			System.out.print("유효타: ");
			((Batter)human).setHit(scanner.nextInt());
			System.out.print("타율: ");
			((Batter)human).setBatAvg(scanner.nextDouble());
			member.put(batterNumber , human);
			
		} else {
			System.out.println("다시 실행하세요 ");
			return;
		}
		
		printAll();
	}
	public void delete() {
		System.out.println("삭제 할 선수의 이름을 입력해주세요.");
		String name = scanner.next();
		int keyNumber = search(name);
		if(keyNumber == -1) {
			System.out.println("해당 선수를 찾을 수 없습니다");
		} else {
			member.remove(keyNumber);
			System.out.println("삭제가 완료되었습니다.");
		}
	}
	
	public int search(String name) {
		Iterator<Integer> iterator = member.keySet().iterator();
		int keyNumber = -1;
		while(iterator.hasNext()) { 
			int key = iterator.next();
			if(member.get(key).getName().equals(name)) {
				keyNumber = key;
				break;
			} 
		}
		
		return keyNumber;
	}
	
	public void select() {
		System.out.println("검색 할 선수의 이름을 입력해 주세요.");
		String name = scanner.next();
		int keyNumber= search(name);
		if(keyNumber == -1) {
			System.out.println("해당 선수를 찾을 수 없습니다");
		} else {
			System.out.println("검색한 선수의 정보 입니다.");
			System.out.println(member.get(keyNumber).toString());
			
		}
	}
	
	public void update() {
		System.out.println("정보를 변경할 선수의 이름을 입력해 주세요");
		String name = scanner.next();
		int keyNumber = search(name);
		if(keyNumber == -1) {
			System.out.println("해당 선수를 찾을 수 없습니다");
		} else {
			//진 횟수가 없을때 (타자 일때)
			if(member.get(keyNumber) instanceof Pitcher) {
				System.out.println("투수의 정보를 변경합니다");
				
				System.out.println("이긴 횟수");
				((Pitcher)member.get(keyNumber)).setWin(scanner.nextInt());
				System.out.println("진 횟수");
				((Pitcher)member.get(keyNumber)).setLose(scanner.nextInt());
				System.out.println("방어율");
				((Pitcher)member.get(keyNumber)).setDef(scanner.nextDouble());
			} else if(member.get(keyNumber) instanceof Batter){
				System.out.println("타자의 정보를 변경합니다");
				
				System.out.println("친 횟수");
				((Batter)member.get(keyNumber)).setBat(scanner.nextInt());
				System.out.println("유효타");
				((Batter)member.get(keyNumber)).setHit(scanner.nextInt());
				System.out.println("타율");
				((Batter)member.get(keyNumber)).setBatAvg(scanner.nextDouble());
				
			}
		}
	}
	
	public void printAll() {
		Iterator<Integer> iterator = member.keySet().iterator();
		while(iterator.hasNext()) {
			int key = iterator.next();
			Human value = member.get(key);
			System.out.println("key : " + key + "value : " + value.toString());
		}
	}
	
	public void sort() {
		System.out.println("1. 투수  2. 타자");
		int choice = scanner.nextInt();
		
		TreeMap<Double, Human> tree = new TreeMap<Double, Human>();	
		
		Iterator<Integer> iterator = member.keySet().iterator();
	
		while(iterator.hasNext()) {
			int key = iterator.next();
			if(member.get(key) instanceof Pitcher) {
				double def = ((Pitcher)member.get(key)).getDef();
				if(tree.containsKey(def)) {
					def = def + 0.001;
				}
				
				tree.put(def, member.get(key));
			} else if(member.get(key) instanceof Batter) {
				double batAvg = ((Batter)member.get(key)).getBatAvg();
				if(tree.containsKey(batAvg)) {
					batAvg = batAvg + 0.001;
				}
				tree.put(batAvg, member.get(key));
				
			}
		}
		
		Iterator<Double> iter = tree.descendingKeySet().iterator();
		
		while(iter.hasNext()) {
			double key = iter.next();
	
			if(choice == 1) {
				if(tree.get(key).getNumber() < 1000) {
					System.out.println(tree.get(key).toString());
				}
			}
			else if(choice == 2){
				if(tree.get(key).getNumber() > 1000) {
					System.out.println(tree.get(key).toString());
				}
			}
		
	}

	}
}

 

 

메인 클래스

package main;

import java.util.Scanner;

import dao.HumanDao;
import human.Human;
import human.Pitcher;

public class mainClass {
	public static void main(String[] args) {
		HumanDao humanDao = new HumanDao();
		Scanner scanner = new Scanner(System.in);
//		humanDao.insert();
		while(true) {
			System.out.println("1. 선수 추가하기");
			System.out.println("2. 삭제 하기");
			System.out.println("3. 검색 하기");
			System.out.println("4. 수정 하기");
			System.out.println("5. 선수들 전체 조회 하기");
			System.out.println("6. 선수들 방어율 / 타율 높은 순위 조회 하기");
			
			int choice = scanner.nextInt();
			if(choice == 1) {
				humanDao.insert();
			} else if (choice == 2) {
				humanDao.delete();
			} else if (choice == 3) {
				humanDao.select();
			} else if (choice == 4) {
				humanDao.update();
			} else if (choice == 5) {
				humanDao.printAll();
			} else if (choice == 6) {
				humanDao.sort();
			}
			
			
		}
		
	}
}

 

콘솔 결과창