Skip to content
Snippets Groups Projects
Commit 4a8e3415 authored by Alasdair Bruce SD2018's avatar Alasdair Bruce SD2018
Browse files

Added Radio buttons and search by author

parent 3db2b891
No related branches found
No related tags found
No related merge requests found
......@@ -21,13 +21,26 @@ public class BookDatabase {
books.add(book);
}
public Book search(String title) {
//Search books by title
public ArrayList<Book> searchByTitle(String title) {
ArrayList<Book> bookList = new ArrayList<>();
for (int i = 0; i < books.size(); i++) {
if (title.trim().toLowerCase().equals(books.get(i).getTitle().trim().toLowerCase())) {
return books.get(i);
bookList.add(books.get(i));
}
}
return null;
return bookList;
}
//Search books by author
public ArrayList<Book> searchByAuthor(String author) {
ArrayList<Book> bookList = new ArrayList<>();
for (int i = 0; i < books.size(); i++) {
if (author.trim().toLowerCase().equals(books.get(i).getAuthor().trim().toLowerCase())) {
bookList.add(books.get(i));
}
}
return bookList;
}
}
......@@ -5,21 +5,34 @@ import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.cs991.ywb18142.mytestapp.MESSAGE";
private BookDatabase books;
private int searchTypeCheck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
books = new BookDatabase();
books.addBook(new Book("Joe", "Book"));
books.addBook(new Book("Joe", "Book2"));
books.addBook(new Book("Steve", "Book"));
books.addBook(new Book("Claire", "Book2"));
books.addBook(new Book("Joe", "Book3"));
books.addBook(new Book("Steve", "Original Book"));
books.addBook(new Book("Clarence", "Books for Days"));
books.addBook(new Book("Joe", "Book4"));
}
// send btn
// Adds a book to the collection when the send button is pressed
//takes book info from the text inputs
public void sendMessage(View view){
EditText editText1 = findViewById(R.id.editText1);
EditText editText2 = findViewById(R.id.editText2);
......@@ -31,26 +44,33 @@ public class MainActivity extends AppCompatActivity {
}
// //display users
// public void displayUsers(View view){
// String userPrint = "";
// Intent intent = new Intent(this, DisplayMessageActivity.class);
// for (int i = 0; i < books.getBooks().size(); i++){
// userPrint = userPrint + books.getBooks().get(i).getAuthor() + books.getBooks().get(i).getTitle() + "\n";
// }
// intent.putExtra(EXTRA_MESSAGE, userPrint);
// startActivity(intent);
// }
//display users
public void searchByTitle(View view){
//Searches for books by whatever is in text box when the search button is pressed
//and dependant on the radio button pressed. Lists the results in the text view
public void searchBooks(View view){
ArrayList<Book> bookList = new ArrayList<>();
TextView textView1 = findViewById((R.id.textView2));
EditText editText1 = findViewById(R.id.editText3);
RadioButton titleSearch = findViewById(R.id.radio_title);
RadioButton authorSearch = findViewById(R.id.radio_author);
String message1 = editText1.getText().toString();
Book book1 = books.search(message1);
if(book1 != null){
String result = "Author: " + book1.getAuthor() + "\n" + "Title: " + book1.getTitle();
String result = "";
if (searchTypeCheck == 0){
bookList = books.searchByTitle(message1);
}
else if (searchTypeCheck == 1){
bookList = books.searchByAuthor(message1);
}
else{
//some sort of error
}
if(bookList.size() > 0){
int count = 1;
for (int i = 0; i < bookList.size(); i++) {
result = result + count + ". " + "Author: " + bookList.get(i).getAuthor() + "\n" + "Title: " + bookList.get(i).getTitle() + "\n";
count++;
}
textView1.setText(result);
}
else {
......@@ -58,5 +78,38 @@ public class MainActivity extends AppCompatActivity {
}
editText1.getText().clear();
}
//Monitors when radio buttons are clicked and sets an integer flag determining which search is used.
//This is an integer right now in case more searches need to be added but anything could be
//done with the radio buttons. There might be a much better way of doing this.
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_title:
if (checked)
searchTypeCheck = 0;
break;
case R.id.radio_author:
if (checked)
searchTypeCheck = 1;
break;
}
}
//display users
// public void displayUsers(View view){
// String userPrint = "";
// Intent intent = new Intent(this, DisplayMessageActivity.class);
// for (int i = 0; i < books.getBooks().size(); i++){
// userPrint = userPrint + books.getBooks().get(i).getAuthor() + books.getBooks().get(i).getTitle() + "\n";
// }
// intent.putExtra(EXTRA_MESSAGE, userPrint);
// startActivity(intent);
// }
}
......@@ -55,14 +55,16 @@
android:id="@+id/editText3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="108dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="72dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="@string/search_text"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2" />
......@@ -70,14 +72,15 @@
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="searchByTitle"
android:layout_marginTop="8dp"
android:layout_marginEnd="64dp"
android:layout_marginRight="64dp"
android:layout_marginBottom="8dp"
android:onClick="searchBooks"
android:text="Search"
app:layout_constraintBaseline_toBaselineOf="@+id/editText3"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/editText3" />
app:layout_constraintTop_toBottomOf="@+id/editText3" />
<TextView
android:id="@+id/textView2"
......@@ -85,7 +88,7 @@
android:layout_height="131dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginTop="76dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/my_border"
......@@ -93,7 +96,39 @@
android:text="Search Results"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText3" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:checkedButton="@+id/radio_title"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2">
<RadioButton
android:id="@+id/radio_title"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/search_title" />
<RadioButton
android:id="@+id/radio_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/search_author" />
</RadioGroup>
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
......@@ -3,6 +3,8 @@
<string name="edit_message1">Enter an Author</string>
<string name="edit_message2">Enter a Title</string>
<string name="button_send">Send</string>
<string name="search_text">Search by title</string>
<string name="search_rejection">No Book By Title</string>
<string name="search_text">Search for books</string>
<string name="search_rejection">No books match the search criteria</string>
<string name="search_title">Title</string>
<string name="search_author">Author</string>
</resources>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment