iT邦幫忙

2025 iThome 鐵人賽

0

目的

測試使用者、食物記錄1~4的功能 , 看有沒有錯誤地方。
在使用者、食物記錄那裡 , 輸錯就回到當前欄位。

1.使用者
*註冊:
只要輸入錯 , 就會回到那個欄位。
先在while外面宣告name , while目的就是為了輸錯能回到當前欄位。

if(!name.isEmpty()) break : 當使用者輸入不是空的 , 就跳出迴圈 , 繼續下 一步。
name、passwordInput、age、gender、height、weight同上。

if(!goal.isEmpty() && goal.matches([\u4e00-\u9fa5]+ ") : matches是String類別的方法 , 用來檢查是否符合給定的正規表達式。後面接的是只能打中文的表達式。
https://ithelp.ithome.com.tw/upload/images/20251016/20178845u9PrZUxG3G.jpg
https://ithelp.ithome.com.tw/upload/images/20251016/2017884564uPY5xd7O.jpg
https://ithelp.ithome.com.tw/upload/images/20251016/20178845pe0bgoYOap.jpg
結果:https://ithelp.ithome.com.tw/upload/images/20251016/2017884542P8tETrfi.jpg


*登入:
姓名跟密碼跟上面一樣改法。
https://ithelp.ithome.com.tw/upload/images/20251016/20178845LdRC0mFIPF.jpg
結果:
https://ithelp.ithome.com.tw/upload/images/20251016/20178845ZhYAkhORKN.jpg


2.食物記錄
*新增食物記錄 :
因為後面需要將參數放入 , 所以不能在個別的while前宣告。

userId、calories、protein、fat、carb、quantity新放了一個try-catch接不是輸入數字的錯誤 , 前面都加上while(true)。
這裡的重複部分不貼圖片 , 改名稱就好

date :在try裡面加break。

在選擇繼續新增那 , 新增return回到case 1。
https://ithelp.ithome.com.tw/upload/images/20251017/20178845MaDjhXbSG3.jpg
https://ithelp.ithome.com.tw/upload/images/20251017/20178845HsGM4FHe0N.jpg
https://ithelp.ithome.com.tw/upload/images/20251017/20178845xqlXaFZFUC.jpg
結果:
https://ithelp.ithome.com.tw/upload/images/20251017/20178845XEuitVg39d.jpg


*查詢
最外面while是為了是否繼續查詢的功能。
userId跟上面一樣。

新增catch(DateTimeParseException)接日期格式的錯誤

在查詢的時候可能會出錯 , 所以List< FoodLog >前加catch。
https://ithelp.ithome.com.tw/upload/images/20251017/20178845GNaCMi5ozc.jpg
https://ithelp.ithome.com.tw/upload/images/20251017/20178845ZZILE42pCG.jpg
結果 :
https://ithelp.ithome.com.tw/upload/images/20251017/20178845LAcLdN29Sj.jpg


*更新
foodId跟UserId同樣寫法 。
https://ithelp.ithome.com.tw/upload/images/20251017/20178845mutc2MRyFg.jpg
結果 :
https://ithelp.ithome.com.tw/upload/images/20251017/20178845wvZspZlkb4.jpg


*刪除
userId 、 foodId 、 date跟前面一樣。
只要有用到資料庫都是這個錯誤處理。
https://ithelp.ithome.com.tw/upload/images/20251017/20178845tMZNo1crId.jpg
結果:
https://ithelp.ithome.com.tw/upload/images/20251017/20178845lQQM4P7Gsk.jpg


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

尚未有邦友留言

立即登入留言