-
Notifications
You must be signed in to change notification settings - Fork 21
implementation #11
base: master
Are you sure you want to change the base?
implementation #11
Changes from 2 commits
3076a83
023cabd
02f56f1
23ff535
830b128
2fd6761
189ada3
1777846
36d1acb
0911b27
80dac44
b4e133b
3a368ba
5cb93d9
d3da531
9d03dc0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,78 @@ | ||
package ru.ok.technopolis.students; | ||
|
||
|
||
import android.content.Intent; | ||
import android.support.annotation.Nullable; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.View; | ||
|
||
import com.getbase.floatingactionbutton.FloatingActionButton; | ||
import ru.ok.technopolis.students.Repository.StudentDataRepository; | ||
|
||
|
||
public class MainActivity extends AppCompatActivity implements View.OnClickListener { | ||
|
||
private StudentDataRepository studentDataRepository = StudentDataRepository.getInstance(); | ||
private StudentAdapter studentAdapter; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
protected void onCreate(Bundle savedInstanceState) | ||
{ | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
setupRecyclerView(); | ||
setupAddButton(); | ||
} | ||
|
||
private void setupAddButton() | ||
{ | ||
FloatingActionButton addButton = findViewById(R.id.activity_main__add_button); | ||
addButton.setOnClickListener(this); | ||
} | ||
|
||
private void setupRecyclerView() { | ||
RecyclerView recyclerView = findViewById(R.id.activity_main__recyclerview); | ||
studentAdapter = new StudentAdapter (studentDataRepository.studentsOnRepository(), this::onStudentClick); | ||
recyclerView.setAdapter(studentAdapter); | ||
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); | ||
recyclerView.setLayoutManager(linearLayoutManager); | ||
} | ||
|
||
private void onStudentClick(Student student) | ||
{ | ||
startActivityForResult(new Intent(this, StudentActivity.class).putExtra("Student", student), 2); | ||
} | ||
|
||
@Override | ||
public void onClick(View v) | ||
{ | ||
startActivityForResult(new Intent(this, StudentActivity.class),1); | ||
} | ||
|
||
@Override | ||
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) | ||
{ | ||
super.onActivityResult(requestCode, resultCode, data); | ||
Student student; | ||
switch (resultCode) | ||
{ | ||
case 1: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
student = (Student) data.getSerializableExtra("NewStudent"); | ||
studentDataRepository.add(student); | ||
break; | ||
case 2: | ||
student = (Student) data.getSerializableExtra("ModifyStudent"); | ||
studentDataRepository.edit(student); | ||
break; | ||
case 3: | ||
student = (Student) data.getSerializableExtra("StudentForDelete"); | ||
studentDataRepository.delete(student); | ||
break; | ||
} | ||
studentAdapter.notifyDataSetChanged(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package ru.ok.technopolis.students.Repository; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.Random; | ||
|
||
import ru.ok.technopolis.students.R; | ||
|
||
public class FemalePhotoRepository implements PhotoRepository { | ||
private static final FemalePhotoRepository ourInstance = new FemalePhotoRepository(); | ||
|
||
public static FemalePhotoRepository getInstance() { | ||
return ourInstance; | ||
} | ||
|
||
private List <Integer> femalePhotos; | ||
|
||
private FemalePhotoRepository() { | ||
femalePhotos = new ArrayList(){{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не хватает даймондов для описания типа дженерика |
||
this.add(R.drawable.female_1); | ||
this.add(R.drawable.female_2); | ||
this.add(R.drawable.female_3); | ||
}}; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лишняя строка |
||
|
||
@Override | ||
public Integer getPhotoInRepository() throws NoSuchElementException { | ||
if(femalePhotos.isEmpty()){ | ||
throw new NoSuchElementException(); | ||
} | ||
int indexFemalePhoto = new Random().nextInt(femalePhotos.size()); | ||
Integer malePhoto = femalePhotos.get(indexFemalePhoto); | ||
femalePhotos.remove(indexFemalePhoto); | ||
return malePhoto; | ||
} | ||
|
||
@Override | ||
public void putPhotoInRepository(Integer photo) { | ||
if(photo == null) | ||
{ | ||
throw new NullPointerException(); | ||
} | ||
femalePhotos.add(photo); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package ru.ok.technopolis.students.Repository; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.Random; | ||
|
||
import ru.ok.technopolis.students.R; | ||
|
||
public class MalePhotoRepository implements PhotoRepository | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно было не разделять фото на 2 репозитория, а сделать один с параметром. Или хотя бы унифицировать методы. Различие только в списке фоток, остальные методы одинаковы |
||
{ | ||
private static final MalePhotoRepository ourInstance = new MalePhotoRepository(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. можно просто instance или repository |
||
|
||
private List <Integer> malePhotos; | ||
|
||
|
||
public static MalePhotoRepository getInstance() { | ||
return ourInstance; | ||
} | ||
|
||
private MalePhotoRepository() { | ||
malePhotos = new ArrayList() {{ | ||
this.add(R.drawable.male_1); | ||
this.add(R.drawable.male_2); | ||
this.add(R.drawable.male_3); | ||
}}; | ||
} | ||
|
||
|
||
@Override | ||
public Integer getPhotoInRepository() throws NoSuchElementException{ | ||
if(malePhotos.isEmpty()){ | ||
throw new NoSuchElementException(); | ||
} | ||
int indexMalePhoto = new Random().nextInt(malePhotos.size()); | ||
Integer malePhoto = malePhotos.get(indexMalePhoto); | ||
malePhotos.remove(indexMalePhoto); | ||
return malePhoto; | ||
} | ||
|
||
@Override | ||
public void putPhotoInRepository(Integer photo) { | ||
if(photo == null) | ||
{ | ||
throw new NullPointerException(); | ||
} | ||
malePhotos.add(photo); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.ok.technopolis.students.Repository; | ||
|
||
public interface PhotoRepository | ||
{ | ||
Integer getPhotoInRepository (); | ||
void putPhotoInRepository (Integer photo); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему Integer и не int? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Здесь должен быть приметив, а не объект |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package ru.ok.technopolis.students.Repository; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import ru.ok.technopolis.students.Student; | ||
|
||
public class StudentDataRepository implements StudentRepository | ||
{ | ||
private static final StudentDataRepository ourInstance = new StudentDataRepository(); | ||
|
||
public static StudentDataRepository getInstance() { | ||
return ourInstance; | ||
} | ||
|
||
private List <Student> students; | ||
|
||
|
||
|
||
private StudentDataRepository() | ||
{ | ||
students = new ArrayList<>(); | ||
} | ||
|
||
@Override | ||
public List studentsOnRepository() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше использовать дженерики и указывать тип списка |
||
{ | ||
return students; | ||
} | ||
|
||
@Override | ||
public void add(Student student) | ||
{ | ||
students.add(student); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Следи за пустыми строками |
||
|
||
@Override | ||
public void delete(Student student) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если идти по списку и удалять что-то из него можно упасть. В таких случаях следует использовать итератор |
||
{ | ||
for(int i = 0; i < students.size(); i++){ | ||
if (students.get(i).getId().equals(student.getId())){ | ||
students.remove(i); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void edit(Student student) { | ||
for(int i = 0; i < students.size(); i++){ | ||
if(students.get(i).getId().equals(student.getId())){ | ||
students.get(i).setFirstName(student.getFirstName()); | ||
students.get(i).setSecondName(student.getSecondName()); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ru.ok.technopolis.students.Repository; | ||
|
||
import java.util.List; | ||
|
||
import ru.ok.technopolis.students.Student; | ||
|
||
public interface StudentRepository | ||
{ | ||
List studentsOnRepository(); | ||
|
||
void add(Student student); | ||
void delete(Student student); | ||
void edit(Student student); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
волшебные слова и числа лучше убирать в константы