手记

JDBC之对岸女孩看过来view

package com.imooc.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import com.imooc.action.GodAction;
import com.imooc.po.God;

public class GodView {
private static final String CONTEXT = "=====欢迎来到女神禁区,下面是女神禁区功能列表=====\n"

  • "字母[m]:主菜单\n"
  • "字母[q]:查看全部女神信息\n"
  • "字母[g]:查看某位女神详细信息\n"
  • "字母[a]:添加女神信息\n"
  • "字母[u]:更新女神信息\n"
  • "字母[d]:删除女神信息\n"
  • "字母[s]:根据名字模糊查询女神信息\n"
  • "字母[b]:退出当前功能,返回主菜单\n"
  • "字母[e]:退出女神禁区\n" ;

    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_BREAK = "BREAK";
    private static final String OPERATION_EXIT = "EXIT";

    public static void main(String[] args) {
    System.out.println(CONTEXT);
    //通过下面方式创建Scanner对象,控制台会一直等待输入,直到敲回车键结束,把所输入的内容传给scanner,作为扫描对象.
    Scanner scan=new Scanner(System.in);
    God g=new God();
    GodAction action=new GodAction();
    List<God> list=new ArrayList<God>();
    //记忆变量,让其在同一个功能模块里
    String prenious=null;
    //步骤
    Integer step=1;

    //怎么保存程序的一直运行
    //scan.hasNext()有输入值就循环,没输入值就不循环  while(true)
    while(scan.hasNext()){
        String flag=scan.next().trim();
        //退出女神
        //if(OPERATION_EXIT.equalsIgnoreCase(flag)):忽略大小写
        if(OPERATION_EXIT.equals(flag.toUpperCase())||OPERATION_EXIT.substring(0, 1).equals(flag.toUpperCase())){
            System.out.println("---已退出女神禁区---");
            break;
        }else if(OPERATION_MAIN.equals(flag.toUpperCase())||OPERATION_MAIN.substring(0, 1).equals(flag.toUpperCase())){
            //返回主菜单
            System.out.println("---返回主菜单---");
            System.out.println(CONTEXT);
    
        }else if(OPERATION_GET.equals(flag.toUpperCase())||OPERATION_GET.substring(0, 1).equals(flag.toUpperCase())||OPERATION_GET.equals(prenious)){
            prenious=OPERATION_GET;
            //查询一个女神
            if(1==step){
                System.out.println("请输入需要查询女神的id");
            }else if(2==step){
    
                try {
                    g=action.queryById(Integer.valueOf(flag));
                    if(null!=g){
                        System.out.println(g.toString());
                        System.out.println("查询一个女神成功");
                        step=1;
                    }else{
                        System.out.println("输入的id不存在");
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("输入id出错,请重新输入"); 
                }
    
            }
            if(OPERATION_GET.equals(prenious)){
                step++;
            }
    
        }else if(OPERATION_UPDATE.equals(flag.toUpperCase())||OPERATION_UPDATE.substring(0, 1).equals(flag.toUpperCase())||OPERATION_UPDATE.equals(prenious)){
            //更新
            prenious=OPERATION_UPDATE;
            //新增女神
            if(1==step){
                System.out.println("请输入跟新女神的[姓名]");
            }else if(2==step){
                g.setUser_name(flag);
                System.out.println("请输入跟新女神的[年龄]");
            }else if(3==step){
                g.setAge(Integer.valueOf(flag));;
                System.out.println("请输入跟新女神的[id]");
            }else if(4==step){
                g.setId(Integer.valueOf(flag));                 
                try {
                    action.update(g);;
                    System.out.println("跟新女神成功");
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("跟新女神失败");
                }
    
            }
    
            if(OPERATION_UPDATE.equals(prenious)){
                step++;
            }
    
        }else if(OPERATION_QUERY.equals(flag.toUpperCase())||OPERATION_QUERY.substring(0, 1).equals(flag.toUpperCase())||OPERATION_QUERY.equals(prenious)){
            prenious=OPERATION_QUERY;
            //查询女神
            list=action.query();
            for (int i = 0; i < list.size(); i++) {
                System.out.print(list.get(i).getId());
                System.out.print(list.get(i).getUser_name());
                System.out.print(list.get(i).getSex());
                System.out.print(list.get(i).getAge());
                System.out.print(list.get(i).getBirthday());
                System.out.println();
            }
        }else if(OPERATION_ADD.equals(flag.toUpperCase())||OPERATION_ADD.substring(0, 1).equals(flag.toUpperCase())||OPERATION_ADD.equals(prenious)){
            prenious=OPERATION_ADD;
            //新增女神
            if(1==step){
                System.out.println("请输入新增女神的[姓名]");
            }else if(2==step){
                g.setUser_name(flag);
                System.out.println("请输入新增女神的[年龄]");
            }else if(3==step){
                g.setAge(Integer.valueOf(flag));
                System.out.println("请输入新增女神的[生日],格式为:yyyy-MM-dd");
            }else if(4==step){
                SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
                Date birthday=null;
                try {
                    birthday = sf.parse(flag);
                    g.setBirthday(birthday);
                    System.out.println("请输入新增女神的[邮箱]");
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("您输入的格式有误,请重新输入");
                    step=3;
                }
    
            }else if(5==step){
                g.setEmail(flag);
                System.out.println("请输入新增女神的[手机号]");
            }else if(6==step){
                g.setMobile(flag);
    
                try {
                    action.add(g);
                    System.out.println("新增女神成功");
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("新增女神失败");
                }
    
            }
    
            if(OPERATION_ADD.equals(prenious)){
                step++;
            }
    
        }else{
            System.out.println("您输入的值为:"+flag);
        }
    
    }

    }

}

0人推荐
随时随地看视频
慕课网APP