パーセル可能なクラスに変換したい次のクラスがあります。このチュートリアルとこのスレッドに従ってみましたが、行き詰まりました。おそらく、あなたは私を正しい方向に押し出すことができますか?
現在のクラス
import java.util.ArrayList;
public class SimpleBookManager implements BookManager {
private ArrayList<Book> allBooks = new ArrayList<Book>();
public ArrayList<Book> getAllBooks(){
return allBooks;
}
public int count(){
return getAllBooks().size();
}
public Book getBook(int index){
return allBooks.get(index);
}
public Book createBook(){
Book book = new Book();
allBooks.add(book);
return book;
}
public void removeBook(Book book){
allBooks.remove(book);
//Remove instance of book
}
public void moveBook (int from, int to){
Book book1 = allBooks.get(from);
Book book2 = allBooks.get(to);
allBooks.add(to, book1);
allBooks.add(from, book2);
}
public int getMinPrice(){
ArrayList<Integer> allPrices = getAllPrices();
int smallestElem=allPrices.get(0);
for(int i=0; i < allPrices.size(); i++){
if (smallestElem > allPrices.get(i)){
smallestElem = allPrices.get(i);
}
}
return smallestElem;
}
public int getMaxPrice(){
ArrayList<Integer> allPrices = getAllPrices();
int biggestElem=allPrices.get(0);
for(int i=0; i < allPrices.size(); i++){
if (biggestElem < allPrices.get(i)){
biggestElem = allPrices.get(i);
}
}
return biggestElem;
}
public float getMeanPrice(){
ArrayList<Integer> allPrices = getAllPrices();
int total=0;
for(int i=0; i < allPrices.size(); i++){
total+=allPrices.get(i);
}
return total/allPrices.size();
}
public int getTotalCost(){
ArrayList<Integer> allPrices = getAllPrices();
int total=0;
for(int i=0; i < allPrices.size(); i++){
total+=allPrices.get(i);
}
return total;
}
public void saveChanges(){
//What to do here
}
private ArrayList<Integer> getAllPrices(){
int totalElements = allBooks.size();
ArrayList<Integer> allBookPrices = new ArrayList<Integer>();
//loop through it
for(int i=0; i < totalElements; i++){
allBookPrices.add(allBooks.get(i).getPrice());
}
return allBookPrices;
}
public SimpleBookManager(){
Book harryPotter1 = createBook();
Book harryPotter2 = createBook();
harryPotter1.setAuthor("JK Rowling");
harryPotter1.setCourse("Harry Potter Kunskap");
harryPotter1.setPrice(199);
harryPotter1.setTitle("Harry Potter and the philosifer Stone");
harryPotter1.setIsbn("9780590353403");
harryPotter2.setAuthor("JK Rowling");
harryPotter2.setCourse("Harry Potter Kunskap");
harryPotter2.setPrice(299);
harryPotter2.setTitle("Harry Potter and snake");
harryPotter2.setIsbn("0439064872");
}
}
どこまで来たか
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
public class SimpleBookManager implements BookManager, Parcelable{
private ArrayList<Book> allBooks = new ArrayList<Book>();
public ArrayList<Book> getAllBooks(){
return allBooks;
}
public int count(){
return getAllBooks().size();
}
public Book getBook(int index){
return allBooks.get(index);
}
public Book createBook(){
Book book = new Book();
allBooks.add(book);
return book;
}
public void removeBook(Book book){
allBooks.remove(book);
//Remove instance of book
}
public void moveBook (int from, int to){
Book book1 = allBooks.get(from);
Book book2 = allBooks.get(to);
allBooks.add(to, book1);
allBooks.add(from, book2);
}
public int getMinPrice(){
ArrayList<Integer> allPrices = getAllPrices();
int smallestElem=allPrices.get(0);
for(int i=0; i < allPrices.size(); i++){
if (smallestElem > allPrices.get(i)){
smallestElem = allPrices.get(i);
}
}
return smallestElem;
}
public int getMaxPrice(){
ArrayList<Integer> allPrices = getAllPrices();
int biggestElem=allPrices.get(0);
for(int i=0; i < allPrices.size(); i++){
if (biggestElem < allPrices.get(i)){
biggestElem = allPrices.get(i);
}
}
return biggestElem;
}
public float getMeanPrice(){
ArrayList<Integer> allPrices = getAllPrices();
int total=0;
for(int i=0; i < allPrices.size(); i++){
total+=allPrices.get(i);
}
return total/allPrices.size();
}
public int getTotalCost(){
ArrayList<Integer> allPrices = getAllPrices();
int total=0;
for(int i=0; i < allPrices.size(); i++){
total+=allPrices.get(i);
}
return total;
}
public void saveChanges(){
//What to do here
}
private ArrayList<Integer> getAllPrices(){
int totalElements = allBooks.size();
ArrayList<Integer> allBookPrices = new ArrayList<Integer>();
//loop through it
for(int i=0; i < totalElements; i++){
allBookPrices.add(allBooks.get(i).getPrice());
}
return allBookPrices;
}
public SimpleBookManager(){
Book harryPotter1 = createBook();
Book harryPotter2 = createBook();
harryPotter1.setAuthor("JK Rowling");
harryPotter1.setCourse("Harry Potter Kunskap");
harryPotter1.setPrice(199);
harryPotter1.setTitle("Harry Potter and the philosifer Stone");
harryPotter1.setIsbn("9780590353403");
harryPotter2.setAuthor("JK Rowling");
harryPotter2.setCourse("Harry Potter Kunskap");
harryPotter2.setPrice(299);
harryPotter2.setTitle("Harry Potter and snake");
harryPotter2.setIsbn("0439064872");
}
//parcel part, not finished_________________________________________________________________________________
public SimpleBookManager(Parcel in){
//...Incomplete
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeStringArray(new String[]{this.UserName,this.Password,String.valueOf(this.Action)});
}
public static final Parcelable.Creator<SimpleBookManager> CREATOR= new Parcelable.Creator<SimpleBookManager>() {
@Override
public SimpleBookManager createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new SimpleBookManager(source); //using parcelable constructor
}
@Override
public SimpleBookManager[] newArray(int size) {
// TODO Auto-generated method stub
return new SimpleBookManager[size];
}
};
}
拡張的な回答を求めるのは大変なことだと思いますが、おそらくコンストラクターがどのように見えるべきか、そしてどのように行われたかを確認できるようにいくつかの方法を教えてください。 . ありがとう =)