1package com.example.l8q3;
2
3import android.os.Bundle;
4import android.view.View;
5import android.widget.AdapterView;
6import android.widget.ArrayAdapter;
7import android.widget.ListView;
8import android.widget.Toast;
9import androidx.appcompat.app.AppCompatActivity;
10import java.util.List;
11import java.util.ArrayList;
12
13public class MainActivity extends AppCompatActivity {
14
15 private ListView doctorListView;
16 private ArrayAdapter<String> adapter;
17 private List<Doctor> doctorList;
18 private DatabaseHelper databaseHelper;
19
20 @Override
21 protected void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.activity_main);
24
25 doctorListView = findViewById(R.id.doctorListView);
26 databaseHelper = new DatabaseHelper(this);
27
28 // Add some sample doctors to the list (you can replace this with actual database data)
29 addSampleDoctors();
30
31 // Initialize adapter
32 adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getDoctorNames());
33 doctorListView.setAdapter(adapter);
34
35 // Set item click listener
36 doctorListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
37 @Override
38 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
39 Doctor selectedDoctor = doctorList.get(position);
40 boolean newStatus = !selectedDoctor.isFree(); // Toggle status
41 databaseHelper.updateDoctorStatus(selectedDoctor.getId(), newStatus);
42 selectedDoctor.setFree(newStatus);
43 adapter.notifyDataSetChanged();
44 String message = "Doctor " + selectedDoctor.getName() + " is now " + (newStatus ? "available" : "occupied");
45 Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
46 }
47 });
48 }
49
50 private void addSampleDoctors() {
51 databaseHelper.addDoctor(new Doctor(1, "Dr. Smith", true));
52 databaseHelper.addDoctor(new Doctor(2, "Dr. Johnson", false));
53 databaseHelper.addDoctor(new Doctor(3, "Dr. Patel", true));
54 databaseHelper.addDoctor(new Doctor(4, "Dr. Garcia", true));
55 databaseHelper.addDoctor(new Doctor(5, "Dr. Lee", false));
56 databaseHelper.addDoctor(new Doctor(6, "Dr. Nguyen", true));
57 databaseHelper.addDoctor(new Doctor(7, "Dr. Khan", true));
58 databaseHelper.addDoctor(new Doctor(8, "Dr. Martinez", false));
59 databaseHelper.addDoctor(new Doctor(9, "Dr. Anderson", true));
60 databaseHelper.addDoctor(new Doctor(10, "Dr. Gonzalez", true));
61
62 // Add more doctors as needed
63 }
64
65 private List<String> getDoctorNames() {
66 doctorList = databaseHelper.getAllDoctors();
67 List<String> doctorNames = new ArrayList<>();
68 for (Doctor doctor : doctorList) {
69 doctorNames.add(doctor.getName() + " (" + (doctor.isFree() ? "Available" : "Occupied") + ")");
70 }
71 return doctorNames;
72 }
73}
74
75
76
77
78// DatabaseHelper.java
79
80package com.example.l8q3;
81
82import android.content.ContentValues;
83import android.content.Context;
84import android.database.Cursor;
85import android.database.sqlite.SQLiteDatabase;
86import android.database.sqlite.SQLiteOpenHelper;
87import java.util.ArrayList;
88import java.util.List;
89
90public class DatabaseHelper extends SQLiteOpenHelper {
91
92 private static final int DATABASE_VERSION = 1;
93 private static final String DATABASE_NAME = "doctors_db";
94 private static final String TABLE_DOCTORS = "doctors";
95 private static final String KEY_ID = "id";
96 private static final String KEY_NAME = "name";
97 private static final String KEY_IS_FREE = "is_free";
98
99 public DatabaseHelper(Context context) {
100 super(context, DATABASE_NAME, null, DATABASE_VERSION);
101 }
102
103 @Override
104 public void onCreate(SQLiteDatabase db) {
105 String CREATE_DOCTORS_TABLE = "CREATE TABLE " + TABLE_DOCTORS + "("
106 + KEY_ID + " INTEGER PRIMARY KEY,"
107 + KEY_NAME + " TEXT,"
108 + KEY_IS_FREE + " INTEGER" + ")";
109 db.execSQL(CREATE_DOCTORS_TABLE);
110 }
111
112 @Override
113 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
114 db.execSQL("DROP TABLE IF EXISTS " + TABLE_DOCTORS);
115 onCreate(db);
116 }
117
118 public void addDoctor(Doctor doctor) {
119 SQLiteDatabase db = this.getWritableDatabase();
120 ContentValues values = new ContentValues();
121 values.put(KEY_NAME, doctor.getName());
122 values.put(KEY_IS_FREE, doctor.isFree() ? 1 : 0);
123 db.insert(TABLE_DOCTORS, null, values);
124 db.close();
125 }
126
127 public List<Doctor> getAllDoctors() {
128 List<Doctor> doctorList = new ArrayList<>();
129 String selectQuery = "SELECT * FROM " + TABLE_DOCTORS;
130 SQLiteDatabase db = this.getWritableDatabase();
131 Cursor cursor = db.rawQuery(selectQuery, null);
132 if (cursor.moveToFirst()) {
133 do {
134 Doctor doctor = new Doctor(cursor.getInt(0), cursor.getString(1), cursor.getInt(2) == 1);
135 doctorList.add(doctor);
136 } while (cursor.moveToNext());
137 }
138 cursor.close();
139 return doctorList;
140 }
141
142 public int updateDoctorStatus(int doctorId, boolean isFree) {
143 SQLiteDatabase db = this.getWritableDatabase();
144 ContentValues values = new ContentValues();
145 values.put(KEY_IS_FREE, isFree ? 1 : 0);
146 return db.update(TABLE_DOCTORS, values, KEY_ID + " = ?", new String[]{String.valueOf(doctorId)});
147 }
148}
149
150
151//Doctor.java
152
153package com.example.l8q3;
154
155public class Doctor {
156 private int id;
157 private String name;
158 private boolean isFree;
159
160 public Doctor(int id, String name, boolean isFree) {
161 this.id = id;
162 this.name = name;
163 this.isFree = isFree;
164 }
165
166 public int getId() {
167 return id;
168 }
169
170 public String getName() {
171 return name;
172 }
173
174 public boolean isFree() {
175 return isFree;
176 }
177
178 public void setFree(boolean free) {
179 isFree = free;
180 }
181}