2-9作業(yè) 簡單的做了一下
package?com.view; import?java.sql.SQLException; import?java.text.ParseException; import?java.text.SimpleDateFormat; import?java.util.ArrayList; import?java.util.Date; import?java.util.HashMap; import?java.util.List; import?java.util.Map; import?java.util.Scanner; import?com.action.GoddessAction; import?com.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)?{ Goddess?goddess?=?new?Goddess(); GoddessAction?action?=?new?GoddessAction(); String?prenious?=?null; Integer?temp?=?1; System.out.println(CONTEXT); //?怎么保持程序一直運(yùn)行 Scanner?sc?=?new?Scanner(System.in); while?(true)?{ String?in?=?sc.next().toString(); if?(OPERATION_EXIT.equals(in.toUpperCase()) ||?OPERATION_EXIT.substring(0,?1).equals(in.toUpperCase()))?{ System.out.println("您已成功退出女神禁區(qū)。"); break; }else?if(OPERATION_SEARCH.equals(in.toUpperCase()) ||?OPERATION_SEARCH.substring(0,?1).equals(in.toUpperCase())){ //根據(jù)女神姓名手機(jī)號查詢女神信息 System.out.println("請輸入女神的姓名:"); String?name?=?sc.next(); System.out.println("請輸入女神的手機(jī)號碼:"); String?mobile?=?sc.next(); List<Map<String,Object>>?params?=?new?ArrayList<Map<String,Object>>(); Map<String,?Object>?map?=?new?HashMap<String,?Object>(); map.put("name",?"user_name"); map.put("rela",?"="); String?realName?=?"'"?+?name?+?"'"; map.put("value",?realName); params.add(map); map?=?new?HashMap<String,?Object>(); map.put("name",?"mobile"); map.put("rela",?"="); String?realMobile?=?"'"?+?mobile?+?"'"; map.put("value",?realMobile); params.add(map); try?{ List<Goddess>?result?=?action.query(params); for?(int?i?=?0;?i?<?result.size();?i++)?{ System.out.println("查詢到的信息為:"+result.get(i).getId()+":"+result.get(i).getUser_name());} }?catch?(Exception?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); System.out.println("請輸入正確的姓名和手機(jī)號!!"); } }else?if?(OPERATION_UPDATE.equals(in.toUpperCase()) ||?OPERATION_UPDATE.substring(0,?1).equals(in.toUpperCase()) ||?OPERATION_UPDATE.equals(prenious))?{ prenious?=?OPERATION_UPDATE; //修改女神信息 if(1?==?temp){ System.out.println("請輸入需要修改的女神ID"); Integer?id?=?sc.nextInt(); goddess.setId(id); System.out.println("請輸入修改后的名字"); }else?if(2?==?temp){ goddess.setUser_name(in); System.out.println("請輸入修改后的年齡"); }else?if(3?==?temp){ goddess.setAge(Integer.valueOf(in)); System.out.println("請輸入修改后的生日,格式如下:yyyy--MM--dd"); }else?if(4?==?temp){ SimpleDateFormat?sf?=?new?SimpleDateFormat("yyyy-MM-dd"); Date?birthday?=?null; try?{ birthday?=?sf.parse(in); goddess.setBirthday(birthday); System.out.println("請輸入修改后的郵箱"); }?catch?(ParseException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); System.out.println("您輸入的格式有誤??!請重新輸入"); temp?=?3; } }else?if(5?==?temp){ goddess.setEmail(in); System.out.println("請輸入修改后的手機(jī)號碼"); }?else?if?(6?==?temp)?{ goddess.setMobile(in); try?{ action.edit(goddess); System.out.println("修改女神成功??!"); }?catch?(Exception?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); System.out.println("修改女神失?。?!"); } } if?(OPERATION_UPDATE.equals(prenious))?{ temp++; } }else?if(OPERATION_MAIN.equals(in.toUpperCase()) ||?OPERATION_MAIN.substring(0,?1).equals(in.toUpperCase())){ //返回主菜單 System.out.println(CONTEXT); }else?if(OPERATION_BREAK.equals(in.toUpperCase()) ????????????????????||OPERATION_BREAK.substring(0,1).equals(in.toUpperCase())){ //退出當(dāng)前功能,返回主菜單 ????????????????System.out.println("你已退出女神區(qū)!"); ????????????????System.out.println(CONTEXT); ????????????????continue; }else?if(OPERATION_DELETE.equals(in.toUpperCase()) ||?OPERATION_DELETE.substring(0,?1).equals(in.toUpperCase()) ||?OPERATION_DELETE.equals(prenious)){ prenious=OPERATION_DELETE; //根據(jù)女神ID刪除女神 System.out.println("請輸入你所要刪除的女神ID:"); Integer?id?=?sc.nextInt(); try?{ action.delete(id); System.out.println("刪除成功!"); }?catch?(Exception?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); System.out.println("輸入的格式有誤,請重新輸入[編號]:"); } }else?if(OPERATION_GET.equals(in.toUpperCase()) ||?OPERATION_GET.substring(0,?1).equals(in.toUpperCase()) ||?OPERATION_GET.equals(prenious)){ prenious=OPERATION_GET; //根據(jù)女神編號查看女神信息 try?{ System.out.println("請根據(jù)女神的編號,來查看女神的基本信息"); Integer?id?=?sc.nextInt(); goddess?=?action.getquery(id); System.out.println("姓名:"+goddess.getUser_name() +"\n"+"性別:"+goddess.getSex()+"\n"+"年齡:"+goddess.getAge()+"\n"+"生日:"+goddess.getBirthday()+"\n" +"手機(jī)號碼:"+goddess.getMobile()+"\n"+"郵箱:"+goddess.getEmail()+"\n" +"更新人:"+goddess.getUpdate_user()+"\n"+"更新日期"+goddess.getUpdate_date()); }?catch?(SQLException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); 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(prenious))?{ prenious?=?OPERATION_ADD; //?新增女神 if?(1?==?temp)?{ System.out.println("請輸入女神的【姓名】"); }?else?if?(2?==?temp)?{ goddess.setUser_name(in); System.out.println("請輸入女神的【年齡】"); }?else?if?(3?==?temp)?{ goddess.setAge(Integer.valueOf(in)); System.out.println("請輸入女神的【生日】,格式如下:yyyy-MM-dd"); }?else?if?(4?==?temp)?{ SimpleDateFormat?sf?=?new?SimpleDateFormat("yyyy-MM-dd"); Date?birthday?=?null; try?{ birthday?=?sf.parse(in); goddess.setBirthday(birthday); System.out.println("請輸入女神的【郵箱】"); }?catch?(ParseException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); System.out.println("您輸入的格式有誤!!請重新輸入"); temp?=?3; } }?else?if?(5?==?temp)?{ goddess.setEmail(in); System.out.println("請輸入女神的【手機(jī)號碼】"); }?else?if?(6?==?temp)?{ goddess.setMobile(in); try?{ action.add(goddess); System.out.println("新增女神成功!!"); }?catch?(Exception?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); System.out.println("新增女神失敗??!"); } } if?(OPERATION_ADD.equals(prenious))?{ temp++; } }?else?{ System.out.println("您輸入的值有誤!請檢查后重新輸入"); } } } }
簡單的做了一下,還有很多問題??!
2019-08-14
https://github.com/jeness/J2EELearning/tree/master/jdbcEmployee
作業(yè)代碼已傳,歡迎批評指正
2019-05-25
package com.imooc.view;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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";
/**
* @param args
* @throws?
*/
public static void main(String[] args){
// TODO Auto-generated method stub
System.out.println(CONTEXT);
//怎么保持程序一直運(yùn)行:永真循環(huán)
Scanner scan = new Scanner(System.in);
Goddess goddess = new Goddess();
GoddessAction action = new GoddessAction();
String prenious = null;//標(biāo)記變量
Integer step = 1;//步驟標(biāo)記
while(scan.hasNext()){
String in = scan.next().toString();
//退出
if(OPERATION_EXIT.equals(in.toUpperCase())
||OPERATION_EXIT.substring(0, 1).equals(in.toUpperCase())){
//輸入exit(自動轉(zhuǎn)換為大寫)或者輸入exit首字母即退出循環(huán)
//當(dāng)輸入字符為EXIT時,退出循環(huán)。
System.out.println("您已成功退出女神禁區(qū)!");
break;
}
//1.添加(添加女神信息)
else if(OPERATION_ADD.equals(in.toUpperCase())
||OPERATION_ADD.substring(0, 1).equals(in.toUpperCase())
||OPERATION_ADD.equals(prenious)){
prenious = OPERATION_ADD;
//新增女神
if(step==1){
System.out.println("請輸入女神的[姓名]");
}else if(step==2){
goddess.setUser_name(in);
System.out.println("請輸入女神的[年齡]");
}
else if(step==3){
goddess.setAge(Integer.valueOf(in));
System.out.println("請輸入女神的[生日],格式如:yyyy-MM-dd");
}else if(step==4){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date birthDay = null;
try {
birthDay = sf.parse(in);
goddess.setBirthday(birthDay);
System.out.println("請輸入女神[郵箱]");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("您輸入的格式有誤,請重新輸入!");
step=4-1;//因?yàn)榻Y(jié)束會自增,定義為3,自增到4還是設(shè)置生日。?
}
}else if(step==5){
goddess.setEmail(in);
System.out.println("請輸入女神[手機(jī)號]");
}else if(step==6){
goddess.setMobile(in);
System.out.println("請輸入女神[性別]");
}else if(step==7){
goddess.setSex(Integer.valueOf(in));
System.out.println("請輸入是否刪除");
}else if(step==8) {
goddess.setIsdel(Integer.valueOf(in));
try {
action.add(goddess);
System.out.println("新增女神成功!");
prenious=null;//標(biāo)記變量還原
step=1;
goddess=null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("新增女神失?。?);
}
}
if(OPERATION_ADD.equals(prenious)){
step++;
}
}
//2.更新(更新女神信息)
else if(OPERATION_UPDATE.equals(in.toUpperCase())
||OPERATION_UPDATE.substring(0, 1).equals(in.toUpperCase())
||OPERATION_UPDATE.equals(prenious)){
prenious=OPERATION_UPDATE;//進(jìn)入更新模塊后將標(biāo)記變量賦值為UPDATE
if(step==1){
System.out.println("請輸入更新女神的[ID]:");
}else if(step==2){
int id = scan.nextInt();
try {
goddess = action.getById(id);
System.out.println("請輸入更新的女神[姓名]:");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("請輸入正確的女神ID哦~");
}
}else if(step==3){
goddess.setUser_name(in);
System.out.println("請輸入更新的女神[年齡]");
}else if(step==4){
goddess.setAge(Integer.valueOf(in));
System.out.println("請輸入更新的女神[生日],格式如:yyyy-MM-dd");
}else if (step==5) {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date birthday = null;
try {
birthday = sf.parse(in);
goddess.setBirthday(birthday);
System.out.println("請輸入更新的女神[郵箱]");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("您輸入的格式有誤,請重新輸入!");
step=5-1;
}
}else if (step==6) {
goddess.setEmail(in);
System.out.println("請輸入更新的女神[手機(jī)號]");
}else if (step==7) {
goddess.setMobile(in);
System.out.println("請輸入更新的女神[性別]");
}else if (step==8) {
goddess.setSex(Integer.valueOf(in));
System.out.println("請輸入是否刪除");
}else if (step==9) {
goddess.setIsdel(Integer.valueOf(in));
try {
action.edit(goddess);
System.out.println("更新女神成功!");
prenious=null;//標(biāo)記變量還原
step=1;
goddess=null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(OPERATION_UPDATE.equals(prenious)){
step++;
}
}
//3.刪除女神(刪除女神信息)
else if(OPERATION_DELETE.equals(in.toUpperCase())
||OPERATION_DELETE.substring(0, 1).equals(in.toUpperCase())){
System.out.println("請輸入要刪除女神的[ID]");
try {
Integer id = new Integer(scan.nextInt());
action.del(id);
System.out.println("女神刪除成功!");
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("女神刪除失敗!");
}
}
//4.全部查詢(查看全部女神的信息)
else if(OPERATION_QUERY.equals(in.toUpperCase())
||OPERATION_QUERY.substring(0,1).equals(in.toUpperCase())){
try {
List<Goddess> list = action.query();
for (Goddess goddess2 : list) {
System.out.println(goddess2.getId()+":"+goddess2.getUser_name());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//5.信息查詢(查詢女神信息(根據(jù)姓名、手機(jī)號來查詢))
else if(OPERATION_SEARCH.equals(in.toUpperCase())
||OPERATION_SEARCH.substring(0, 1).equals(in.toUpperCase())
||OPERATION_SEARCH.equals(prenious)){
prenious=OPERATION_SEARCH;
List<Map<String, Object>> params = new ArrayList<Map<String,Object>>();
Map<String, Object> map = new HashMap<String, Object>();
if(step==1){
System.out.println("請輸入女神的[姓名]");
}else if (step==2) {
map.put("name","user_name" );
map.put("rela", "=");
map.put("value", in);
System.out.println("請輸入女神的[手機(jī)號碼]");
}else if(step==3){
map.put("name","mobile");
map.put("rela", "=");
map.put("value", in);
params.add(map);
try {
List<Goddess> list=action.query(params);
System.out.println("查詢到的女神信息如下:");
for (Goddess goddess2 : list) {
System.out.println(goddess2.getId()+":"+goddess2.getUser_name());
}
prenious=null;//標(biāo)記變量還原
step=1;
goddess=null;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(OPERATION_SEARCH.equals(prenious)){
step++;
}
}
//6.id查詢(查看某位女神的詳細(xì)信息)
else if (OPERATION_GET.equals(in.toUpperCase())
||OPERATION_GET.substring(0, 1).equals(in.toUpperCase())) {
System.out.println("請輸入要查看女神的[ID]");
int id =scan.nextInt();
try {
Goddess g= action.getById(id);
System.out.println(g.getId()+":"+g.getUser_name());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//7.主菜單
else if(OPERATION_MAIN.equals(in.toUpperCase())
||OPERATION_MAIN.substring(0, 1).equals(in.toUpperCase())){
System.out.println(CONTEXT);
continue;
}
//8.返回上一級
else if(OPERATION_BREAK.equals(in.toUpperCase())
||OPERATION_BREAK.substring(0, 1).equals(in.toUpperCase())) {
System.out.println("您已返回主菜單");
System.out.println(CONTEXT);
continue;
}
else{
System.out.println("您輸入的值為:"+in);
}
}
}
}