From 0dc0449c9c0c42eead0c6ed8ad58d43bc7658d99 Mon Sep 17 00:00:00 2001 From: ahhhlvin Date: Sun, 30 Aug 2015 18:08:14 -0400 Subject: [PATCH 1/3] commit 6PM --- src/main/AndroidManifest.xml | 6 - src/main/java/nyc/c4q/Book.java | 174 ++++++++++++++++++ src/main/java/nyc/c4q/Fragment_pace_calc.java | 96 ++++++++++ src/main/java/nyc/c4q/LibraryActivity.java | 28 +++ src/main/java/nyc/c4q/ListActivity.java | 173 ++++++++++++++--- src/main/java/nyc/c4q/Member.java | 86 +++++++++ .../java/nyc/c4q/PaceCalculatorActivity.java | 19 ++ src/main/java/nyc/c4q/Person.java | 12 ++ .../res/layout/activity_pace_calculator.xml | 13 +- src/main/res/raw/books.json | 2 +- 10 files changed, 573 insertions(+), 36 deletions(-) create mode 100644 src/main/java/nyc/c4q/Book.java create mode 100644 src/main/java/nyc/c4q/Fragment_pace_calc.java create mode 100644 src/main/java/nyc/c4q/Member.java diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index f96e9d3..47056e3 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -16,12 +16,6 @@ - - - - { + + TextView house, name; + + + public ListAdapter(Context context, int resource) { + super(context, resource); + } + + public ListAdapter(Context context, int resource, Person[] objects) { + super(context, resource, objects); + } + + + + @Override + public View getView(int position, View v, ViewGroup parent) { + + Person person = getItem(position); + + if (v == null) { + LayoutInflater vi; + vi = LayoutInflater.from(getContext()); + v = vi.inflate(R.layout.listitem_member, null); + } + + house = (TextView) v.findViewById(R.id.text_house); + name = (TextView) v.findViewById(R.id.text_name); + + house.setText(person.getHouse().toString()); + + + if (buttonName.getText().toString().equals("Last, First")) { + name.setText(person.getLastName() + ", " + person.getFirstName()); + } else if (buttonName.getText().toString().equals("First Last")){ + name.setText(person.getFirstName() + " " + person.getLastName()); + listAdapter.notifyDataSetChanged(); + list.setAdapter(listAdapter); + } + + if (buttonColor.getText().toString().equals("Show Color")) { + v.setBackgroundColor(Color.BLACK); + } else if (buttonColor.getText().toString().equals("Hide Color")) { + if (house.getText().equals("Gryffindor")) { + v.setBackgroundResource(R.color.gryffindor_red); + } else if (house.getText().equals("Ravenclaw")) { + v.setBackgroundResource(R.color.ravenclaw_blue); + } else if (house.getText().equals("Slytherin")) { + v.setBackgroundResource(R.color.slytherin_green); + } else if (house.getText().equals("Hufflepuff")) { + v.setBackgroundResource(R.color.hufflepuff_yellow); + } + } + + + return v; + } } } + diff --git a/src/main/java/nyc/c4q/Member.java b/src/main/java/nyc/c4q/Member.java new file mode 100644 index 0000000..9e751bb --- /dev/null +++ b/src/main/java/nyc/c4q/Member.java @@ -0,0 +1,86 @@ +package nyc.c4q; + +import com.google.gson.annotations.SerializedName; + +/** + * Created by alvin2 on 8/30/15. + */ +public class Member { + + @SerializedName("id") + private int memberID; + + @SerializedName("name") + private String memberName; + + @SerializedName("dob_month") + private int memberDOBmonth; + + @SerializedName("dob_day") + private int memberDOBday; + + @SerializedName("dob_year") + private int memberDOByear; + + @SerializedName("city") + private String memberCity; + + @SerializedName("state") + private String memberState; + + public int getMemberID() { + return memberID; + } + + public void setMemberID(int memberID) { + this.memberID = memberID; + } + + public String getMemberName() { + return memberName; + } + + public void setMemberName(String memberName) { + this.memberName = memberName; + } + + public int getMemberDOBmonth() { + return memberDOBmonth; + } + + public void setMemberDOBmonth(int memberDOBmonth) { + this.memberDOBmonth = memberDOBmonth; + } + + public int getMemberDOBday() { + return memberDOBday; + } + + public void setMemberDOBday(int memberDOBday) { + this.memberDOBday = memberDOBday; + } + + public int getMemberDOByear() { + return memberDOByear; + } + + public void setMemberDOByear(int memberDOByear) { + this.memberDOByear = memberDOByear; + } + + public String getMemberCity() { + return memberCity; + } + + public void setMemberCity(String memberCity) { + this.memberCity = memberCity; + } + + public String getMemberState() { + return memberState; + } + + public void setMemberState(String memberState) { + this.memberState = memberState; + } +} diff --git a/src/main/java/nyc/c4q/PaceCalculatorActivity.java b/src/main/java/nyc/c4q/PaceCalculatorActivity.java index 5c0616e..12ef6f3 100644 --- a/src/main/java/nyc/c4q/PaceCalculatorActivity.java +++ b/src/main/java/nyc/c4q/PaceCalculatorActivity.java @@ -1,14 +1,33 @@ package nyc.c4q; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.os.Bundle; +import android.widget.Button; +import android.widget.EditText; +import android.widget.FrameLayout; +import android.widget.LinearLayout; public class PaceCalculatorActivity extends FragmentActivity { + FrameLayout frameLayout; + LinearLayout linearLayout; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pace_calculator); + + Fragment fragment = new Fragment_pace_calc(); + + frameLayout = (FrameLayout) findViewById(R.id.activity_pace_calculator); + linearLayout = (LinearLayout) findViewById(R.id.linear_layout); + + + android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); + fragmentManager.beginTransaction().replace(R.id.linear_layout, fragment).commit(); + + } } diff --git a/src/main/java/nyc/c4q/Person.java b/src/main/java/nyc/c4q/Person.java index 2f2f167..f29d842 100644 --- a/src/main/java/nyc/c4q/Person.java +++ b/src/main/java/nyc/c4q/Person.java @@ -10,4 +10,16 @@ public Person(String firstName, String lastName, House house) { this.lastName = lastName; this.house = house; } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public House getHouse() { + return house; + } } diff --git a/src/main/res/layout/activity_pace_calculator.xml b/src/main/res/layout/activity_pace_calculator.xml index ed76b3e..72b06d2 100644 --- a/src/main/res/layout/activity_pace_calculator.xml +++ b/src/main/res/layout/activity_pace_calculator.xml @@ -5,4 +5,15 @@ android:layout_width="match_parent" android:layout_height="match_parent" tools:context="nyc.c4q.PaceCalculatorActivity" - /> + > + + + + + + + diff --git a/src/main/res/raw/books.json b/src/main/res/raw/books.json index 8cc9673..79b378e 100644 --- a/src/main/res/raw/books.json +++ b/src/main/res/raw/books.json @@ -838,7 +838,7 @@ "isbn": "0439554934", "isbn13": "9780439554930", "publisher": "Scholastic", - "publishyear": 2003. + "publishyear": 2003, "checkedout": true, "checkedoutby": 38, "checkoutdateyear": 2015, From c085e21aa3636b3fc0dbc9c203606e56eb30cc98 Mon Sep 17 00:00:00 2001 From: ahhhlvin Date: Sun, 30 Aug 2015 18:14:26 -0400 Subject: [PATCH 2/3] final commit --- src/main/java/nyc/c4q/Book.java | 12 ++++++------ src/main/java/nyc/c4q/LibraryActivity.java | 13 +++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main/java/nyc/c4q/Book.java b/src/main/java/nyc/c4q/Book.java index 5025160..93d6d23 100644 --- a/src/main/java/nyc/c4q/Book.java +++ b/src/main/java/nyc/c4q/Book.java @@ -8,10 +8,10 @@ public class Book { @SerializedName("id") - private int bookTitle; + private int bookID; @SerializedName("title") - private String bookID; + private String bookTitle; @SerializedName("author") private String bookAuthor; @@ -52,19 +52,19 @@ public class Book { @SerializedName("duedateyear") private int bookDueYear; - public int getBookTitle() { + public String getBookTitle() { return bookTitle; } - public void setBookTitle(int bookTitle) { + public void setBookTitle(String bookTitle) { this.bookTitle = bookTitle; } - public String getBookID() { + public int getBookID() { return bookID; } - public void setBookID(String bookID) { + public void setBookID(int bookID) { this.bookID = bookID; } diff --git a/src/main/java/nyc/c4q/LibraryActivity.java b/src/main/java/nyc/c4q/LibraryActivity.java index ae0fcfb..d1b892e 100644 --- a/src/main/java/nyc/c4q/LibraryActivity.java +++ b/src/main/java/nyc/c4q/LibraryActivity.java @@ -3,6 +3,7 @@ import android.app.Activity; import android.os.Bundle; import android.view.View; +import android.widget.Button; import android.widget.EditText; import android.widget.TextView; @@ -13,6 +14,7 @@ public class LibraryActivity extends Activity { + Button buttonMemberInfo, buttonBookInfo, buttonCheckedOut; public TextView textDisplay; public EditText inputParameter; Book book; @@ -26,12 +28,23 @@ protected void onCreate(Bundle savedInstanceState) { inputParameter = (EditText) findViewById(R.id.input_parameter); textDisplay = (TextView) findViewById(R.id.text_display); + buttonMemberInfo = (Button) findViewById(R.id.button_getMember); + buttonBookInfo = (Button) findViewById(R.id.button_getBook); + buttonCheckedOut = (Button) findViewById(R.id.button_getCheckedOut); + buttonCheckedOut.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + checkOut(member.getMemberID(), book.getBookID()); + } + }); + Gson gson = new Gson(); book = gson.fromJson("books.json", Book.class); member = gson.fromJson("members.json", Member.class); + } public void checkOut(int memberId, int bookId) { From b23c5979e7532482037a1446e50645b7e73c9a2d Mon Sep 17 00:00:00 2001 From: ahhhlvin Date: Sun, 27 Sep 2015 22:39:10 -0400 Subject: [PATCH 3/3] finished all exercises --- build.gradle | 4 + src/main/java/nyc/c4q/Book.java | 382 +++++++++++++---- .../java/nyc/c4q/FirstNameComparator.java | 18 + src/main/java/nyc/c4q/JsonHelper.java | 41 ++ src/main/java/nyc/c4q/LastNameComparator.java | 16 + src/main/java/nyc/c4q/LibraryActivity.java | 298 +++++++++++-- .../java/nyc/c4q/LibraryDatabaseContract.java | 44 ++ .../java/nyc/c4q/LibraryDatabaseHelper.java | 395 ++++++++++++++++++ src/main/java/nyc/c4q/ListActivity.java | 59 ++- src/main/java/nyc/c4q/Member.java | 185 ++++++-- src/main/java/nyc/c4q/MySQLiteOpenHelper.java | 181 ++++++++ 11 files changed, 1438 insertions(+), 185 deletions(-) create mode 100644 src/main/java/nyc/c4q/FirstNameComparator.java create mode 100644 src/main/java/nyc/c4q/JsonHelper.java create mode 100644 src/main/java/nyc/c4q/LastNameComparator.java create mode 100644 src/main/java/nyc/c4q/LibraryDatabaseContract.java create mode 100644 src/main/java/nyc/c4q/LibraryDatabaseHelper.java create mode 100644 src/main/java/nyc/c4q/MySQLiteOpenHelper.java diff --git a/build.gradle b/build.gradle index 7c6b66b..bfc6ca9 100644 --- a/build.gradle +++ b/build.gradle @@ -51,7 +51,11 @@ dependencies { // better version of Java's HttpURLConnection compile 'com.squareup.okhttp:okhttp:2.4.0' + compile 'com.j256.ormlite:ormlite-android:4.48' compile 'com.google.code.gson:gson:2.3.1' + compile fileTree(dir: 'libs', include: '*.jar') + compile 'com.squareup.retrofit:retrofit:1.9.0' + compile 'de.greenrobot:eventbus:2.2.1' // TODO: requires special build of robolectric right now. working on this... testCompile('org.robolectric:robolectric:2.4') { diff --git a/src/main/java/nyc/c4q/Book.java b/src/main/java/nyc/c4q/Book.java index 93d6d23..f56a489 100644 --- a/src/main/java/nyc/c4q/Book.java +++ b/src/main/java/nyc/c4q/Book.java @@ -1,174 +1,372 @@ package nyc.c4q; -import com.google.gson.annotations.SerializedName; +import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.table.DatabaseTable; + +import java.util.HashMap; +import java.util.Map; /** - * Created by alvin2 on 8/30/15. + * Created by Hoshiko on 8/30/15. */ + +@DatabaseTable(tableName = "books") public class Book { + @DatabaseField + private Integer id; + + @DatabaseField + private String title; + + @DatabaseField + private String author; + + @DatabaseField + private String isbn; + + @DatabaseField + private String isbn13; + + @DatabaseField + private String publisher; - @SerializedName("id") - private int bookID; + @DatabaseField + private Integer publishyear; - @SerializedName("title") - private String bookTitle; + @DatabaseField + private Boolean checkedout; - @SerializedName("author") - private String bookAuthor; + @DatabaseField + private Integer checkedoutby; - @SerializedName("isbn") - private String bookISBN; + @DatabaseField + private Integer checkoutdateyear; - @SerializedName("isbn13") - private String bookISBN13; + @DatabaseField + private Integer checkoutdatemonth; - @SerializedName("publisher") - private String bookPublisher; + @DatabaseField + private Integer checkoutdateday; - @SerializedName("publishyear") - private int bookPublishYear; + @DatabaseField + private Integer duedateyear; - @SerializedName("checkedout") - private int bookCheckedOut; + @DatabaseField + private Integer duedatemonth; - @SerializedName("checkedoutby") - private int bookCheckedOutBy; + @DatabaseField + private Integer duedateday; - @SerializedName("checkoutdateyear") - private int bookCheckedOutYear; + private Map additionalProperties = new HashMap(); - @SerializedName("checkoutdatemonth") - private int bookCheckedOutMonth; - @SerializedName("checkoutdateday") - private int bookCheckedOutDay; + public Book(Integer id, String title, String author, String isbn, String isbn13, String publisher, Integer publishyear, Boolean checkedout, Integer checkedoutby, Integer checkoutdateyear, Integer checkoutdatemonth, Integer checkoutdateday, Integer duedateyear, Integer duedatemonth, Integer duedateday) { + this.id = id; + this.title = title; + this.author = author; + this.isbn = isbn; + this.isbn13 = isbn13; + this.publisher = publisher; + this.publishyear = publishyear; + this.checkedout = checkedout; + this.checkedoutby = checkedoutby; + this.checkoutdateyear = checkoutdateyear; + this.checkoutdatemonth = checkoutdatemonth; + this.checkoutdateday = checkoutdateday; + this.duedateyear = duedateyear; + this.duedatemonth = duedatemonth; + this.duedateday = duedateday; + } - @SerializedName("duedatemonth") - private int bookDueMonth; + public Book(Integer id, String title, String author, String isbn, String isbn13, String publisher, Integer publishyear) { + this.id = id; + this.title = title; + this.author = author; + this.isbn = isbn; + this.isbn13 = isbn13; + this.publisher = publisher; + this.publishyear = publishyear; - @SerializedName("duedateday") - private int bookDueDay; + } - @SerializedName("duedateyear") - private int bookDueYear; + Book (){} + /** + * + * @return + * The id + */ + public Integer getId() { + return id; + } - public String getBookTitle() { - return bookTitle; + /** + * + * @param id + * The id + */ + public void setId(Integer id) { + this.id = id; } - public void setBookTitle(String bookTitle) { - this.bookTitle = bookTitle; + /** + * + * @return + * The title + */ + public String getTitle() { + return title; } - public int getBookID() { - return bookID; + /** + * + * @param title + * The title + */ + public void setTitle(String title) { + this.title = title; } - public void setBookID(int bookID) { - this.bookID = bookID; + /** + * + * @return + * The author + */ + public String getAuthor() { + return author; } - public String getBookAuthor() { - return bookAuthor; + /** + * + * @param author + * The author + */ + public void setAuthor(String author) { + this.author = author; } - public void setBookAuthor(String bookAuthor) { - this.bookAuthor = bookAuthor; + /** + * + * @return + * The isbn + */ + public String getIsbn() { + return isbn; } - public String getBookISBN() { - return bookISBN; + /** + * + * @param isbn + * The isbn + */ + public void setIsbn(String isbn) { + this.isbn = isbn; } - public void setBookISBN(String bookISBN) { - this.bookISBN = bookISBN; + /** + * + * @return + * The isbn13 + */ + public String getIsbn13() { + return isbn13; } - public String getBookISBN13() { - return bookISBN13; + /** + * + * @param isbn13 + * The isbn13 + */ + public void setIsbn13(String isbn13) { + this.isbn13 = isbn13; } - public void setBookISBN13(String bookISBN13) { - this.bookISBN13 = bookISBN13; + /** + * + * @return + * The publisher + */ + public String getPublisher() { + return publisher; } - public String getBookPublisher() { - return bookPublisher; + /** + * + * @param publisher + * The publisher + */ + public void setPublisher(String publisher) { + this.publisher = publisher; } - public void setBookPublisher(String bookPublisher) { - this.bookPublisher = bookPublisher; + /** + * + * @return + * The publishyear + */ + public Integer getPublishyear() { + return publishyear; } - public int getBookPublishYear() { - return bookPublishYear; + /** + * + * @param publishyear + * The publishyear + */ + public void setPublishyear(Integer publishyear) { + this.publishyear = publishyear; } - public void setBookPublishYear(int bookPublishYear) { - this.bookPublishYear = bookPublishYear; + /** + * + * @return + * The checkedout + */ + public Boolean getCheckedout() { + return checkedout; } - public int getBookCheckedOut() { - return bookCheckedOut; + /** + * + * @param checkedout + * The checkedout + */ + public void setCheckedout(Boolean checkedout) { + this.checkedout = checkedout; } - public void setBookCheckedOut(int bookCheckedOut) { - this.bookCheckedOut = bookCheckedOut; + /** + * + * @return + * The checkedoutby + */ + public Integer getCheckedoutby() { + return checkedoutby; } - public int getBookCheckedOutBy() { - return bookCheckedOutBy; + /** + * + * @param checkedoutby + * The checkedoutby + */ + public void setCheckedoutby(Integer checkedoutby) { + this.checkedoutby = checkedoutby; } - public void setBookCheckedOutBy(int bookCheckedOutBy) { - this.bookCheckedOutBy = bookCheckedOutBy; + /** + * + * @return + * The checkoutdateyear + */ + public Integer getCheckoutdateyear() { + return checkoutdateyear; } - public int getBookCheckedOutYear() { - return bookCheckedOutYear; + /** + * + * @param checkoutdateyear + * The checkoutdateyear + */ + public void setCheckoutdateyear(Integer checkoutdateyear) { + this.checkoutdateyear = checkoutdateyear; } - public void setBookCheckedOutYear(int bookCheckedOutYear) { - this.bookCheckedOutYear = bookCheckedOutYear; + /** + * + * @return + * The checkoutdatemonth + */ + public Integer getCheckoutdatemonth() { + return checkoutdatemonth; } - public int getBookCheckedOutMonth() { - return bookCheckedOutMonth; + /** + * + * @param checkoutdatemonth + * The checkoutdatemonth + */ + public void setCheckoutdatemonth(Integer checkoutdatemonth) { + this.checkoutdatemonth = checkoutdatemonth; } - public void setBookCheckedOutMonth(int bookCheckedOutMonth) { - this.bookCheckedOutMonth = bookCheckedOutMonth; + /** + * + * @return + * The checkoutdateday + */ + public Integer getCheckoutdateday() { + return checkoutdateday; } - public int getBookCheckedOutDay() { - return bookCheckedOutDay; + /** + * + * @param checkoutdateday + * The checkoutdateday + */ + public void setCheckoutdateday(Integer checkoutdateday) { + this.checkoutdateday = checkoutdateday; } - public void setBookCheckedOutDay(int bookCheckedOutDay) { - this.bookCheckedOutDay = bookCheckedOutDay; + /** + * + * @return + * The duedateyear + */ + public Integer getDuedateyear() { + return duedateyear; } - public int getBookDueMonth() { - return bookDueMonth; + /** + * + * @param duedateyear + * The duedateyear + */ + public void setDuedateyear(Integer duedateyear) { + this.duedateyear = duedateyear; } - public void setBookDueMonth(int bookDueMonth) { - this.bookDueMonth = bookDueMonth; + /** + * + * @return + * The duedatemonth + */ + public Integer getDuedatemonth() { + return duedatemonth; } - public int getBookDueDay() { - return bookDueDay; + /** + * + * @param duedatemonth + * The duedatemonth + */ + public void setDuedatemonth(Integer duedatemonth) { + this.duedatemonth = duedatemonth; } - public void setBookDueDay(int bookDueDay) { - this.bookDueDay = bookDueDay; + /** + * + * @return + * The duedateday + */ + public Integer getDuedateday() { + return duedateday; } - public int getBookDueYear() { - return bookDueYear; + /** + * + * @param duedateday + * The duedateday + */ + public void setDuedateday(Integer duedateday) { + this.duedateday = duedateday; } - public void setBookDueYear(int bookDueYear) { - this.bookDueYear = bookDueYear; + public Map getAdditionalProperties() { + return this.additionalProperties; } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + } diff --git a/src/main/java/nyc/c4q/FirstNameComparator.java b/src/main/java/nyc/c4q/FirstNameComparator.java new file mode 100644 index 0000000..a6302ed --- /dev/null +++ b/src/main/java/nyc/c4q/FirstNameComparator.java @@ -0,0 +1,18 @@ +package nyc.c4q; + +import java.util.Comparator; + +/** + * Created by alvin2 on 9/26/15. + */ +public class FirstNameComparator implements Comparator { + + @Override + public int compare(Person person, Person person2) { + if (person.firstName.equals(person2.firstName)) { + return person.lastName.compareTo(person2.lastName); + } + + return person.firstName.compareTo(person2.firstName); + } +} diff --git a/src/main/java/nyc/c4q/JsonHelper.java b/src/main/java/nyc/c4q/JsonHelper.java new file mode 100644 index 0000000..36ab811 --- /dev/null +++ b/src/main/java/nyc/c4q/JsonHelper.java @@ -0,0 +1,41 @@ +package nyc.c4q; + +import android.content.Context; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.ArrayList; + +/** + * Created by Hoshiko on 9/1/15. + */ +public class JsonHelper { + + // TODO: This class converts the raw JSON data into java objects (within an ArrayList) to be accessed by LibraryActivity.java + + public static ArrayList loadBookJsonRawUsingGson(Context context, int jsonResource) { + + InputStream raw = context.getResources().openRawResource(jsonResource); + Reader rd = new BufferedReader(new InputStreamReader(raw)); + Gson gson = new Gson(); + // TODO: TypeToken forces JSON to convert into an ArrayList of Books instead of just ONE book + ArrayList bookResponse = gson.fromJson(rd, new TypeToken>() {}.getType()); + return bookResponse; + } + + public static ArrayList loadMemberJsonRawUsingGson(Context context, int jsonResource) { + + InputStream raw = context.getResources().openRawResource(jsonResource); + Reader rd = new BufferedReader(new InputStreamReader(raw)); + Gson gson = new Gson(); + + ArrayList memberResponse = gson.fromJson(rd, new TypeToken>() {}.getType()); + + return memberResponse; + } +} diff --git a/src/main/java/nyc/c4q/LastNameComparator.java b/src/main/java/nyc/c4q/LastNameComparator.java new file mode 100644 index 0000000..d4e6310 --- /dev/null +++ b/src/main/java/nyc/c4q/LastNameComparator.java @@ -0,0 +1,16 @@ +package nyc.c4q; + +/** + * Created by alvin2 on 9/26/15. + */ +public class LastNameComparator implements java.util.Comparator { + @Override + public int compare(Person person, Person person2) { + if (person.lastName.equals(person2.lastName)) { + return person.firstName.compareTo(person2.firstName); + } + + return person.lastName.compareTo(person2.lastName); + } +} + diff --git a/src/main/java/nyc/c4q/LibraryActivity.java b/src/main/java/nyc/c4q/LibraryActivity.java index d1b892e..c8753c1 100644 --- a/src/main/java/nyc/c4q/LibraryActivity.java +++ b/src/main/java/nyc/c4q/LibraryActivity.java @@ -1,50 +1,116 @@ package nyc.c4q; import android.app.Activity; +import android.os.AsyncTask; import android.os.Bundle; import android.view.View; -import android.widget.Button; import android.widget.EditText; import android.widget.TextView; -import com.google.gson.Gson; - +import java.sql.SQLException; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; public class LibraryActivity extends Activity { - Button buttonMemberInfo, buttonBookInfo, buttonCheckedOut; - public TextView textDisplay; + // TODO: + // 1) Member/Book Class + // 2) JsonHelper + // 3) MYSQLiteOpenHelper + // 4) LibraryActivity + + + // TODO: This class inserts & retrieves the data from the SQLite database + public EditText inputParameter; - Book book; - Member member; + List allMembers; + List allBooks; + MySQLiteOpenHelper helper; + String memberName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_library); - inputParameter = (EditText) findViewById(R.id.input_parameter); - textDisplay = (TextView) findViewById(R.id.text_display); - - buttonMemberInfo = (Button) findViewById(R.id.button_getMember); - buttonBookInfo = (Button) findViewById(R.id.button_getBook); - buttonCheckedOut = (Button) findViewById(R.id.button_getCheckedOut); - buttonCheckedOut.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - checkOut(member.getMemberID(), book.getBookID()); + + helper = MySQLiteOpenHelper.getInstance(getApplicationContext()); + + try { + allMembers =helper.loadAllMember(); + allBooks = helper.loadAllBook(); + + } catch (SQLException e) { + e.printStackTrace(); + } + + if (allBooks.size() == 0){ + + ArrayList bookList = new ArrayList<>(); + bookList = JsonHelper.loadBookJsonRawUsingGson(this, R.raw.books); + for (int i = 0; i < bookList.size(); i++){ + + Book book = bookList.get(i); + int id = book.getId(); + String title = book.getTitle(); + String author = book.getAuthor(); + String isbn = book.getIsbn(); + String isbn13 = book.getIsbn13(); + String publisher = book.getPublisher(); + int publishyear = book.getPublishyear(); + Boolean checkedout = book.getCheckedout(); + if (checkedout != null) { + int checkedoutby = book.getCheckedoutby(); + int checkoutdateyear = book.getCheckoutdateyear(); + int checkoutdatemonth = book.getCheckoutdatemonth(); + int checkoutdateday = book.getCheckoutdateday(); + int duedateyear = book.getDuedateyear(); + int duedatemonth = book.getDuedatemonth(); + int duedateday = book.getDuedateday(); + + helper.insertBookCheckedOutData(id, title, author, isbn, isbn13, publisher, publishyear, checkedout, + checkedoutby, checkoutdateyear, checkoutdatemonth, checkoutdateday, duedateyear, duedatemonth, duedateday); + } else{ + helper.insertBookData(id, title, author, isbn, isbn13, publisher, publishyear); + } } - }); + } + + try { + allMembers =helper.loadAllMember(); + } catch (SQLException e) { + e.printStackTrace(); + } - Gson gson = new Gson(); - book = gson.fromJson("books.json", Book.class); - member = gson.fromJson("members.json", Member.class); + if (allMembers.size() == 0){ + ArrayList memberList = new ArrayList<>(); + memberList = JsonHelper.loadMemberJsonRawUsingGson(this, R.raw.members); + for (int i = 0; i < memberList.size(); i++){ + Member member = memberList.get(i); + String name = member.getName(); + int memberId = member.getId(); + String city = member.getCity(); + String state = member.getState(); + int dobDay = member.getDobDay(); + int dobMonth = member.getDobMonth(); + int dobYear = member.getDobYear(); + helper.insertMemberData(memberId, name, dobMonth, dobDay, dobYear, city, state); + } + } + + } + + @Override + protected void onResume() { + super.onResume(); +// LibraryDataSource dataSource = new LibraryDataSource(this); } public void checkOut(int memberId, int bookId) { @@ -62,36 +128,200 @@ public boolean checkIn(int memberId, int bookId) { return false; } - public void button_getMember_onClick(View view) { - String name = inputParameter.getText().toString(); + public void button_getMember_onClick(View view) throws SQLException { + if (inputParameter.equals("")) { - // TODO Display member information for the member with the given name. + } else { + String name = inputParameter.getText().toString(); - textDisplay.setText(member.getMemberID() + "\n" + member.getMemberName() + "\n" + member.getMemberDOBmonth() + "/" + member.getMemberDOBday() + "/" + member.getMemberDOByear() + "\n" + - member.getMemberCity() + ", " + member.getMemberState()); + // TODO Display member information for the member with the given name. + new MemberDataSeachTask().execute(name); + } } public void button_getBook_onClick(View view) { - String isbn = inputParameter.getText().toString(); - - // TODO Display book information for the book with the given ISBN. - textDisplay.setText(book.getBookID() + "\n" + book.getBookTitle() + "\n" + book.getBookAuthor() + "\n" + book.getBookISBN() + "\n" + book.getBookISBN13() + "\n" + book.getBookPublisher() + "\n" + book.getBookPublishYear()); + if (inputParameter.equals("")) { + } else { + String isbn = inputParameter.getText().toString(); + // TODO Display book information for the book with the given ISBN. + new BooksDataSeachTask().execute(isbn); + } } public void button_getCheckedOut_onClick(View view) { - String name = inputParameter.getText().toString(); + if (inputParameter.equals("")) { + + } else { + + memberName = inputParameter.getText().toString(); + + // TODO Display a list of books that the member with the given name + // currently has checked out, ordered by due date, with the + // earliest due first. + + new BookCheckOutTask().execute(memberName); + } + } + + + + private class MemberDataSeachTask extends AsyncTask { + String name; + Member member; + @Override + protected Member doInBackground(String... names) { + + + try { + member = helper.loadMemberData(names[0]); + } catch (SQLException e) { + e.printStackTrace(); + } + return member; + } + + @Override + protected void onPostExecute(Member member) { + String memberName = member.getName(); + int memberId = member.getId(); + String city = member.getCity(); + String state = member.getState(); + int dobDay = member.getDobDay(); + int dobMonth = member.getDobMonth(); + int dobYear = member.getDobYear(); + + String result = "id: " + String.valueOf(memberId) + "\n" + + "name: " + memberName + "\n" + + "dob: " + String.valueOf(dobMonth) + "/" + String.valueOf(dobDay)+ "/" + String.valueOf(dobYear)+"\n" + + "location: " + city + ", " + state; + TextView textView = (TextView)findViewById(R.id.text_display); + textView.setText(result); + } + } - // TODO Display a list of books that the member with the given name - // currently has checked out, ordered by due date, with the - // earliest due first. + private class BooksDataSeachTask extends AsyncTask { + String isbn; + Book book; + @Override + protected Book doInBackground(String... isbns) { + + try { + book = helper.loadBookData(isbns[0]); + } catch (SQLException e) { + e.printStackTrace(); + } + return book; + } + + @Override + protected void onPostExecute(Book book) { + int id = book.getId(); + String title = book.getTitle(); + String author = book.getAuthor(); + String isbn = book.getIsbn(); + String isbn13 = book.getIsbn13(); + String publisher = book.getPublisher(); + int publishyear = book.getPublishyear(); + + String result = "id: " + String.valueOf(id) + "\n" + + "title: " + title + "\n" + + "author: " + author + "\n" + + "isbn: " + isbn + "\n" + + "isbn13: " + isbn13 + "\n" + + "publisher: " + publisher + "\n" + + "publication year: " + String.valueOf(publishyear); + TextView textView = (TextView) findViewById(R.id.text_display); + textView.setText(result); + + } } + + private class BookCheckOutTask extends AsyncTask >{ + ArrayList books; + + + + @Override + protected ArrayList doInBackground(String... memberNames) { + try { + books = (ArrayList) helper.loadCheckedOutBooks(memberNames[0]); + + } catch (SQLException e) { + e.printStackTrace(); + } + + return books; + } + + private String getFormattedDueDate(Book book) { + + int dueMonth = Integer.valueOf(String.valueOf(book.getDuedatemonth())); + int dueDate = Integer.valueOf(String.valueOf(book.getDuedateday())); + int dueYear = Integer.valueOf(String.valueOf(book.getDuedateyear())); + + return String.format("%04d%02d%02d", dueYear, dueMonth,dueDate); + } + + @Override + protected void onPostExecute(ArrayList books) { + String bookResult = ""; + Collections.sort(books,new Comparator(){ + @Override + public int compare(Book lhs, Book rhs) { + + String lhsDueDate = getFormattedDueDate(lhs); + String rhsDueDate = getFormattedDueDate(rhs); + + + return lhsDueDate.compareTo(rhsDueDate); + + } + + }); + + for (int i = 0; i 0; + } + + public void populateData() { + SQLiteDatabase db = getWritableDatabase(); + populateMembersData(db); + populateBooksData(db); + db.close(); + } + + private void populateMembersData(SQLiteDatabase db) { + JSONArray members = getJsonArray("members"); + for (int i = 0, length = members.length(); i != length; ++i) { + try { + JSONObject member = members.getJSONObject(i); + + ContentValues values = new ContentValues(); + values.put(LibraryDatabaseContract.Members._ID, member.getInt("id")); + values.put(LibraryDatabaseContract.Members.COLUMN_NAME_NAME, member.getString("name")); + values.put(LibraryDatabaseContract.Members.COLUMN_NAME_DOB_MONTH, member.getInt("dob_month")); + values.put(LibraryDatabaseContract.Members.COLUMN_NAME_DOB_DAY, member.getInt("dob_day")); + values.put(LibraryDatabaseContract.Members.COLUMN_NAME_DOB_YEAR, member.getInt("dob_year")); + values.put(LibraryDatabaseContract.Members.COLUMN_NAME_CITY, member.getString("city")); + values.put(LibraryDatabaseContract.Members.COLUMN_NAME_STATE, member.getString("state")); + + db.insert(LibraryDatabaseContract.Members.TABLE_NAME, null, values); + } + catch (JSONException e) { + Log.e("JSON EXCEPTION", e.getMessage()); + e.printStackTrace(); + } + } + } + + private void populateBooksData(SQLiteDatabase db) { + JSONArray books = getJsonArray("books"); + for (int i = 0, length = books.length(); i != length; ++i) { + try { + JSONObject book = books.getJSONObject(i); + + ContentValues values = new ContentValues(); + values.put(LibraryDatabaseContract.Books._ID, book.getInt("id")); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_TITLE, book.getString("title")); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_AUTHOR, book.getString("author")); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_ISBN, book.getString("isbn")); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_ISBN13, book.getString("isbn13")); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_PUBLISHER, book.getString("publisher")); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_PUBLISH_YEAR, book.getInt("publishyear")); + + if (book.has("checkedout") && book.getBoolean("checkedout")) { + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKED_OUT, book.getBoolean("checkedout") ? 1 : 0); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKED_OUT_BY, book.getInt("checkedoutby")); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKOUT_MONTH, book.getInt("checkoutdatemonth")); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKOUT_DAY, book.getInt("checkoutdateday")); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKOUT_YEAR, book.getInt("checkoutdateyear")); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_DUE_MONTH, book.getInt("duedatemonth")); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_DUE_DAY, book.getInt("duedateday")); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_DUE_YEAR, book.getInt("duedateyear")); + } + + db.insert(LibraryDatabaseContract.Books.TABLE_NAME, null, values); + } + catch (JSONException e) { + Log.e("JSON EXCEPTION", e.getMessage()); + e.printStackTrace(); + } + } + } + + private JSONArray getJsonArray(String file) { + int resId = resources.getIdentifier(file, "raw", packageName); + InputStream is = resources.openRawResource(resId); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + + String line; + StringBuilder sb = new StringBuilder(); + try { + while ((line = br.readLine()) != null) { + sb.append(line); + } + } + catch (IOException e) { + Log.e("DATABASE EXCEPTION", e.getMessage()); + e.printStackTrace(); + } + + String json = sb.toString(); + JSONArray jsonArray = null; + try { + jsonArray = new JSONArray(json); + } + catch (JSONException e) { + Log.e("JSON EXCEPTION", e.getMessage()); + e.printStackTrace(); + } + + return jsonArray; + } + + public String getMember(String name) { + SQLiteDatabase db = getReadableDatabase(); + + final String[] projection = { + LibraryDatabaseContract.Members._ID, + LibraryDatabaseContract.Members.COLUMN_NAME_NAME, + LibraryDatabaseContract.Members.COLUMN_NAME_DOB_MONTH, + LibraryDatabaseContract.Members.COLUMN_NAME_DOB_DAY, + LibraryDatabaseContract.Members.COLUMN_NAME_DOB_YEAR, + LibraryDatabaseContract.Members.COLUMN_NAME_CITY, + LibraryDatabaseContract.Members.COLUMN_NAME_STATE + }; + + final String selection = LibraryDatabaseContract.Members.COLUMN_NAME_NAME + "=?"; + + final String[] selectionArgs = { name }; + + Cursor cursor = db.query(LibraryDatabaseContract.Members.TABLE_NAME, + projection, + selection, + selectionArgs, + null, + null, + null); + + if (cursor.getCount() < 1) { return null; } + + cursor.moveToFirst(); + String result = + "id: " + cursor.getInt(0) + "\n" + + "name: " + cursor.getString(1) + "\n" + + "dob: " + cursor.getInt(2) + "/" + cursor.getInt(3) + "/" + cursor.getInt(4) + "\n" + + "location: " + cursor.getString(5) + ", " + cursor.getString(6); + + db.close(); + return result; + } + + public String getBook(String isbn) { + SQLiteDatabase db = getReadableDatabase(); + + final String[] projection = { + LibraryDatabaseContract.Books._ID, + LibraryDatabaseContract.Books.COLUMN_NAME_TITLE, + LibraryDatabaseContract.Books.COLUMN_NAME_AUTHOR, + LibraryDatabaseContract.Books.COLUMN_NAME_ISBN, + LibraryDatabaseContract.Books.COLUMN_NAME_ISBN13, + LibraryDatabaseContract.Books.COLUMN_NAME_PUBLISHER, + LibraryDatabaseContract.Books.COLUMN_NAME_PUBLISH_YEAR + }; + + final String selection = LibraryDatabaseContract.Books.COLUMN_NAME_ISBN + "=?"; + + final String[] selectionArgs = { isbn }; + + Cursor cursor = db.query(LibraryDatabaseContract.Books.TABLE_NAME, + projection, + selection, + selectionArgs, + null, + null, + null); + + if (cursor.getCount() < 1) { return null; } + + cursor.moveToFirst(); + String result = + "id: " + cursor.getInt(0) + "\n" + + "title: " + cursor.getString(1) + "\n" + + "author: " + cursor.getString(2) + "\n" + + "isbn: " + cursor.getString(3) + "\n" + + "isbn13: " + cursor.getString(4) + "\n" + + "publisher: " + cursor.getString(5) + "\n" + + "publication year: " + cursor.getInt(6); + + db.close(); + return result; + } + + public String getCheckedOut(String name) { + SQLiteDatabase db = getReadableDatabase(); + + final String query = + "SELECT b." + LibraryDatabaseContract.Books.COLUMN_NAME_TITLE + + ", b." + LibraryDatabaseContract.Books.COLUMN_NAME_AUTHOR + + ", b." + LibraryDatabaseContract.Books.COLUMN_NAME_CHECKOUT_MONTH + + ", b." + LibraryDatabaseContract.Books.COLUMN_NAME_CHECKOUT_DAY + + ", b." + LibraryDatabaseContract.Books.COLUMN_NAME_CHECKOUT_YEAR + + ", b." + LibraryDatabaseContract.Books.COLUMN_NAME_DUE_MONTH + + ", b." + LibraryDatabaseContract.Books.COLUMN_NAME_DUE_DAY + + ", b." + LibraryDatabaseContract.Books.COLUMN_NAME_DUE_YEAR + + " FROM " + LibraryDatabaseContract.Members.TABLE_NAME + " m" + + " JOIN " + LibraryDatabaseContract.Books.TABLE_NAME + " b" + + " ON m." + LibraryDatabaseContract.Members._ID + "=b." + LibraryDatabaseContract.Books.COLUMN_NAME_CHECKED_OUT_BY + + " WHERE m." + LibraryDatabaseContract.Members.COLUMN_NAME_NAME + "=?" + + " ORDER BY b." + LibraryDatabaseContract.Books.COLUMN_NAME_DUE_YEAR + " ASC" + + ", b." + LibraryDatabaseContract.Books.COLUMN_NAME_DUE_MONTH + " ASC" + + ", b." + LibraryDatabaseContract.Books.COLUMN_NAME_DUE_DAY + " ASC"; + + final String[] selectionArgs = { name }; + + Cursor cursor = db.rawQuery(query, selectionArgs); + + StringBuilder sb = new StringBuilder("name: " + name); + + while (cursor.moveToNext()) { + sb.append( + "\n-----\n" + + "title: " + cursor.getString(0) + "\n" + + "author: " + cursor.getString(1) + "\n" + + "checkout date: " + cursor.getInt(2) + "/" + cursor.getInt(3) + "/" + cursor.getInt(4) + "\n" + + "due date: " + cursor.getInt(5) + "/" + cursor.getInt(6) + "/" + cursor.getInt(7) + ); + } + + db.close(); + return sb.toString(); + } + + public void checkOut(int memberId, int bookId) { + SQLiteDatabase db = getWritableDatabase(); + + ContentValues values = new ContentValues(); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKED_OUT, 1); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKED_OUT_BY, memberId); + + Calendar calendar = Calendar.getInstance(); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKOUT_YEAR, calendar.get(Calendar.YEAR)); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKOUT_MONTH, calendar.get(Calendar.MONTH) + 1); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKOUT_DAY, calendar.get(Calendar.DATE)); + + calendar.add(Calendar.DATE, 14); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_DUE_YEAR, calendar.get(Calendar.YEAR)); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_DUE_MONTH, calendar.get(Calendar.MONTH) + 1); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_DUE_DAY, calendar.get(Calendar.DATE)); + + final String where = LibraryDatabaseContract.Books._ID + "=?"; + final String[] whereArgs = { String.valueOf(bookId) }; + + db.update(LibraryDatabaseContract.Books.TABLE_NAME, values, where, whereArgs); + db.close(); + } + + public boolean checkIn(int memberId, int bookId) { + boolean isLate = checkLate(memberId, bookId); + + SQLiteDatabase db = getWritableDatabase(); + + ContentValues values = new ContentValues(); + values.put(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKED_OUT, 0); + values.putNull(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKED_OUT_BY); + values.putNull(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKOUT_YEAR); + values.putNull(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKOUT_MONTH); + values.putNull(LibraryDatabaseContract.Books.COLUMN_NAME_CHECKOUT_DAY); + values.putNull(LibraryDatabaseContract.Books.COLUMN_NAME_DUE_YEAR); + values.putNull(LibraryDatabaseContract.Books.COLUMN_NAME_DUE_MONTH); + values.putNull(LibraryDatabaseContract.Books.COLUMN_NAME_DUE_DAY); + + final String where = LibraryDatabaseContract.Books._ID + "=?"; + final String[] whereArgs = { String.valueOf(bookId) }; + + db.update(LibraryDatabaseContract.Books.TABLE_NAME, values, where, whereArgs); + db.close(); + + return isLate; + } + + private boolean checkLate(int memberId, int bookId) { + SQLiteDatabase db = getReadableDatabase(); + + final String[] projection = { + LibraryDatabaseContract.Books.COLUMN_NAME_DUE_YEAR, + LibraryDatabaseContract.Books.COLUMN_NAME_DUE_MONTH, + LibraryDatabaseContract.Books.COLUMN_NAME_DUE_DAY + }; + + final String selection = LibraryDatabaseContract.Books._ID + "=? AND " + + LibraryDatabaseContract.Books.COLUMN_NAME_CHECKED_OUT_BY + "=?"; + + final String[] selectionArgs = { + String.valueOf(bookId), + String.valueOf(memberId) + }; + + Cursor cursor = db.query(LibraryDatabaseContract.Books.TABLE_NAME, + projection, + selection, + selectionArgs, + null, + null, + null); + + if (cursor.getCount() < 1) { return false; } + + cursor.moveToFirst(); + Calendar dueDate = Calendar.getInstance(); + dueDate.set(cursor.getInt(0), cursor.getInt(1) - 1, cursor.getInt(2)); + + db.close(); + return dueDate.after(Calendar.getInstance()); + } +} diff --git a/src/main/java/nyc/c4q/ListActivity.java b/src/main/java/nyc/c4q/ListActivity.java index 5871d1c..d4c1c4b 100644 --- a/src/main/java/nyc/c4q/ListActivity.java +++ b/src/main/java/nyc/c4q/ListActivity.java @@ -26,6 +26,8 @@ public class ListActivity extends Activity { public ListView list; ListAdapter listAdapter; Button buttonName, buttonColor; + boolean isColorShown; + SharedPreferences preferences; public static final Person[] PEOPLE = { new Person("Hannah", "Abbott", House.Hufflepuff), @@ -63,20 +65,26 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); + preferences = this.getSharedPreferences(SHARED_FILE, Context.MODE_PRIVATE); + isColorShown = preferences.getBoolean("color", false); + list = (ListView) findViewById(R.id.list); - listAdapter = new ListAdapter(getApplicationContext(), R.layout.listitem_member, PEOPLE); + listAdapter = new ListAdapter(getApplicationContext(), R.layout.listitem_member, PEOPLE, isColorShown); list.setAdapter(listAdapter); + buttonName = (Button) findViewById(R.id.button_name); buttonName.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (buttonName.getText().toString().equals("Last, First")) { buttonName.setText("First Last"); + listAdapter.setFirstnameFirst(true); listAdapter.notifyDataSetChanged(); list.setAdapter(listAdapter); } else if (buttonName.getText().toString().equals("First Last")) { buttonName.setText("Last, First"); + listAdapter.setFirstnameFirst(false); listAdapter.notifyDataSetChanged(); list.setAdapter(listAdapter); } @@ -90,37 +98,54 @@ public void onClick(View view) { public void onClick(View view) { if (buttonColor.getText().toString().equals("Show Color")) { buttonColor.setText("Hide Color"); - list.setAdapter(listAdapter); + listAdapter.setColorShown(false); + SharedPreferences.Editor editor = preferences.edit(); + editor.putBoolean("color", false); + // .commit() runs in the foreground so when onDestroy() is called on quitting the app, the info will be safely written to SharedPrefs before the app is destroyed + editor.commit(); + // refreshes the view of the listview + listAdapter.notifyDataSetChanged(); } else if (buttonColor.getText().toString().equals("Hide Color")) { buttonColor.setText("Show Color"); - list.setAdapter(listAdapter); + listAdapter.setColorShown(true); + SharedPreferences.Editor editor = preferences.edit(); + editor.putBoolean("color", true); + editor.commit(); + listAdapter.notifyDataSetChanged(); } } }); -// SharedPreferences preferences = this.getSharedPreferences(SHARED_FILE, Context.MODE_PRIVATE); -// SharedPreferences.Editor editor = preferences.edit(); -// editor.putInt("image_file", MODE_PRIVATE); -// editor.commit(); - - } public class ListAdapter extends ArrayAdapter { TextView house, name; + boolean firstNameFirst; + boolean isColorShown; + + public ListAdapter(Context context, int resource, Person[] objects, boolean isColorShown) { + super(context, resource, objects); + this.isColorShown = isColorShown; - public ListAdapter(Context context, int resource) { - super(context, resource); } - public ListAdapter(Context context, int resource, Person[] objects) { - super(context, resource, objects); + public void setColorShown(boolean isColorShown) { + this.isColorShown = isColorShown; } + public void setFirstnameFirst(boolean enabled) { + firstNameFirst = enabled; + + if (enabled) { + Arrays.sort(PEOPLE, new FirstNameComparator()); + } else { + Arrays.sort(PEOPLE, new LastNameComparator()); + } + } @Override public View getView(int position, View v, ViewGroup parent) { @@ -141,15 +166,13 @@ public View getView(int position, View v, ViewGroup parent) { if (buttonName.getText().toString().equals("Last, First")) { name.setText(person.getLastName() + ", " + person.getFirstName()); - } else if (buttonName.getText().toString().equals("First Last")){ + } else if (buttonName.getText().toString().equals("First Last")) { name.setText(person.getFirstName() + " " + person.getLastName()); - listAdapter.notifyDataSetChanged(); - list.setAdapter(listAdapter); } - if (buttonColor.getText().toString().equals("Show Color")) { + if (isColorShown) { v.setBackgroundColor(Color.BLACK); - } else if (buttonColor.getText().toString().equals("Hide Color")) { + } else { if (house.getText().equals("Gryffindor")) { v.setBackgroundResource(R.color.gryffindor_red); } else if (house.getText().equals("Ravenclaw")) { diff --git a/src/main/java/nyc/c4q/Member.java b/src/main/java/nyc/c4q/Member.java index 9e751bb..3a694aa 100644 --- a/src/main/java/nyc/c4q/Member.java +++ b/src/main/java/nyc/c4q/Member.java @@ -1,86 +1,189 @@ package nyc.c4q; import com.google.gson.annotations.SerializedName; +import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.table.DatabaseTable; + +import java.util.HashMap; +import java.util.Map; /** - * Created by alvin2 on 8/30/15. + * Created by Hoshiko on 8/30/15. */ -public class Member { - @SerializedName("id") - private int memberID; +// CREATED USING JSONSCHEMA2POJO.ORG -- COPY & PASTE FROM JSON TO CONVERT GSON; SELECT "NONE" + +@DatabaseTable(tableName = "members") +public class Member { + @DatabaseField + private Integer id; - @SerializedName("name") - private String memberName; + @DatabaseField + private String name; + // "@SeralizedName" is used when the java object isn't named the same as in the JSON @SerializedName("dob_month") - private int memberDOBmonth; + @DatabaseField + private Integer dobMonth; @SerializedName("dob_day") - private int memberDOBday; + @DatabaseField + private Integer dobDay; @SerializedName("dob_year") - private int memberDOByear; + @DatabaseField + private Integer dobYear; + + @DatabaseField + private String city; + + @DatabaseField + private String state; + private Map additionalProperties = new HashMap(); + + Member (){} + + public Member(int id, String name, int dobMonth, int dobDay, int dobYear, String city, String state) { + this.id = id; + this.name = name; + this.dobMonth = dobMonth; + this.dobDay = dobDay; + this.dobYear = dobYear; + this.city = city; + this.state = state; + } - @SerializedName("city") - private String memberCity; + /** + * + * @return + * The id + */ + public Integer getId() { + return id; + } - @SerializedName("state") - private String memberState; + /** + * + * @param id + * The id + */ + public void setId(Integer id) { + this.id = id; + } - public int getMemberID() { - return memberID; + /** + * + * @return + * The name + */ + public String getName() { + return name; } - public void setMemberID(int memberID) { - this.memberID = memberID; + /** + * + * @param name + * The name + */ + public void setName(String name) { + this.name = name; } - public String getMemberName() { - return memberName; + /** + * + * @return + * The dobMonth + */ + public Integer getDobMonth() { + return dobMonth; } - public void setMemberName(String memberName) { - this.memberName = memberName; + /** + * + * @param dobMonth + * The dob_month + */ + public void setDobMonth(Integer dobMonth) { + this.dobMonth = dobMonth; } - public int getMemberDOBmonth() { - return memberDOBmonth; + /** + * + * @return + * The dobDay + */ + public Integer getDobDay() { + return dobDay; } - public void setMemberDOBmonth(int memberDOBmonth) { - this.memberDOBmonth = memberDOBmonth; + /** + * + * @param dobDay + * The dob_day + */ + public void setDobDay(Integer dobDay) { + this.dobDay = dobDay; } - public int getMemberDOBday() { - return memberDOBday; + /** + * + * @return + * The dobYear + */ + public Integer getDobYear() { + return dobYear; } - public void setMemberDOBday(int memberDOBday) { - this.memberDOBday = memberDOBday; + /** + * + * @param dobYear + * The dob_year + */ + public void setDobYear(Integer dobYear) { + this.dobYear = dobYear; } - public int getMemberDOByear() { - return memberDOByear; + /** + * + * @return + * The city + */ + public String getCity() { + return city; } - public void setMemberDOByear(int memberDOByear) { - this.memberDOByear = memberDOByear; + /** + * + * @param city + * The city + */ + public void setCity(String city) { + this.city = city; } - public String getMemberCity() { - return memberCity; + /** + * + * @return + * The state + */ + public String getState() { + return state; } - public void setMemberCity(String memberCity) { - this.memberCity = memberCity; + /** + * + * @param state + * The state + */ + public void setState(String state) { + this.state = state; } - public String getMemberState() { - return memberState; + public Map getAdditionalProperties() { + return this.additionalProperties; } - public void setMemberState(String memberState) { - this.memberState = memberState; + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); } } diff --git a/src/main/java/nyc/c4q/MySQLiteOpenHelper.java b/src/main/java/nyc/c4q/MySQLiteOpenHelper.java new file mode 100644 index 0000000..13fc453 --- /dev/null +++ b/src/main/java/nyc/c4q/MySQLiteOpenHelper.java @@ -0,0 +1,181 @@ +package nyc.c4q; + +import android.content.Context; +import android.database.SQLException; +import android.database.sqlite.SQLiteDatabase; +import android.util.Log; + +import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; +import com.j256.ormlite.dao.Dao; +import com.j256.ormlite.dao.RuntimeExceptionDao; +import com.j256.ormlite.support.ConnectionSource; +import com.j256.ormlite.table.TableUtils; + +import java.util.List; + + + + +/** + * Created by Hoshiko on 8/30/15. + */ +public class MySQLiteOpenHelper extends OrmLiteSqliteOpenHelper { + + // TODO: Once JSON data from raw file is converted into ArrayList of Book/Member objects, create methods USING JSONHELPER.lass to interact objects with SQLite Database + + public static final String DATABASE_NAME = "mydb.db"; + public static final int DATABASE_VERSION = 2; + + private static MySQLiteOpenHelper mHelper; + + // PRIMARY KEY "INTEGER" + private Dao memberDao = null; + private Dao bookDao = null; + + private RuntimeExceptionDao simpleRuntimeDao = null; + private RuntimeExceptionDao simpleRuntimeDao2 = null; + + public MySQLiteOpenHelper(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + public static synchronized MySQLiteOpenHelper getInstance(Context context) { + if (mHelper == null) { + mHelper = new MySQLiteOpenHelper(context.getApplicationContext()); + } + + return mHelper; + } + + /** + * This is called when the database is first created. Usually you should call createTable statements here to create + * the tables that will store your data. + */ + @Override + public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) { + try { + TableUtils.createTable(connectionSource, Book.class); + TableUtils.createTable(connectionSource, Member.class); + } catch (SQLException e) { + Log.e(MySQLiteOpenHelper.class.getName(), "Can't create database", e); + throw new RuntimeException(e); + } catch (java.sql.SQLException e) { + e.printStackTrace(); + } + + } + + // onUpgrade only updates the TABLE columns (not the entries) + @Override + public void onUpgrade(SQLiteDatabase db,ConnectionSource connectionSource, int oldVersion, int newVersion) { + + try { + TableUtils.dropTable(connectionSource, Member.class, false); + TableUtils.dropTable(connectionSource, Book.class, false); + + }catch (java.sql.SQLException e) { + e.printStackTrace(); + } + onCreate(db, connectionSource); + + } + + public Dao getMembeDao() { + if (null == memberDao) { + try { + memberDao = getDao(Member.class); + }catch (java.sql.SQLException e) { + e.printStackTrace(); + } + } + return memberDao; + } + + + public void insertMemberData(int id, String name, int dobMonth, int dobDay, int dobYear, String city, String state) throws SQLException { + + Member member = new Member(id, name, dobMonth, dobDay, dobYear, city, state); + Dao dao = null; + try { + dao = getDao(Member.class); + dao.create(member); + } catch (java.sql.SQLException e) { + e.printStackTrace(); + } + + } + + + public Member loadMemberData(String name) throws SQLException, java.sql.SQLException { + // queryForFirst() will return just ONE result object + return getDao(Member.class).queryBuilder().where().eq("name", name).queryForFirst(); + } + + public List loadAllMember () throws java.sql.SQLException { + // no where statement because getting ALL objects from row + return getDao(Member.class).queryForAll(); + } + + public Dao getBookDao() { + if (null == bookDao) { + try { + bookDao = getDao(Book.class); + }catch (java.sql.SQLException e) { + e.printStackTrace(); + } + } + return bookDao; + } + + public void insertBookData(int id, String title, String author, String isbn, + String isbn13, String publisher, int publishyear) throws SQLException { + + Book book = new Book(id, title, author, isbn, isbn13, publisher, publishyear); + Dao dao = null; + try { + dao = getDao(Book.class); + dao.create(book); + } catch (java.sql.SQLException e) { + e.printStackTrace(); + } + + } + + public void insertBookCheckedOutData(int id, String title, String author, String isbn, + String isbn13, String publisher, int publishyear, + Boolean checkedout, int checkedoutby, int checkoutdateyear, + int checkoutdatemonth, int checkoutdateday, int duedateyear, + int duedatemonth, int duedateday) throws SQLException { + + Book book = new Book(id, title, author, isbn, isbn13, publisher, publishyear, checkedout, + checkedoutby, checkoutdateyear, checkoutdatemonth, checkoutdateday, duedateyear, duedatemonth, duedateday); + Dao dao = null; + try { + dao = getDao(Book.class); + dao.create(book); + } catch (java.sql.SQLException e) { + e.printStackTrace(); + } + + } + + + + public Book loadBookData(String isbn) throws SQLException, java.sql.SQLException { + return getDao(Book.class).queryBuilder().where().eq("isbn", isbn).queryForFirst(); + } + + public List loadAllBook () throws java.sql.SQLException { + return getDao(Book.class).queryForAll(); + } + + public List loadCheckedOutBooks (String name) throws java.sql.SQLException { + Member member = getDao(Member.class).queryBuilder().where().eq("name", name).queryForFirst(); + // once member object is accessed, go into row entry and retrieve member ID to access checked out books + int memberId = member.getId(); + + // .query() because not sure if the member borrowed more than 1 book + return getDao(Book.class).queryBuilder().where().eq("checkedout", true).and().eq("checkedoutby", memberId).query(); + } + +} \ No newline at end of file