第二次数据库作业--view

package 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);
                }
            }
        }
    }
时间: 2024-08-09 09:06:33

第二次数据库作业--view的相关文章

第二次数据库作业--gui

1 package gui; 2 3 import action.C2SAction; 4 import action.CourseAction; 5 import action.StudentAction; 6 import javafx.application.Application; 7 import javafx.geometry.Insets; 8 import javafx.scene.Scene; 9 import javafx.scene.control.*; 10 import

第二次数据库作业--Main

import dao.CourseDao; import model.Course; import view.C2SView; import view.CourseView; import view.StudentView; import java.sql.SQLException; import java.util.List; import java.util.Scanner; /** * Created by yinus * Date:2016/4/8 * Time:16:09 */ pub

第二次数据库作业--action

package action; import dao.C2SDao; import model.C2S; import java.sql.SQLException; import java.util.List; /** * Created by yinus * Date:2016/4/6 * Time:18:43 */ public class C2SAction { public void add(C2S c) throws SQLException { C2SDao cd = new C2S

第二次数据库作业--model

package model; import java.util.Date; /** * Created by yinus * Date:2016/4/6 * Time:18:44 */ public class C2S { private int stu_id; private int cou_id; private double credit; private Date create_time; private Date update_time; public C2S() { } public

第二次数据库作业--dao

package dao; import model.C2S; import utils.Dbtuil; import java.sql.*; import java.util.ArrayList; import java.util.List; /** * Created by yinus * Date:2016/4/6 * Time:18:44 */ public class C2SDao { public void add(C2S cs) throws SQLException { Conne

Oracle 用脚本安装第二个数据库

安装第二个数据库: 登录oracle用户进入家目录,添加配置环境变量: vi .bash_profier ORACLE_SID=prod2 临时环境变量: $export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1 $export ORACLE_SID=prod2 创建第二个数据库文件目录: $mkdir -p /u01/app/oracle/oradata/prod2 创建sys用户密码文件: $cd /u01/app/oracle/prod

数据库——视图(View)相关

数据库——视图(View)相关 1. 综述 相关博客:http://www.cnblogs.com/pony/archive/2008/07/15/1243210.html(数据库视图介绍) 视图有 可插入的(insert) 和 可更新的(update) . 插入限制: 1. 视图中不包含原表中不能为空的列时,不能插入.因为视图会试图插入空值: 2. 视图中包含统计函数的结果 (如MAX()最大值),或者包含计算列,不能插入; 3. 使用了distinct.group by语句的视图,不能插入.

SDN第二次上机作业

SDN第二次上机作业 1.安装floodlight 参考链接:http://www.sdnlab.com/19189.html 2.生成拓扑并连接控制器floodlight,利用控制器floodlight查看图形拓扑 拓扑如图所示 提交要求:控制器floodlight所示可视化图形拓扑的截图,及主机拓扑连通性检测截图 3.利用字符界面下发流表,使得'h1'和'h2' ping 不通 参考链接:http://www.sdnlab.com/19394.html 提交要求:流表截图,及拓扑连通性截图

数据库作业14——综合练习(二) 反馈情况

数据库作业14--综合练习(二) 反馈情况 一.作业要求复述 1.创建数据库CPXS,保存于E盘根目录下以自己学号+姓第一个字母(阿拉伯数字+大写字母)方式创建的文件夹中,初始大小5MB,最大20MB,以10%方式增长,日志文件存于同一文件夹,初始大小2MB,最大5MB,以1MB方式增长: 2.创建表CP,CPBH为主键,8位数字,CPMC,长度12个字符,JG为精确到小数点后2位,KCL为整数,除了KCL,其他都不能为空: 3.使用INSERT输入数据,具体数据如下: '10001100',