1. 程式人生 > >java面向物件高階分層例項_測試類(main方法所在的類)

java面向物件高階分層例項_測試類(main方法所在的類)

package bdqn.studentSys;
/**
 * main類
 * @author Administrator
 *
 */
import java.util.*;

import bdqn.studentSys.Dao.StudentDao;
import bdqn.studentSys.Dao.impl.StudentDaoImpl;
import bdqn.studentSys.entity.Student;
public class StudentSys {
	public static void main(String []args){
		Scanner in=new Scanner(System.in);
		//查詢全部學生
		getAllStudent();
		
		//新增
		System.out.println("==============新增學生資訊==============");
		System.out.println("請輸入學生姓名:");
		String name=in.next();
		System.out.println("請輸入學生密碼:");
		String pwd=in.next();
		System.out.println("請輸入學生年齡:");
		int age=in.nextInt();
		System.out.println("請輸入學生性別:");
		String sex=in.next();
		Student stu=new Student();
		stu.setName(name);
		stu.setPwd(pwd);
		stu.setAge(age);
		stu.setSex(sex);
		addStudent(stu);
		
		//修改
		System.out.println("==============修改學生資訊==============");
		System.out.println("請輸入要修改的學生學號:");
		int no=in.nextInt();
		System.out.println("請輸入學生姓名:");
		String name1=in.next();
		System.out.println("請輸入學生密碼:");
		String pwd1=in.next();
		System.out.println("請輸入學生年齡:");
		int age1=in.nextInt();
		System.out.println("請輸入學生性別:");
		String sex1=in.next();
		Student stu1=new Student();
		stu.setName(name1);
		stu.setPwd(pwd1);
		stu.setAge(age1);
		stu.setSex(sex1);
		updateStudent(stu1);
		
		//刪除
		System.out.println("==============刪除學生資訊==============");
		System.out.println("請輸入要刪除的學生學號:");
		int no1=in.nextInt();
		delStudent(no1);
	}
	static StudentDao sdao=new StudentDaoImpl();
	//查詢全部學生
	static void getAllStudent(){
		List<Student> slist=sdao.getAllStudent();
		System.out.println("姓名\t\t密碼\t\t年齡\t\t性別");
		for (Student stu : slist) {
			System.out.print(stu.getName()+"\t\t");
			System.out.print(stu.getPwd()+"\t\t");
			System.out.print(stu.getAge()+"\t\t");
			System.out.println(stu.getSex()+"\t\t");
		}
	}
	
	//新增學生資訊
	static void addStudent(Student stu){
		int rel=sdao.addStudent(stu);
		try {
			if(rel>0){
				System.out.println("新增成功!");
			}else{
				System.out.println("新增失敗!");
			}
		} catch (Exception e) {
			System.out.println("操作異常!"+e);
		}
	}
	//修改學生資訊
	static void updateStudent(Student stu){
		int rel=sdao.UpdateStudent(stu);
		try {
			if(rel>0){
				System.out.println("修改成功!");
			}else{
				System.out.println("新增失敗!");
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("操作異常"+e);
		}
	}
	//刪除學生資訊
	static void delStudent(int stuno){
		int rel=sdao.delStudent(stuno);
		try {
			if(rel>0){
				System.out.println("刪除成功!");
			}else{
				System.out.println("刪除失敗");
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("操作異常"+e);
		}
	}
}