当前位置:Gxlcms > 数据库问题 > 第二次数据库作业--view

第二次数据库作业--view

时间:2021-07-01 10:21:17 帮助过:3人阅读

view; import action.C2SAction; import action.CourseAction; import action.StudentAction; import model.C2S; import model.Course; import model.Student; import java.sql.SQLException; import java.util.List; import java.util.Scanner; import java.util.regex.Pattern; /** * Created by yinus * Date:2016/4/6 * Time:19:45 */ public class C2SView { private static final String OPERATE_ADD = "ADD"; private static final String OPERATE_EXIT = "EXIT"; private static final String OPERATE_UPDATE = "UPDATE"; private static final String OPERATE_QUERY = "QUERY"; private static final String OPERATE_DELETE = "DELETE"; private static final String PROMPT="COURSE2STUDENT[A/D/U/Q/E]>"; private static final String Menu =""+ "[DELETE]DELETE COURSE\n"+ "[UPDATE]UPDATE COURSE\n"+ "[QUERY]QUERY COURSE\n"+ "[EXIT]EXIT\n"+ "IGNORE CASE & MAY USE THE FIRST LETTER TO OPERATE\n" + PROMPT; public static void view() throws SQLException { System.out.print(Menu); Scanner in = new Scanner(System.in); while (in.hasNext()){ String keyWord = in.next().toUpperCase(); if (keyWord.equals(OPERATE_EXIT) || keyWord.substring(0, 1).equals(OPERATE_EXIT.substring(0,1))) { break; } else if (keyWord.equals(OPERATE_QUERY) || keyWord.substring(0, 1).equals(OPERATE_QUERY.substring(0,1))) { System.out.println("using S+id+C+id to query a specific course or only using C+number,S+number to query courses"); System.out.println("for example ,S2C2 reference the student whose id is 2 and the course id is 2."); String str = in.next(); if (Pattern.matches("^[sS][0-9]+[cC][0-9]+$",str)){ String[] ss = str.split("[sScC]"); C2SAction ca = new C2SAction(); C2S c = ca.query(Integer.parseInt(ss[1]), Integer.parseInt(ss[2])); System.out.println(c); } else if (Pattern.matches("^[cC][0-9]+[sS][0-9]+$",str)){ String[] ss = str.split("[sScC]"); C2SAction ca = new C2SAction(); C2S c = ca.query(Integer.parseInt(ss[2]), Integer.parseInt(ss[1])); System.out.println(c); } else if(Pattern.matches("^[sS][0-9]+$",str)){ C2SAction ca = new C2SAction(); List<C2S> cs = ca.querys(Integer.parseInt(str.substring(1)), true); cs.forEach(System.out::println); } else if (Pattern.matches("^[cC][0-9]+$",str)){ C2SAction ca = new C2SAction(); List<C2S> cs = ca.querys(Integer.parseInt(str.substring(1)), false); cs.forEach(System.out::println); } System.out.print(PROMPT); } else if (keyWord.equals(OPERATE_DELETE)||keyWord.substring(0,1).equals(OPERATE_DELETE.substring(0,1))){ System.out.println("using S+id+C+id to delete a specific course or only using C+number,S+number to delete courses"); System.out.println("for example ,S2C2 reference the student whose id is 2 and the course id is 2."); String str = in.next(); if (Pattern.matches("^[sS][0-9]+[cC][0-9]+$",str)){ String[] ss = str.split("[sScC]"); C2SAction ca = new C2SAction(); ca.delete(Integer.parseInt(ss[1]),Integer.parseInt(ss[2])); System.out.println("DELETED SUCCESSFULLY"); } else if (Pattern.matches("^[cC][0-9]+[sS][0-9]+$",str)){ String[] ss = str.split("[sScC]"); C2SAction ca = new C2SAction(); ca.delete(Integer.parseInt(ss[2]), Integer.parseInt(ss[1])); System.out.println("DELETED SUCCESSFULLY"); } else if(Pattern.matches("^[sS][0-9]+$",str)){ C2SAction ca = new C2SAction(); ca.delete(Integer.parseInt(str.substring(1)), true); System.out.println("DELETED SUCCESSFULLY"); } else if (Pattern.matches("^[cC][0-9]+$",str)){ C2SAction ca = new C2SAction(); ca.delete(Integer.parseInt(str.substring(1)), false); System.out.println("DELETED SUCCESSFULLY"); } System.out.print(PROMPT); } else if (keyWord.equals(OPERATE_UPDATE)||keyWord.substring(0,1).equals(OPERATE_UPDATE.substring(0,1))){ System.out.println("enter the stuID:"); int sid = in.nextInt(); System.out.println("enter the couID"); int cid=in.nextInt(); C2SAction ca = new C2SAction(); C2S c = ca.query(sid, cid); System.out.println("enter the new credit"); double nCredit = in.nextDouble(); c.setCredit(nCredit); ca.update(c); System.out.println("UPDATED SUCCESSFULLY"); System.out.print(PROMPT); } else if (keyWord.equals(OPERATE_ADD)||keyWord.substring(0,1).equals(OPERATE_ADD.substring(0,1))){ System.out.println("enter the stuID:"); int sid = in.nextInt(); StudentAction sa = new StudentAction(); Student student = sa.queryById(sid); if (student!=null){ System.out.println("enter the couID"); int cid=in.nextInt(); CourseAction caa = new CourseAction(); Course course =caa.queryById(cid); if(course!=null){ System.out.println("enter the credit"); double cre = in.nextDouble(); C2SAction ca = new C2SAction(); ca.add(new C2S(sid,cid,cre)); } else { System.out.println("NO SUCH COURSE"); } } else { System.out.println("NO SUCH STUDENT"); } System.out.println("ADDED SUCCESSFULLY"); System.out.print(PROMPT); } } } }
package view;

import action.C2SAction;
import action.CourseAction;
import dao.C2SDao;
import model.Course;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

/**
 * Created by yinus
 * Date:2016/4/6
 * Time:19:45
 */

public class CourseView {
    private static final String OPERATE_EXIT = "EXIT";
    private static final String OPERATE_ADD = "ADD";
    private static final String OPERATE_UPDATE = "UPDATE";
    private static final String OPERATE_QUERY = "QUERY";
    private static final String OPERATE_DELETE = "DELETE";
    private static final String PROMPT="COURSE[A/D/U/Q/E]>";
    private static final String Menu =""+
            "[ADD]ADD COURSE\n"+
            "[DELETE]DELETE COURSE\n"+
            "[UPDATE]UPDATE COURSE\n"+
            "[QUERY]QUERY COURSE\n"+
            "[EXIT]EXIT\n"+
            "IGNORE CASE & MAY USE THE FIRST LETTER TO OPERATE" +
            PROMPT;
    public static void view() throws SQLException {
        System.out.println(Menu);
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String keyWord = input.next().toUpperCase();
            if (keyWord.equals(OPERATE_EXIT) || keyWord.substring(0, 1).equals(OPERATE_EXIT.substring(0,1))) {
                break;
            }
            else if (keyWord.equals(OPERATE_QUERY) || keyWord.substring(0, 1).equals(OPERATE_QUERY.substring(0,1))) {
                System.out.println("please enter the id or title of the course to be queried");
                String str = input.next();
                CourseAction courseAction = new CourseAction();
                if (Pattern.matches("\\d+", str)) {
                    Course course = courseAction.queryById(Integer.parseInt(str));
                    System.out.println(course);
                } else {
                    List<Course> courses = courseAction.queryByName(str);
                    courses.forEach(System.out::println);
                } 
                System.out.print(PROMPT);
            }
            else if (keyWord.equals(OPERATE_ADD) || keyWord.substring(0, 1).equals(OPERATE_ADD.substring(0,1))) {
                CourseAction courseAction = new CourseAction();
                System.out.println("please enter the id and title of the course to be added ,separated by spaces");
                int id = input.nextInt();
                String title = input.next();
                courseAction.add(new Course(id, title));
                System.out.println("added successfully");
                System.out.print(PROMPT);
            }
            else if (keyWord.equals(OPERATE_DELETE) || keyWord.substring(0, 1).equals(OPERATE_DELETE.substring(0,1))) {
                CourseAction courseAction = new CourseAction();
                System.out.println("please enter the id of the course to be delete");
                int id = input.nextInt();
                courseAction.delete(id);
                C2SAction ca =new C2SAction();
                ca.delete(id,false);
                System.out.println("deleted successfully");
                System.out.print(PROMPT);
            }
            else if (keyWord.equals(OPERATE_UPDATE) || keyWord.substring(0, 1).equals(OPERATE_UPDATE.substring(0,1))) {
                System.out.println("please enter the id of the course to be updated");
                int id = input.nextInt();
                CourseAction courseAction = new CourseAction();
                Course course=courseAction.queryById(id);
                System.out.println("please enter the new title of the course");
                String title = input.next();
                course.setTitle(title);
                courseAction.update(course);
                System.out.println("updated successfully");
                System.out.print(PROMPT);
            }
        }
    }
}
package view;

import action.C2SAction;
import action.StudentAction;
import model.Student;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

/**
 * Created by yinus
 * Date:2016/4/6
 * Time:19:45
 */

public class StudentView {
    private static final String OPERATE_EXIT = "EXIT";
    private static final String OPERATE_ADD = "ADD";
    private static final String OPERATE_UPDATE = "UPDATE";
    private static final String OPERATE_QUERY = "QUERY";
    private static final String OPERATE_DELETE = "DELETE";
    private static final String PROMPT="STUDENT[A/D/U/Q/E]>";
    private static final String Menu =""+
            "[ADD]ADD COURSE\n"+
            "[DELETE]DELETE COURSE\n"+
            "[UPDATE]UPDATE COURSE\n"+
            "[QUERY]QUERY COURSE\n"+
            "[EXIT]EXIT\n"+
            "IGNORE CASE & MAY USE THE FIRST LETTER TO OPERATE" +
            PROMPT;
    public static void view() throws SQLException {
        System.out.print(Menu);
        Scanner input = new Scanner(System.in);
        while (input.hasNext()){
            String keyWord = input.next().toUpperCase();
            if (keyWord.equals(OPERATE_EXIT) || keyWord.substring(0, 1).equals(OPERATE_EXIT.substring(0,1))) {
                break;
            }
            else if (keyWord.equals(OPERATE_QUERY) || keyWord.substring(0, 1).equals(OPERATE_QUERY.substring(0,1))){
                System.out.println("please enter the id or name of the student to be queried");
                String str = input.next();
                StudentAction studentAction = new StudentAction();
                if (Pattern.matches("\\d+",str)) {
                    Student student = studentAction.queryById(Integer.parseInt(str));
                    System.out.println(student);
                }
                else{
                    List<Student> students  = studentAction.queryByName(str);
                    students.forEach(System.out::println);
                    }
                System.out.print(PROMPT);
            }
            else if (keyWord.equals(OPERATE_ADD) || keyWord.substring(0, 1).equals(OPERATE_ADD.substring(0,1))){
                StudentAction studentAction = new StudentAction();
                System.out.println("please enter the id and name of the student to be added ,separated by spaces");
                int id = input.nextInt();
                String name = input.next();
                studentAction.add(new Student(id,name));
                System.out.println("added successfully");
                System.out.print(PROMPT);
            }
            else if (keyWord.equals(OPERATE_DELETE) || keyWord.substring(0, 1).equals(OPERATE_DELETE.substring(0,1))){
                StudentAction studentAction = new StudentAction();
                System.out.println("please enter the id of the student to be deleted");
                int id = input.nextInt();
                studentAction.delete(id);
                C2SAction ca = new C2SAction();
                ca.delete(id,true);
                System.out.println("deleted successfully");
                System.out.print(PROMPT);
            }
            else if (keyWord.equals(OPERATE_UPDATE) || keyWord.substring(0, 1).equals(OPERATE_UPDATE.substring(0,1))){
                System.out.println("please enter the id of the student to updated");
                int id = input.nextInt();
                StudentAction studentAction = new StudentAction();
                Student student = studentAction.queryById(id);
                System.out.println("enter the new name of the student");
                String name = input.next();
                student.setName(name);
                studentAction.update(student);
                System.out.println("updated successfully");
                System.out.print(PROMPT);
                }
            }
        }
    }

 

第二次数据库作业--view

标签:

人气教程排行