I am using Framework for Integrated Test. I know how to use ColumnFixture, RowFixture and ActionFixture basically. Now my problem is, if I have nested objects like Customer object is holding Address object with some fields, how can I parse such kind of objects.
ex:
package com.sample;
import java.sql.Date;
public class Customer {
private String name;
private int no;
private Date dob;
private Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
#Override
public String toString() {
return "Customer [name=" + name + ", no=" + no + ", dob=" + dob
+ ", address=" + address + "]";
}
}
package com.sample;
public class Address {
private int dno;
private String street;
private String city;
public int getDno() {
return dno;
}
public void setDno(int dno) {
this.dno = dno;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
#Override
public String toString() {
return "Address [dno=" + dno + ", street=" + street + ", city=" + city
+ "]";
}
}
Now, in my my fixture, I want to check (using ActionFixture) getCustomer() method that returns customer object. Now in the parse(String s, Type) where 's' is string format of customer object coming from input file, how can I convert it into Customer object.
Is my approach proper?
Related
I am just wondering why my getTotal_Score() is null in my model class but I can retrieve it successfully from the database...I have tried both capital Integer and just a normal int, but that doesn't work...I have also tried Long type but I still get a null variable..The other thing that is puzzling me too is that when I use int type instead of long, it doesn't work.... Can you retrieve the numbers as int simple by doing:
int score = (int) snapshop.child("Glen Family Medical Centre").child("Total Score");
I can only get the above to work with long type...
public class Information { // variables have to match in firebase database or it will show null
private String Address;
private String Name;
private String Phone_No;
private String Suburb;
private String State;
private String Postcode;
private String Doctor;
private int Total_Score;
#SuppressWarnings("unused")
public Information() {
}
#SuppressWarnings("unused")
public Information(String address, String name, String phone_No, String suburb, String state, String postcode, String doctor, int total_score) {
Address = address;
Name = name;
Phone_No = phone_No;
Suburb = suburb;
State = state;
Postcode = postcode;
Doctor = doctor;
Total_Score = total_score;
}
public int getTotal_Score() {
return Total_Score;
}
public void setTotal_Score(int total_Score) {
Total_Score = total_Score;
}
public String getAddress() {
return Address;
}
#SuppressWarnings("unused")
public void setAddress(String address) {
Address = address;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getPhone_No() {
return Phone_No;
}
#SuppressWarnings("unused")
public void setPhone_No(String phone_No) {
Phone_No = phone_No;
}
public String getSuburb() {
return Suburb;
}
#SuppressWarnings("unused")
public void setSuburb(String suburb) {
Suburb = suburb;
}
public String getState() {
return State;
}
#SuppressWarnings("unused")
public void setState(String state) {
State = state;
}
public String getPostcode() {
return Postcode;
}
#SuppressWarnings("unused")
public void setPostcode(String postcode) {
Postcode = postcode;
}
public String getDoctor() {
return Doctor;
}
#SuppressWarnings("unused")
public void setDoctor(String doctor) {
Doctor = doctor;
}
}
In my other class, I have used:
Information info = snapshot.getValue(Information.class);
assert info != null;
String txt = "Medical Clinic: " + info.getName() + "Total Score: " + info.getTotal_Score();
list.add(txt);
I wrote a REST web service which is returning JSON as below
[{"id":0,"name":"Vishal","age":"23","dob":"21/1/1992","phone":"9966558","sslc":"90","hsc":"90","college":"90"},
{"id":0,"name":"Karthik","age":"27","dob":"14/8/1988","phone":"995674","sslc":"99","hsc":"100","college":"100"},
{"id":0,"name":"Jeeva","age":"29","dob":"10/1/1987","phone":"77422","sslc":"99","hsc":"99","college":"100"},
{"id":0,"name":"Arya","age":"26","dob":"10/1/1989","phone":"55668","sslc":"100","hsc":"99","college":"99"}]
But I want the output with the "student" appended as below.
{"student":[{"id":0,"name":"Vishal","age":"23","dob":"21/1/1992","phone":"9966558","sslc":"90","hsc":"90","college":"90"},
{"id":0,"name":"Karthik","age":"27","dob":"14/8/1988","phone":"995674","sslc":"99","hsc":"100","college":"100"},
{"id":0,"name":"Jeeva","age":"29","dob":"10/1/1987","phone":"77422","sslc":"99","hsc":"99","college":"100"},
{"id":0,"name":"Arya","age":"26","dob":"10/1/1989","phone":"55668","sslc":"100","hsc":"99","college":"99"}]}
how can I achieve this output?
Below is the Product Class
#XmlRootElement(name="student")
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public Student() {
super();
}
public Student(int id, String name, String age, String dob, String phone,
String sslc, String hsc, String college) {
super();
this.id = id;
this.name = name;
this.age = age;
this.dob = dob;
this.phone = phone;
this.sslc = sslc;
this.hsc = hsc;
this.college = college;
}
private int id;
private String name;
private String age;
private String dob;
private String phone;
private String sslc;
private String hsc;
private String college;
#XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#XmlElement
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
#XmlElement
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
#XmlElement
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
#XmlElement
public String getSslc() {
return sslc;
}
public void setSslc(String sslc) {
this.sslc = sslc;
}
#XmlElement
public String getHsc() {
return hsc;
}
public void setHsc(String hsc) {
this.hsc = hsc;
}
#XmlElement
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
#Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", dob=" + dob + ", phone=" + phone + ", sslc=" + sslc
+ ", hsc=" + hsc + ", college=" + college + "]";
}
}
Below is the service class.
#GET
#Path("/student.srv")
#Produces("application/json")
public Response getStudentJson(){
DAOLayer daoLayer=new DAOLayer();
List<Student> studentsList=null;
try {
studentsList=daoLayer.getStudents();
} catch (SQLException e) {
e.printStackTrace();
}
return Response.ok(studentsList).build();
}
Kindly help me to achieve the above mentioned output.
Thanks in Advance.
To get the desired output, you will have to create one single root object containing a List<Student> student and return it:
Root.java
#XmlRootElement(name="root")
public class Root implements Serializable {
#XmlList
private List<Student> student = new ArrayList<Student>();
// getter and setter
}
Service.java
#GET
#Path("/student.srv")
#Produces("application/json")
public Response getStudentJson(){
DAOLayer daoLayer=new DAOLayer();
List<Student> studentsList=null;
try {
studentsList=daoLayer.getStudents();
} catch (SQLException e) {
e.printStackTrace();
}
Root root = new Root();
root.setStudent(studentsList),
return Response.ok(root).build();
}
data_user = "{"id":1,"lastName":"lastName","name":"name","school":{"id":1}}"
public class School {
private int id;
private String name;
}
public class User {
private int id;
private String lastName;
private String name;
private School school;
}
How to deserialize Json data_user to java object User?
I tried with Gson :
Gson gson = new Gson();
User user = gson.fromJson(data_user, User.class)
But I have an error with this code because the Json contains a school which hasn't the school's name.
How Can I serialize the Json to java Object?
School.java
public class School {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return "School [id=" + id + ", name=" + name + "]";
}
}
User.java
public class User {
private int id;
private String lastName;
private String name;
private School school;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
#Override
public String toString() {
return "User [id=" + id + ", lastName=" + lastName + ", name=" + name
+ ", school=" + school + "]";
}
}
Main.java
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.testgson.beans.User;
public class Main {
private static Gson gson;
static {
gson = new GsonBuilder().create();
}
public static void main(String[] args) {
String j = "{\"id\":1,\"lastName\":\"lastName\",\"name\":\"ignacio\",\"school\":{\"id\":1}}";
User u = gson.fromJson(j, User.class);
System.out.println(u);
}
}
Result
User [id=1, lastName=lastName, name=ignacio, school=School [id=1, name=null]]
Try with the Jackson Library. With Gson with should have not any problem, I tried with the code of #Saurabh and it work well
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
That's the error i get when trying to execute my program. Honestly i really dont know where to start my code.
i have a button name old and new.
i want the textfieldMemberID to display yearToday and an array of members who joined the organization. example if i click new, textfieldMemberId value will automatically display "2012-000001" and if ic lick again new then it will display "2012-000002".
'
i have a Member Class :
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class Member {
private ArrayList accounts;
private String MemberId;
private String VotersId;
private String FirstName;
private String MiddleName;
private String LastName;
private String LotNo;
private String Street;
private String Barangay;
private String City;
private String Region;
private int Age;
private String Gender;
private Date Birthday;
private String ContactNo;
private String EmailAddress;
private int size=0;
private int yearToday = Calendar.getInstance().get(Calendar.YEAR);
public Member(String MemberId) {
NumberFormat formatter = new DecimalFormat("000000");
size= this.accounts.size()+1;
this.setMemberId(""+yearToday + "-" + (formatter.format(size)));
} //im trying to have the solution here
public Member(String MemberId,String FirstName,String MiddleName,String LastName){
this.setMemberId(MemberId);
this.setFirstName(FirstName);
this.setMiddleName(MiddleName);
this.setLastName(LastName);
}
public int getAge() {
return Age;
}
public void setAge(int Age) {
this.Age = Age;
}
public String getBarangay() {
return Barangay;
}
public void setBarangay(String Barangay) {
this.Barangay = Barangay;
}
public Date getBirthday() {
return Birthday;
}
public void setBirthday(Date Birthday) {
this.Birthday = Birthday;
}
public String getCity() {
return City;
}
public void setCity(String City) {
this.City = City;
}
public String getContactNo() {
return ContactNo;
}
public void setContactNo(String ContactNo) {
this.ContactNo = ContactNo;
}
public String getEmailAddress() {
return EmailAddress;
}
public void setEmailAddress(String EmailAddress) {
this.EmailAddress = EmailAddress;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}
public String getGender() {
return Gender;
}
public void setGender(String Gender) {
this.Gender = Gender;
}
public String getLastName() {
return LastName;
}
public void setLastName(String LastName) {
this.LastName = LastName;
}
public String getLotNo() {
return LotNo;
}
public void setLotNo(String LotNo) {
this.LotNo = LotNo;
}
public String getMemberId() {
return MemberId;
}
public void setMemberId(String MemberId) {
this.MemberId = MemberId;
}
public String getMiddleName() {
return MiddleName;
}
public void setMiddleName(String MiddleName) {
this.MiddleName = MiddleName;
}
public String getRegion() {
return Region;
}
public void setRegion(String Region) {
this.Region = Region;
}
public String getStreet() {
return Street;
}
public void setStreet(String Street) {
this.Street = Street;
}
public String getVotersId() {
return VotersId;
}
public void setVotersId(String VotersId) {
this.VotersId = VotersId;
}
public ArrayList getAccounts() {
return accounts;
}
public void setAccounts(ArrayList accounts) {
this.accounts = accounts;
}
#Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Member other = (Member) obj;
if ((this.MemberId == null) ? (other.MemberId != null) : !this.MemberId.equals(other.MemberId)) {
return false;
}
return true;
}
#Override
public int hashCode() {
int hash = 5;
hash = 89 * hash + (this.MemberId != null ? this.MemberId.hashCode() : 0);
return hash;
}
#Override
public String toString() {
return "Member{" + "MemberId=" + MemberId + ", VotersId=" + VotersId + ", FirstName=" + FirstName + ", MiddleName=" + MiddleName + ", LastName=" + LastName + ", LotNo=" + LotNo + ", Street=" + Street + ", Barangay=" + Barangay + ", City=" + City + ", Region=" + Region + ", Age=" + Age + ", Gender=" + Gender + ", Birthday=" + Birthday + ", ContactNo=" + ContactNo + ", EmailAddress=" + EmailAddress + '}';
}
}
im trying to call the getmemberId by this code in Button"New"
String last="";
last = member.getMemberId();
this.txtMembersID.setText((last));
but i got this error.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
pls help. thank u.
The issue occurs on this line of your first constructor:
size= this.accounts.size()+1;
Because accounts has not been initialized yet.
Adding this line before it:
accounts = new ArrayList();
Should fix your problem.
Also, for future reference, one of the most helpful pieces of information you can provide here would be the number of the line causing the exception- makes things a lot easier to solve!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is my class for Address Book Entry
public class AddressBookEntry {
private String firstname;
private String lastname;
private int streetno;
private String street;
private String city;
private int telephonenumber;
private String emailaddress;
public AddressBookEntry(String firstname, String lastname, int streetno,
String street, String city, int telephonenumber, String emailaddress) {
super();
this.firstname = firstname;
this.lastname = lastname;
this.streetno = streetno;
this.street = street;
this.city = city;
this.telephonenumber = telephonenumber;
this.emailaddress = emailaddress;
}
public AddressBookEntry() {
// TODO Auto-generated constructor stub
}
THESE ARE THE SETTER-GETTER
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getStreetno() {
return streetno;
}
public void setStreetno(int streetno) {
this.streetno = streetno;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getTelephonenumber() {
return telephonenumber;
}
public void setTelephonenumber(int telephonenumber) {
this.telephonenumber = telephonenumber;
}
public String getEmailaddress() {
return emailaddress;
}
public void setEmailaddress(String emailaddress) {
this.emailaddress = emailaddress;
}
THIS IS THE CUSTOM METHODS
public String fullName() {
return this.firstname + " " + this.lastname;
}
public String fullAddress() {
return this.streetno + " " + this.street + " st. " + this.city + " city";
}
public void displayAddress(){
System.out.println("Name : " + fullName());
System.out.println("Address : " + fullAddress());
System.out.println("Telephone Number : " + this.telephonenumber);
System.out.println("Email Address : " + this.emailaddress + "#gmail.com"+"\n");
}
}
NOW THIS IS THE CLASS FOR THE ADDRESSBOOK
import java.util.ArrayList;
import java.util.List;
public class AddressBook {
private List<AddressBookEntry> listOfEntries;
public AddressBook() {
this.listOfEntries = new ArrayList<>();
}
public void add(AddressBookEntry addressBookEntry) {
this.listOfEntries.add(addressBookEntry);
}
public void delete(int index) {
this.listOfEntries.remove(index);
}
public AddressBookEntry get(int index) {
return this.listOfEntries.get(index);
}
public AddressBookEntry[] viewAll() {
AddressBookEntry[] result = new AddressBookEntry[this.listOfEntries.size()];
this.listOfEntries.toArray(result);
return result;
}
}
How can I create a class address book that has a limit of 100 entries of AddressBookEntry objects using the given code above using arrays? While it includes a menu: AddEntry, DeleteEntry, ViewAllEntries, and UpdateEntry?
If you want fixed-sized you should use an array instead of List.
If for some reason you cannot change the list, you can provide the size to the ArrayList constructor.
public AddressBook() {
this.listOfEntries = new ArrayList<>(100);
}
However, you need to check the size before inserting the element.
public void add(AddressBookEntry addressBookEntry) {
if(this.listOfEntries.size() <= 100)
this.listOfEntries.add(addressBookEntry);
else
throw new IllegalStateException("Address book is full");
}