I am teaching my self to code in Java. I am a beginner but not totally clueless about programing. I have decided to make an application that tracks tickets, clients and devices for a computer repair shop. I have made a "lot" of code for that application, but as I said I am a beginner can I don know what is the "right way"(best, smartest and convenient) to implement the data-structure of the application.
I decided to use custom data classes that are value objects in HashMap with integer keys, and that represent data tables that mirror data tables in databases. Am I doing it wrong?
public class Clients
{
private String firstName;
private String lastName;
private String primePhoneNum;
private String secondPhoneNum;
private String email;
private String address;
private MarketingTypes marketing;
//getters and setters
}
public HashMap<Integer,Clients> clientsTable = new HashMap<Integer, Clients>();
I ran in to trouble when I tried to make search function that returns value object in a HashMap based on that object particular field value. For example:
public class SearchDeviceTypes
{
private Map<Integer, DeviceTypes> deviceTypesTable;
public SearchDeviceTypes(Map<Integer, DeviceTypes> deviceTypesTable)
{
this.deviceTypesTable = deviceTypesTable;
}
public boolean isNameTaken(String name)
{
return deviceTypesTable.entrySet().stream()
.anyMatch(deviceType->deviceType.getValue().getName().equals(name));
}
public DeviceTypes getByID(int id)
{
return deviceTypesTable.get(id);
}
public Map<Integer, DeviceTypes> filterByName(String text)
{
return deviceTypesTable.entrySet().stream()
.filter(deviceType->deviceType.getValue().getName().contains(text))
.collect(Collectors.toMap(deviceType -> deviceType.getKey(), deviceType -> deviceType.getValue()));
}
public DeviceTypes getByName(String name)
{
//How to implement this?
return null;
}
}
I would like to help me to learn how to implement this kind of data structure. Thank you in advance!
.findFirst()instead of the collect may need to map to getValue.