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>
71 changes: 70 additions & 1 deletion app/src/main/java/ru/ok/technopolis/students/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,83 @@
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.Instance;
private StudentAdapter studentAdapter;
private final String dataResponse = "Student";
private final int ACTION_ON_STUDENT_CLICK = 2;
private final int ACTION_CREATE_STUDENT_CLICK = 1;

public class MainActivity extends AppCompatActivity {

@Override
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(dataResponse, student), ACTION_ON_STUDENT_CLICK);
}

@Override
public void onClick(View v) {
startActivityForResult(new Intent(this, StudentActivity.class), ACTION_CREATE_STUDENT_CLICK);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null) {
return;
}
Student student;
String dataRequest;
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" это тоже константа

dataRequest = "NewStudent";
student = (Student) data.getSerializableExtra(dataRequest);
studentDataRepository.add(student);
break;
case 2:
dataRequest = "ModifyStudent";
student = (Student) data.getSerializableExtra(dataRequest);
studentDataRepository.edit(student);
break;
case 3:
dataRequest = "StudentForDelete";
student = (Student) data.getSerializableExtra(dataRequest);
studentDataRepository.delete(student);
break;
}
studentAdapter.notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 {

public static final FemalePhotoRepository Instance = new FemalePhotoRepository();

private List<Integer> femalePhotos;

private FemalePhotoRepository() {
femalePhotos = new ArrayList<Integer>() {{
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 int getPhotoInRepository() throws NoSuchElementException {
if (femalePhotos.isEmpty()) {
throw new NoSuchElementException();
}
int indexFemalePhoto = new Random().nextInt(femalePhotos.size());
int femalePhoto = femalePhotos.get(indexFemalePhoto);
femalePhotos.remove(indexFemalePhoto);
return femalePhoto;
}

@Override
public void putPhotoInRepository(int photo) {
femalePhotos.add(photo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 {
public static final MalePhotoRepository Instance = new MalePhotoRepository();

private List<Integer> malePhotos;

private MalePhotoRepository() {
malePhotos = new ArrayList<Integer>() {{
this.add(R.drawable.male_1);
this.add(R.drawable.male_2);
this.add(R.drawable.male_3);
}};
}


@Override
public int getPhotoInRepository() throws NoSuchElementException {
if (malePhotos.isEmpty()) {
throw new NoSuchElementException();
}
int indexMalePhoto = new Random().nextInt(malePhotos.size());
int malePhoto = malePhotos.get(indexMalePhoto);
malePhotos.remove(indexMalePhoto);
return malePhoto;
}

@Override
public void putPhotoInRepository(int photo) {
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 {
int getPhotoInRepository();

void putPhotoInRepository(int photo);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ru.ok.technopolis.students.Repository;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import ru.ok.technopolis.students.Student;

public class StudentDataRepository implements StudentRepository {

public static final StudentDataRepository Instance = new StudentDataRepository();

private List<Student> students;

private StudentDataRepository() {
students = new ArrayList<>();
}

@Override
public List<Student> studentsOnRepository() {
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) {
Iterator<Student> iterator = students.iterator();
while (iterator.hasNext()) {
Student nextStudent = iterator.next();
if (nextStudent.getId().equals(student.getId())) {
iterator.remove();
}
}
}

@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,15 @@
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);
}
20 changes: 14 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,23 @@
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 +51,6 @@ public int getPhoto() {
public void setPhoto(int photo) {
this.photo = photo;
}


}
Loading