How to inherit and display inherited class object list as a part of other object - public

I have 3 classes (Air, Ocean and Ground) representing 3 modes of transportation:
Air:
Class Air
{
public int ID;
public datetime Start;
public datetime End;
public string FlightNo;
}
Ground:
Class Ground
{
public int ID;
public datetime Start;
public datetime End;
public string VehicleNo;
public string DriverName;
public string DriverMobile;
}
Ocean:
Class Ocean
{
public int ID;
public datetime Start;
public datetime End;
public string VessalName;
public string FTZNo;
}
I have an object named Job. Transportation is a part of Job. Job can have multiple Transportation(Air, Ocean, Ground)
How to assign Transportation list in Job class?

The question is unclear.In my understanding your question is how to assign a Transportation list to Job object? Right? I assumed Job class has a member of type
List<Tranportation> transList;
And all the Air,Water and Ground are subclass of Transportation. so you can assign the list with a setter method. say
void setTransportationList(List<Transportation> x){
this.transList=x;
}
EDIT
Lets say you have to print the details of the traspotations used in the Job object
Class Job{
public List<Transportation> trainsList;
public void setTransportationList(List<Transportation> x){
this.transList=x;
}
void printTransportationDetails(){
Iterator itr = this.transList.iterator();
while(itr.hasNext()) {
Transportation element = itr.next();
element.printDetails();
}
}
}
Now your Transportation subclass should have a printDetails() member function.

Related

how to convert inner class with modelmapper

modelmapper 3.1.0
i have following class:
#Data
public class Outter {
private int id;
private String name;
private List<Inner> innerList;
}
#Data
public class Inner {
private int id;
private String code;
}
i want to convert Outter to OutterDto, and i tried create the OutterDto as follows:
#Data
public class OutterDto {
private String name;
private List<InnerDto> innerList;
#Data
class InnerDto {
private String code;
}
}
the InnerDto is the inner class of OutterDto.
however, it thrown an error:
Failed to instantiate instance of destination example.model.OutterDto$InnerDto. Ensure that example.model.OutterDto$InnerDto has a non-private no-argument constructor.
even i add a constructor public InnterDto() {} for InnerDto.
so does the modelmapper support for inner class?
if it does, how can i do that?

JHipster EntityMapper interface (mapstruct): map a Spring projection interface

I've generated a new project with JHipster v4.6.0 generator and I'm using its EntityMapper interface to do the mapping between domain and DTO objects.
public interface EntityMapper <D, E> {
public E toEntity(D dto);
public D toDto(E entity);
public List <E> toEntity(List<D> dtoList);
public List <D> toDto(List<E> entityList);
}
I need to use the Spring projection to have a smaller domain and DTO objects, (I don't want all fields of the entity), so I've created an interface with only the getters of the fields I need, and I've created a method in the repository which retrive this interface type (following the Spring reference guide)
public interface ClienteIdENome {
Long getId();
String getNome();
}
#Repository
public interface ClienteRepository extends JpaRepository<Cliente,Long> {
ClienteIdENome findById(Long id);
}
The query findById retrieve a ClienteIdENome object with only id and nome fields.
Now, I would like to map this object in the following DTO:
public class ClienteIdENomeDTO implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
#NotNull
#Size(max = 50)
private String nome;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
So, I created the mapper interface:
#Mapper(componentModel = "spring", uses = {})
public interface ClienteIdENomeMapper extends EntityMapper<ClienteIdENomeDTO, ClienteIdENome> {
}
But Eclipse report to me an error in the EntityMapper interface for the method "public E toEntity(D dto)" with the message:
No implementation type is registered for return type it.andrea.ztest01.repository.ClienteIdENome.
Any help?
Thanks a lot
Your ClienteIdENome is not really an entity. I would argue that you don't need to use the EntityMapper, but you need to define a one way mapper. From ClienteIdENome to ClienteIdENomeDTO.
Your mapper needs to look like:
public interface ClienteIdENomeMapper {
ClienteIdENomeDTO toDto(ClienteIdENome entity);
List <ClienteIdENomeDTO> toDto(List<ClienteIdENome> entityList);
}
I don't know JHipster, so I can't say what will mean using a mapper different than EntityMapper.

Enum-Like object which contains properties

I am trying to figure out a way to have a class full of static objects which each can have a variety of static properties.
I want to be able to pass these properties around and even set them as static properties of other object and I also want to be able to switch through the objects.
Here is an example illustrating what I mean:
Creating and Sending a Message
class Program
{
static void Main(string[] args)
{
MarketOrder Order = new MarketOrder("DELL", MessageProperties.SecurityType.Equity, MessageProperties.ExchangeDestination.ARCA.PostOnly);
SendOrder(Order);
Console.ReadLine();
}
public static void SendOrder(MarketOrder Order)
{
switch (Order.SecurityType)
{
case MessageProperties.SecurityType.Equity:
// Equity sending logic here
break;
case MessageProperties.SecurityType.Option:
// Option sending logic here
break;
case MessageProperties.SecurityType.Future:
// Future sending logic here
break;
}
}
}
This does not want to compile because it won't let me switch the Order.SecurityType object.
MarketOrder Class
public class MarketOrder
{
public readonly string Symbol;
public readonly MessageProperties.SecurityType SecurityType;
public readonly MessageProperties.ExchangeDestination ExchangeDestination;
public MarketOrder(string Symbol, MessageProperties.SecurityType SecurityType, MessageProperties.ExchangeDestination ExchangeDestination)
{
this.Symbol = Symbol;
this.SecurityType = SecurityType;
this.ExchangeDestination = ExchangeDestination;
}
}
MessageProperties Class
public abstract class MessageProperties
{
public class ExchangeDestination
{
public readonly string Value;
public readonly double ExchangeFee;
public ExchangeDestination(string Value, double ExchangeFeed)
{
this.Value = Value;
this.ExchangeFee = ExchangeFee;
}
public abstract class ARCA
{
public static ExchangeDestination Only = new ExchangeDestination("ARCA.ONLY", 0.01);
public static ExchangeDestination PostOnly = new ExchangeDestination("ARCA.ONLYP", 0.02);
}
public abstract class NYSE
{
public static ExchangeDestination Only = new ExchangeDestination("NYSE.ONLY", 0.01);
public static ExchangeDestination PostOnly = new ExchangeDestination("NYSE.ONLYP", 0.03);
}
}
public class SecurityType
{
public readonly string Value;
public SecurityType(string Value)
{
this.Value = Value;
}
public static SecurityType Equity = new SecurityType("EQ");
public static SecurityType Option = new SecurityType("OPT");
public static SecurityType Future = new SecurityType("FUT");
}
}
Enums work perfectly for what I am trying to do except it is hard to have multiple properties of an enum value. I considered using Attributes on Enums to set the properties but getting those vs. getting static properties of objects is substantially slower and my application is extremely speed/latency sensitive.
Is there perhaps a better way of accomplishing what I am trying to do?
Thanks in advance for your help!
William

Javolution - reading variable-length string

How to read variable length String from a C struct using Javolution API?
For example the code below is used to get a fixed size String-
public final UTF8String data= new UTF8String(100);
Can anyone give me an example for reading variable length String.
This is what we have and we are learning as well:
public class EvDasTestResults extends AbstractServiceJavolutionObject
{
public final Signed32 result = new Signed32();
public final UTF8String description;
public EvDasTestResults(int size)
{
description = new UTF8String(size);
}
}
public abstract class AbstractServiceJavolutionObject extends Struct
{
#Override
public ByteOrder byteOrder()
{
return ByteOrder.nativeOrder();
}
#Override
public boolean isPacked()
{
return true;
}
}

Strange nullPointerException in Java

I'm writing an app for Java ME, and I need a class for holding some data(PhoneBook). When I'm trying to launch this app, I'm always getting a nullPointerException. I'm calling the constructor of a class, and it allocates memory for 10 elements, so it shouldn't be null. What am I doing wrong?
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class TinyMIDlet extends MIDlet implements CommandListener {
private PhoneBook phoneBook = new PhoneBook();
public void initPhoneBook() {
phoneBook.records[0].Name = new String("abc");
}
protected void startApp() {
initPhoneBook();
}
public class Record {
public String Name;
public String Number;
public String email;
public String Group;
}
public class PhoneBook {
public Record[] records;
PhoneBook() {
records = new Record[10];
}
}
}
The array of records isn't null, but each individual element of it is. You need to instantiate each element as well, right now it's just an array with 10 null entries.
phoneBook.records[0].Name = new String("abc");
should be
phoneBook.records[0] = new Record();
phoneBook.records[0].Name= new String("abc");// or = "abc"
I'm not reputable enough yet (heh) to edit Tom's detailed answer, but, to be precise
phoneBook.records[0] = new something();
should be
phoneBook.records[0] = new Record();

Resources