目的
在離開系統前 , 問使用者有沒有要刪除或更新資料。
1.選單
寫在食物記錄系統的case0下面 :
if(curretUserId) != null : 如果使用者已登入 , 就執行下面的程式碼。
這裡的choice在食物記錄那就宣告過 , 這裡不用再寫一次。
2.Main
這裡新增2個case :
case 1(更新使用者) :
User currentuser = user_dao.getUserById : 呼叫getUserById的方法(後面會創) , 把currentUserById , 取得結果後傳回型別為User的currentUser變數。
為了取得原本使用者資料 , 後面需要用到
這裡不用先宣告newPass是因為 , 只會用到這一次且不需要在其他地方用
if(newPass.isEmpty()) newPass = currentUser.getPassword() : 如果使用者沒輸入東西(按Enter) , 就保留原密碼。
Entre 保留原年齡:"+currentUser.getAge() + " :要印出原本的年齡讓使用者知道。
int newAge = current.getAge() : 先預設newAge為目前使用者的原本年齡 , 避免使用者不輸入值 。
if (ageInput.isEmpty()) break :如果使用者按Enter就直接跳出迴圈 , 不進行修改。
||:或 , 擇一條件。
if(genderInput.equals("M") || genderInput.equals("F") : 如果輸入值為M或F , 就更新資料。
if(newGoal.length() > 30) : 限制字數為30字以內
user_dao.updateCurrentUser : 呼叫updateCurrentUser方法 , 並傳入參數。
3.UserDAO的getUserById
SELECT*FROM users WHERE userId: 找users表裡的userId。
return User : 如果讀到資料就回傳新的User物件 。
4.UserDAO的updateCurrentUser
這裡只是改名字跟一些地方 , 其他的都一樣。
把userId , password , age , gender , height , weight , goal參數丟入。
set的部分直接放參數名。
5.結果