怎么用java+sql数据库做一个学生信息管理系统
1个回答
2015-12-01 · 知道合伙人数码行家
关注
展开全部
Student 类
public class Student
{
private int id;
private int age;
private int score;
private String name;
public Student()
{
}
public Student(int id, int age, int score, String name)
{
this.id = id;
this.age = age;
this.score = score;
this.name = name;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public int getScore()
{
return score;
}
public void setScore(int score)
{
this.score = score;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public String toString()
{
return "学号:" + id + " 姓名:" + name + " 年龄:" + age + " 成绩:" + score;
}
}
Manager类
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Manager
{
private List<Student> list;
public Manager(List<Student> list)
{
this.list = list;
}
public List<Student> getList()
{
return list;
}
public void setList(List<Student> list)
{
this.list = list;
}
//添加学生
public void add(Student s)
{
list.add(s);
}
//根据学生学号返回学生年龄
public int search(int id)
{
for(Iterator<Student> iter = list.iterator(); iter.hasNext();)
{
Student s = iter.next();
if(s.getId() == id)
{
return s.getAge();
}
}
return -1;
}
//删除学生
public void remove(int id)
{
for(Iterator<Student> iter = list.iterator(); iter.hasNext();)
{
Student s = iter.next();
if(s.getId() == id)
{
list.remove(s);
}
}
}
//计算总成绩
public int allScore()
{
int score = 0;
int temp = 0;
for(Iterator<Student> iter = list.iterator(); iter.hasNext();)
{
Student s = iter.next();
temp = s.getScore();
score += temp;
}
return score;
}
//修改成绩
public void update(int id)
{
for(Iterator<Student> iter = list.iterator(); iter.hasNext();)
{
Student s = iter.next();
if(s.getId() == id)
{
s.setScore(s.getScore() + 10);
}
}
}
}
测试类 Client
import java.util.ArrayList;
import java.util.List;
public class Client
{
public static void main(String[] args)
{
List<Student> list = new ArrayList<Student>();
Manager manager = new Manager(list);//创建一个管理者
Student s1 = new Student();//无参构造方法创建的学生实例
//通过方法设置s1的属性
s1.setId(201105);
s1.setAge(20);
s1.setScore(100);
s1.setName("zhangsan");
Student s2 = new Student(201101,21,98,"lisi");//通过带参数的构造方法创建实例
Student s3 = new Student(201108,25,95,"zhaoliu");
Student s4 = new Student(201110,23,80,"xiaoming");
Student s5 = new Student(201106,28,78,"hello");
//放到集合当中
manager.getList().add(s1);//添加学生
manager.getList().add(s2);
manager.getList().add(s3);
manager.getList().add(s4);
manager.getList().add(s5);
System.out.println(list);
System.out.println(manager.allScore());
System.out.println(manager.search(201110));//根据学生学号查询学生年龄
manager.remove(201110);//删除学生
manager.update(201101);//修改成绩
}
}
可以完成你上述的基本要求,如果改动可以自行修改 很简单。
public class Student
{
private int id;
private int age;
private int score;
private String name;
public Student()
{
}
public Student(int id, int age, int score, String name)
{
this.id = id;
this.age = age;
this.score = score;
this.name = name;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public int getScore()
{
return score;
}
public void setScore(int score)
{
this.score = score;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public String toString()
{
return "学号:" + id + " 姓名:" + name + " 年龄:" + age + " 成绩:" + score;
}
}
Manager类
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Manager
{
private List<Student> list;
public Manager(List<Student> list)
{
this.list = list;
}
public List<Student> getList()
{
return list;
}
public void setList(List<Student> list)
{
this.list = list;
}
//添加学生
public void add(Student s)
{
list.add(s);
}
//根据学生学号返回学生年龄
public int search(int id)
{
for(Iterator<Student> iter = list.iterator(); iter.hasNext();)
{
Student s = iter.next();
if(s.getId() == id)
{
return s.getAge();
}
}
return -1;
}
//删除学生
public void remove(int id)
{
for(Iterator<Student> iter = list.iterator(); iter.hasNext();)
{
Student s = iter.next();
if(s.getId() == id)
{
list.remove(s);
}
}
}
//计算总成绩
public int allScore()
{
int score = 0;
int temp = 0;
for(Iterator<Student> iter = list.iterator(); iter.hasNext();)
{
Student s = iter.next();
temp = s.getScore();
score += temp;
}
return score;
}
//修改成绩
public void update(int id)
{
for(Iterator<Student> iter = list.iterator(); iter.hasNext();)
{
Student s = iter.next();
if(s.getId() == id)
{
s.setScore(s.getScore() + 10);
}
}
}
}
测试类 Client
import java.util.ArrayList;
import java.util.List;
public class Client
{
public static void main(String[] args)
{
List<Student> list = new ArrayList<Student>();
Manager manager = new Manager(list);//创建一个管理者
Student s1 = new Student();//无参构造方法创建的学生实例
//通过方法设置s1的属性
s1.setId(201105);
s1.setAge(20);
s1.setScore(100);
s1.setName("zhangsan");
Student s2 = new Student(201101,21,98,"lisi");//通过带参数的构造方法创建实例
Student s3 = new Student(201108,25,95,"zhaoliu");
Student s4 = new Student(201110,23,80,"xiaoming");
Student s5 = new Student(201106,28,78,"hello");
//放到集合当中
manager.getList().add(s1);//添加学生
manager.getList().add(s2);
manager.getList().add(s3);
manager.getList().add(s4);
manager.getList().add(s5);
System.out.println(list);
System.out.println(manager.allScore());
System.out.println(manager.search(201110));//根据学生学号查询学生年龄
manager.remove(201110);//删除学生
manager.update(201101);//修改成绩
}
}
可以完成你上述的基本要求,如果改动可以自行修改 很简单。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询