-2

i have created a custom class which can get date and time as strings i want to access the object and set data to the class any suggestions here is my code

public class DateTime {
private String Date;
private String Time;

public DateTime(String Date,String Time) {
    this.Date=Date;
    this.Time=Time;

}

public void showDateAndTime() {

    System.out.println("entery Date is :");
    System.out.println(Date);
    System.out.println("entery time is :");
    System.out.println(Time);

}
}

public class Vehicle {

        public int ID;
        public String Brand;
            public String type;
        public DateTime entryTimeDate;

}

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);


        Vehicle v1=new Vehicle();
        v1.entryTimeDate**********
}

i want to set date time value to v1 object any suggestions?

Rayan Liyanage
  • 35
  • 1
  • 10
  • 1
    Unclear what is being asked and what has been tried, but v1 is a Vehicle right? So the compiler won't allow a Vehicle object to be set on a DateTime reference Side note, try the java.time.LocalDateTime class, much more functionality and testing than rolling your own class – Nathan Adams May 26 '19 at 18:19
  • 1
    `v1.entryDateTime = new DateTime("Tomorrow", "now!!!")` or whatever time and date values you want. – Joakim Danielson May 26 '19 at 18:23

1 Answers1

0

Instead of having public fields in Vehicle class, make them private and then use accessor and mutator methods in order to gain access to them. Then vehicle.getEntryTimeDate() will be enough to give you the DateTime object of the vehicle. With other words:

public class Vehicle {
    private int id;
    public String brand;
    public String type;
    public DateTime entryTimeDate;

    public Vehicle()
    {
        setId(0);
        setBrand("");
        setType("");
        setEntryTimeDate(new DateTime("DATE", "TIME"));
    }

    public Vehicle(int id, String brand, String type, DateTime entryTimeDate) {
        this.id = id;
        this.brand = brand;
        this.type = type;
        this.entryTimeDate = entryTimeDate;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public DateTime getEntryTimeDate() {
        return entryTimeDate;
    }

    public void setEntryTimeDate(DateTime entryTimeDate) {
        this.entryTimeDate = entryTimeDate;
    }

    public static class DateTime{
        private String date;
        private String time;

        public DateTime(String date,String time) {
            this.setDate(date);
            this.setTime(time);

        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }
    }

    public static void main(String[] args) {
        Vehicle v1 = new Vehicle();
        v1.getEntryTimeDate().setTime("Time of the vehicle");
        v1.getEntryTimeDate().setDate("Date of the vehicle.");
    }
}

Some notes: Ignore the static keyword in DateTime class. I did it in order to create an SSCCE. Also notice the field naming convention i followed (camel-case). It is the most used naming convention by Java programmers.

George Z.
  • 6,643
  • 4
  • 27
  • 47
  • it returns Exception in thread "main" java.lang.NullPointerException in lines of ` v1.getEntryTimeDate().setTime("Time of the vehicle"); v1.getEntryTimeDate().setDate("Date of the vehicle.");` – Rayan Liyanage May 26 '19 at 18:43
  • @RayanLiyanage I edited my post. It was my mistake indeed. I never initialized the `entryTimeDate` object of the `Vehicle` in the default constructor. So the `getEntryTimeDate` method was returning null. Now the default constructor initializes all the fields, avoiding NPE in all getters. – George Z. May 26 '19 at 18:49