1:首先得拥有一个Eclipse适配环境与软件。
2:如何实现学生系统的增·添·改·查?
3:最重要的是如何简单快速上手?
4:期末好帮手?
话不多说,直接开整!
第一步·需要建立一个·Manager管理系统(单独一个class类):
public class manager {
public String username ="******"(用户名)
public String password = "*******"(用户密码)
}
第二步:需要定义主类Student(单独一个类):只有定义了,主方法才能使用!
public class Student {
private String sid;
private String name;
private String age;
private String address;
public Student(){}
public Student(String sid,String name,String age,String address){
this.sid=sid;
this.name=name;
this.age=age;
this.address=address;
}
public String getSid(){
return sid;
}
public void setSid(String sid){
this.sid=sid;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getAge(){
return age;
}
public void setAge(String age){
this.age=age;
}
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address=address;
}
}
第三步:需要建立一个测试类StudentManager(注意主方法在这里呦,也就是用这一步去运行!):
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManager {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList <Student> array=new ArrayList<Student>();
System.out.println("--------欢迎来到学生管理系统安全界面-----------");
Scanner in=new Scanner(System.in);
System.out.println("请输入您的用户名:");
String paw=in.nextLine();
System.out.println("请输入您的用户密码:");
String mi=in.nextLine();
manager man=new manager();
if(paw.equals(man.username)&&mi.equals(man.password)){
while(true){
System.out.println("--------欢迎来到学生管理系统-----------");
System.out.println("****************************************");
System.out.println("1.添加学生");
System.out.println("2.删除学生");
System.out.println("3.修改学生");
System.out.println("4.查看学生");
System.out.println("5.退出");
System.out.println("*****************************************");
System.out.println("请输入你的选择");
Scanner scanner=new Scanner(System.in);
int line=scanner.nextInt();
switch(line){
case 1:
addStudent(array);
break;
case 2:
deleteStudent(array);
break;
case 3:
updataStudent(array);
break;
case 4:
findAllStudent(array);
break;
case 5:
System.out.println("谢谢使用!");
System.exit(0);
}
}
}
else{
System.out.println("您输入的信息有误,请重新输入");
}
}
public static void addStudent(ArrayList <Student> array){
Scanner sc=new Scanner(System.in);
String sid;
while(true){
System.out.println("请输入学生学号:");
sid=sc.nextLine();
boolean flag=isUsed(array,sid);
if(flag){
System.out.println("你输入的学号已经被使用,请重新输入");
}else{
break;
}
}
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
String age = sc.nextLine();
System.out.println("请输入学生居住地:");
String address = sc.nextLine();
Student s = new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
array.add(s);
System.out.println("添加学生成功");
}
public static boolean isUsed(ArrayList<Student> array, String sid) {
boolean flag = false;
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
if (s.getSid().equals(sid)) {
flag = true;
break;
}
}
return flag;
}
public static void findAllStudent(ArrayList<Student> array) {
if (array.size() == 0) {
System.out.println("无信息,请先添加信息再查询");
return;
}
System.out.println("学号\t\t\t姓名\t\t年龄\t\t居住地");
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getSid() + "\t\t" + s.getName() + "\t\t" + s.getAge() + "岁\t\t" + s.getAddress());
}
}
public static void deleteStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的学生的学号");
String sid = sc.nextLine();
int index = -1;
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
if (s.getSid().equals(sid)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("该学号不存在,请重新输入");
} else {
array.remove(index);
System.out.println("删除学生成功");
}
}
public static void updataStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的学生的学号");
String sid = sc.nextLine();
int index = -1;
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
if (s.getSid().equals(sid)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("该学号不存在,请重新输入");
} else {
for (int j = 0; j < array.size(); j++) {
Student student = array.get(j);
if (student.getSid().equals(sid)) {
System.out.println("请输入学生的新姓名");
String name = sc.nextLine();
System.out.println("请输入学生的新年龄");
String age = sc.nextLine();
System.out.println("请输入学生的新居住地");
String address = sc.nextLine();
Student s = new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
array.set(j, s);
System.out.println("修改学生成功");
break;
}
}
}
}
}
在这里我多做一个登录界面!使系统更加好看美观!