软工课程录入文件
软工课程录入文件
一,题目要求:
(1)新课程信息必须唯一,如有重复,提示用户“课程名称重复,重新录入”;
(2)要求判断任课教师为王建民、刘立嘉、刘丹、王辉、杨子光五位教师的其中一位。
(3)要求上课地点开头为“一教、二教、三教、基教”中的一种;
(4)将新课程信息添加入库。
二,代码
//信1805-1王正帅20183544
package jxlPacakge;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
public class t1 extends JFrame implements ActionListener {
public static String pp[][] = new String[1000][3];
private JLabel claadd=new JLabel("上课地点");
private JButton jbD=new JButton("添加");
private JButton jbR=new JButton("重置");
private JLabel jlY=new JLabel("课程名称");
private JLabel jlMB=new JLabel("上课老师");
private JTextField jtxtName =new JTextField();
private JTextField jtxt =new JTextField();
private JTextField jtxttea= new JTextField();
private JPanel jp=new JPanel();//面板
private int f2=0;
private static int sum=0;
public void name() {
jlY.setBounds(20, 10, 80, 26);
jp.add(jlY);
}
public void teacher() {
jlMB.setBounds(20, 60, 80, 26);
jp.add(jlMB);
}
public void add() {
jbD.setBounds(60, 180, 90,26);
jp.add(jbD);
jbR.setBounds(170, 180, 90,26);
jp.add(jbR);
jbR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Empty();
}
});
jbD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int f=0;
int f5=0;
char ch [] = new char[2];
String n=jtxtName.getText();String p=jtxttea.getText();String d=jtxt.getText();String t = "";
if(d.length()>=2) {
ch[0]=d.charAt(0);
ch[1]=d.charAt(1);
t = new String (ch);
}
for(int i=0;i<sum;i++) {
if(n.equals(pp[i][0])) {
f5=1;
JOptionPane.showMessageDialog(null,"课程重复");
}
}
if(f5==1) {}
else {f++;}
if(p.equals("王建民")||p.equals("刘立嘉")||p.equals("刘丹")||p.equals("王辉")||p.equals("杨子光")) {f++;}
else {
JOptionPane.showMessageDialog(null,"没有这个教师");
}
if(t.equals("基教")||t.equals("一教")||t.equals("二教")||t.equals("三教")) {f++;}
else {
JOptionPane.showMessageDialog(null,"上课的地址错误");
}
if(f==3) {
pp[sum][0]=n;pp[sum][1]=p;pp[sum][2]=d;
sum++;
try {
writeFile ();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null,"添加成功");
}
}
});
}
public void RegistrationE() {
jbD.setVisible(false);
this.setBounds(1000, 200, 300, 250);//设置界面出现在屏幕上的位置
}
public t1(){
jp.setLayout(null); //满换行
jp.setBackground(Color.ORANGE);
claadd.setBounds(20, 110, 80, 26);//设置文字30据左,120上下
jp.add(claadd);//显示
teacher();name();
add();
jtxtName.setBounds(80,10,180,30);//设置文本框80左右10上下(位置)180长30宽
jp.add(jtxtName);
jtxtName.addActionListener(this);
jtxt.setBounds(80,110,180,30);
jp.add(jtxt);
jtxt.addActionListener(this);
jtxttea.setBounds(80,60,180,30);
jp.add(jtxttea);
jtxttea.addActionListener(this);
this.add(jp);
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.setResizable(false);
this.setBounds(800, 200, 300, 250);//设置界面出现在屏幕上的位置
this.setVisible(true);
}
public void Empty() {
jtxt.setText("");
jtxtName.setText("");
jtxttea.setText("");
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jtxtName){
jtxttea.requestFocus();
}
}
public static void writeFile () throws IOException {
FileWriter fw_o=new FileWriter("D:\\JAVA\\java 书\\1.txt");
BufferedWriter bw=new BufferedWriter(fw_o);
String s=new String();
int i=0;
for(i=0;i<sum;i++) {
System.out.println((i+1)+": "+pp[i][0]+" "+pp[i][1]+" "+pp[i][2]);
bw.write((i+1)+": "+pp[i][0]+" "+pp[i][1]+" "+pp[i][2]);
bw.newLine();
}
bw.close();
}
public static void main(String[] args) {
t1 l1=new t1();
l1.setTitle("课程添加");
}
}
三,运行测试:
1,正确情况:
2,错误情况: