第十周周二(冲刺第九天)
今天:写了注册时的合法性校验,写了增加订单功能,包括kd和df,代码量500行。
明天:继续补全细小功能。
package com.example.kydy; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.example.kydy.BEAN.DFinfo; import com.example.kydy.BEAN.Delivery; import com.example.kydy.BEAN.IPV4; import com.example.kydy.BEAN.KDinfo; import com.example.kydy.BEAN.Meal; import com.example.kydy.Utils.CallBack; import com.example.kydy.Utils.OkHttpUtils; import com.google.gson.Gson; import java.time.LocalDateTime; public class Payment_DF_Activity extends AppCompatActivity implements View.OnClickListener { private androidx.appcompat.widget.Toolbar tb_toOrder; private String username; private String type; //区分带饭可快递的订单界面 private TextView tv_nowTime_df, tv_df_username; private EditText et_endTime, et_resName, et_dishName, et_remark, et_money; private Button btn_submit; //份数操控 private TextView tv_add_n, tv_reduce_n, tv_order_number; private int number = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_payment_df); Intent intent = getIntent(); username = intent.getStringExtra("username"); type = intent.getStringExtra("type"); initData(); initView(); } private void initData() { tv_df_username = findViewById(R.id.tv_df_username); //用户名 tv_nowTime_df = findViewById(R.id.tv_nowTime_df); //当前时间 et_endTime = findViewById(R.id.et_endTime); //预期时间 et_resName = findViewById(R.id.et_resName); //餐厅名称 et_dishName = findViewById(R.id.et_dishName); //菜品名称 et_remark = findViewById(R.id.et_remark); //备注 et_money = findViewById(R.id.et_money); btn_submit = findViewById(R.id.btn_submit); tb_toOrder = findViewById(R.id.tb_toOrder_df); //份数操控 tv_add_n = findViewById(R.id.tv_add_n); tv_reduce_n = findViewById(R.id.tv_reduce_n); tv_add_n.setOnClickListener(this); tv_reduce_n.setOnClickListener(this); tb_toOrder.setOnClickListener(this); } private void initView() { show1();//显示用户名 创建时间 submit(); } private void show1() { LocalDateTime time = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { time = LocalDateTime.now(); } tv_df_username.setText("订单用户:" + username); tv_nowTime_df.setText("订单创建时间:" + time); } private void submit() { btn_submit.setOnClickListener(view -> { String endAt = et_endTime.getText().toString(); String etP = et_resName.getText().toString(); String order_name = et_dishName.getText().toString(); String order_remark = et_remark.getText().toString(); String destination = "宿舍";//*********************** //************************ String money = et_money.getText().toString(); LocalDateTime time = null; LocalDateTime finalTime = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { time = LocalDateTime.now(); } if (!endAt.isEmpty()) { try { int endMinute = Integer.parseInt(endAt); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { finalTime = time.plusMinutes(endMinute); } deal(etP, order_name, order_remark, destination ,money, finalTime); } catch (NumberFormatException e) { // 处理转换异常,例如用户输入的不是数字 Toast.makeText(this, "请输入有效的分钟数", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "结束时间不能为空", Toast.LENGTH_SHORT).show(); } }); } private void deal(String re_name, String order_name, String order_remark, String destination, String total_amount, LocalDateTime endAt) { String username1 = username; Meal delivery = new Meal(username1, re_name, order_name, number, order_remark, destination , total_amount,0, 0, endAt.toString()); // 使用Gson库将Delivery对象转换为JSON字符串 Gson gson = new Gson(); String deliveryJson = gson.toJson(delivery); OkHttpUtils.getInstance().doPost("http://" + IPV4.ipv4 + "/meals/addMeal?useSSL=true", new CallBack() { @Override public void onSuccess(String result) { // 使用Gson解析JSON字符串 Gson gson = new Gson(); // 假设返回的JSON直接对应于Delivery对象 try { DFinfo dFinfo = gson.fromJson(result, DFinfo.class); // 解析成功,现在可以使用responseDelivery对象了 Log.e("e", "解析后的Delivery对象: " + dFinfo.toString()); Toast.makeText(Payment_DF_Activity.this, "增加成功!", Toast.LENGTH_SHORT).show(); // 根据需要处理responseDelivery对象,例如更新UI或存储数据 Intent intent = new Intent(Payment_DF_Activity.this, OrderActivity.class); intent.putExtra("username", username); intent.putExtra("type", type); //startActivity(intent); finish(); } catch (Exception e) { // JSON解析出错时的处理 Log.e("e", "解析错误: ", e); } } @Override public void onError(Exception e) { Toast.makeText(Payment_DF_Activity.this, "增加失败", Toast.LENGTH_SHORT).show(); } }, deliveryJson); } //份数操控 @Override public void onClick(View view) { if(view.getId() == R.id.tv_add_n){ number++; tv_order_number.setText(String.valueOf(number)); } if (view.getId() == R.id.tv_reduce_n){ if (number == 0){//份数等于0,无操作 return; } number--; tv_order_number.setText(String.valueOf(number)); } if (view.getId() == R.id.tb_toOrder_df){ finish(); } } }
package com.example.kydy; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.example.kydy.BEAN.Delivery; import com.example.kydy.BEAN.IPV4; import com.example.kydy.BEAN.KDinfo; import com.example.kydy.Utils.CallBack; import com.example.kydy.Utils.OkHttpUtils; import com.google.gson.Gson; import java.time.LocalDateTime; public class Payment_KD_Activity extends AppCompatActivity implements View.OnClickListener { private androidx.appcompat.widget.Toolbar tb_toOrder; private String username; private String type; //区分带饭可快递的订单界面 private TextView tv_nowTime, tv_kd_username; private EditText et_endAt, et_p, et_trackNum, et_destination, et_money; private Button btn_submit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_payment_kd); Intent intent = getIntent(); username = intent.getStringExtra("username"); type = intent.getStringExtra("type"); initData(); initView(); } private void initData() { tv_kd_username = findViewById(R.id.tv_kd_username); tv_nowTime = findViewById(R.id.tv_nowTime); et_endAt = findViewById(R.id.et_endAt); et_p = findViewById(R.id.et_p); et_trackNum = findViewById(R.id.et_trackNum); et_destination = findViewById(R.id.et_destination); et_money = findViewById(R.id.et_money); btn_submit = findViewById(R.id.btn_submit); tb_toOrder = findViewById(R.id.tb_toOrder_kd); tb_toOrder.setOnClickListener(this); } private void initView() { show1();//显示用户名 创建时间 submit(); } @Override public void onClick(View view) { if(view.getId() == R.id.tb_toOrder_kd){ //关闭该activity finish(); } else { } } private void show1() { LocalDateTime time = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { time = LocalDateTime.now(); } tv_kd_username.setText("订单用户:" + username); tv_nowTime.setText("订单创建时间:" + time); } private void submit() { btn_submit.setOnClickListener(view -> { String endAt = et_endAt.getText().toString(); String etP = et_p.getText().toString(); String trackNum = et_trackNum.getText().toString(); String destination = et_destination.getText().toString(); String money = et_money.getText().toString(); LocalDateTime time = null; LocalDateTime finalTime = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { time = LocalDateTime.now(); } if (!endAt.isEmpty()) { try { int endMinute = Integer.parseInt(endAt); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { finalTime = time.plusMinutes(endMinute); } deal(etP, trackNum, destination, money, finalTime); } catch (NumberFormatException e) { // 处理转换异常,例如用户输入的不是数字 Toast.makeText(this, "请输入有效的分钟数", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "结束时间不能为空", Toast.LENGTH_SHORT).show(); } }); } private void deal(String trackingAddress, String trackingNumber, String destinationAddress, String totalAmount, LocalDateTime endAt) { String username1=username; Delivery delivery = new Delivery(username1, trackingAddress, trackingNumber, destinationAddress, totalAmount, 0, 0, endAt.toString()); // 使用Gson库将Delivery对象转换为JSON字符串 Gson gson = new Gson(); String deliveryJson = gson.toJson(delivery); OkHttpUtils.getInstance().doPost("http://" + IPV4.ipv4 + "/express/addDelivery?useSSL=true", new CallBack() { @Override public void onSuccess(String result) { // 使用Gson解析JSON字符串 Gson gson = new Gson(); // 假设返回的JSON直接对应于Delivery对象 try { KDinfo kDinfo = gson.fromJson(result, KDinfo.class); // 解析成功,现在可以使用responseDelivery对象了 Log.e("e", "解析后的Delivery对象: " + kDinfo.toString()); Toast.makeText(Payment_KD_Activity.this, "增加成功!", Toast.LENGTH_SHORT).show(); // 根据需要处理responseDelivery对象,例如更新UI或存储数据 Intent intent = new Intent(Payment_KD_Activity.this, OrderActivity.class); intent.putExtra("username", username); intent.putExtra("type", type); startActivity(intent); finish(); } catch (Exception e) { // JSON解析出错时的处理 Log.e("e", "解析错误: ", e); } } @Override public void onError(Exception e) { Toast.makeText(Payment_KD_Activity.this, "增加失败", Toast.LENGTH_SHORT).show(); } }, deliveryJson); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)