성적관리프로그램 V2.0
public class Student implements Comparable<Student> {
private String hakbun, name;
private int kor, eng, mat, edp, tot, ranking;
private double avg;
private char grade;
public Student(String hakbun, String name, int kor, int eng, int mat, int edp) {
this.hakbun = hakbun;
this.name = name;
this.kor = kor;
this.eng = eng;
this.mat = mat;
this.edp = edp;
this.ranking = 1;
}
@Override
public int compareTo(Student s){
return this.getTot() - s.getTot();
}
public String [] toArray(){
String [] array = new String[10];
array[0] = this.hakbun; array[1] = this.name;
array[2] = String.valueOf(this.kor); array[3] = String.valueOf(this.eng);
array[4] = String.valueOf(this.mat); array[5] = String.valueOf(this.edp);
array[6] = String.valueOf(this.tot); array[7] = String.valueOf(this.avg);
array[8] = String.valueOf(this.grade); array[9] = String.valueOf(this.ranking);
return array;
}
@Override
public String toString(){
return String.format("%-10s%7s%5d%5d%5d%5d%5d%10.2f%5c%5d\n",
this.hakbun, this.name, this.kor, this.eng, this.mat, this.edp,
this.tot, this.avg, this.grade, this.ranking);
}
public int getTot() {
return tot;
}
public void setTot(int tot) {
this.tot = tot;
}
public int getRanking() {
return ranking;
}
public void setRanking(int ranking) {
this.ranking = ranking;
}
public double getAvg() {
return avg;
}
public void setAvg(double avg) {
this.avg = avg;
}
public char getGrade() {
return grade;
}
public void setGrade(char grade) {
this.grade = grade;
}
public int getKor() {
return kor;
}
public int getEng() {
return eng;
}
public int getMat() {
return mat;
}
public int getEdp() {
return edp;
}
}
import java.awt.BorderLayout;
import java.awt.Font;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class InputPanel extends JPanel {
private Vector<Student> vector;
private JTextField tf;
private JButton btnBrowse, btnLoad;
private Font font;
private JLabel lblPath;
private JPanel p;
private JFrame f;
private JTabbedPane tab;
InputPanel(Vector<Student> vector, JFrame f, JTabbedPane tab){
this.vector = vector;
this.f = f;
this.tab = tab;
this.font = new Font("NanumGotic", Font.PLAIN, 15);
tf = new JTextField(20); tf.setFont(font);
tf.setEditable(false);
btnBrowse = new JButton("Browse...");
btnBrowse.addActionListener(new InputAction(this.vector, this.tf, this.f, this.tab));
btnBrowse.setFont(font);
btnLoad = new JButton("Load");
btnLoad.addActionListener(new InputAction(this.vector, this.tf, this.f, this.tab));
btnLoad.setFont(font);
p = this.getPanel();
this.add(this.p);
}
private JPanel getPanel(){
JPanel pTemp = new JPanel();
pTemp.setLayout(new BorderLayout(20,40));
pTemp.setBorder(new TitledBorder("성적파일경로"));
pTemp.add(this.tf, "Center");
pTemp.add(this.btnBrowse, "East");
pTemp.add(this.btnLoad, "South");
return pTemp;
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class InputAction implements ActionListener {
private Vector<Student> vector;
private JTextField tf;
private JFrame f;
private JTabbedPane tab;
public InputAction(Vector<Student> vector, JTextField tf, JFrame f, JTabbedPane tab){
this.vector = vector;
this.tf = tf;
this.f = f;
this.tab = tab;
}
@Override
public void actionPerformed(ActionEvent evt){
switch(evt.getActionCommand()){
case "Browse..." : setFile(); break;
case "Load" : input(this.tf.getText()); break;
}
}
private void setFile(){
JFileChooser jc = new JFileChooser(".");
int choice = jc.showOpenDialog(null);
if(choice == JFileChooser.CANCEL_OPTION){
JOptionPane.showMessageDialog(this.f, "반드시 파일을 선택해야 합니다",
"File Not Found", JOptionPane.ERROR_MESSAGE);
}else if(choice == JFileChooser.APPROVE_OPTION){
File file = jc.getSelectedFile();
this.tf.setText(file.getAbsolutePath());
}
}
private void input(String path){ //Load 버튼을 눌렀을 때
if(this.tf.getText() == null || this.tf.getText().length() == 0){
//경로가 선택되지 않았다면
JOptionPane.showMessageDialog(this.f, "반드시 파일을 선택해야 합니다",
"File Not Found", JOptionPane.ERROR_MESSAGE);
return;
}
StringTokenizer st = null;
String line = null;
Scanner scan = null;
try{
scan = new Scanner(new File(path));
this.vector.clear();
while((line = scan.nextLine()) != null){
//System.out.println(line);
st = new StringTokenizer(line);
String [] array = new String[st.countTokens()];
int i = 0;
while(i < array.length){
array[i++] = st.nextToken();
}
Student student = new Student(array[0].trim(), array[1].trim(),
Integer.parseInt(array[2].trim()), Integer.parseInt(array[3].trim()),
Integer.parseInt(array[4].trim()), Integer.parseInt(array[5].trim()));
this.vector.addElement(student);
}
}catch(FileNotFoundException ex){
JOptionPane.showMessageDialog(this.f, "선택하신 파일을 찾을 수 없습니다.",
"File Not Found", JOptionPane.ERROR_MESSAGE);
}catch(NoSuchElementException ex){}
JOptionPane.showMessageDialog(this.f,
this.vector.size() + "명의 학생이 잘 처리됐습니다.",
"Success", JOptionPane.INFORMATION_MESSAGE);
this.tab.setEnabledAt(2, true);
}
}
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class InputPanel1 extends JPanel {
private Vector<Student> vector;
private JFrame f;
private JLabel lblHakbun, lblName, lblKor,lblEng, lblMat, lblEdp;
private JTextField tfHakbun, tfName, tfKor, tfEng, tfMat, tfEdp;
private JButton btnContinue, btnBreak;
private Font font;
private JTabbedPane tab;
public InputPanel1(Vector<Student> vector, JFrame f, JTabbedPane tab) {
this.vector = vector;
this.f = f;
this.tab = tab;
this.font = new Font("NanumGotic", Font.PLAIN, 20);
this.setLayout(new BorderLayout());
this.add("Center", getCenter());
this.add("South", getSouth());
}
private JPanel getSouth(){
JPanel p = new JPanel();
p.add(this.btnContinue = new JButton("입력하기"));
this.btnContinue.setFont(font);
this.btnContinue.addActionListener(
new InputAction1(this.vector, this.f, this.tab, this.tfHakbun, tfName,
this.tfKor, this.tfEng, this.tfMat, this.tfEdp));
p.add(this.btnBreak = new JButton("출력하기"));
//if(this.vector.size() == 0) this.btnBreak.setEnabled(false);
this.btnBreak.setFont(font);
this.btnBreak.addActionListener(
new InputAction1(this.vector, this.f, this.tab, this.tfHakbun, tfName,
this.tfKor, this.tfEng,this.tfMat, this.tfEdp));
return p;
}
private JPanel getCenter(){
JPanel p, inner;
inner = new JPanel();
inner.setBorder(new TitledBorder("성적데이터입력"));
inner.setLayout(new GridLayout(6,2,10,10));
inner.add(this.lblHakbun = new JLabel("학번"));
this.lblHakbun.setFont(font);
inner.add(this.tfHakbun = new JTextField(10));
this.tfHakbun.setFont(font);
inner.add(this.lblName = new JLabel("이름"));
this.lblName.setFont(font);
inner.add(this.tfName = new JTextField());
this.tfName.setFont(font);
inner.add(this.lblKor = new JLabel("국어"));
this.lblKor.setFont(font);
inner.add(this.tfKor = new JTextField());
this.tfKor.setFont(font);
inner.add(this.lblEng = new JLabel("영어"));
this.lblEng.setFont(font);
inner.add(this.tfEng = new JTextField());
this.tfEng.setFont(font);
inner.add(this.lblMat = new JLabel("수학"));
this.lblMat.setFont(font);
inner.add(this.tfMat = new JTextField());
this.tfMat.setFont(font);
inner.add(this.lblEdp = new JLabel("전산"));
this.lblEdp.setFont(font);
inner.add(this.tfEdp = new JTextField());
this.tfEdp.setFont(font);
p = new JPanel();
p.add(inner);
return p;
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class InputAction1 implements ActionListener {
private Vector<Student> vector;
private JFrame f;
private JTabbedPane tab;
private JTextField tfHakbun, tfName, tfKor, tfEng, tfMat, tfEdp;
public InputAction1(Vector<Student> vector, JFrame f, JTabbedPane tab,
JTextField tfHakbun, JTextField tfName, JTextField tfKor,
JTextField tfEng, JTextField tfMat, JTextField tfEdp) {
this.vector = vector;
this.f = f;
this.tab = tab;
this.tfHakbun = tfHakbun;
this.tfName = tfName;
this.tfKor = tfKor;
this.tfEng = tfEng;
this.tfMat = tfMat;
this.tfEdp = tfEdp;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("입력하기")){
if(!this.check()){
JOptionPane.showMessageDialog(this.f, "입력되지 않은 데이터가 있습니다",
"Warning", JOptionPane.WARNING_MESSAGE);
this.tfHakbun.requestFocus();
}else{
Student s = new Student(this.tfHakbun.getText().trim(),
this.tfName.getText().trim(),
Integer.parseInt(this.tfKor.getText().trim()),
Integer.parseInt(this.tfEng.getText().trim()),
Integer.parseInt(this.tfMat.getText().trim()),
Integer.parseInt(this.tfEdp.getText().trim()));
this.vector.addElement(s);
JOptionPane.showMessageDialog(this.f, "입력됐습니다",
"Success", JOptionPane.INFORMATION_MESSAGE);
this.tab.setEnabledAt(2, true);
this.tfHakbun.setText(""); this.tfName.setText("");
this.tfKor.setText(""); this.tfEng.setText("");
this.tfMat.setText(""); this.tfEdp.setText("");
}
}else if(e.getActionCommand().equals("출력하기")){
if(this.vector.size() == 0){
JOptionPane.showMessageDialog(this.f, "데이터가 없습니다.",
"Error", JOptionPane.ERROR_MESSAGE);
this.tfHakbun.requestFocus();
return;
}
this.tab.setSelectedIndex(2);
}
}
private boolean check(){
if(this.tfHakbun.getText().length() == 0 ||
this.tfName.getText().length() == 0 ||
this.tfKor.getText().length() == 0 ||
this.tfEng.getText().length() == 0 ||
this.tfMat.getText().length() == 0 ||
this.tfEdp.getText().length() == 0) return false;
else return true;
}
}
import java.util.Vector;
public class Calc {
private Vector<Student> vector;
public Calc(Vector<Student> vector) {
this.vector = vector;
}
public void calc(){
for(int i = 0 ; i < this.vector.size() ; i++){
Student s = this.vector.get(i);
s.setTot(s.getKor() + s.getEng() + s.getMat() + s.getEdp());
s.setAvg(s.getTot() / 4.);
s.setGrade(this.getGrade(s.getAvg()));
}
}
private char getGrade(double avg){
if(avg <=100 && avg >= 90) return 'A';
else if(avg < 90 && avg >= 80) return 'B';
else if(avg < 80 && avg >= 70) return 'C';
else if(avg < 70 && avg >= 60) return 'D';
else return 'F';
}
}
import java.util.Vector;
public class Sort {
private Vector<Student> vector;
public Sort(Vector<Student> vector) {
this.vector = vector;
for(int i = 0 ; i < this.vector.size() ; i++)
this.vector.get(i).setRanking(1);
}
public void sort(){ //랭킹을 위한 선택정렬
for(int i = 0 ; i < this.vector.size() - 1 ; i++){
for(int j = i +1 ; j < this.vector.size() ; j++){
if(this.vector.get(i).compareTo(this.vector.get(j)) > 0){
this.vector.get(j).setRanking(this.vector.get(j).getRanking() + 1);
}else if(this.vector.get(i).compareTo(this.vector.get(j)) < 0){
this.vector.get(i).setRanking(this.vector.get(i).getRanking() +1);
}
}
}
}
}
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
public class StudentModel extends DefaultTableModel {
private Vector<Student> vector;
private int choice;
public StudentModel(Vector<Student> vector, int choice) {
this.vector = vector;
this.choice = choice;
setModelData();
}
private void setModelData(){
Vector<String> columnVector = new Vector<String>(1,1);
String [] array = {"학번", "이름", "국어", "영어", "수학", "전산", "총점",
"평균", "학점", "등수"};
for(int i = 0 ; i < choice ; i++) columnVector.addElement(array[i]);
Vector<Object> dataVector = new Vector<Object>(1,1);
for(int i = 0 ; i < this.vector.size() ; i++){
Vector<String> tempVector = new Vector<String>(1,1);
for(String str : this.vector.get(i).toArray())
tempVector.addElement(str);
tempVector.setSize(choice);
tempVector.trimToSize();
dataVector.addElement(tempVector);
}
this.setDataVector(dataVector, columnVector);
}
}
import java.awt.BorderLayout;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class OutputPanel extends JPanel{
private Vector<Student> vector;
private JFrame f;
private JScrollPane pane;
private JTable table;
private JButton btnOriginal, btnCalc, btnSort, btnDelete;
private JPanel p;
OutputPanel(Vector<Student> vector, JFrame f) {
this.vector = vector;
this.f = f;
this.p = new JPanel();
this.table = new JTable();
this.btnOriginal = new JButton("원본보기");
this.btnOriginal.addActionListener(new OutputAction(this.vector, this.f, this.table));
this.btnCalc = new JButton("계산하기");
this.btnCalc.addActionListener(new OutputAction(this.vector, this.f, this.table));
this.btnSort = new JButton("소팅하기");
this.btnSort.addActionListener(new OutputAction(this.vector, this.f, this.table));
this.btnDelete = new JButton("삭제하기");
this.btnDelete.addActionListener(new OutputAction(this.vector, this.f, this.table));
pane = new JScrollPane(this.table,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.setLayout(new BorderLayout());
this.add("Center", pane);
this.p.add(this.btnOriginal); this.p.add(this.btnCalc); this.p.add(this.btnSort);
this.p.add(this.btnDelete);
this.add("North", p);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
public class OutputAction implements ActionListener {
private JFrame f;
private Vector<Student> vector;
private JTable table;
OutputAction(Vector<Student> vector, JFrame f, JTable table){
this.vector = vector;
this.f = f;
this.table = table;
}
@Override
public void actionPerformed(ActionEvent e) {
switch(e.getActionCommand()){
case "원본보기": viewOriginal(); break;
case "계산하기": calc(); break;
case "소팅하기": sort(); break;
case "삭제하기": delete(); break;
}
}
private void delete() {
int [] array = this.table.getSelectedRows(); //선택한 행을 array 배열에 넣음.
// for(int i = 0 ; i<array.length ; i++){
// Student s = this.vector.get(array[i]);
// this.vector.removeElement(s);
//앞에서부터 삭제하면 두개이상 지울 때, 삭제한 파일 다음 값이 앞으로 오게 되는데 다음 값을 지울 때 위치에대한 에러가 발생
for(int i = array.length-1 ; i>=0 ; i--){
this.vector.removeElementAt(array[i]);
}
this.sort(); //삭제된 것을 제외하고 다시 정렬.
}
private void viewOriginal(){
this.table.setModel(new StudentModel(this.vector, 6));
}
private void calc(){
Calc calc = new Calc(this.vector);
calc.calc();
this.table.setModel(new StudentModel(this.vector, 9));
}
private void sort(){
Sort sort = new Sort(this.vector);
sort.sort();
this.table.setModel(new StudentModel(this.vector, 10));
}
}
import java.awt.Container;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
public class Main {
private JFrame f;
private JTabbedPane tab;
private Container con;
private Vector<Student> vector;
private Main(){
this.f = new JFrame("성적관리프로그램 V2.0");
this.tab = new JTabbedPane();
this.con = this.f.getContentPane();
this.vector = new Vector<Student>(1,1);
}
private void display(){
this.f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.f.setResizable(false);
this.tab.addTab("파일데이타입력", new InputPanel(this.vector, this.f, this.tab));
this.tab.addTab("일반데이타입력", new InputPanel1(this.vector, this.f, this.tab));
this.tab.addTab("데이터출력", new OutputPanel(this.vector, this.f));
this.tab.setEnabledAt(2, false);
this.con.add(this.tab);
this.f.setSize(500, 400);
this.f.setLocationRelativeTo(null);
this.f.setVisible(true);
}
public static void main(String[] args) {
new Main().display();
}
}
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
===================================================================
환자관리프로그램
1.
public class Person implements Comparable<Person> {
private int num; // 번호
private String part; // 진찰부서
private int jcash; // 진찰비
private int icash; // 입원비
private double tot; // 진료비
private int day; // 입원일
private int age; // 나이
public String [] toArray(){
String [] array = new String[5];
array[0] = String.valueOf(this.num);
array[1] = this.part;
array[2] = String.valueOf(this.jcash);
array[3] = String.valueOf(this.icash);
array[4] = String.valueOf((int)this.tot);
return array;
}
@Override
public int compareTo(Person p){
return (int)(this.getTot() - p.getTot());
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getPart() {
return part;
}
public void setPart(String part) {
this.part = part;
}
public int getJcash() {
return jcash;
}
public void setJcash(int jcash) {
this.jcash = jcash;
}
public int getIcash() {
return icash;
}
public void setIcash(int icash) {
this.icash = icash;
}
public double getTot() {
return tot;
}
public void setTot(double tot) {
this.tot = tot;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class Input extends JPanel {
private Vector<Person> vector;
private JFrame f;
private JLabel lblNumber, lblCode, lblDays,lblAge;
private JTextField tfNumber, tfCode, tfDays, tfAge;
private JButton btnAdd;
private Font font;
private JTabbedPane tab;
public Input(Vector<Person> vector, JFrame f, JTabbedPane tab) {
this.vector = vector;
this.f = f;
this.tab = tab;
this.font = new Font("NanumGotic", Font.PLAIN, 20);
this.setLayout(new BorderLayout());
this.add("Center", getCenter());
this.add("South", getSouth());
}
private JPanel getSouth(){
JPanel p = new JPanel();
p.add(this.btnAdd = new JButton("Add"));
this.btnAdd.setFont(font);
this.btnAdd.addActionListener(
new InputAction1(this.vector, this.f, this.tab, this.tfNumber, tfCode,
this.tfDays, this.tfAge));
return p;
}
private JPanel getCenter(){
JPanel p, inner;
inner = new JPanel();
inner.setBorder(new TitledBorder("Data Input"));
inner.setLayout(new GridLayout(4,2,10,10));
inner.add(this.lblNumber = new JLabel("Number :"));
this.lblNumber.setFont(font);
inner.add(this.tfNumber = new JTextField(10));
this.tfNumber.setFont(font);
inner.add(this.lblCode = new JLabel("Code :"));
this.lblCode.setFont(font);
inner.add(this.tfCode = new JTextField());
this.tfCode.setFont(font);
inner.add(this.lblDays = new JLabel("Days :"));
this.lblDays.setFont(font);
inner.add(this.tfDays = new JTextField());
this.tfDays.setFont(font);
inner.add(this.lblAge = new JLabel("Age :"));
this.lblAge.setFont(font);
inner.add(this.tfAge = new JTextField());
this.tfAge.setFont(font);
/* inner.add(this.btnAdd = new JButton("Add"));
this.btnAdd.setFont(font);
this.btnAdd.addActionListener(new InputAction1(this.vector, this.f, this.tab, this.tfNumber, tfCode, this.tfDays, this.tfAge));
*/
p = new JPanel();
p.add(inner);
return p;
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class InputAction1 implements ActionListener {
private Vector<Person> vector;
private JFrame f;
private JTabbedPane tab;
private JTextField tfNumber, tfCode, tfDays, tfAge;
int count = 0;
public InputAction1(Vector<Person> vector, JFrame f, JTabbedPane tab,
JTextField tfNumber, JTextField tfCode, JTextField tfDays,
JTextField tfAge) {
this.vector = vector;
this.f = f;
this.tab = tab;
this.tfNumber = tfNumber;
this.tfCode = tfCode;
this.tfDays = tfDays;
this.tfAge = tfAge;
}
@Override
public void actionPerformed(ActionEvent e) {
String msg = null;
if(e.getActionCommand().equals("Add")){
if(count == 5){
JOptionPane.showMessageDialog(this.f, "더 이상 데이터를 입력할 수 없습니다.",
"Warning", JOptionPane.WARNING_MESSAGE);
}
else{
if(!this.check()){
JOptionPane.showMessageDialog(this.f, "입력되지 않은 데이터가 있습니다",
"Warning", JOptionPane.WARNING_MESSAGE);
this.tfNumber.requestFocus();
}else{
Person p = new Person();
p.setNum(Integer.parseInt(this.tfNumber.getText().trim()));
String part = null;
switch(this.tfCode.getText().trim())
{
case "MI" : part = "외과"; break;
case "NI" : part = "내과"; break;
case "SI" : part = "피부과"; break;
case "TI" : part ="소아과"; break;
case "VI" : part = "산부인과"; break;
case "WI" : part = "비뇨기과"; break;
}
p.setPart(part);
p.setDay(Integer.parseInt(this.tfDays.getText().trim()));
p.setAge(Integer.parseInt(this.tfAge.getText().trim()));
this.vector.addElement(p);
count++;
if(count == 1) msg = "1st ";
else if(count == 2) msg = "2nd ";
else msg = count +"rd ";
JOptionPane.showMessageDialog(this.f, msg+"patients added successfully.",
"Success", JOptionPane.INFORMATION_MESSAGE);
// this.tab.setEnabledAt(2, true);
this.tfNumber.setText(""); this.tfCode.setText("");
this.tfDays.setText(""); this.tfAge.setText("");
}
}
}
/* else if(e.getActionCommand().equals("출력하기")){
if(this.vector.size() == 0){
JOptionPane.showMessageDialog(this.f, "데이터가 없습니다.",
"Error", JOptionPane.ERROR_MESSAGE);
this.tfNumber.requestFocus();
return;
}
this.tab.setSelectedIndex(2);
}
*/ }
private boolean check(){
if(this.tfNumber.getText().length() == 0 ||
this.tfCode.getText().length() == 0 ||
this.tfDays.getText().length() == 0 ||
this.tfAge.getText().length() == 0 ) return false;
else return true;
}
}
import java.util.Vector;
public class Calc {
private Vector<Person> vector;
Person person;
public Calc(Vector<Person> vector)
{
this.vector = vector;
}
void calc()
{
int num1 = 0;
double num2 = 0;
for(int i = 0; i <this.vector.size(); i++)
{
person = vector.get(i);
if(person.getDay() <= 3){ num1 = 30000; num2 = 1.00; }
else if(person.getDay() >= 10 && person.getDay() < 15) { num1 = 25000; num2 = 0.85; }
else if(person.getDay() >= 15 && person.getDay() < 20) { num1 = 25000; num2 = 0.80; }
else if(person.getDay() >= 20 && person.getDay() < 30) { num1 = 25000; num2 = 0.77; }
else if(person.getDay() >= 30 && person.getDay() < 100) { num1 = 25000; num2 = 0.72; }
else if(person.getDay() >= 100) { num1 = 25000; num2 = 0.68; }
person.setIcash((int)(person.getDay() * num1 * num2)); //입원비.....
if(person.getAge() < 10) person.setJcash(7000);
else if(person.getAge() >= 10 && person.getAge() < 20) person.setJcash(5000);
else if(person.getAge() >= 20 && person.getAge() < 30) person.setJcash(8000);
else if(person.getAge() >= 30 && person.getAge() < 40) person.setJcash(7000);
else if(person.getAge() >= 40 && person.getAge() < 50) person.setJcash(4500);
else if(person.getAge() >= 50) person.setJcash(2300);
person.setTot(person.getIcash() + person.getJcash()); //진료비......
}
}
}
import java.util.Vector;
public class Sort
{
private Vector<Person> vector;
Person temp = null;
public Sort(Vector<Person> vector)
{
this.vector = vector;
//for(int i = 0 ; i < this.vector.size() ; i++)
//this.vector.get(i).setRanking(1);
}
public void sort()
{
for(int i = 0 ; i < this.vector.size() - 1 ; i++){
for(int j = i + 1 ; j < this.vector.size() ; j++){
if(this.vector.get(i).compareTo(this.vector.get(j)) < 0)
{
this.temp = (Person)this.vector.get(i);
this.vector.set(i,this.vector.get(j));
this.vector.set(j, this.temp);
}
}
}
}
}
import java.awt.BorderLayout;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class OutputPanel extends JPanel {
private Vector<Person> vector;
private JFrame f;
private JTable table;
private JButton btnDisplay, btnPrint;
private JPanel p;
private JScrollPane pane;
OutputPanel(Vector<Person> vector, JFrame f){
this.vector = vector;
this.f = f;
this.table = new JTable();
this.p = new JPanel();
this.btnDisplay = new JButton("Display");
this.btnDisplay.addActionListener(new OutputAction(this.vector, this.f, this.table));
this.btnPrint = new JButton("Print");
this.btnPrint.addActionListener(new OutputAction(this.vector, this.f, this.table));
pane = new JScrollPane(this.table,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.setLayout(new BorderLayout());
this.add("Center", pane);
this.p.add(this.btnDisplay); this.p.add(this.btnPrint);
this.add("North", p);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
public class OutputAction implements ActionListener {
private JFrame f;
private Vector<Person> vector;
private JTable table;
OutputAction(Vector<Person> vector, JFrame f, JTable table) {
this.vector = vector;
this.f = f;
this.table = table;
}
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand().trim())
{
case "Display" : viewDisplay();
break;
case "Print" : viewPrint();
break;
}
}
private void viewDisplay() {
if(vector.size()==0){
JOptionPane.showMessageDialog(this.f, "Patients Data empty",
"Warning", JOptionPane.WARNING_MESSAGE);}
Calc calc = new Calc(this.vector);
calc.calc();
Sort sort = new Sort(this.vector);
sort.sort();
this.table.setModel(new PersonModel(this.vector));
}
private void viewPrint() {
System.out.println(" <<Hospital Management Program>>");
System.out.println("----------------------------------------------------------");
System.out.println("No Department Consulation Charge Hospital Charge SUM");
System.out.println("----------------------------------------------------------");
for(int i = 0; i< this.vector.size(); i ++){
Person person = vector.get(i);
System.out.println(person.getNum() + "\t" + person.getPart() + "\t" + person.getJcash()
+"\t" + person.getIcash() + "\t" + (int)person.getTot());
}
}
}
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
public class PersonModel extends DefaultTableModel {
private Vector<Person> vector;
public PersonModel(Vector<Person> vector) {
this.vector = vector;
setModelData();
}
private void setModelData() {
Vector<String> columnVector = new Vector<String>(1, 1);
String[] array = { "No", "Department", "Consultation Charge",
"Hospital Charge", "SUM" };
for (int i = 0; i < array.length; i++)
columnVector.addElement(array[i]);
Vector<Object> dataVector = new Vector<Object>(1, 1);
for (int i = 0; i < this.vector.size(); i++) {
Vector<String> tempVector = new Vector<String>(1, 1);
for (String str : this.vector.get(i).toArray())
tempVector.addElement(str);
// tempVector.setSize(choice);
tempVector.trimToSize();
dataVector.addElement(tempVector);
}
this.setDataVector(dataVector, columnVector);
}
}
import java.awt.Container;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
public class Main {
private JFrame f;
private JTabbedPane tab;
private Container con;
private Vector<Person> vector;
private Main(){
this.f = new JFrame("Hospital Management Program");
this.tab = new JTabbedPane();
this.con = this.f.getContentPane();
this.vector = new Vector<Person>(1,1);
}
private void display(){
this.f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.f.setResizable(false);
this.tab.addTab("Input Data Panel", new Input(this.vector, this.f, this.tab));
this.tab.addTab("Display Data Panel", new OutputPanel(this.vector, this.f));
this.con.add(this.tab);
this.f.setSize(500, 400);
this.f.setLocationRelativeTo(null);
this.f.setVisible(true);
}
public static void main(String[] args) {
new Main().display();
}
}
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
2.v샘
public class Patients {
private String no;
private String code;
private int days;
private int age;
private String department;
private int jinchalfee;
private int ipwonfee;
private int sum;
public Patients(String no, String code, int days, int age) {
this.no = no; this.code = code;
this.days = days; this.age = age;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getJinchalfee() {
return jinchalfee;
}
public void setJinchalfee(int jinchalfee) {
this.jinchalfee = jinchalfee;
}
public int getIpwonfee() {
return ipwonfee;
}
public void setIpwonfee(int ipwonfee) {
this.ipwonfee = ipwonfee;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public String getNo() {
return no;
}
public String getCode() {
return code;
}
public int getDays() {
return days;
}
public int getAge() {
return age;
}
}
import java.awt.Container;
import java.awt.Font;
import java.awt.Toolkit;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
public class Main {
private JFrame f;
private JTabbedPane tab;
private Container con;
private Vector<Patients> vector;
private Font font;
private Main(){
this.vector = new Vector<Patients>(1,1);
this.font = new Font("NanumGothic", Font.BOLD, 25);
this.f = new JFrame("Hospital Management Program");
this.f.addWindowListener(new WindowControl(this.f));
this.con = f.getContentPane();
this.tab = new JTabbedPane();
}
private void display(){
this.tab.add(new LoginView(this.font, this.tab), "Login");
this.tab.add(new InputView(vector, this.font, this.tab), "Input Data Panel");
this.tab.add(new OutputView(vector), "Display Data Panel");
this.tab.setEnabledAt(0, true);
this.tab.setEnabledAt(1, false);
this.tab.setEnabledAt(2, false);
this.con.add(this.tab, "Center");
this.f.setSize(800,600);
this.f.setLocationRelativeTo(null);
this.f.setVisible(true);
}
public static void main(String[] args) {
new Main().display();
}
}
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
class WindowControl extends WindowAdapter {
private JFrame f;
WindowControl(JFrame f){
this.f = f;
}
public void windowClosing(WindowEvent evt){
int result = JOptionPane.showConfirmDialog(f, "정말 종료하시겠습니까 ? ","Window Close", JOptionPane.YES_NO_OPTION);
if(result == JOptionPane.YES_OPTION) {
System.exit(0);
}else{
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
}
}
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
public class LoginView extends JPanel {
private Font font;
private JTabbedPane tab;
private JPanel p, pLogin, pCenter, pWest, pSouth;
private JLabel labelID,labelPwd;
private JTextField tfID;
private JPasswordField tfPwd;
private JButton btnOk, btnCancel;
private MyCanvas mc;
LoginView(Font font, JTabbedPane tab) {
this.font = font;
this.tab = tab;
p = new JPanel();
pCenter = new JPanel();
pLogin = new JPanel();
pWest = new JPanel();
pSouth = new JPanel();
mc = new MyCanvas();
initComponent();
}
private void initComponent(){
p.setLayout(new BorderLayout());
p.add(mc, "Center");
pLogin.setLayout(new BorderLayout());
pLogin.setBorder(new TitledBorder(
new BevelBorder(BevelBorder.RAISED), "Login",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, this.font, Color.RED));
pCenter.setLayout(new GridLayout(2,1,10,10));
pCenter.add(tfID = new JTextField(10));
tfID.setFont(this.font);
pCenter.add(tfPwd = new JPasswordField(10));
tfPwd.setFont(this.font);
pWest.setLayout(new GridLayout(2,1,10,10));
pWest.add(labelID = new JLabel("아이디 : "));
labelID.setFont(this.font);
pWest.add(labelPwd = new JLabel("패스워드 : "));
labelPwd.setFont(this.font);
pSouth.add(btnOk = new JButton("OK"));
btnOk.setFont(this.font);
btnOk.addActionListener(new LoginActionController(this, this.tfID, this.tfPwd, this.tab));
pSouth.add(btnCancel = new JButton("Cancel"));
btnCancel.addActionListener(new LoginActionController(this, this.tfID, this.tfPwd, this.tab));
btnCancel.setFont(this.font);
pLogin.add("West",pWest);
pLogin.add("Center", pCenter);
pLogin.add("South", pSouth);
p.add(pLogin, "South");
add(p);
}
private class MyCanvas extends Canvas{
private Image img;
MyCanvas(){
img = Toolkit.getDefaultToolkit().getImage("images/hospital.gif");
this.setSize(300,350);
}
@Override
public void paint(Graphics g){
g.drawImage(img, p.getWidth() / 2 - this.img.getWidth(this) / 2,
p.getHeight() / 2 - this.img.getHeight(this) / 2, this.img.getWidth(this), this.img.getHeight(this), this);
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class LoginActionController implements ActionListener {
private JPanel p;
private JTextField tfID;
private JPasswordField tfPwd;
private JTabbedPane pane;
private final String strID = "javasoft";
private final String strPwd = "123456";
LoginActionController(JPanel p,JTextField tfID, JPasswordField tfPwd, JTabbedPane pane){
this.p = p;
this.tfID = tfID;
this.tfPwd = tfPwd;
this.pane = pane;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("OK")){
if(this.tfID.getText().trim().equals(this.strID) &&
this.tfPwd.getText().trim().equals(this.strPwd)){
JOptionPane.showMessageDialog(this.p, this.strID + "님! 환영합니다.", "Success",
JOptionPane.INFORMATION_MESSAGE);
this.pane.setEnabledAt(1, true);
this.pane.setSelectedIndex(1);
}else{
JOptionPane.showMessageDialog(this.p, "아이디 혹은 패스워드가 일치하지 않습니다.", "Failure",
JOptionPane.WARNING_MESSAGE);
this.pane.transferFocus();
}
}else{
JOptionPane.showMessageDialog(this.p, "로그인해 주십시오.", "Warning",
JOptionPane.WARNING_MESSAGE);
this.tfID.requestFocus();
}
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
class InputView extends JPanel {
private Vector<Patients> vector;
private JPanel panel, pSouth, pCenter, pWest;
private JButton btnAdd;
private JComboBox<String> cbCode;
private JTextField tfNo, tfDays, tfAge;
private JLabel labelNo, labelCode, labelDays, labelAge;
private Font font;
private PatientsAddController addAction;
private JTabbedPane pane;
InputView(Vector<Patients> vector, Font font, JTabbedPane pane){
this.vector = vector;
this.font = font;
this.pane = pane;
panel = new JPanel();
pSouth = new JPanel();
pCenter = new JPanel();
pWest = new JPanel();
initComponent();
}
private void initComponent(){
panel.setLayout(new BorderLayout());
panel.setBorder(new TitledBorder(
new BevelBorder(BevelBorder.RAISED), "Data Input",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, this.font, Color.RED));
pSouth.add(btnAdd = new JButton("Add"));
btnAdd.setFont(font);
btnAdd.setBorder(new BevelBorder(BevelBorder.RAISED));
pCenter.setLayout(new GridLayout(4,1,10,10));
tfNo = new JTextField(15); tfNo.setFont(font);
String [] array = {"--Code--", "MI", "NI", "SI","TI", "VI", "WI"};
cbCode = new JComboBox<String>(array);
cbCode.setFont(font);
tfDays = new JTextField(15); tfDays.setFont(font);
tfAge = new JTextField(15); tfAge.setFont(font);
pCenter.add(tfNo); pCenter.add(cbCode);
pCenter.add(tfDays); pCenter.add(tfAge);
pWest.setLayout(new GridLayout(4,1,10,10));
labelNo = new JLabel("Number : ", JLabel.RIGHT);
labelNo.setFont(font);
labelCode = new JLabel("Code : ", JLabel.RIGHT);
labelCode.setFont(font);
labelDays = new JLabel("Days : ", JLabel.RIGHT);
labelDays.setFont(font);
labelAge = new JLabel("Age : ", JLabel.RIGHT);
labelAge.setFont(font);
pWest.add(labelNo); pWest.add(labelCode);
pWest.add(labelDays); pWest.add(labelAge);
panel.add("South", pSouth);
panel.add("West", pWest);
panel.add("Center", pCenter);
addAction = new PatientsAddController(this.vector, this, this.pane,
tfNo, cbCode, tfDays, tfAge);
btnAdd.addActionListener(addAction);
add(panel);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
class PatientsAddController implements ActionListener {
private Vector<Patients> vector;
private JPanel panel;
private JComboBox<String> cbCode;
private JTextField tfNo, tfDays, tfAge;
private JTabbedPane pane;
PatientsAddController(Vector<Patients> vector, JPanel panel, JTabbedPane pane,
JTextField tfNo, JComboBox<String> cbCode,
JTextField tfDays, JTextField tfAge){
this.vector = vector; this.panel = panel;
this.pane = pane;
this.tfNo = tfNo; this.cbCode = cbCode;
this.tfDays = tfDays; this.tfAge = tfAge;
}
@Override
public void actionPerformed(ActionEvent e) {
try{
checkData();
Patients p = new Patients(this.tfNo.getText().trim(),
this.cbCode.getSelectedItem().toString(),
Integer.parseInt(this.tfDays.getText().trim()),
Integer.parseInt(this.tfAge.getText().trim()));
this.tfNo.setText("");
this.cbCode.setSelectedIndex(0);
this.tfDays.setText("");
this.tfAge.setText("");
this.vector.addElement(p);
new PatientsCalc(p);
new PatientsSort(this.vector);
JOptionPane.showMessageDialog(this.panel,
this.vector.size() + "th patients added successfully",
"Message",
JOptionPane.INFORMATION_MESSAGE);
this.pane.setEnabledAt(2, true);
}catch(PatientException ex){
JOptionPane.showMessageDialog(this.panel, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
this.cbCode.transferFocusBackward();
}
}
private void checkData() throws PatientException{
if(this.tfNo.getText().trim().length() > 0 &&
this.cbCode.getSelectedIndex() > 0 &&
this.tfDays.getText().trim().length() > 0 &&
this.tfAge.getText().trim().length() > 0) return;
else throw new PatientException("모든 데이터는 필수 입력입니다.");
}
}
class PatientsCalc {
private Patients p; //ȯ���Ѹ? ���
PatientsCalc(Patients p){
this.p = p;
calc();
}
private void calc(){
String code = this.p.getCode();
p.setDepartment(Util.getDepartment(code));
int defaultIpwonFee = 0;
int days = this.p.getDays();
if(days <= 3) defaultIpwonFee = 30000;
else defaultIpwonFee = 25000;
int totalIpwonFee = defaultIpwonFee * days;
double rate = Util.getRate(days);
int ipwonFee = (int)(totalIpwonFee * rate);
p.setIpwonfee(ipwonFee);
int jinchalFee = Util.getJinchalFee(p.getAge());
p.setJinchalfee(jinchalFee);
int sum = jinchalFee + ipwonFee;
p.setSum(sum);
}
}
//Utility Class
public class Util {
public static String getDepartment(String code){
String department = null;
switch(code){
case "MI" : department = "외과"; break;
case "NI" : department = "내과"; break;
case "SI" : department = "피부과"; break;
case "TI" : department = "소아과"; break;
case "VI" : department = "산부인과"; break;
case "WI" : department = "비뇨기과"; break;
}
return department;
}
public static int getJinchalFee(int age){
int jinchalfee = 0;
switch(age / 10){
case 0 : jinchalfee = 7000; break;
case 1 : jinchalfee = 5000; break;
case 2 : jinchalfee = 8000; break;
case 3 : jinchalfee = 7000; break;
case 4 : jinchalfee = 4500; break;
default : jinchalfee = 2300;
}
return jinchalfee;
}
public static double getRate(int days){
double rate = 0.0;
if(days < 10) rate = 1.00;
else if(days >= 10 && days < 15) rate = 0.85;
else if(days >= 15 && days < 20) rate = 0.80;
else if(days >= 20 && days < 30) rate = 0.77;
else if(days >= 30 && days < 100) rate = 0.72;
else rate = 0.68;
return rate;
}
}
import java.util.Vector;
public class PatientsSort {
private Vector<Patients> vector;
PatientsSort(Vector<Patients> vector) {
this.vector = vector;
insertionSort();
}
private void insertionSort(){
int i, j;
Patients temp;
for(i = 1 ; i < this.vector.size() ; i++){
temp = this.vector.get(i);
for(j = i - 1 ; j >= 0 && this.vector.get(j).getSum() < temp.getSum() ; j--){
this.vector.set(j+1, this.vector.get(j));
}
this.vector.set(j + 1, temp);
}
}
}
@SuppressWarnings("serial")
public class PatientException extends Exception {
public PatientException(String msg){
super(msg);
}
}
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import java.util.Vector;
import java.awt.Font;
public class OutputView extends JPanel {
private Vector<Patients> vector;
private JPanel panel;
private JButton btnDisplay, btnPrint;
private JTable table;
private JScrollPane pane;
private PrintController print;
private DisplayAction display;
public OutputView(Vector<Patients> vector){
this.setLayout(new BorderLayout());
this.vector = vector;
panel = new JPanel();
btnDisplay = new JButton("Display");
btnPrint = new JButton("Print");
panel.add(btnDisplay); panel.add(btnPrint);
table = new JTable();
table.setRowHeight(40);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFont(new Font("NanumGothic", Font.PLAIN, 25));
pane = new JScrollPane(table);
print = new PrintController(this.vector);
btnPrint.addActionListener(print);
display = new DisplayAction(this.vector, this, table);
btnDisplay.addActionListener(display);
this.add("North", panel);
this.add("Center", pane);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class PrintController implements ActionListener {
private Vector<Patients> vector;
public PrintController(Vector<Patients> vector){
this.vector = vector;
}
@Override
public void actionPerformed(ActionEvent e) {
printLabel();
if(this.vector.size() == 0){
System.out.println(" 현재 입력된 환자 데이터가 없습니다.");
return;
}
for(int i = 0 ; i < this.vector.size() ; i++){
Patients p = this.vector.get(i);
System.out.printf("%2s%13s%,20d%,20d%,20d\n",
p.getNo(), p.getDepartment(),
p.getJinchalfee(), p.getIpwonfee(),
p.getSum());
}
}
private void printLabel(){
System.out.println("\n <<Hospital Management Program>>");
System.out.println("-------------------------------------------------------------------------------");
System.out.println("No Department Consultation Charge Hospital Charge SUM");
System.out.println("-------------------------------------------------------------------------------");
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
public class DisplayAction implements ActionListener {
private Vector<Patients> vector;
private JPanel panel;
private JTable table;
public DisplayAction(Vector<Patients> vector, JPanel panel, JTable table){
this.vector = vector;
this.panel = panel;
this.table = table;
}
@Override
public void actionPerformed(ActionEvent e) {
if(this.vector.size() == 0){
JOptionPane.showMessageDialog(this.panel,
"Patients Data is empty.", "Warning",
JOptionPane.WARNING_MESSAGE);
return;
}
this.table.setModel(new PatientsModel(this.vector));
}
}
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
public class PatientsModel extends DefaultTableModel{
private Vector<Patients> vector;
public PatientsModel(Vector<Patients> vector){
this.vector = vector;
dataAddToTable();
}
private void dataAddToTable(){
String [] columnArray = {"No", "Department", "Consultation Charge", "Hospital Charge", "SUM"};
Vector<String> colVector = new Vector<String>(1,1);
for(String str : columnArray) colVector.addElement(str);
Vector<Object> dataVector = new Vector<Object>(1,1);
for(int i = 0 ; i < this.vector.size(); i++){
Patients p = this.vector.get(i);
Vector<String> row = new Vector<String>(1,1);
row.addElement(p.getNo());
row.addElement(p.getDepartment());
row.addElement(String.format("%,d", p.getJinchalfee()));
row.addElement(String.format("%,d", p.getIpwonfee()));
row.addElement(String.format("%,d",p.getSum()));
dataVector.addElement(row);
}
this.setDataVector(dataVector, colVector);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
class PatientOutputController implements ActionListener {
private JPanel panel;
private JTabbedPane pane;
private Vector<Patients> vector;
PatientOutputController(JPanel panel, JTabbedPane pane, Vector<Patients> vector){
this.panel = panel;
this.pane = pane;
this.vector = vector;
}
@Override
public void actionPerformed(ActionEvent e) {
if(this.vector.size() > 0) this.pane.setSelectedIndex(2);
else {
JOptionPane.showMessageDialog(this.panel, "�Էµ� �����Ͱ� ����ϴ�", "Error",
JOptionPane.ERROR_MESSAGE);
this.pane.transferFocus();
}
}
}
=======================================================================
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
JavaSoft 회사 상품별판매 이익금 및 이익율표 작성하기 위한 프로그램
제품관리프로그램 (파일 입력, 로드, 계산, 파일출력)
public class Product {
private String name;
private int su;
private int saleprice;
private int buyprice;
private int fee;
private int salemoney; //판매금액
private int buymoney; //매입금액
private int money; //이익금
private double rate; //이익율
public Product(String name, int su, int saleprice, int buyprice, int fee) {
this.name = name;
this.su = su;
this.saleprice = saleprice;
this.buyprice = buyprice;
this.fee = fee;
}
@Override
public String toString(){
return String.format("%-15s%,10d%,10d%,10d%,10d%,20d%10.2f",
this.name, this.su, this.saleprice, this.buyprice, this.fee, this.money, this.rate);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSu() {
return su;
}
public void setSu(int su) {
this.su = su;
}
public int getSaleprice() {
return saleprice;
}
public void setSaleprice(int saleprice) {
this.saleprice = saleprice;
}
public int getBuyprice() {
return buyprice;
}
public void setBuyprice(int buyprice) {
this.buyprice = buyprice;
}
public int getFee() {
return fee;
}
public void setFee(int fee) {
this.fee = fee;
}
public int getSalemoney() {
return salemoney;
}
public void setSalemoney(int salemoney) {
this.salemoney = salemoney;
}
public int getBuymoney() {
return buymoney;
}
public void setBuymoney(int buymoney) {
this.buymoney = buymoney;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
}
import java.util.Vector;
import javax.swing.JFrame;
public class Main {
private Vector<Product> vector;
private ProductView pv;
private Main(){
this.vector = new Vector<Product>(1,1);
this.pv = new ProductView(this.vector);
this.pv.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.pv.addWindowListener(new WindowController(this.pv));
}
private void display(){
this.pv.setTitle("제품관리프로그램");
this.pv.setSize(600, 400);
this.pv.setLocationRelativeTo(null);
this.pv.setVisible(true);
}
public static void main(String[] args) {
new Main().display();
}
}
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
public class WindowController extends WindowAdapter {
private ProductView pv;
public WindowController(ProductView pv){
this.pv = pv;
}
@Override
public void windowClosing(WindowEvent evt){
int choice = JOptionPane.showConfirmDialog(this.pv,
"정말 종료하시겠습니까?", "Question",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(choice == JOptionPane.YES_OPTION){
System.exit(0);
}
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
public class ProductView extends JFrame {
private Vector<Product> vector;
private JScrollPane pane;
private JTable table;
private JPanel pNorth;
private JLabel label;
private JTextField tf;
private JButton btnOpen, btnLoad, btnCalc, btnSave;
private Font font;
private Container con;
public ProductView(Vector<Product> vector) {
this.con = this.getContentPane();
this.con.setLayout(new BorderLayout());
this.vector = vector;
this.table = new JTable();
this.font = new Font("Loma", Font.PLAIN, 15);
this.pane = new JScrollPane(this.table,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.pNorth = getNorth();
this.con.add("North", this.pNorth);
this.con.add("Center", this.pane);
}
private JPanel getNorth(){
JPanel p = new JPanel();
JPanel pInner = new JPanel();
pInner.setBorder(new TitledBorder(
new BevelBorder(BevelBorder.RAISED),
"파일경로", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.TOP, new Font("NanumGothic", Font.BOLD, 20),
Color.RED));
pInner.add(this.label = new JLabel("Source : ", JLabel.RIGHT));
this.label.setFont(this.font);
pInner.add(this.tf = new JTextField(18));
this.tf.setFont(font);
this.tf.setEditable(false);
pInner.add(this.btnOpen = new JButton(
new ImageIcon("images/open.gif")));
this.btnOpen.setToolTipText("파일열기"); //마우스에 대면 설명이 올라오는 기능.
this.btnOpen.addActionListener(new ProductActionController(this, this.table, this.tf,
this.vector, 1)); //무엇이 눌렸는지 ProductionAction이 알게 하기 위해서, 식별자를 써줌. 1이 눌르면 파일열기~
pInner.add(this.btnLoad = new JButton(
new ImageIcon("images/load.gif")));
this.btnLoad.setToolTipText("데이터로드하기");
this.btnLoad.addActionListener(new ProductActionController(this, this.table, this.tf,
this.vector, 2));
pInner.add(this.btnCalc = new JButton(
new ImageIcon("images/calc.gif")));
this.btnCalc.setToolTipText("계산하기");
this.btnCalc.addActionListener(new ProductActionController(this, this.table, this.tf,
this.vector, 3));
pInner.add(this.btnSave = new JButton(
new ImageIcon("images/save.gif")));
this.btnSave.setToolTipText("파일로 저장하기");
this.btnSave.addActionListener(new ProductActionController(this, this.table, this.tf,
this.vector, 4));
p.add(pInner);
return p;
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class ProductActionController implements ActionListener {
private JFrame f;
private JTable table;
private Vector<Product> vector;
private JTextField tf;
private int choice;
public ProductActionController(JFrame f, JTable table, JTextField tf,
Vector<Product> vector, int choice) {
this.f = f;
this.table = table;
this.tf = tf;
this.vector = vector;
this.choice = choice;
}
@Override
public void actionPerformed(ActionEvent e) {
switch(this.choice){
case 1 : new OpenAction(this.f, this.tf); break;
case 2 :
try{
new LoadAction(this.tf, this.table, this.vector); break;
}catch(FileNotFoundException ex){
JOptionPane.showMessageDialog(this.f, ex.getMessage());
}
break;
case 3 : new CalcAction(this.f, this.table, this.vector); break;
case 4 : new SaveAction(this.f, this.vector); break; //다이얼로그를 띄어야 하니 프레임, 내보낼 벡터
}
}
}
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class OpenAction {
private JTextField tf;
private JFrame f;
OpenAction(JFrame f, JTextField tf){
this.f = f;
this.tf = tf;
this.open();
}
private void open(){
JFileChooser jc = new JFileChooser(".");
int choice = jc.showOpenDialog(this.f);
if(choice == JFileChooser.APPROVE_OPTION){ //확인버튼을 눌렀다면
File file = jc.getSelectedFile();
this.tf.setText(file.getAbsolutePath());
}
}
}
import java.io.FileNotFoundException;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.JTextField;
public class LoadAction {
private JTextField tf;
private JTable table;
private Vector<Product> vector;
LoadAction(JTextField tf, JTable table, Vector<Product> vector) throws FileNotFoundException{
this.tf = tf;
this.table = table;
this.vector = vector;
setData();
}
private void setData() throws FileNotFoundException{
this.table.setModel(new ProductModel(this.tf, this.vector));
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Vector;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class ProductModel extends DefaultTableModel {
private JTextField tf;
private Vector<Product> vector;
ProductModel(JTextField tf, Vector<Product> vector) throws FileNotFoundException{
this.tf = tf;
this.vector = vector;
setData();
//assert (this.vector.size() == 7) : "Error"; //이런식으로 확인해봐도 되고.
setTable();
}
private void setTable(){
Vector<String> columeVector = new Vector<String>(1,1);
String [] array = {"상품명", "수량", "판매단가", "매입단가", "운송료"};
for(String str : array) columeVector.addElement(str);
Vector<Object> dataVector = new Vector<Object>(1,1);
for(int i = 0 ; i < this.vector.size() ; i++){
Vector<String> temp = new Vector<String>(1,1);
temp.addElement(this.vector.get(i).getName());
temp.addElement("" + this.vector.get(i).getSu()); //정수를 스트링으로 방법2
temp.addElement(String.valueOf(this.vector.get(i).getSaleprice())); //정수를 스트링으로 방법2
temp.addElement(Utility.toString(this.vector.get(i).getSu())); //Utility.java를 만들어 쉽게 포멧팅
temp.addElement(Utility.toString(this.vector.get(i).getFee()));
dataVector.addElement(temp);
}
this.setDataVector(dataVector, columeVector);
}
private void setData() throws FileNotFoundException{
File file = new File(this.tf.getText().trim());
Scanner scan = new Scanner(file);
String line = null;
try{
while((line = scan.nextLine()) != null){
String [] array = line.split("\\s+");
//String name, int su, int saleprice, int buyprice, int fee
Product p = new Product(array[0],
Integer.parseInt(array[1]), Integer.parseInt(array[2]),
Integer.parseInt(array[3]), Integer.parseInt(array[4]));
this.vector.addElement(p);
}
}catch(java.util.NoSuchElementException ex){} //그냥하면 한 줄이 더찍히기 때문에 이렇게 처리 해줌
}
}
public class Utility { //포메팅이 필요한 애들은 호출.
public static String toString(int value){
return String.format("%,d", value);
}
public static String toString(double value){
return String.format("%.2f", value);
}
}
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class CalcAction {
private JFrame f;
private JTable table;
private Vector<Product> vector;
public CalcAction(JFrame f, JTable table, Vector<Product> vector) {
this.f = f;
this.table = table;
this.vector = vector;
calc();
this.setTable();
}
private void setTable(){
Vector<String> columeVector = new Vector<String>(1,1);
String [] array = {"상품명", "수량", "판매단가", "매입단가", "운송료", "이익금", "이익율"};
for(String str : array) columeVector.addElement(str);
Vector<Object> dataVector = new Vector<Object>(1,1);
for(int i = 0 ; i < this.vector.size() ; i++){
Vector<String> temp = new Vector<String>(1,1);
temp.addElement(this.vector.get(i).getName());
temp.addElement("" + this.vector.get(i).getSu()); //정수를 스트링으로 방법2
temp.addElement(String.valueOf(this.vector.get(i).getSaleprice())); //정수를 스트링으로 방법2
temp.addElement(String.valueOf(this.vector.get(i).getBuyprice()));
temp.addElement(String.valueOf(this.vector.get(i).getFee()));
temp.addElement(String.valueOf(this.vector.get(i).getMoney()));
temp.addElement(String.valueOf(this.vector.get(i).getRate()));
temp.addElement(Utility.toString(this.vector.get(i).getMoney()));
temp.addElement(Utility.toString(this.vector.get(i).getRate()));
dataVector.addElement(temp);
}
//this.setDataVector(dataVector, columeVector); //디폴트 테이블모델에 자식이 아니기 때문에 setDataVector를 그대로 쓸 순 없다.
this.table.setModel(new DefaultTableModel(dataVector, columeVector));
}
private void calc(){
for(int i = 0 ; i < this.vector.size(); i++){
Product p = this.vector.get(i);
//판매금액
int salemoney = p.getSu() * p.getSaleprice();
//매입금액
int buymoney = p.getSu() * p.getBuyprice();
//이익금
int money = salemoney - (buymoney + p.getFee());
//이익율
double rate = (double)money / buymoney * 100;
p.setSalemoney(salemoney);
p.setBuymoney(buymoney);
p.setMoney(money);
p.setRate(rate);
}
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class SaveAction {
private JFrame f;
private Vector<Product> vector;
public SaveAction(JFrame f, Vector<Product> vector) {
this.f = f;
this.vector = vector;
save();
}
private void save(){
File file = null;
BufferedWriter bw = null;
JFileChooser jc = new JFileChooser(".");
int choice = jc.showSaveDialog(this.f);
if(choice == JFileChooser.APPROVE_OPTION){ //저장을 눌렀다면
file = jc.getSelectedFile();
try{
bw = new BufferedWriter(new FileWriter(file));
//BufferedOutputStream은 이미지 사운드 내보낼 때, BufferedWriter은 파일처리를 할 때 쓰임
printLabel(bw);
for(int i = 0 ; i < this.vector.size() ; i++){
bw.write(this.vector.get(i).toString() + "\n");
}
bw.flush();
JOptionPane.showMessageDialog(this.f, file.getAbsolutePath() + " 에 성공적으로 저장됐습니다", "Success",
JOptionPane.INFORMATION_MESSAGE);
}catch(java.io.IOException ex){
JOptionPane.showMessageDialog(this.f, ex.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}finally{
try{
bw.close();
}catch(java.io.IOException ex){}
}
}
}
private void printLabel(BufferedWriter bw) throws java.io.IOException{ //writer는 없으면 만들고 있으면 덮어쓴다.
bw.write(" <<상품별 판매 이익금 및 이익율표>>\n");
bw.write("=======================================================\n");
bw.write("상품명 수량 판매단가 매입단가 운송료 이익금 이익율\n");
bw.write("=======================================================\n");
bw.flush();
}
}