package JavaWebDemo.java.student;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentManager {
static List<Student> list=new ArrayList<>();
StudentDao dao=new StudentDao();
/**
//main函数是JavaSE入口函数
public static void main(String[] args) {
//sysout.out 输出信息必须要有的
//System.out.println("Hello Word");
//简单的获取到student累的对象
//Student 变量类型
//student 学生的对象
// = 是赋值语法创建了2个对象
//new Student()构造函数
Student student=new Student();
System.out.println("请输入学生的年龄: ");
Scanner sc=new Scanner(System.in);//获取的是一个输入流的对象
int age=sc.nextInt();
student.setAge(age);
//student.setSex("男");
System.out.println("学生student的年龄"+student.getAge()+"岁~");
//System.out.println("学生student的性别是"+student.getSex()+"生");
}
**/
public static void main(String[] args) {
//在static修士的方法A中调用非static修士的方法B
//1.先获取方法B所在类的对象(实例),在通过对象调方法B
//2.直接在方法B中添加static关键字
StudentManager manager=new StudentManager();
manager.studentMain();
//studentMain();
}
/** 每个方法只做一件事 **/
public void studentMain(){
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("请输入:");
boolean flag=true;
Scanner sc=new Scanner(System.in);
StudentManager student=new StudentManager();
while(flag){
int actionIndex=sc.nextInt();
switch(actionIndex){
case 1:
System.out.println("查询结果:");
//查询的时候从list查询
/**
Iterator<String> iterator=list.iterator();
int i=0;
while(iterator.hasNext()){
String str=iterator.next();
i++;
System.out.println(str);
}
**/
student.query();
break;
case 2:
System.out.println("新增");
//System.out.println("请输入要新增学生的信息:");
//新增就是向全局变量list添加元素
//String in=sc.next();
/**
list.add(0,"1001,王明,21,男, 1班");
list.add(1,"1002,丽丽,20,女, 1班");
list.add(2,"1003,小华,20,女, 2班");
list.add(3,"1004,小刚,22,男, 2班");
**/
//list.add(in);
//System.out.println("新增结果:");
student.insert();
break;
case 3:
System.out.println("修改");
student.update();
break;
case 4:
System.out.println("删除");
// String queryNo=sc.next();
student.delete();
break;
}
}
}
public void insert(){
Scanner scanner=new Scanner(System.in);
System.out.println("请输入要新增学生的信息:");
System.out.println("请输入学生学号:");
String stuNo=scanner.next();
System.out.println("请输入学生姓名:");
String stuName=scanner.next();
System.out.println("请输入学生班级:");
int classId=scanner.nextInt();
int id=1;
Student stu=new Student(stuNo, stuName, classId);
// stu.setStuNo(stuNo);//变量命名的一致性(连续性)
// stu.setStuName(stuName);
//stu.setClassId(classId);
// list.add(stu);
int result=dao.insert(stu);
if(result>0){
System.out.println("新增成功");
}
else{
System.out.println("操作失败");
}
List<Student> studentlist=new ArrayList<>();
System.out.println("学号 学生姓名 所在班级ID");
studentlist= dao.query("");
for (Student student : studentlist) {
System.out.println("学号:"+student.getStuNo()+" 姓名:"+student.getStuName()
+" 年龄:"+student.getAge()+" 性别:"+student.getSex()+" 班级ID:"+student.getClassId());
}
//// //list是要遍历的集合,Student是集合中的类型,s是保存遍历元素的临时变量
// for(Student s:list){ //foreach
// System.out.println(s.getStuNo()+" "
// +s.getStuName()+" "+s.getClassId());
//
// }
StudentManager manager=new StudentManager();
manager.studentMain();
}
public void query(){
System.out.println("是否待条件查询(Y:待条件查询,N:全部查询):");
Scanner sc=new Scanner(System.in);
String flag=sc.next();
List<Student> studentlist=new ArrayList<>();
if("Y".equals(flag)){
//进行模糊查询
System.out.println("请输入学生的学号:");
String queryData=sc.next();
studentlist=dao.query(queryData);
}else{
studentlist=dao.query("");
}
for (Student student : studentlist) {
System.out.println("学号:"+student.getStuNo()+" 姓名:"+student.getStuName()
+" 年龄:"+student.getAge()+" 性别:"+student.getSex()+" 班级ID:"+student.getClassId());
}
System.out.println("查询成功");
StudentManager manager = new StudentManager();
manager.studentMain();
}
public void update(){
System.out.println("请输入要修改学生学号:");
Scanner sc=new Scanner(System.in);
String stuNo=sc.next();
List<Student> studentlist=new ArrayList<>();
studentlist=dao.query(stuNo);
for (Student student : studentlist) {
System.out.println("学号:"+student.getStuNo()+" 姓名:"+student.getStuName()
+" 年龄:"+student.getAge()+" 性别:"+student.getSex()+" 班级ID:"+student.getClassId());
}
// String queryNo=sc.next();
// Student student =null;
// for(Student s:list){
// if(queryNo.equals(s.getStuNo())){
// student=s;
// // break;
// }
// if(student!=null){
// System.out.println("学号 学生姓名 所在班级ID");
// System.out.println(student.getStuNo()+" "
// +student.getStuName()+" "+student.getClassId());
// System.out.println("请输入想要修改学生学号:");
// String stuNo=sc.next();
System.out.println("请输入想要修改学生姓名:");
String stuName=sc.next();
System.out.println("请输入想要修改学生班级:");
int classId=sc.nextInt();
int id =1;
Student stu=new Student(stuNo,stuName, classId);
StudentDao dao=new StudentDao();
int result= dao.update(stu);
if(result>0){
System.out.println("修改成功");
studentlist=dao.query("");
for (Student student : studentlist) {
System.out.println("学号:"+student.getStuNo()+" 姓名:"+student.getStuName()
+" 年龄:"+student.getAge()+" 性别:"+student.getSex()+" 班级ID:"+student.getClassId());
}
}
else{
System.out.println("操作失败");
}
//
// int count=-1;
// for(Student s1:list){
// count++;
// if(queryNo.equals(s1.getStuNo())){
// s1=stu;
// list.set(count, stu);
// System.out.println(s1.getStuNo()+" "
// +s1.getStuName()+" "+s1.getClassId());
//
// break;
// }
// }
StudentManager manager=new StudentManager();
manager.studentMain();
}
//}
//}
public void delete(){
System.out.println("请输入要删除的学生学号:");
Scanner sc=new Scanner(System.in);
List<Student> studentlist=new ArrayList<>();
String queryNo=sc.next();
String stuNo=queryNo;
StudentDao dao=new StudentDao();
int result= dao.delete(stuNo);
if(result>0){
System.out.println("删除成功");
studentlist=dao.query