自己完成了基本的增刪改查功能,還有個問題就是不能連續(xù)兩次操作,比如不能連續(xù)添加或者修改,老師的添加代碼也不能實(shí)現(xiàn),時間原因沒有再做完善。下面是視圖層View的代碼。
package com.imooc.view;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import com.imooc.action.GoddessAction;
import com.imooc.model.Goddess;
public class View {
private static final String CONTEXT = "歡迎來到女神禁區(qū):\n" + "下面是女神禁區(qū)的功能列表:\n"
+ "[MAIN/M]:主菜單\n" + "[QUERY/Q]:查看全部女神的信息\n"
+ "[GET/G]:查看某位女神的詳細(xì)信息\n" + "[ADD/A]:添加女神信息\n"
+ "[UPDATE/U]:更新女神信息\n" + "[DELETE/D]:刪除女神信息\n"
+ "[SEARCH/S]:查詢女神信息(根據(jù)姓名、手機(jī)號來查詢)\n" + "[EXIT/E]:退出女神禁區(qū)\n"
+ "[BREAK/B]:退出當(dāng)前功能,返回主菜單";
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) throws SQLException {
System.out.println(CONTEXT);
// 怎么保持程序一直運(yùn)行
Scanner scan = new Scanner(System.in);
Goddess goddess = new Goddess();
GoddessAction action = new GoddessAction();
String previous = null;
Integer step = 1,stepU = 1,stepD = 1;
while (scan.hasNext()) {
String in = scan.next().toString();
if (OPERATION_EXIT.equals(in.toUpperCase())
|| OPERATION_EXIT.substring(0, 1).equals(in.toUpperCase())) {
System.out.println("您已成功退出女神禁區(qū)。");
break;
}else if (OPERATION_UPDATE.equals(in.toUpperCase())
|| OPERATION_UPDATE.substring(0, 1).equals(in.toUpperCase())
|| OPERATION_UPDATE.equals(previous)) {
previous = OPERATION_UPDATE;
// 修改女神信息
if (1 == stepU) {
System.out.println("請輸入需要修改的女神ID");
Integer inId=scan.nextInt();
? ? ? ? ? ? ? ? ? ? Goddess goddess2=action.get(inId);
? ? ? ? ? ? ? ? ? ? System.out.println(goddess2.toString());
? ? ? ? ? ? ? ? ? ? goddess.setId(inId);
? ? ? ? ? ? ? ? ? ? System.out.println("請輸入修改后的名字");
? ? ? ? ? ? ? ? ? ??
}else if (2 == stepU) {
goddess.setUser_name(in);
System.out.println("請輸入修改后的年齡");
} else if (3 == stepU) {
goddess.setAge(Integer.valueOf(in));
System.out.println("請輸入修改后的生日,格式如:yyyy-MM-dd");
} else if (4 == stepU) {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date birthday = null;
try {
birthday = sf.parse(in);
goddess.setBirthday(birthday);
System.out.println("請輸入修改后的女神的郵箱");
} catch (ParseException e) {
e.printStackTrace();
System.out.println("您輸入的格式有誤,請重新輸入");
step = 3;
}
} else if (5 == stepU) {
goddess.setEmail(in);
System.out.println("請輸入修改后的女神的手機(jī)號");
} else if (6 == stepU) {
goddess.setMobile(in);
try {
action.edit(goddess);
System.out.println("修改女神信息成功");
previous=null;
continue;
} catch (Exception e) {
e.printStackTrace();
System.out.println("修改女神信息失敗");
}
}
if (OPERATION_UPDATE.equals(previous)) {
stepU++;
}
} else if (OPERATION_DELETE.equals(in.toUpperCase())
|| OPERATION_DELETE.substring(0, 1).equals(in.toUpperCase())
|| OPERATION_DELETE.equals(previous)) {
//刪除信息
previous = OPERATION_DELETE;
if(1 == stepD){
System.out.println("請輸入需要刪除女神的ID");
Integer inId=scan.nextInt();
? ? ? ? ? ? ? ? Goddess goddess3=action.get(inId);
? ? ? ? ? ? ? ? System.out.println(goddess3.toString());
? ? ? ? ? ? ? ? action.del(inId);
? ? ? ? ? ? ? ? System.out.println("刪除成功");
? ? ? ? ? ? ? ? previous = null;
continue;
? ? ? ? ? ? ? ??
}else{
System.out.println("操作失敗");
}
}else if (OPERATION_QUERY.equals(in.toUpperCase())
|| OPERATION_QUERY.substring(0, 1).equals(in.toUpperCase())) {
try {
List<Goddess> list = action.query();
for (Goddess go : list) {
System.out.println(go.getId() + ",姓名:"
+ go.getUser_name());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (OPERATION_ADD.equals(in.toUpperCase())
|| OPERATION_ADD.substring(0, 1).equals(in.toUpperCase())
|| OPERATION_ADD.equals(previous)) {
previous = OPERATION_ADD;
// 新增女神
if (1 == step) {
System.out.println("請輸入女神的[姓名]");
} else if (2 == step) {
goddess.setUser_name(in);
System.out.println("請輸入女神的[年齡]");
} else if (3 == step) {
goddess.setAge(Integer.valueOf(in));
System.out.println("請輸入女神的[生日],格式如:yyyy-MM-dd");
} else if (4 == step) {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date birthday = null;
try {
birthday = sf.parse(in);
goddess.setBirthday(birthday);
System.out.println("請輸入女神的[郵箱]");
} catch (ParseException e) {
e.printStackTrace();
System.out.println("您輸入的格式有誤,請重新輸入");
step = 3;
}
} else if (5 == step) {
goddess.setEmail(in);
System.out.println("請輸入女神的[手機(jī)號]");
} else if (6 == step) {
goddess.setMobile(in);
try {
action.add(goddess);
System.out.println("新增女神成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("新增女神失敗");
}
}
if (OPERATION_ADD.equals(previous)) {
step++;
}
} else {
System.out.println("您輸入的值為:" + in);
}
}
}
}
2019-01-22
新增成功,將對應(yīng)的step重置為1;