学生管理系统练习

来源:互联网 发布:泛雅网络教学平台app 编辑:程序博客网 时间:2024/06/16 06:47
import javax.swing.JOptionPane;


public class Study {
static Student [] Array =new Student[20];
public static int number = 0;
public static String user = "xxy686172";
public static String passwd = "123456";


public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "欢迎光临");
boolean isLand = login();
if (isLand == false) {
JOptionPane.showMessageDialog(null, "非法用户");
System.exit(0);
}


while (true) {
String str = JOptionPane.showInputDialog(null,
"1、添加\n2、显示\n3、删除\n4、查找\n5、修改\n6、排序\n7、退出");
int item = Integer.parseInt(str);
switch (item) {
case 1:
add();
break;
case 2:
show();
break;
case 3:
delete();
break;
case 4:
find();
break;
case 5:
change();
break;
case 6:
sort();
break;
case 7:
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "请输入命令1--7");
}
}


}


// 登录
public static boolean login() {
for (int i = 0; i < 3; i++) {
String userName = JOptionPane.showInputDialog(null, "请输入用户名:");
String pwd = JOptionPane.showInputDialog(null, "请输入密码:");
if (user.equals(userName) && passwd.equals(pwd)) {
return true;
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
}
return false;
}


// 添加
public static void add() {
String codeStr = JOptionPane.showInputDialog(null, "请输入学号");
String nameStr = JOptionPane.showInputDialog(null, "请输入姓名");
String gradeStr = JOptionPane.showInputDialog(null, "请输入成绩");
Student s =new Student();
s.code = Integer.parseInt(codeStr);
s.name = nameStr;
s.grade = Integer.parseInt(gradeStr);
Array[number]=s;
number++;
show();
}


// 显示
public static void show() {
String info = "学号     姓名     成绩   \n\t";
for (int i = 0; i < number; i++) {
info += Array[i].code + "   \t  " + Array[i].name + "  \t    "
+ Array[i].grade + "\n";
}
JOptionPane.showMessageDialog(null, info);
}


// 删除
public static void delete() {


int index = findname();
if (index != -1) {
for (int i = index; i < number; i++) {
Array[i] = Array[i + 1];
number--;
show();
}
}
}


// 查找
public static void find() {
int index = findname();
if (index != -1) {
JOptionPane.showMessageDialog(null, "学号     姓名     成绩     \n"
+ Array[index].code + "     " + Array[index].name + "     "
+ Array[index].grade + "\t");
}
}


// 修改
public static void change() {


int index = findname();
if (index != -1) {
String changeCode = JOptionPane.showInputDialog(null, "请输入学生的学号");
String changeName = JOptionPane.showInputDialog(null, "请输入学生的姓名");
String changeGrade = JOptionPane.showInputDialog(null, "请输入学生的成绩");
Array[index].name = changeName;
Array[index].code = Integer.parseInt(changeCode);
Array[index].code = Integer.parseInt(changeGrade);
show();
}


}


// 排序
public static void sort(){
for(int i=0;i<number;i++){
for(int j =i+1;j<number;j++)
if(Array[i].grade<Array[j].grade){

Student Temp=Array[i];
Array[i]=Array[j];
Array[j]=Temp;
}
}show();
}



// 查找方法
public static int findname() {
String name = JOptionPane.showInputDialog(null, "请输入你要找的学生的姓名");
for (int i = 0; i < number; i++) {
if (name.equals(Array[i].name)) {
return i;
}
}
JOptionPane.showMessageDialog(null, "查无此人");
return -1;


}
}
0 0
原创粉丝点击