본문 바로가기

JAVA/UI

야구멤버 Ui

package main;

import dao.BaseballDao;
import view.MenuView;

public class mainClass {

	public static void main(String[] args) {
				
		BaseballDao.getInstance();
		new MenuView();		
	}

}
package dao;

import java.util.List;

import dto.Batter;
import dto.Human;
import dto.Pitcher;
import file.FileClass;

public class BaseballDao {

	private static BaseballDao dao = null;
	public List<Human> list;
	int memberNumber; 
	
	FileClass fc = new FileClass(); 
	
	private BaseballDao() {		
		// file로부터 data 읽어 들이기
		list = fc.fileLoad();
				
		// list에서 제일 마지막 선수의 number 취득
		memberNumber = list.get(list.size() - 1).getNumber();
		
		if(memberNumber >= 2000) {
			memberNumber = memberNumber - 1000;			
		}
		memberNumber = memberNumber + 1;
	}
	
	public static BaseballDao getInstance() {
		if(dao == null) {
			dao = new BaseballDao();
		}
		return dao;
	}
	
	public boolean insert(Human h) {
		
		if(h instanceof Pitcher) {
			h.setNumber(memberNumber);
		}else if(h instanceof Batter){
			h.setNumber(memberNumber + 1000);
		}
		
		boolean b = list.add(h);
		
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i).toString());
		}
		memberNumber++;
		
		return b;
	}
	
	public Human delete(String name) {
		
		return null;
	}
	
	public Human select(String name) {
		
		return null;
	}
	
	public boolean update(String name) {
		
		return true;
	}
	
	public List<Human> allprint() {
		return this.list;
	}
	
	public void dataSave() {
		fc.fileSave(list);		
	}
	
	
}
package panel;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import dao.BaseballDao;
import dto.Human;
import dto.Pitcher;
import view.InsertView;
import view.MenuView;

public class PitcherPanel extends JPanel implements ActionListener{

	JLabel label[] = new JLabel[6];
	JTextField textField[] = new JTextField[6];
	
	JButton menuBtn, insertBtn;
	
	InsertView insertview;
	
	public PitcherPanel(InsertView insertview) {
		setLayout(null);
		
		this.insertview = insertview;
		
		String title[] = { "이름:", "나이:", "신장:", "승:", "패:", "방어율" };
		
		for (int i = 0; i < title.length; i++) {
			label[i] = new JLabel();
			label[i].setText( title[i] );
			label[i].setFont(new Font("돋움", Font.BOLD, 18));
			label[i].setBounds(30, 50 + 40 * i, 100, 30);
			
			textField[i] = new JTextField();
			textField[i].setBounds(180, 50 + 40 * i, 200, 30);
			
			add(label[i]);
			add(textField[i]);
		}
		
		menuBtn = new JButton("메뉴");
		menuBtn.setBounds(150, 320, 150, 50);
		menuBtn.addActionListener(this);
		add(menuBtn);
		
		insertBtn = new JButton("추가");
		insertBtn.setBounds(320, 320, 150, 50);
		insertBtn.addActionListener(this);
		add(insertBtn);
				
		setBounds(0, 0, 640, 450);
		setBackground(new Color(200, 128, 0));
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		
		if(obj == menuBtn) {
		//	JOptionPane.showMessageDialog(null, "menuBtn");
			insertview.dispose();
			new MenuView();
		}
		else if(obj == insertBtn) {
			
			BaseballDao ball = BaseballDao.getInstance();	
			Human human = null;
			
			for (int i = 0; i < textField.length; i++) {
				if(textField[i].getText().equals("")) {
					JOptionPane.showMessageDialog(null, "모두 작성해 주십시오");
					return;
				}
			}			
			
			String name = textField[0].getText();
			int age = Integer.parseInt(textField[1].getText());
			double height = Double.parseDouble(textField[2].getText());
			int win = Integer.parseInt(textField[3].getText());
			int lose = Integer.parseInt(textField[4].getText());
			double defence = Double.parseDouble(textField[5].getText());
			
			human = new Pitcher(0, name, age, height, win, lose, defence)	;		
			
			boolean b = ball.insert(human);
			if(b) {
				JOptionPane.showMessageDialog(null, "성공적으로 추가하였습니다");
				
				for (int i = 0; i < textField.length; i++) {
					textField[i].setText("");
				}
			}else {
				JOptionPane.showMessageDialog(null, "추가되지 않았습니다");
			}			
			
		}
	}
}






package panel;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import dao.BaseballDao;
import dto.Batter;
import dto.Human;
import view.InsertView;
import view.MenuView;

public class BatterPanel extends JPanel implements ActionListener{

	JLabel label[] = new JLabel[6];
	JTextField textField[] = new JTextField[6];
	
	JButton menuBtn, insertBtn;
	
	InsertView insertview;
	
	public BatterPanel(InsertView insertview) {
		setLayout(null);
		
		this.insertview = insertview;
		
		String title[] = { "이름:", "나이:", "신장:", "타수:", "안타:", "타율" };
		
		for (int i = 0; i < title.length; i++) {
			label[i] = new JLabel();
			label[i].setText( title[i] );
			label[i].setFont(new Font("돋움", Font.BOLD, 18));
			label[i].setBounds(30, 50 + 40 * i, 100, 30);
			
			textField[i] = new JTextField();
			textField[i].setBounds(180, 50 + 40 * i, 200, 30);
			
			add(label[i]);
			add(textField[i]);
		}
		
		menuBtn = new JButton("메뉴");
		menuBtn.setBounds(150, 320, 150, 50);
		menuBtn.addActionListener(this);
		add(menuBtn);
		
		insertBtn = new JButton("추가");
		insertBtn.setBounds(320, 320, 150, 50);
		insertBtn.addActionListener(this);
		add(insertBtn);
				
		setBounds(0, 0, 640, 450);
		setBackground(new Color(0, 200, 255));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		
		if(obj == menuBtn) {
		//	JOptionPane.showMessageDialog(null, "menuBtn");
			insertview.dispose();
			new MenuView();
		}
		else if(obj == insertBtn) {
			
			BaseballDao ball = BaseballDao.getInstance();	
			Human human = null;
			
			for (int i = 0; i < textField.length; i++) {
				if(textField[i].getText().equals("")) {
					JOptionPane.showMessageDialog(null, "모두 작성해 주십시오");
					return;
				}
			}			
			
			String name = textField[0].getText();
			int age = Integer.parseInt(textField[1].getText());
			double height = Double.parseDouble(textField[2].getText());
			int batcount = Integer.parseInt(textField[3].getText());
			int hit = Integer.parseInt(textField[4].getText());
			double hitAvg = Double.parseDouble(textField[5].getText());
			
			human = new Batter(0, name, age, height, batcount, hit, hitAvg);	
			
			boolean b = ball.insert(human);
			if(b) {
				JOptionPane.showMessageDialog(null, "성공적으로 추가하였습니다");
				
				for (int i = 0; i < textField.length; i++) {
					textField[i].setText("");
				}
			}else {
				JOptionPane.showMessageDialog(null, "추가되지 않았습니다");
			}			
			
		}
		
	}
}
package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import dao.BaseballDao;

public class MenuView extends JFrame implements ActionListener{

	JButton button[];
	
	public MenuView() {
		super("MenuView");
		
		setLayout(null);
		
		// 추가, 삭제, 검색, 수정, 모두출력
		
		JLabel label = new JLabel("메뉴");
		label.setHorizontalAlignment(JLabel.CENTER);
		label.setFont(new Font("바탕", Font.BOLD, 48));
		label.setBounds(200, 30, 200, 50);
		add(label);
		
		String title[] = { "선수추가", "선수삭제", "선수검색", "선수수정", "모두출력", "저  장" };
		button = new JButton[title.length];
		
		//button 
		for (int i = 0; i < title.length; i++) {
			button[i] = new JButton();
			button[i].setLabel(title[i]);
			button[i].setBounds(180, 120 + 60 * i, 260, 50);
			button[i].addActionListener(this);
			add(button[i]);
		}
		
		
		setBounds(0, 0, 640, 580);
		setBackground(Color.gray);
		setVisible(true);
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if(obj == button[0]) {			// insert
			new InsertView();
			this.dispose();
		}else if(obj == button[1]) {	// delete
			new DeleteView();
			this.dispose();
		}else if(obj == button[2]) {	// select
			
		}else if(obj == button[3]) {	// update
			
		}else if(obj == button[4]) {	// all print   
			
		}else if(obj == button[5]) {	// data save   
			BaseballDao dao = BaseballDao.getInstance();
			dao.dataSave();
			JOptionPane.showMessageDialog(null, "저장 돠었습니다.");
		}
		
		
	}
}






package view;

import javax.swing.JFrame;

public class DeleteView extends JFrame {

}
package file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import dao.BaseballDao;
import dto.Batter;
import dto.Human;
import dto.Pitcher;

public class FileClass {

	File file = new File("d:\\tmp\\baseball.txt");
	
	public FileClass() {
		createFile();
	}
	
	public void createFile() {
		try {
			if(file.createNewFile()) {
				System.out.println("파일 생성 성공!");
			}else {
				System.out.println("동일한 이름의 파일이 존재합니다");
			}
		} catch (IOException e) {}
	}
	
	public List<Human> fileLoad() {
		List<Human> list = new ArrayList<Human>();
		
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
					
			// 배열 저장
			String str;		
			while( (str = br.readLine()) != null ) {
				// str -> 번호-이름-나이-신장-				
				// 문자열을 자른다
				String data[] = str.split("-");				
				int number = Integer.parseInt(data[0]);
				if(number < 2000) {
					// 투수				
					Pitcher p = new Pitcher(	Integer.parseInt(data[0]), 
												data[1], 
												Integer.parseInt(data[2]), 
												Double.parseDouble(data[3]), 
												Integer.parseInt(data[4]), 
												Integer.parseInt(data[5]), 
												Double.parseDouble(data[6]) );
					list.add(p);
				}
				else {
					Batter b = new Batter(		Integer.parseInt(data[0]), 
												data[1], 
												Integer.parseInt(data[2]), 
												Double.parseDouble(data[3]), 
												Integer.parseInt(data[4]), 
												Integer.parseInt(data[5]), 
												Double.parseDouble(data[6]) );
					list.add(b);
				}				
			}
			br.close();
			
		} catch (FileNotFoundException e) {			
			e.printStackTrace();
		} catch (IOException e) {			
			e.printStackTrace();
		}		
		
		return list;
	}
	
	public void fileSave(List<Human> list) {				
		
		try {
			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
			
			for (int i = 0; i < list.size(); i++) {
				Human h = list.get(i);
				pw.println(h.toString());
			}			
			pw.close();
			
		} catch (IOException e) {			
			e.printStackTrace();
		}			
	}
	
}