Member log in issue - java

I'm having an issue implementing a member log in, in to my library system. This is the code I have so for
Here is my code for from the main system.
case 1:
String StudentUser;
System.out.println("\nEnter Student ID: ");
StudentUser = br.readLine();
for (StudentSignUp: memberList)
{
if (StudentSignUp.getStudentId().equals()){
newMember();
break;
}else
{
system.exit(0);
}
}
Here is the code for the member class...
public class Member
{
private List<StudentSignUp> memberList;
public Member()
{
memberList = new ArrayList<StudentSignUp>();
}
public void newMember(StudentSignUp student)
{
memberList.add(student);
}
public String toString()
{
String totalmem = "\n ";
for (int i=0; i<memberList.size(); i++)
{
StudentSignUp b = memberList.get(i);
totalmem = totalmem + b.toString();
}
return totalmem;
}
}
Here is my code for the StudentSignUp Class...
public class StudentSignUp implements Serializable
{
// instance variables - replace the example below with your own
private int libraryNumber;
private String StudentID;
private String Username;
private String FullName;
private String Address;
private String email;
private String PhoneNumber;
/**
* Add the required feilds to become a member.
*/
public StudentSignUp( int libraryNumber,String FullName, String
StudentID, String Username, String Address, String email, String
strong textPhoneNumber)
{
this.FullName = FullName;
this.StudentID = StudentID;
this.libraryNumber = libraryNumber;
this.Username = Username;
this.Address = Address;
this.email = email;
this.PhoneNumber = PhoneNumber;
}
public String toString()
{
return "\nFullName: " +FullName + "\nStudentID: " +StudentID +
"\nLibrary Number: " +libraryNumber + "\nUsername: " +Username +
"\nAddresss:
" +Address + "\nE-mail: " +email + "\nPhone Number: " +PhoneNumber;
}
public String getStudentId()
{
return StudentID;
}
}

Public void newMember(StudentSignUp student)
{
memberList.add(studend)
}

Related

How to display the Reviews section of my database

I have tried to reorganised my database by adding a child called ("Reviews") that should include reviews added by the users but I keep getting a null displayed on the screen. I can only assume that the variables have not been saved into my modal class. I thought that my calling getReview() in my modal class should work:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference(); // getReference() is the root
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
list.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Information info = snapshot.getValue(Information.class);
assert info != null;
String txt = "Medical Clinic: " + info.getName() + " \n\n " + "Review: " + info.getReview() + "\n\nServices Provided: " + info.getDorevitch() + " : " + info.getSkin_Cancer() + " : " + info.getEar_Suctioning();
list.add(txt);
// list.add(snapshot.getValue().toString());
}
adapter.notifyDataSetChanged();
}
This is my modal class
enter code hepublic 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 String Dorevitch;
private String Skin_Cancer;
private String Ear_Suctioning;
private String Reviews;
public Information() {
}
public Information(String address, String name, String phone_No, String suburb, String state, String postcode, String dorevitch, String skin_cancer, String ear_suctioning, String doctor, String review) {
Address = address;
Name = name;
Phone_No = phone_No;
Suburb = suburb;
State = state;
Postcode = postcode;
Doctor = doctor;
Dorevitch = dorevitch;
Skin_Cancer = skin_cancer;
Ear_Suctioning = ear_suctioning;
Reviews = review;
}
public String getDorevitch() {
return Dorevitch;
}
public void setDorevitch(String dorevitch) {
Dorevitch = dorevitch;
}
public String getSkin_Cancer() {
return Skin_Cancer;
}
public void setSkin_Cancer(String skin_Cancer) {
Skin_Cancer = skin_Cancer;
}
public String getEar_Suctioning() {
return Ear_Suctioning;
}
public void setEar_Suctioning(String ear_Suctioning) {
Ear_Suctioning = ear_Suctioning;
}
public String getState() {
return State;
}
public void setState(String state) {
State = state;
}
public String getDoctor() {
return Doctor;
}
public void setDoctor(String doctor) {
Doctor = doctor;
}
public String getSuburb() {
return Suburb;
}
public void setSuburb(String suburb) {
Suburb = suburb;
}
public String getReview() {
return Reviews;
}
public void setReview(String review) {
Reviews = review;
}
public String getAddress() {
return Address;
}
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;
}
public void setPhone_No(String phone_No) {
Phone_No = phone_No;
}
public String getPostcode() {
return Postcode;
}
public void setPostcode(String postcode) {
Postcode = postcode;
}
I can get it to work but don't know why it is only working if I pull out from the database by doing the following code and not using my modal class?
String reviews = String.valueOf(snapshot.child("Reviews").getValue());
String services = String.valueOf(snapshot.child("Services").getValue());
What is the difference between String.valueof and toString()?

I had a problem with getting datas from Json output

i was trying to show Json output in my Android app. I'm dropping the site link here: https://jsonplaceholder.typicode.com/users
When i try to show ResponseDTO.java output in AddressPage, it works. But when change it to Address.java or AddressDTO.java, i get null value in my app(in AddressPage)
AddressPage.java:
public class AdressPage extends AppCompatActivity {
String id, usr_name;
List<Address> userAdressList;
TextView street, suite, city, zipcode, latText, lngText;
List<GeoDTO> geoDTOList;
Float lat, lng;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_address_page_adapter);
defVars();
get_parameters();
request();
}
public void get_parameters() {
Bundle bundle = getIntent().getExtras();
id = bundle.getString("post_id");
usr_name = bundle.getString("user_name");
}
public void defVars() {
street = findViewById(R.id.street);
suite = findViewById(R.id.suite);
city = findViewById(R.id.city);
zipcode = findViewById(R.id.zipcode);
latText = findViewById(R.id.lat);
lngText = findViewById(R.id.lng);
}
public void assignment(List<Address> addresses) {
street.setText(""+addresses.get(0).getStreet());
suite.setText(""+addresses.get(0).getSuite());
city.setText(""+addresses.get(0).getCity());
zipcode.setText(""+addresses.get(0).getZipcode());
/*zipcode.setText(id);
street.setText(id);
suite.setText(usr_name);
city.setText(usr_name);*/
}
public void request() {
userAdressList = new ArrayList<>();
Call<List<Address>> call = ManagerAll.getInstance().managerGetAdress(id, usr_name);
call.enqueue(new Callback<List<Address>>() {
#Override
public void onResponse(Call<List<Address>> call, Response<List<Address>> response) {
if (response.isSuccessful()) {
userAdressList = response.body();
assignment(userAdressList);
}
}
#Override
public void onFailure(Call<List<Address>> call, Throwable t) {
}
});
}
}
AddressDTO or Address (Same codes):
public class Address implements Serializable {
private String zipcode;
private Geo geo;
private String suite;
private String city;
private String street;
public void setZipcode(String zipcode){
this.zipcode = zipcode;
}
public String getZipcode(){
return zipcode;
}
public void setGeo(Geo geo){
this.geo = geo;
}
public Geo getGeo(){
return geo;
}
public void setSuite(String suite){
this.suite = suite;
}
public String getSuite(){
return suite;
}
public void setCity(String city){
this.city = city;
}
public String getCity(){
return city;
}
public void setStreet(String street){
this.street = street;
}
public String getStreet(){
return street;
}
#Override
public String toString(){
return
"Address{" +
"zipcode = '" + zipcode + '\'' +
",geo = '" + geo + '\'' +
",suite = '" + suite + '\'' +
",city = '" + city + '\'' +
",street = '" + street + '\'' +
"}";
}
}
RestApi interface:
public interface RestApi {
#GET("/users")
Call<List<ResponseDTO>> bring();
#GET("/users") Call<List<ResponseDTO>> bringResponse(#Query("id")String id, #Query("username")String usr_name);
#GET("/users") Call<List<Address>> bringAdress(#Query("id")String id, #Query("username")String usr_name);
//#GET("/users") Call<List<GeoDTO>> bringGeo(#Query("id")String id, #Query("username")String usr_name);
}
ManagerAll.java:
public class ManagerAll extends BaseManager {
private static ManagerAll ourInstance = new ManagerAll();
public static synchronized ManagerAll getInstance() {
return ourInstance;
}
public Call<List<ResponseDTO>> bringCall() {
Call<List<ResponseDTO>> x = getRestApi().bring();
return x;
}
public Call<List<Address>> managerGetAdress(String post, String username) {
Call<List<Address>> y = getRestApi().bringAdress(post, username);
return y;
}
public Call<List<ResponseDTO>> managerBringResponse(String post, String username) {
Call<List<ResponseDTO>> z = getRestApi().bringResponse(post, username);
return z;
}
}
This is ResponseDTO:
public class ResponseDTO implements Serializable {
private String website;
private AddressDTO address;
private String phone;
private String name;
private CompanyDTO company;
private int id;
private String email;
private String username;
public void setWebsite(String website){
this.website = website;
}
public String getWebsite(){
return website;
}
public void setAddress(AddressDTO address){
this.address = address;
}
public AddressDTO getAddress(){
return address;
}
public void setPhone(String phone){
this.phone = phone;
}
public String getPhone(){
return phone;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setCompany(CompanyDTO company){
this.company = company;
}
public CompanyDTO getCompany(){
return company;
}
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setEmail(String email){
this.email = email;
}
public String getEmail(){
return email;
}
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return username;
}
#Override
public String toString(){
return
"ResponseDTO{" +
"website = '" + website + '\'' +
",address = '" + address + '\'' +
",phone = '" + phone + '\'' +
",name = '" + name + '\'' +
",company = '" + company + '\'' +
",id = '" + id + '\'' +
",email = '" + email + '\'' +
",username = '" + username + '\'' +
"}";
}
}
As i said, when i set the list in AddressPage AddressDto(or Address) to Response i can get datas. But it gives me null value when i set it to AddressDto.
GeoDTO:
public class GeoDTO implements Serializable {
private String lng;
private String lat;
public void setLng(String lng){
this.lng = lng;
}
public String getLng(){
return lng;
}
public void setLat(String lat){
this.lat = lat;
}
public String getLat(){
return lat;
}
#Override
public String toString(){
return
"GeoDTO{" +
"lng = '" + lng + '\'' +
",lat = '" + lat + '\'' +
"}";
}
}
CompanyDTO:
public class CompanyDTO implements Serializable {
private String bs;
private String catchPhrase;
private String name;
public void setBs(String bs){
this.bs = bs;
}
public String getBs(){
return bs;
}
public void setCatchPhrase(String catchPhrase){
this.catchPhrase = catchPhrase;
}
public String getCatchPhrase(){
return catchPhrase;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
#Override
public String toString(){
return
"CompanyDTO{" +
"bs = '" + bs + '\'' +
",catchPhrase = '" + catchPhrase + '\'' +
",name = '" + name + '\'' +
"}";
}
}
Address class is not similar to your json response so you are not getting data from your service . You can read first ResponseDTO data then read address data from json.

Getting null while Parsing XML

I'm trying to parse strig xml using JaxB API.
Below is my code
XML
http://www.devx.com/supportitems/showSupportItem.php?co=38898&supportitem=listing1
Pojo Classes
#XmlRootElement(name = "employees")
public class FileWrapper {
private LinkedHashSet<Employee> employees;
public LinkedHashSet<Employee> getEmployees() {
return employees;
}
#XmlElement(name = "employee")
public void setEmployees(LinkedHashSet<Employee> employees) {
this.employees = employees;
}
}
Employee.java
import javax.xml.bind.annotation.XmlAttribute;
public class Employee {
private String division;
private String firstname;
private String id;
private String title;
private String building;
private String room;
private String supervisor;
private String lastname;
public String getDivision() {
return division;
}
#XmlAttribute
public void setDivision(String division) {
this.division = division;
}
public String getFirstname() {
return firstname;
}
#XmlAttribute
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getId() {
return id;
}
#XmlAttribute
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
#XmlAttribute
public void setTitle(String title) {
this.title = title;
}
public String getBuilding() {
return building;
}
#XmlAttribute
public void setBuilding(String building) {
this.building = building;
}
public String getRoom() {
return room;
}
#XmlAttribute
public void setRoom(String room) {
this.room = room;
}
public String getSupervisor() {
return supervisor;
}
#XmlAttribute
public void setSupervisor(String supervisor) {
this.supervisor = supervisor;
}
public String getLastname() {
return lastname;
}
#XmlAttribute
public void setLastname(String lastname) {
this.lastname = lastname;
}
#Override
public String toString() {
return "ClassPojo [division = " + division + ", firstname = " + firstname + ", id = " + id + ", title = "
+ title + ", building = " + building + ", room = " + room + ", supervisor = " + supervisor
+ ", lastname = " + lastname + "]";
}
}
MainEntry
public class TestEntryPoint {
public static void main(String[] args) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(FileWrapper.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader("XML from above link as String");
FileWrapper person = (FileWrapper) unmarshaller.unmarshal(reader);
List<Employee> emp = new ArrayList<Employee>(person.getEmployees());
System.out.println(emp.get(0).getFirstname());
}
}
So when am trying to extract any tag's value its showing null. Is there any problem with my XML's data structure or pojo classes ? I'm stuck in this from last couple of hours.
What am doing wrong ? Canyone suggest please ?
Thanks
Annotations in Employee class refer to XML attributes instead of the elements.
Here is corrected version:
public class Employee {
private String division;
private String firstname;
private String id;
private String title;
private String building;
private String room;
private String supervisor;
private String lastname;
public String getDivision() {
return division;
}
#XmlElement(name = "division")
public void setDivision(String division) {
this.division = division;
}
public String getFirstname() {
return firstname;
}
#XmlElement(name = "firstname")
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getId() {
return id;
}
#XmlAttribute
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
#XmlElement(name = "title")
public void setTitle(String title) {
this.title = title;
}
public String getBuilding() {
return building;
}
#XmlElement(name = "building")
public void setBuilding(String building) {
this.building = building;
}
public String getRoom() {
return room;
}
#XmlElement(name = "room")
public void setRoom(String room) {
this.room = room;
}
public String getSupervisor() {
return supervisor;
}
#XmlElement(name = "supervisor")
public void setSupervisor(String supervisor) {
this.supervisor = supervisor;
}
public String getLastname() {
return lastname;
}
#XmlElement(name = "lastname")
public void setLastname(String lastname) {
this.lastname = lastname;
}
#Override
public String toString() {
return "ClassPojo [division = " + division + ", firstname = " + firstname + ", id = " + id + ", title = "
+ title + ", building = " + building + ", room = " + room + ", supervisor = " + supervisor
+ ", lastname = " + lastname + "]";
}
}

Work with methods, setters and getters in java

I have this class:
public class Person
{
/**
*
*/
private static final long serialVersionUID = 1L;
private String firstName = "Vasya";
private String lastName = "Pupkin";
private Integer age = 58;
private Integer phone = 2;
#Override
public String toString()
{
return "Person [firstName=" + firstName + ", lastName=" + lastName
+ ", age=" + age + "]";
}
public void setName(String name)
{
firstName = name;
}
public void setLastName(String lName)
{
lastName = lName;
}
public void setAge(Integer personAge)
{
age = personAge;
}
public void setPhone(Integer personPhone)
{
phone = personPhone;
}
public String getName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public Integer getAge()
{
return age;
}
public Integer getPhone()
{
return phone;
}
public void Init()
{
this.setName("");
this.setLastName("");
this.setPhone(0);
this.setAge(0);
}
}
I create an variable: Person somePerson, then I call method setName from that variable somePerson:
somePerson.setName("");
but it raises an error.
Based on the provided code, the following should work:
Person somePerson = new Person();
somePerson.setName("");
If it doesn't, then something else is going on.

Getting null with getters and setters

When I place the getters and setters in a method, then call that method in the main method, I get a value of null despite having set the value to something else! Additionally, I'm not receiving any errors from the compiler, so I'm sure it's a logical error somewhere but I cannot figure it out.
This is from the class containing the methods in question:
public class URTest {
UserRegistration _user = new UserRegistration();
public static void main(String[] args) {
String fieldName;
UserRegistration _user = new UserRegistration();
URTest _run = new URTest();
Scanner input = new Scanner(System.in);
fieldName = input.nextLine();
_run.test(fieldName);
System.out.println(_user.getUsername());
}
public void test(String fieldName) {
UserRegistration _user = new UserRegistration();
Scanner input = new Scanner(System.in);
String result;
System.out.print("Enter " + fieldName + ": ");
result = input.nextLine();
while(true) {
if(result.equals("")) {
System.out.print("Please enter a value for the " + fieldName + ": ");
result = input.nextLine();
} else {
break;
}
if(fieldName.equals("username")) {
_user.setUsername(result);
}
}
}
}
Here is the class containing my getters and setters:
import java.io.Serializable;
public class UserRegistration implements Serializable {
private String username;
private int userId;
private String firstName;
private String lastName;
private String password;
private String email;
public UserRegistration() {
this.username = username;
this.userId = userId;
}
public UserRegistration(String uName, int id, String uPassword) {
this.username = uName;
this.userId = id;
this.password = uPassword;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
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 void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Override
public String toString() {
String name = getFirstName() + getLastName();
String output = name + "has the user id " + getUserId() +
"username " + getUsername() + " and email " + getEmail();
return output;
}
}
You are calling
UserRegistration _user = new UserRegistration();
And in your class
public UserRegistration() {
this.username = username;
this.userId = userId;
}
When the above constructor gets called, there is no value to be set to the member variable and therefore you are getting null
In your calling method,
UserRegistration _user = new UserRegistration("name","id1");
OR
set default values in your constructor:
public UserRegistration() {
this.username = "name";
this.userId = "id1";
}

Categories