- Subject
public class Subject{
private String name;//学科名称
private String number;
public Subject() {}
public Subject(String name, String number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
2.Student
import java.util.Scanner;
public class Student {
private String name;//姓名
private String ID;//学号
private double grade;//成绩
private Subject subject;//学科(学科内容、学科编号)
Scanner sc = new Scanner(System.in);
public Student() {}
public Student(String ID, String name, double grade, Subject subject) {
this.ID = ID;
this.name = name;
this.grade = grade;
this.subject = subject;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
public Scanner getSc() {
return sc;
}
public void setSc(Scanner sc) {
this.sc = sc;
}
//输入学生信息方法
public void scc() {
System.out.print("请输入学生的姓名:");
String name = sc.next();
System.out.print("请输入学生的学号:");
String ID = sc.next();
System.out.print("请输入学生的成绩:");
double grade = sc.nextInt();
System.out.print("请输入学生的学科:");
String subject = sc.next();
System.out.print("请输入学生的学科编号:");
String number = sc.next();
new Student(ID, name, grade, new Subject(subject, number));
System.out.println("添加成功!!!!!");
}
}
3.Main
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
List<Student> list = new ArrayList();
// list.add(scc());
System.out.println(list.get(0).getID());
}
//界面函数
public void infa() {
System.out.println("欢迎光临学生信息管理系统");
System.out.println("请选择功能");
System.out.println("1、添加学生信息");
System.out.println("2、获得指定学生成绩");
System.out.println("3、获得指定学号的成绩");
System.out.println("4、根据学生ID修改姓名");
System.out.println("5、根据姓名修改指定学科成绩");
System.out.println("6、删除指定学生及成绩");
System.out.println("0、退出");
}
//创建学生对象
public Student create() {
System.out.print("请输入学生的姓名:");
String name = sc.next();
System.out.print("请输入学生的学号:");
String ID = sc.next();
System.out.print("请输入学生的成绩:");
double grade = sc.nextInt();
System.out.print("请输入学生的学科:");
String subject = sc.next();
System.out.print("请输入学生的学科编号:");
String number = sc.next();
Student s = new Student(ID, name, grade, new Subject(subject, number));
System.out.println("添加成功!!!!!");
return s;
}
//获得指定学生成绩
public void println(List<Student> list) {
System.out.print("请输入您要查询的学生姓名为:");
String name = sc.next();
for (int i = 0; i < list.size(); i++) {
if ((list.get(i).getName().equals(name))) {
System.out.println("您查找的学生" + name + "的成绩为:" + list.get(i).getGrade() );
}else {
if (i == list.size() - 1) {//如果最后一次还没有找学生则输出学生不存在
System.out.println("对不起,您要查找的学生不存在");
}
}
}
}
//获得指定学号的成绩
public void numberGrade(List<Student> list) {
System.out.print("请输入您要查询的学生学号为:");
String ID = sc.next();
for (int i = 0; i < list.size(); i++) {
if ((list.get(i).getID().equals(ID))) {
System.out.println("您查找学号为" + ID + "的成绩为:" + list.get(i).getGrade() );
break;
}else {
if (i == list.size() - 1) {//如果最后一次还没有找学生则输出学生不存在
System.out.println("对不起,您要查找的学生不存在");
}
}
}
}
//根据学生ID修改姓名
public void updataName(List<Student> list) {
System.out.print("请输入您要修改姓名的学生ID为:");
String ID = sc.next();
for (int i = 0; i < list.size(); i++) {
if ((list.get(i).getID().equals(ID))) {
System.out.println("请输入要修改的姓名");
String name = sc.next();
list.get(i).setName(name);
list.set(i, new Student( list.get(i).getID(), list.get(i).getName(), list.get(i).getGrade(), list.get(i).getSubject()));
System.out.println("ID为" + ID + "现在姓名修改为:" + list.get(i).getName() );
break;
}else {
if (i == list.size() - 1) {//如果最后一次还没有找学生则输出学生不存在
System.out.println("对不起,您要查找的学生不存在");
}
}
}
}
//根据姓名修改指定学科成绩
public void updataGrade(List<Student> list) {
System.out.print("请输入您要修改成绩的学生的姓名:");
String name = sc.next();
for (int i = 0; i < list.size(); i++) {
if ((list.get(i).getID().equals(name))) {
System.out.println("请输入要修改的姓名");
double grade = sc.nextInt();
list.get(i).setGrade(grade);
list.set(i, new Student( list.get(i).getID(), list.get(i).getName(), list.get(i).getGrade(), list.get(i).getSubject()));
System.out.println("姓名为" + name + "现在成绩修改为:" + list.get(i).getGrade() );
break;
}else {
if (i == list.size() - 1) {//如果最后一次还没有找学生则输出学生不存在
System.out.println("对不起,您要查找的学生不存在");
}
}
}
}
//删除指定学生及成绩
public void delect(List<Student> list) {
System.out.print("请输入您要删除学生的姓名:");
String name = sc.next();
for (int i = 0; i < list.size(); i++) {
if ((list.get(i).getName().equals(name))) {
list.remove(list.get(i));
System.out.println("这个学生现在被你删除的连渣都不剩了,开心吧!!");
}
}
}
}
4.主方法
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Text {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
List<Student> list = new ArrayList();
Main main = new Main();
int select = 1;
int count = 0;
while (select != 0) {
main.infa();
select = sc.nextInt();//功能选择
switch (select) {
case 1: list.add(main.create());
count++;
System.out.println("已添加学生人数为:" + count );
break;
case 2: main.println(list);
break;
case 3: main.numberGrade(list);
break;
case 4: main.updataName(list);
break;
case 5: main.updataGrade(list);
break;
case 6: main.delect(list);
break;
default:
break;
}
}
}
}
运行截图
在JDBC分类中有连接数据库的的demo