Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

implementation #11

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.getbase:floatingactionbutton:1.9.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".StudentActivity">

</activity>
</application>

</manifest>
68 changes: 66 additions & 2 deletions app/src/main/java/ru/ok/technopolis/students/MainActivity.java
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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

волшебные слова и числа лучше убирать в константы

}

@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:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Константы создал, а чего не используешь? ACTION_CREATE_STUDENT_CLICK?
  2. "NewStudent" это тоже константа

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(){{
Copy link

Choose a reason for hiding this comment

The 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);
}};
}

Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно было не разделять фото на 2 репозитория, а сделать один с параметром. Или хотя бы унифицировать методы. Различие только в списке фоток, остальные методы одинаковы

{
private static final MalePhotoRepository ourInstance = new MalePhotoRepository();
Copy link

Choose a reason for hiding this comment

The 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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему Integer и не int?

Copy link

Choose a reason for hiding this comment

The 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()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше использовать дженерики и указывать тип списка

{
return students;
}

@Override
public void add(Student student)
{
students.add(student);
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Следи за пустыми строками


@Override
public void delete(Student student)
Copy link

Choose a reason for hiding this comment

The 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);
}
23 changes: 17 additions & 6 deletions app/src/main/java/ru/ok/technopolis/students/Student.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package ru.ok.technopolis.students;

public class Student {
import java.io.Serializable;
import java.util.UUID;

public class Student implements Serializable {

private String firstName;
private String secondName;
private boolean maleGender;
private int photo;
private UUID id;

public Student(String firstName, String secondName, boolean maleGender, int photo) {
this.firstName = firstName;
this.secondName = secondName;
this.maleGender = maleGender;
this.photo = photo;


public Student()
{
id = UUID.randomUUID();
}

public UUID getId()
{
return id;
}

public String getFirstName() {
Expand Down Expand Up @@ -45,4 +54,6 @@ public int getPhoto() {
public void setPhoto(int photo) {
this.photo = photo;
}


}
Loading