2011年4月19日
<Java> JVA403-護士排班表
重點:
1. 存讀檔
2. 即時取得ComboBox內容 並計算
============================
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class JVA403 extends Frame implements ItemListener
{
BufferedWriter bw = null;
BufferedReader br = null;
//護士名字
String name[] =new String[4];
String weekDay[] = {"週一","週二","週三","週四","週五"};
//Panel pn1 包含了 Panel pn1a 及 Panel pn1b
Panel pn1 = new Panel();
//pn1a 用來放排班表
Panel pn1a = new Panel();
Choice chAM[] = new Choice[5]; //週一至週五早班
Choice chPM[] = new Choice[5]; //週一至週五晚班
Choice chNigh[] = new Choice[5]; //週一至週五夜班
//pn1b 放的是每位護士的班次統計表
Panel pn1b = new Panel();
Label lname[] = new Label[4]; //顯示每位護士名字
int AM[] = new int[4]; //每一位護士早班次數
int PM[] = new int[4]; //每一位護士晚班次數
int Nigh[] = new int[4]; //每一位護士夜班次數
Label lbAM[] = new Label[4]; //顯示每一位護士早班次數
Label lbPM[] = new Label[4]; //顯示每一位護士晚班次數
Label lbNigh[] = new Label[4]; //顯示每一位護士夜班次數
//pn3 是最下方的按鈕區
Panel pn3 = new Panel();
Button b1 = new Button("輸出");
Button b2 = new Button("離開");
public JVA403()
{
super("急診科護士排班表");
addWindowListener(new WinListener());
}
public static void main(String[] args)
{
JVA403 jv04 = new JVA403();
jv04.setup();
jv04.setSize(400,300);
jv04.setVisible(true);
}
void setup()
{
//將護士的名單讀入
try{
BufferedReader br = new BufferedReader(new FileReader("JVA04-1.txt"));
String str = "";
int i = 0;
do{
str = br.readLine();
if(str == null)
break;
name[i] = str;
i++;
}while(true);
}catch(FileNotFoundException fe){
System.out.println("找不到檔案");
}catch(IOException ie){
System.out.println("IOException");
}catch(Exception ee){
}
//請您完成這部份
setpn1();
setpn3();
add(pn1);
add(pn3,BorderLayout.SOUTH);
}
//設定排班表內容
void setpn1()
{
pn1a.setLayout(new GridLayout(4,0));
pn1a.add(new Label(" "));
pn1a.add(new Label(weekDay[0]));
pn1a.add(new Label(weekDay[1]));
pn1a.add(new Label(weekDay[2]));
pn1a.add(new Label(weekDay[3]));
pn1a.add(new Label(weekDay[4]));
pn1a.add(new Label("早班"));
for(int h=0; h<chAM.length; h++)
{
chAM[h] = new Choice();
chPM[h] = new Choice();
chNigh[h] = new Choice(); //週一至週五夜班
//將名單放入下拉式選單
//請您完成這部份
for(int i = 0; i<name.length ; i++ ){
chAM[h].add(name[i]);
chPM[h].add(name[i]);
chNigh[h].add(name[i]);
}
chAM[h].addItemListener(this);
chPM[h].addItemListener(this);
chNigh[h].addItemListener(this);
}
pn1a.add(chAM[0]);
pn1a.add(chAM[1]);
pn1a.add(chAM[2]);
pn1a.add(chAM[3]);
pn1a.add(chAM[4]);
pn1a.add(new Label("晚班"));
pn1a.add(chPM[0]);
pn1a.add(chPM[1]);
pn1a.add(chPM[2]);
pn1a.add(chPM[3]);
pn1a.add(chPM[4]);
pn1a.add(new Label("夜班"));
pn1a.add(chNigh[0]);
pn1a.add(chNigh[1]);
pn1a.add(chNigh[2]);
pn1a.add(chNigh[3]);
pn1a.add(chNigh[4]);
//設定班別統計表
pn1b.setLayout(new GridLayout(4,4));
for(int a=0; a<lname.length; a++)
{
lname[a] = new Label(name[a]);
}
pn1b.add(new Label(" "));
pn1b.add(lname[0]);
pn1b.add(lname[1]);
pn1b.add(lname[2]);
pn1b.add(lname[3]);
pn1b.add(new Label("早班"));
lbAM[0] = new Label("");
lbAM[1] = new Label("");
lbAM[2] = new Label("");
lbAM[3] = new Label("");
pn1b.add(lbAM[0]);
pn1b.add(lbAM[1]);
pn1b.add(lbAM[2]);
pn1b.add(lbAM[3]);
pn1b.add(new Label("晚班"));
lbPM[0] = new Label("");
lbPM[1] = new Label("");
lbPM[2] = new Label("");
lbPM[3] = new Label("");
pn1b.add(lbPM[0]);
pn1b.add(lbPM[1]);
pn1b.add(lbPM[2]);
pn1b.add(lbPM[3]);
pn1b.add(new Label("夜班"));
lbNigh[0] = new Label("");
lbNigh[1] = new Label("");
lbNigh[2] = new Label("");
lbNigh[3] = new Label("");
pn1b.add(lbNigh[0]);
pn1b.add(lbNigh[1]);
pn1b.add(lbNigh[2]);
pn1b.add(lbNigh[3]);
pn1.setLayout(new GridLayout(0,1));
pn1.add(pn1a);
pn1.add(pn1b);
}
void setpn3()
{
b1.addActionListener(new act());
pn3.add(b1);
b2.addActionListener(new act());
pn3.add(b2);
}
//對Item的變動作出即時反應
public void itemStateChanged(ItemEvent e)
{
//先將資料清空
for(int i=0; i<AM.length; i++)
{
AM[i] =0;
PM[i] =0;
Nigh[i] = 0;
}
//計算每一位護士早班次數
//請您完成這部份
for(int i=0 ; i<chAM.length ; i++){
for(int j=0 ; j<name.length ; j++){
if(chAM[i].getSelectedItem().equals(name[j])){
AM[j]++;
}
}
}
//計算每一位護士晚班次數
//請您完成這部份
for(int i=0 ; i<chPM.length ; i++){
for(int j=0 ; j<name.length ; j++){
if(chPM[i].getSelectedItem().equals(name[j])){
PM[j]++;
}
}
}
//計算每一位護士夜班次數
//請您完成這部份
for(int i=0 ; i<chNigh.length ; i++){
for(int j=0 ; j<name.length ; j++){
if(chNigh[i].getSelectedItem().equals(name[j])){
Nigh[j]++;
}
}
}
//將資料填入畫面
for(int i=0; i<AM.length; i++)
{
lbAM[i].setText("" + AM[i]);
lbPM[i].setText("" + PM[i]);
lbNigh[i].setText("" + Nigh[i]);
}
}
//建立一個用來處理按鈕的 ActionListener inner class
class act implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b2)
{
System.exit(0);
}
else if(e.getSource() == b1)
{
StringBuffer sbAM = new StringBuffer("");
StringBuffer sbPM = new StringBuffer("");
StringBuffer sbNight = new StringBuffer("");
//請撰寫這部份,將排班資料寫入檔案
try
{
BufferedWriter bw = new BufferedWriter(new FileWriter("JVA04.txt"));
bw.write("值班通知書");
bw.newLine();
bw.newLine();
for(int i = 0 ; i<name.length ; i++){
sbAM.delete(0,sbAM.length());
sbPM.delete(0,sbPM.length());
sbNight.delete(0,sbNight.length());
for(int h = 0 ; h < chAM.length ; h++){
if(chAM[h].getSelectedItem().equals(name[i])){
sbAM.append(" "+weekDay[h]);
}
if(chPM[h].getSelectedItem().equals(name[i])){
sbPM.append(" "+weekDay[h]);
}
if(chNigh[h].getSelectedItem().equals(name[i])){
sbNight.append(" "+weekDay[h]);
}
}
bw.write(name[i]+":");
bw.newLine();
bw.newLine();
bw.write("早班:");
bw.write(" "+sbAM);
bw.newLine();
bw.write("晚班:");
bw.write(" "+sbPM);
bw.newLine();
bw.write("夜班:");
bw.write(" "+sbNight);
bw.newLine();
bw.newLine();
}
bw.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
/* 覆寫 WindowAdapter 中的 windowClosing()
讓關閉視窗的按鈕有作用
*/
class WinListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
e.getWindow().dispose();
System.exit(0);
}
}
}
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言