티스토리 뷰

프로그래밍/Java Programming

자바 도서관리 AWT

방구석개발 2018. 4. 7. 04:25

AWT 를 이용하여 도서관리시스템 제작


1. Book.java


package Library;

public class Book {
	String bookTitle, author, publisher;
	int price, publYear;
	int sum, avg;

	public void SetBook(String bt, String at, int price, int chulyear, String chulpansa) {
		this.bookTitle = bt;
		this.author = at;
		this.price = price;
		this.publYear = chulyear;
		this.publisher = chulpansa;

	}

	public void output() {
		String out = "";
		out = "제목= " + this.bookTitle + "저자= " + this.author + "가격=" + this.price + "출판연도=" + this.publYear + "출판사="
				+ this.publisher + "합계=" + this.sum + "평균=" + this.avg;
		return;
	}
}

2. MyLibrary


package Library;

import java.awt.*;
import java.awt.event.*;

public class MyLibrary extends Frame implements ActionListener, ItemListener {

	MenuBar mb = new MenuBar();
	Menu work = new Menu("Work");// 상단메뉴
	Book[] book = new Book[10];
	int count = 0;
	FileDialog[] fd = new FileDialog[2];// 파일저장,불러오기

	MenuItem[] mi = new MenuItem[6];
	String[] miname = { "입력", "계산", "출력", "파일저장", "파일불러오기", "종료" };
	Panel[] p = new Panel[3];// 패널
	Label[] la = new Label[6];
	String[] laname = { "책제목", "저자", "가격", "출판연도", "출판사", "출력" };
	TextField[] tf = new TextField[6];
	Button[] button = new Button[4];
	String btname[] = { "입력", "계산", "출력", "종료" };
	TextArea ta = new TextArea(10, 20);

	GridLayout gl = new GridLayout(6, 2);
	GridLayout gl2 = new GridLayout(1, 6);

	public MyLibrary(String t) {
		super(t);
		setLayout(null);

		for (int i = 0; i < mi.length; i++) {
			mi[i] = new MenuItem(miname[i]);
			mi[i].addActionListener(this);
			work.add(mi[i]);
		}

		mb.add(work);
		setMenuBar(mb);

		for (int i = 0; i < p.length; i++) {
			p[i] = new Panel();
		}

		for (int i = 0; i < la.length; i++) {
			la[i] = new Label(laname[i]);
		}

		for (int i = 0; i < tf.length; i++) {
			la[i] = new Label(laname[i]);
			tf[i] = new TextField(10);
			p[0].add(la[i]);
			p[0].add(tf[i]);
		}
		for (int i = 0; i < button.length; i++) {
			button[i] = new Button(btname[i]);
			button[i].addActionListener(this);
			p[2].add(button[i]);
		}
		p[0].setBounds(20, 70, 150, 300);
		p[0].setBackground(Color.gray);
		p[0].setLayout(gl);
		add(p[0]);

		p[1].setBounds(200, 70, 200, 300);
		p[1].setBackground(Color.BLUE);
		add(p[1]);
		p[1].add(la[5]);
		p[1].add(ta);

		p[2].setBounds(20, 400, 700, 100);
		p[2].setBackground(Color.pink);
		p[2].setLayout(gl2);
		add(p[2]);

		fd[0] = new FileDialog(this, "Save", FileDialog.SAVE);
		fd[1] = new FileDialog(this, "Save", FileDialog.LOAD);

		setSize(700, 600);
		setVisible(true);
		addWindowListener(new MyListener());
	}

	public void actionPerformed(ActionEvent e) {
		Object o = e.getSource();
		int sum = 0;
		double avg = 0;

		if (o == button[0] || o == mi[0]) {
			book[count] = new Book();
			book[count].SetBook(tf[0].getText(), tf[1].getText(), Integer.parseInt(tf[2].getText()),
					Integer.parseInt(tf[3].getText()), tf[4].getText());
			count++;
			for (int i = 0; i < tf.length; i++) {
				tf[i].setText("");
			}

		} else if (o == button[1] || o == mi[1]) {
			for (int i = 0; i < count; i++) {
				sum += book[i].price;
				avg = (double) sum / count;
				ta.setText("합계: " + sum);
				ta.append("평균: " + avg + "\n");

			}
		} else if (o == button[2] || o == mi[2]) {
			for (int i = 0; i < count; i++) {
				ta.append("책제목=" + book[i].bookTitle + "\n");
				ta.append("저자=" + book[i].author + "\n");
				ta.append("가격=" + book[i].price + "\n");
				ta.append("출판연도=" + book[i].publYear + "\n");
				ta.append("출판사=" + book[i].publisher + "\n");
			}
		} else if (o == button[3] || o == mi[5]) {
			System.exit(0);
		}
		if (o == mi[3]) {// 메뉴파일저장
			fd[0].setVisible(true);
		} else if (o == mi[4]) {
			fd[0].setVisible(true);
		}

	}

	public void itemStateChanged(ItemEvent e) {
	}
}

3. MyLibrary_Test.java


package Library;

public class MyLibrary_Test {

	public static void main(String[] args) {
		MyLibrary ml = new MyLibrary("책관리시스템");

	}

}

4. MyListener.java


package Library;

import java.awt.*;
import java.awt.event.*;

public class MyListener implements WindowListener {
	public void windowClosing(WindowEvent ev) {
		System.exit(0);
	}

	public void windowActivated(WindowEvent ev) {
	}

	public void windowClosed(WindowEvent e) {
	}

	public void windowDeactivated(WindowEvent e) {
	}

	public void windowDeiconified(WindowEvent e) {
	}

	public void windowIconified(WindowEvent e) {
	}

	public void windowOpened(WindowEvent e) {
	}

}


결과





'프로그래밍 > Java Programming' 카테고리의 다른 글

AWT 기본만들기  (0) 2018.04.07
학생성적관리시스템 AWT  (0) 2018.04.07
자바 강수량 구하기 AWT  (0) 2018.04.07
스레드 파일입력  (0) 2018.04.06
스레드 Thread 기본  (0) 2018.04.06
댓글
공지사항
글 보관함
최근에 올라온 글
최근에 달린 댓글