iT邦幫忙

2025 iThome 鐵人賽

0
自我挑戰組

飲食營養追蹤 + 健康分析 Web 系統系列 第 19

使用者資料更新功能

  • 分享至 

  • xImage
  •  

目的

在離開系統前 , 問使用者有沒有要刪除或更新資料。

1.選單
寫在食物記錄系統的case0下面 :
if(curretUserId) != null : 如果使用者已登入 , 就執行下面的程式碼。

這裡的choice在食物記錄那就宣告過 , 這裡不用再寫一次。
https://ithelp.ithome.com.tw/upload/images/20251016/20178845l3bBCCvdzn.jpg


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就直接跳出迴圈 , 不進行修改。

||:或 , 擇一條件。
https://ithelp.ithome.com.tw/upload/images/20251016/20178845DFLC4fAbgn.jpg

if(genderInput.equals("M") || genderInput.equals("F") : 如果輸入值為M或F , 就更新資料。
https://ithelp.ithome.com.tw/upload/images/20251016/20178845twNl2Yqf0z.jpg

if(newGoal.length() > 30) : 限制字數為30字以內
https://ithelp.ithome.com.tw/upload/images/20251016/20178845qJVcqC55mV.jpg

user_dao.updateCurrentUser : 呼叫updateCurrentUser方法 , 並傳入參數。
https://ithelp.ithome.com.tw/upload/images/20251016/20178845XLozmJi1Vc.jpg


3.UserDAO的getUserById
SELECT*FROM users WHERE userId: 找users表裡的userId。

return User : 如果讀到資料就回傳新的User物件 。
https://ithelp.ithome.com.tw/upload/images/20251016/20178845WyNLM6hfgZ.jpg


4.UserDAO的updateCurrentUser
這裡只是改名字跟一些地方 , 其他的都一樣。
把userId , password , age , gender , height , weight , goal參數丟入。

set的部分直接放參數名。

https://ithelp.ithome.com.tw/upload/images/20251016/20178845i42NtPE12l.jpg


5.結果
https://ithelp.ithome.com.tw/upload/images/20251016/20178845xQebY5NI1D.jpg
https://ithelp.ithome.com.tw/upload/images/20251016/20178845E7qmDQpdgw.jpg


上一篇
使用者登入和註冊
下一篇
使用者資料刪除功能
系列文
飲食營養追蹤 + 健康分析 Web 系統21
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言