Can anyone help me out? I am trying to figure out what to put in this getLecture body where the question marks are:
import java.util.ArrayList;
public class Schedule
{
private ArrayList<Lecture> lectures;
public Schedule(ArrayList<Lecture> lectures)
{
lectures = new ArrayList<Lecture>();
}
public Lecture getLecture(long date, long time)
{
???
}
public ArrayList<Lecture> getLectures() {
return lectures;
}
public void addLecture(Lecture lecture)
{
lectures.add(lecture);
}
public void removeLecture(Lecture lecture)
{
for(int i = 0; i < lectures.size(); i++)
{
if(lectures.get(i) == lecture)
{
lectures.remove(i);
}
}
}
}
I am trying to compare the time, with the parameters given to that method, then to make a for loop going through a list, preferably a for each loop. Inside of the loop I need to take the time of the Lecture object created inside the method, and compare it to the time of the Lecture from the list. This is the lecture class:
package application;
import java.util.ArrayList;
public class Lecture {
private String message;
private long time;
private long date;
private ArrayList<Object> materials;
public Lecture() {
int date;
int time;
}
public String sendMessage(String message) {
return message;
}
public void uploadMaterial(Object material) {
materials.add(material);
}
public Object getMaterial(Object material) {
return material;
}
public void removeMaterial(Object material) {
for(int i = 0; i < materials.size(); i++)
{
if(material == materials.get(i))
materials.remove(i);
}
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
}
Related
I know this is a dumb question, but please help out.
I have three model classes
Student class
public class Student {
private String department;
private String year;
private String division;
private String batch;
private Timetable timetable;
public Student() {}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getDivision() {
return division;
}
public void setDivision(String division) {
this.division = division;
}
public String getBatch() {
return batch;
}
public void setBatch(String batch) {
this.batch = batch;
}
public Timetable getTimetable() {
return timetable;
}
public void setTimetable(Timetable timetable) {
this.timetable = timetable;
}
}
Timetable class
public class Timetable implements Parcelable {
private String day;
private List<Lecture> lectures;
public Timetable(String day, List<Lecture> lectures) {
this.day = day;
this.lectures = lectures;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public List<Lecture> getLectures() {
return lectures;
}
public void setLectures(List<Lecture> lectures) {
this.lectures = lectures;
}
}
Lecture class
public class Lecture implements Parcelable {
private String code;
private String professorCode;
private String room;
private long startTime;
private long endTime;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getProfessorCode() {
return professorCode;
}
public void setProfessorCode(String professorCode) {
this.professorCode = professorCode;
}
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
public long getStartTime() {
return startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public long getEndTime() {
return endTime;
}
public void setEndTime(long endTime) {
this.endTime = endTime;
}
}
Now at this point because there is a nested containment in my data model, I am stuck in how I should structure my data in Firebase Cloud Firestore.
Student have department, year, division, Timetable.
Timetable has day and list of Lectures.
Lecture have code, professorCode, room, startTime, endTime
It'll be really great if someone helps to structure this data in firestore in terms of collection and documents. I am new to Firestore and I am finding collections and documents difficult.
I could have used Realtime Database, but there are some issues so I have to use Firestore only.
Thanks in advance
My problem is that I have to use one Arraylist and adding two options
1.Task (contents,startTime,startEnd,difficulty)
contents- String
startTime,startEnd- Localtime
difficulty- boolean easy/difficult
Happening (contents,startTime,startEnd,important,training)
contents- String
startTime,startEnd- Localtime
important- boolean Yes/No
training- boolean Yes/No
Is it possible merge two different options to one arraylist ??
Interface for common properties
public interface MyI {
public String getContens();
public LocalTime getStartTime();
public LocalTime getStartEnd();
}
Then create models for reach of the unique instances:
public class Task implements MyI {
private String contens;
private LocalTime startTime;
private LocalTime startEnd;
private boolean difficulty;
public boolean isDifficulty() {
return difficulty;
}
public void setDifficulty(boolean difficulty) {
this.difficulty = difficulty;
}
public void setContens(String contens) {
this.contens = contens;
}
public void setStartTime(LocalTime startTime) {
this.startTime = startTime;
}
public void setStartEnd(LocalTime startEnd) {
this.startEnd = startEnd;
}
#Override
public String getContens() {
return this.contens;
}
#Override
public LocalTime getStartTime() {
return this.startTime;
}
#Override
public LocalTime getStartEnd() {
return this.startEnd;
}
}
public class Happening implements MyI {
private String contens;
private LocalTime startTime;
private LocalTime startEnd;
private boolean important;
private boolean training;
public boolean isImportant() {
return important;
}
public void setImportant(boolean important) {
this.important = important;
}
public boolean isTraining() {
return training;
}
public void setTraining(boolean training) {
this.training = training;
}
public void setContens(String contens) {
this.contens = contens;
}
public void setStartTime(LocalTime startTime) {
this.startTime = startTime;
}
public void setStartEnd(LocalTime startEnd) {
this.startEnd = startEnd;
}
#Override
public String getContens() {
return this.contens;
}
#Override
public LocalTime getStartTime() {
return this.startTime;
}
#Override
public LocalTime getStartEnd() {
return this.startEnd;
}
}
Now you can create a list using the Interface to hold both types of models:
List<MyI> myList = new ArrayList<MyI>();
You can define a common super class to those classes. For example an abstract class or an interface and then you will create the List upon that super-class or interface.
I tried to implement custom TiledDataSource for using with paging library. When I used LivePagedListProvider like returns type for my Dao method it worked fine (after table items updating - ui updated automatically).
#Query("SELECT * FROM " + Table.States.PLAY_STATE + ", "+Table.Chart.ARTIST+ " ORDER BY position ASC")
LivePagedListProvider<Artist> loadArtists();
But when I try implement custom TiledDataSource for LivePagerListProvider table updates not triggered my observers.
Abstract generic class:
public abstract class PagedNetworkBoundResource<ResultType, RequestType> extends TiledDataSource<ResultType> {
#Override
public int countItems() {
return DataSource.COUNT_UNDEFINED;
}
#Override
public List<ResultType> loadRange(int startPosition, int count) {
fetchFromNetwork(startPosition, count);
return loadFromDb(startPosition, count);
}
#WorkerThread
private void fetchFromNetwork(int startPosition, int count) {
if (createCall(startPosition, count) != null)
try {
Response<RequestType> response = createCall(startPosition, count).execute();
if (response.isSuccessful() && response.code() == 200) {
saveCallResult(response.body());
}
} catch (IOException e) {
e.printStackTrace();
}
}
#WorkerThread
protected abstract void saveCallResult(#NonNull RequestType item);
#WorkerThread
protected abstract List<ResultType> loadFromDb(int startPosition, int count);
#WorkerThread
protected abstract Call<RequestType> createCall(int startPosition, int count);
public LiveData<PagedList<ResultType>> getAsLiveData() {
return new LivePagedListProvider<Integer, ResultType>() {
#Override
protected DataSource<Integer, ResultType> createDataSource() {
return PagedNetworkBoundResource.this;
}
}.create(0, new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPageSize(20)
.setInitialLoadSizeHint(20)
.build());
}
}
My dao method for this case:
#Query("SELECT * FROM " + Table.States.PLAY_STATE + ", "+Table.Chart.ARTIST+ " ORDER BY position ASC LIMIT (:limit) OFFSET (:offset)")
List<Artist> loadArtists(int offset, int limit);
I update Table.States.PLAY_STATE.
public void updatePlayerState(PlayerStateEntity state){
new Thread(() -> {
dao.deleteState();
dao.insertState(state);
}).run();
}
#Dao
public interface PlayStateDao {
#Insert(onConflict = OnConflictStrategy.REPLACE)
void insertState(PlayerStateEntity playEntity);
#Query("DELETE FROM " + Table.States.PLAY_STATE)
void deleteState();
#Query("SELECT * FROM "+Table.States.PLAY_STATE)
PlayerStateEntity getPlayerState();
}
#Entity(tableName = Table.States.PLAY_STATE)
public class PlayerStateEntity extends IdEntity {
#ColumnInfo(name = "album_played_id")
private Long albumPlayedId = -1L;
#ColumnInfo(name = "track_played_id")
private Long trackPlayedId = -1L;
#ColumnInfo(name = "artist_played_id")
private Long artistPlayedId = -1L;
#ColumnInfo(name = "state")
private PlayingState state;
#ColumnInfo(name = "playing_type")
private PlayingType playingType;
public Long getAlbumPlayedId() {
return albumPlayedId;
}
public void setAlbumPlayedId(Long albumPlayedId) {
this.albumPlayedId = albumPlayedId;
}
public Long getTrackPlayedId() {
return trackPlayedId;
}
public void setTrackPlayedId(Long trackPlayedId) {
this.trackPlayedId = trackPlayedId;
}
public Long getArtistPlayedId() {
return artistPlayedId;
}
public void setArtistPlayedId(Long artistPlayedId) {
this.artistPlayedId = artistPlayedId;
}
public PlayingState getState() {
return state;
}
public void setState(PlayingState state) {
this.state = state;
}
public PlayingType getPlayingType() {
return playingType;
}
public void setPlayingType(PlayingType playingType) {
this.playingType = playingType;
}
}
class Artist extends PlayEntity{
private String name;
private String link;
private String picture;
#ColumnInfo(name = "picture_small")
private String pictureSmall;
#ColumnInfo(name = "picture_medium")
private String pictureMedium;
#ColumnInfo(name = "picture_big")
private String pictureBig;
#ColumnInfo(name = "picture_xl")
private String pictureXl;
private Boolean radio;
private String tracklist;
private Integer position;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getPictureSmall() {
return pictureSmall;
}
public void setPictureSmall(String pictureSmall) {
this.pictureSmall = pictureSmall;
}
public String getPictureMedium() {
return pictureMedium;
}
public void setPictureMedium(String pictureMedium) {
this.pictureMedium = pictureMedium;
}
public String getPictureBig() {
return pictureBig;
}
public void setPictureBig(String pictureBig) {
this.pictureBig = pictureBig;
}
public String getPictureXl() {
return pictureXl;
}
public void setPictureXl(String pictureXl) {
this.pictureXl = pictureXl;
}
public Boolean getRadio() {
return radio;
}
public void setRadio(Boolean radio) {
this.radio = radio;
}
public String getTracklist() {
return tracklist;
}
public void setTracklist(String tracklist) {
this.tracklist = tracklist;
}
public Integer getPosition() {
return position;
}
public void setPosition(Integer position) {
this.position = position;
}
#Override
public boolean isItemPlaying() {
return getId() == getArtistPlayedId().longValue() && getPlayingType() == PlayingType.Artist && getState() == PlayingState.Playing;
}
}
public abstract class PlayEntity extends PlayerStateEntity {
public abstract boolean isItemPlaying();
}
public class ArtistsRepository {
private final ChartArtistDao chartArtistDao;
private final DeezerService deezerService;
#Inject
public ArtistsRepository(ChartArtistDao chartArtistDao, DeezerService deezerService) {
this.chartArtistDao = chartArtistDao;
this.deezerService = deezerService;
}
public LiveData<PagedList<ChartArtistDao.Artist>> getArtist() {
return new PagedNetworkBoundResource<ChartArtistDao.Artist, ModelList<ChartArtistEntity>>() {
#Override
protected void saveCallResult(#NonNull ModelList<ChartArtistEntity> item) {
if (item != null) {
chartArtistDao.saveArtists(item.getItems());
}
}
#Override
protected List<ChartArtistDao.Artist> loadFromDb(int startPosition, int count) {
return chartArtistDao.loadArtists(startPosition, count);
}
#Override
protected Call<ModelList<ChartArtistEntity>> createCall(int startPosition, int count) {
return deezerService.getChartArtist(startPosition, count);
}
}.getAsLiveData();
}
}
For each Artist items I add fields from PlayerStateEntity (not good solution but this easy way to represent state of ui items). After PlayerStateEntity table updates Room should notify about data changes, but doesn't do it.
I understand that Room doesn't know about query what I used, and can't updates my RecyclerView which provide by paging library. But maybe some one knows how to notify Room about tables which I used inside mine DataSource for future triggering ui updates?
The problem was related with custom DataSource realization. When data has changed, LivePagedListProvider should create a new DataSource instance for right ui updating. I used the same instance, so my previous solution is not right.
I have a tableview in javafx which i want to populate with objects type Course. The idea is that in my Course class, I hava a composite primary key which is a diifferent class CourseId. I want to add in one column of the tableview the courseno which is present in CourseId class, but i dont know how to get it.
My course class:
package com.licenta.ascourses.ui.model;
import java.io.Serializable;
public class Course implements Serializable {
private CourseId idCourse = new CourseId();
private int year;
private int semester;
private String discipline;
private String professor;
public Course() {
}
public Course(CourseId idCourse, int year, int semester) {
super();
this.idCourse = idCourse;
this.year = year;
this.semester = semester;
}
public Course(CourseId idCourse, int year, int semester, String discipline, String professor) {
this.idCourse=idCourse;
this.year = year;
this.semester = semester;
this.discipline = discipline;
this.professor = professor;
}
public CourseId getIdCourse() {
return idCourse;
}
public void setIdCourse(CourseId idCourse) {
this.idCourse = idCourse;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getSemester() {
return semester;
}
public void setSemester(int semester) {
this.semester = semester;
}
public String getDiscipline() {
return discipline;
}
public void setDiscipline(String discipline) {
this.discipline = discipline;
}
public String getProfessor() {
return professor;
}
public void setProfessor(String professor) {
this.professor = professor;
}
}
My courseId class:
package com.licenta.ascourses.ui.model;
import java.io.Serializable;
public class CourseId implements Serializable {
private int idDiscipline;
private int idProfessor;
private int courseNo;
public CourseId() {
}
public CourseId(int idDiscipline, int idProfessor, int courseNo) {
super();
this.idDiscipline = idDiscipline;
this.idProfessor = idProfessor;
this.courseNo = courseNo;
}
public int getIdDiscipline() {
return idDiscipline;
}
public void setIdDiscipline(int idDiscipline) {
this.idDiscipline = idDiscipline;
}
public int getIdProfessor() {
return idProfessor;
}
public void setIdProfessor(int idProfessor) {
this.idProfessor = idProfessor;
}
public int getCourseNo() {
return courseNo;
}
public void setCourseNo(int courseNo) {
this.courseNo = courseNo;
}
public boolean equals(Object o) {
return true;
}
public int hashCode() {
return 1;
}
}
columnNumarCurs.setCellValueFactory(new PropertyValueFactory<Course, Integer>(""));
columnAn.setCellValueFactory(new PropertyValueFactory<Course, Integer>("year"));
columnSemestru.setCellValueFactory(new PropertyValueFactory<Course, Integer>("semester"));
columnDisciplina.setCellValueFactory(new PropertyValueFactory<Course, String>("discipline"));
columnProfesor.setCellValueFactory(new PropertyValueFactory<Course, String>("professor"));
The setCellValueFactory method requires a Callback<CellDataFeatures, ObservableValue>: i.e. a function that maps a CellDataFeatures object to an ObservableValue containing the value to be displayed. Since the value you have is an int, and assuming columnNumarCurs is a TableColumn<Course, Number>, the appropriate ObservableValue type is an IntegerProperty. So you can do:
columnNumarCurs.setCellValueFactory(
cellData -> new SimpleIntegerProperty(cellData.getValue().getIdCourse().getCourseNo()));
This question already has answers here:
Sort ArrayList of custom Objects by property
(29 answers)
Closed 6 years ago.
I have an object List with the name of cars and in this list I have some vehicle object. all objects values are different to each other. In all object here is custom field with the name of unit and in unit is another field with the name of unitType ... which is "T20" , "VT11" or any other ..
I want to sort this cars List according to unitType like:
if cars(0).getUnit().getUnitType().equals("T20") sort all the value first then other "VT11" and then all other...
How can I do this?? Is any suitable method in java?
my Unit class:
package com.vehicletracking.vtss.classes.bean;
import java.io.*;
public class Unit implements Serializable{
private int code;
private int unitId;
private String unitType; //X8, X1, X1+, VT10, VT200, SPT100
private Sim sim;
private String commType; //CSD, SMS, GPRS, AUTO
private int modemCode;
private long IMEI;
private String serialNo;
private InputReportProfile inputReportProfile;
private String firmware;
private String packageName;
private String password;
public Unit() {
}
public Unit(int unitId) {
this.unitId = unitId;
}
public Unit(int unitId, String unitType) {
this.unitId = unitId;
this.unitType = unitType;
}
public Unit(int unitId, String unitType, Sim sim) {
this.unitId = unitId;
this.unitType = unitType;
this.sim = sim;
}
public void setUnitId(int unitId) {
this.unitId = unitId;
}
public int getUnitId() {
return this.unitId;
}
public void setUnitType(String unitType) {
this.unitType = unitType;
}
public String getUnitType() {
return this.unitType;
}
public void setSim(Sim sim) {
this.sim = sim;
}
public void setCommType(String commType) {
this.commType = commType;
}
public void setModemCode(int modemCode) {
this.modemCode = modemCode;
}
public void setSerialNo(String serialNo) {
this.serialNo = serialNo;
}
public void setCode(int code) {
this.code = code;
}
public void setInputReportProfile(InputReportProfile inputReportProfile) {
this.inputReportProfile = inputReportProfile;
}
public void setPassword(String password) {
this.password = password;
}
public void setIMEI(long IMEI) {
this.IMEI = IMEI;
}
public Sim getSim() {
return this.sim;
}
public String getCommType() {
return commType;
}
public int getModemCode() {
return modemCode;
}
public String getSerialNo() {
return serialNo;
}
public int getCode() {
return code;
}
public InputReportProfile getInputReportProfile() {
return inputReportProfile;
}
public String getPassword() {
return password;
}
public long getIMEI() {
return IMEI;
}
public int hashCode() {
return unitId;
}
public boolean equals(Object obj) {
if (obj instanceof Unit) {
return this.unitId == ((Unit) obj).getUnitId();
}
return false;
}
public String toString() {
return this.unitId + "/" + this.unitType;
}
public String getFirmware() {
return firmware;
}
public void setFirmware(String firmware) {
this.firmware= firmware;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
}
Car List:
List cars;
From Java 8 Documentation for Comparable:
This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.
According to above, you just have to implement Comparable, and provide an implementation for compareTo for your object. Collections.sort takes care of the rest.