2024.6.15

第七十四天

所花时间:2小时

代码量:400+

博客量:1

了解到的知识点:

会议——预约+会议室查询

package com.example.huiyi;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.huiyi.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

// Activity to book a meeting room
public class YuyueActivity extends AppCompatActivity {
    private EditText meetingNameEditText, meetingContentEditText, beginTimeEditText, endTimeEditText, participantsEditText, roomIdEditText;
    private Button bookButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yuyue);

        meetingNameEditText = findViewById(R.id.meetingNameEditText);
        meetingContentEditText = findViewById(R.id.meetingContentEditText);
        beginTimeEditText = findViewById(R.id.beginTimeEditText);
        endTimeEditText = findViewById(R.id.endTimeEditText);
        participantsEditText = findViewById(R.id.participantsEditText);
        roomIdEditText = findViewById(R.id.roomIdEditText);
        bookButton = findViewById(R.id.bookButton);

        bookButton.setOnClickListener(v -> bookMeeting());
    }

    private void bookMeeting() {
        String meetingName = meetingNameEditText.getText().toString();
        String meetingContent = meetingContentEditText.getText().toString();
        String beginTime = beginTimeEditText.getText().toString();
        String endTime = endTimeEditText.getText().toString();
        String participants = participantsEditText.getText().toString();
        String roomId = roomIdEditText.getText().toString();

        new Thread(() -> {
            Connection connection = JDBCUtils.getConn();
            try {
                // Check if the room is available
                String checkSql = "SELECT COUNT(*) FROM Meeting WHERE MeetroomId = ? AND " +
                        "((MeetingBegin < ? AND MeetingEnd > ?) OR (MeetingBegin < ? AND MeetingEnd > ?) OR " +
                        "(MeetingBegin >= ? AND MeetingEnd <= ?))";
                try (PreparedStatement ps = connection.prepareStatement(checkSql)) {
                    ps.setString(1, roomId);
                    ps.setString(2, endTime);
                    ps.setString(3, endTime);
                    ps.setString(4, beginTime);
                    ps.setString(5, beginTime);
                    ps.setString(6, beginTime);
                    ps.setString(7, endTime);
                    try (ResultSet rs = ps.executeQuery()) {
                        if (rs.next() && rs.getInt(1) > 0) {
                            runOnUiThread(() -> Toast.makeText(this, "会议室在该时间段内不可用", Toast.LENGTH_LONG).show());
                            return;
                        }
                    }
                }

                // Book the meeting
                String insertSql = "INSERT INTO Meeting (MeetingName, MeetingContent, MeetingBegin, MeetingEnd, MeetingNum, Participants, MeetroomId, UserID, AuditStatus) VALUES (?, ?, ?, ?, ?, ?, ?, ?, '待审核')";
                try (PreparedStatement ps = connection.prepareStatement(insertSql)) {
                    ps.setString(1, meetingName);
                    ps.setString(2, meetingContent);
                    ps.setString(3, beginTime);
                    ps.setString(4, endTime);
                    ps.setInt(5, participants.split(",").length);
                    ps.setString(6, participants);
                    ps.setString(7, roomId);
                    ps.setString(8, getCurrentUserId());  // Assume a method to get current user ID
                    int rows = ps.executeUpdate();
                    if (rows > 0) {
                        runOnUiThread(() -> Toast.makeText(this, "会议申请提交成功", Toast.LENGTH_LONG).show());
                        finish();
                    } else {
                        runOnUiThread(() -> Toast.makeText(this, "会议申请提交失败", Toast.LENGTH_LONG).show());
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                JDBCUtils.close(connection);
            }
        }).start();
    }

    private String getCurrentUserId() {
        // Implement this method to return the currently logged-in user's ID
        return "1";  // Placeholder
    }
}
<!-- res/layout/activity_book_meeting.xml -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <EditText
            android:id="@+id/meetingNameEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="会议主题" />

        <EditText
            android:id="@+id/meetingContentEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="会议内容" />

        <EditText
            android:id="@+id/beginTimeEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="开始时间"
            android:inputType="datetime" />

        <EditText
            android:id="@+id/endTimeEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="结束时间"
            android:inputType="datetime" />

        <EditText
            android:id="@+id/participantsEditText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="参会人数" />
        <EditText
            android:id="@+id/participantsEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="参会人员(用逗号分隔)" />

        <EditText
            android:id="@+id/roomIdEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="会议室ID" />

        <Button
            android:id="@+id/bookButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="预约会议" />
    </LinearLayout>
</ScrollView>
package com.example.huiyi;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.huiyi.entity.MeetingRoom;

import java.util.List;

public class MeetingRoomAdapter extends ArrayAdapter<MeetingRoom> {
    private int resourceId;

    public MeetingRoomAdapter(Context context, List<MeetingRoom> objects) {
        super(context, R.layout.meeting_room_item, objects);
        resourceId = R.layout.meeting_room_item;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MeetingRoom room = getItem(position);
        View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);

        TextView nameTextView = view.findViewById(R.id.meetingRoomName);
        TextView addressTextView = view.findViewById(R.id.meetingRoomAddress);
        TextView capacityTextView = view.findViewById(R.id.meetingRoomCapacity);
        TextView statusTextView = view.findViewById(R.id.meetingRoomStatus);

        nameTextView.setText(room.getName());
        addressTextView.setText(room.getAddress());
        capacityTextView.setText(String.valueOf(room.getCapacity()));
        statusTextView.setText(room.getStatus());

        return view;
    }
}
<!-- res/layout/meeting_room_item.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp">

    <TextView
        android:id="@+id/meetingRoomName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="会议室名称"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/meetingRoomAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="地址" />

    <TextView
        android:id="@+id/meetingRoomCapacity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="容纳人数" />

    <TextView
        android:id="@+id/meetingRoomStatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="状态" />
</LinearLayout>
package com.example.huiyi;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.huiyi.entity.MeetingRoom;
import com.example.huiyi.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

// Activity to search for meeting rooms
public class SearchActivity extends AppCompatActivity {
    private ListView listView;
    private List<MeetingRoom> meetingRoomList = new ArrayList<>();
    private MeetingRoomAdapter adapter;
    private EditText statusEditText, capacityEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        listView = findViewById(R.id.listView);
        statusEditText = findViewById(R.id.statusEditText);
        capacityEditText = findViewById(R.id.capacityEditText);
        adapter = new MeetingRoomAdapter(this, meetingRoomList);
        listView.setAdapter(adapter);

        findViewById(R.id.searchButton).setOnClickListener(v -> searchMeetingRooms());
    }

    private void searchMeetingRooms() {
        String status = statusEditText.getText().toString();
        String capacityStr = capacityEditText.getText().toString();
        int capacity = capacityStr.isEmpty() ? 0 : Integer.parseInt(capacityStr);

        new Thread(() -> {
            Connection connection = JDBCUtils.getConn();
            String sql = "SELECT * FROM MeetingRoom WHERE Status = ? AND Capacity >= ?";
            try (PreparedStatement ps = connection.prepareStatement(sql)) {
                ps.setString(1, status);
                ps.setInt(2, capacity);
                try (ResultSet rs = ps.executeQuery()) {
                    meetingRoomList.clear();
                    while (rs.next()) {
                        MeetingRoom room = new MeetingRoom(rs.getInt("MeetroomId"), rs.getString("MeetroomName"),
                                rs.getString("Address"), rs.getString("ReadyState"), rs.getString("Status"),
                                rs.getInt("Capacity"));
                        meetingRoomList.add(room);
                    }
                    runOnUiThread(() -> adapter.notifyDataSetChanged());
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
<!-- res/layout/activity_search_meeting_rooms.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <EditText
        android:id="@+id/statusEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="状态" />

    <EditText
        android:id="@+id/capacityEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="容纳人数"
        android:inputType="number"/>

    <Button
        android:id="@+id/searchButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查询" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
package com.example.huiyi;

import android.os.Bundle;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.huiyi.entity.MeetingRoom;
import com.example.huiyi.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

// Activity to display available meeting rooms
public class LiulanActivity extends AppCompatActivity {
    private ListView listView;
    private List<MeetingRoom> meetingRoomList = new ArrayList<>();
    private MeetingRoomAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_liulan);

        listView = findViewById(R.id.listView);
        adapter = new MeetingRoomAdapter(this, meetingRoomList);
        listView.setAdapter(adapter);

        fetchAvailableMeetingRooms();
    }

    private void fetchAvailableMeetingRooms() {
        new Thread(() -> {
            Connection connection = JDBCUtils.getConn();
            String sql = "SELECT * FROM MeetingRoom WHERE ReadyState = '可用'";
            try (PreparedStatement ps = connection.prepareStatement(sql); ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    MeetingRoom room = new MeetingRoom(rs.getInt("MeetroomId"), rs.getString("MeetroomName"),
                            rs.getString("Address"), rs.getString("ReadyState"), rs.getString("Status"),
                            rs.getInt("Capacity"));
                    meetingRoomList.add(room);
                }
                runOnUiThread(() -> adapter.notifyDataSetChanged());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
<!-- res/layout/activity_meeting_rooms.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

 

posted @ 2024-06-17 02:47  cvjj  阅读(31)  评论(0)    收藏  举报