问答详情
源自:2-9 JDBC实战---搭建视图层(下)

关于视图层修改女神的实现问题?

有谁知道视图层具体实现如何修改的功能,关于字段为Null时,该怎么修改?

提问者:海滩小贝壳 2018-09-26 01:54

个回答

  • 慕粉7419830
    2018-11-05 14:50:22

    package view;

    import java.io.*;

    import java.sql.SQLException;

    import java.text.ParseException;

    import java.text.SimpleDateFormat;

    import java.util.*;


    import action.GodessAction;

    import model.Godess;


    public class View {

    private static final String CONTEXT="欢迎来到女神禁区:\n" +

    "下面是女神禁区的功能列表:\n" +

    "[MAIN/M]:主菜单\n" +

    "[QUERY/Q]:查看全部女神的信息\n" +

    "[GET/G]:查看某位女神的详细信息\n" +

    "[ADD/A]:添加女神信息\n" +

    "[UPDATE/U]:更新女神信息\n" +

    "[DELETE/D]:删除女神信息\n" +

    "[SEARCH/S]:查询女神信息(根据姓名、手机号来查询)\n" +

    "[EXIT/E]:退出女神禁区\n" +

    "[BREAK/B]:退出当前功能,返回主菜单";

    private static final String OPERATION_MAIN="MAIN";

    private static final String OPERATION_QUERY="QUERY";

    private static final String OPERATION_GET="GET";

    private static final String OPERATION_ADD="ADD";

    private static final String OPERATION_UPDATE="UPDATE";

    private static final String OPERATION_DELETE="DELETE";

    private static final String OPERATION_SEARCH="SEARCH";

    private static final String OPERATION_EXIT="EXIT";

    private static final String OPERATION_BREAK="BREAK";

    public static void main(String[] args) {

    System.out.println(CONTEXT);

    Scanner scan = new Scanner(System.in);

    Godess g = new Godess();

    GodessAction ga = new GodessAction();

    String previous = null;

    Integer step=1;

    while(scan.hasNext()) {

    String input = scan.next().toString();

    System.out.println("你输入的值为:"+input); 

    if(OPERATION_EXIT.equals(input.toUpperCase())||OPERATION_EXIT.substring(0, 1).equals(input.toUpperCase())) {

    System.out.println("你已成功退出!");

    break;

    }else if(OPERATION_UPDATE.equals(input.toUpperCase())||OPERATION_UPDATE.substring(0, 1).equals(input.toUpperCase())) {

    Godess gu= new Godess();

    gu.setUser_name("小兰");

    gu.setAge(30);

    gu.setSex(0);

    gu.setBirthday(new Date());

    gu.setMobile("15648790987");

    gu.setEmail("xiaolan@163.com");

    gu.setIsdel(1);

            gu.setId(3);

    try {

    ga.update(gu);

    System.out.println("你已成功修改!");

    } catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }else if(OPERATION_DELETE.equals(input.toUpperCase())||OPERATION_DELETE.substring(0, 1).equals(input.toUpperCase())) {

    System.out.println("输入要删除的女神id:");

    Integer id = scan.nextInt();

    try {

    ga.delete(id);

    System.out.println("你已成功删除!");

    } catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }else if(OPERATION_QUERY.equals(input.toUpperCase())||OPERATION_QUERY.substring(0, 1).equals(input.toUpperCase())) {

    try {

    List<Godess> gds = ga.queryAll();

    System.out.println("查询成功,查询结果如下:");

    for(Godess gd:gds) {

    System.out.println("姓名:"+gd.getUser_name()+" "+"年龄:"+gd.getAge());

    }

    } catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    System.out.println("查询失败");

    }

    }else if(OPERATION_ADD.equals(input.toUpperCase())||

    OPERATION_ADD.substring(0, 1).equals(input.toUpperCase())||OPERATION_ADD.equals(previous)) {

    previous = "ADD";

    if(step==1) {

    System.out.println("请输入女神的[姓名]");

    }else if(step==2) {

    g.setUser_name(input);

    System.out.println("请输入女神的[年龄]");

    }else if(step==3) {

    g.setAge(Integer.valueOf(input));

    System.out.println("请输入女神的[性别]");

    }else if(step==4) {

    g.setSex(Integer.valueOf(input));

    System.out.println("请输入女神的[生日] 格式为yyyy-mm-dd");

    }else if(step==5) {

    SimpleDateFormat sd = new SimpleDateFormat("yyyy-mm-dd");

    Date birthday = null;

    try {

    birthday = sd.parse(input);

    g.setBirthday(birthday);

    System.out.println("请输入女神的[邮箱]");

    } catch (ParseException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    System.out.println("你输入的生日格式有误,请重新输入");

    step = 4;

    }

    g.setBirthday(birthday);

    }else if(step == 6) {

    g.setEmail(input);

    System.out.println("请输入女神的[手机号]");

    }else if(step==7){  

    g.setMobile(input);

    System.out.println("请输入女神的[是否删除标志]");

    }else {

    g.setIsdel(Integer.valueOf(input));

    try {

    ga.add(g);

    System.out.println("新增女神成功");

    } catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    System.out.println("新增女神失败");

    }

    }

    step++;

    }else{

    System.out.println("新增女神失败");

    }

    }

    }


    }