I want to get discount from the getter in the employee class which extends person class. The discount varies upon hourlyWorked. I have set up parameters.
I can get the discount when i use setMethods. e.g if i use
e.setHourlyWorked(56);
System.out.println(e.getHourlyWorked());
System.out.println(e.getDiscount());
I get the result as expected.
But i cant get e.getDiscount()); from the parameter itself i.e
Employee e= new Employee(23, 22,"Nab","Kar","000");
System.out.println(e.getHourlyWorked());
System.out.println(e.getDiscount());
I get 0 as discount.
Person class
public class Person {
//private fields
private String firstName;
private String surname;
private String phoneNumber;
private int discount;
public Person(){
//Default Constructors
firstName="John";
surname="SMITH";
phoneNumber="0000";
}//constructors with all the inputs
public Person(String fn, String sn, String pn){
firstName=fn;
surname=sn;
phoneNumber=pn;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String fn) {
firstName=fn;
}
public String getSurname() {
return surname;
}
public void setSurname(String sn) {
surname = sn;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String pn) {
phoneNumber = pn;
}
#Override
public String toString(){
return getFirstName()+" "+getSurname()+" "+getPhoneNumber();
}
Employee class
package lollyShopSystem;
public class Employee extends Person {
private double hourlyWorked;
private double wage;
private int dis;
public Employee(){
//Default Constructors
hourlyWorked=30;
wage=34;
}
public Employee(double hourlyWorked, double wage, String fn, String sn, String pn) {
super(fn, sn, pn);
this.hourlyWorked = hourlyWorked;
this.wage = wage;
}
public double getHourlyWorked() {
return this.hourlyWorked;
}
public void setHourlyWorked(double hourlyWorked) {
this.hourlyWorked = hourlyWorked;
if (this.hourlyWorked<=20){
this.dis=5;
}
else if (this.hourlyWorked>20 && this.hourlyWorked<=30){
this.dis=10;
}
else {
this.dis=15;
}
}
public double getWage() {
return this.wage;
}
public void setWage(double wage) {
this.wage = wage;
}
public int getDiscount(){
return this.dis;
}
}
Assigning hourlyWorked using the constructor does not change the discount. You can replace the assignment of the field with the setHourlyWorked function instead.
public Employee(double hourlyWorked, double wage, String fn, String sn, String pn) {
super(fn, sn, pn);
setHourlyWorked(hourlyWorked);
this.wage = wage;
}
Related
I have a User class and a Pet class. A user can have multiple pets.
I'm trying to retreive a user document and convert it to a user object as follows:
loggedInUser = documentSnapshot.toObject(User.class);
It throws the following exception:
java.lang.RuntimeException: Could not deserialize object. Can't convert object of type com.google.firebase.firestore.DocumentReference to type com.example.pawsibilities.Pet (found in field 'pets.[0]')
Here is an example of a user in firestore. One of its fields is an array of references (pets).
My pet class look like this in Firestore, and it looks like this in Java:
public class Pet {
private String name;
private String type;
private LocalDate birthday;
private String breed;
public enum Gender {
Female,
Male,
Unknown
}
private Gender gender;
private boolean neutered;
private float height;
private float weight;
private String healthNotes;
private boolean lostStatus = false;
private LocalDate lostSince = null;
private ArrayList<LastSeenDetails> lastSeenDetailsList = null;
public Pet() { }
public Pet(String name, String type, LocalDate birthday, String breed, Gender gender, Boolean neutered, float height, float weight, String healthNotes) {
this.name = name;
this.type = type;
this.birthday = birthday;
this.breed = breed;
this.gender = gender;
this.neutered = neutered;
this.height = height;
this.weight = weight;
this.healthNotes = healthNotes;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getBirthday() {
return birthday.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
}
public String getLostSince() {
return lostSince.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT));
}
public String getBreed() {
return breed;
}
public String getGender() {
return gender.name();
}
public String getHeight() {
return height + " cm";
}
public String getWeight() {
return weight + " kg";
}
public String getHealthNotes() {
return healthNotes;
}
public ArrayList<LastSeenDetails> getLastSeenDetailsList() {
return lastSeenDetailsList;
}
public boolean isLost(){ return lostStatus; }
public String isNeutered() {
if (neutered == true) {
return "Yes";
} else
return "No";
}
public void setLostStatus(boolean lostStatus) {
this.lostStatus = lostStatus;
}
public void setLostSince(LocalDate time) { this.lostSince = time; }
public void setLastSeenDetailsList(ArrayList<LastSeenDetails> lastSeenDetailsList) {
this.lastSeenDetailsList = lastSeenDetailsList;
}
}
It has an empty constructor and all fields have a getter method. I can't seem to find the issue...
It's not necessary to convert document to User, as the retrieved document is an object of type User.
Try this,
User user = documentSnapshot.getData();
or
User user = documentSnapshot.get(String field);
public class Client {
private String idNum;
private int driverLicence;
private String name;
private String surname;
private String mailAddress;
private String address;
private int phoneNum;
public Client(String idNum, int driverLicence, String name, String surname, String mailAddress, String address, int phoneNum) {
this.address=address;
this.driverLicence=driverLicence;
this.idNum=idNum;
this.mailAddress=mailAddress;
this.name=name;
this.phoneNum=phoneNum;
this.surname=surname;
}
public String getIdNum() {
return idNum;
}
public void setIdNum(String idNum) {
this.idNum = idNum;
}
public int getDriverLicence() {
return driverLicence;
}
public void setDriverLicence(int driverLicence) {
this.driverLicence = driverLicence;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getMailAddress() {
return mailAddress;
}
public void setMailAddress(String mailAddress) {
this.mailAddress = mailAddress;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
this.address = address;
}
public int getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(int phoneNum) {
this.phoneNum = phoneNum;
}
}
THE VALUE OF THE FIELD Client.idNum IS NOT USED
for some reason i am getting this kind of error on every field i have written on this class
ALL getters and setters are generated from eclipse
and all my other classes are fine but for some reason this specific class gives this "error"
i have wasted a lot of time on this and can't seem to find the reason why this happends
any ideas?
I copy pasted my code in, and an issue that may be causing your problem is that the code below returns the incorrect instance variable. Your instance variable is "address" not "Address".
public String getAddress() {
return Address;
}
This question already has answers here:
Why is each public class in a separate file?
(11 answers)
Closed 3 years ago.
I am using Eclipse to run this code program to test a Person class and its subclasses. In Eclipse it shows there are errors--that each child class must be defined in its own file.
I am learning Java, and would like to know if this is a must? Or can I make it work with parent and child classes all in one file? If I'm missing something, please point me in the right direction. Thank you!
Here is my code: [I put this is all in one file on Eclipse]
import java.util.*;
//Test program to test Person class and its subclasses
public class Test {
public static void main(String[] args) {
Person person = new Person("person");
Student student = new Student ("student");
Employee employee = new Employee("employee");
Faculty faculty = new Faculty("faculty");
Staff staff = new Staff("staff");
//invoke toString() methods
System.out.println(person.toString());
System.out.println(student.toString());
System.out.println(employee.toString());
System.out.println(faculty.toString());
System.out.println(staff.toString());
}
}
//Defining class Person
public class Person {
protected String name;
protected String address;
protected String phoneNum;
protected String email;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress () {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getEmail() {
return email;
}
public void setEmail (String email) {
this.email = email;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}
//Defines class Student extends Person
public class Student extends Person {
public static final String FRESHMAN = "freshman";
public static final String SOPHMORE = "sophmore";
public static final String JUNIOR = "junior";
public static final String SENIOR = "senior";
protected String classStatus;
public Student(String name) {
super(name);
}
public Student(String name, String classStatus) {
super(name);
this.classStatus = classStatus;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}
//Defines class Employee extends Person
public class Employee extends Person {
protected double salary;
protected String office;
protected MyDate dateHired;
public Employee(String name) {
this(name, 0, "none", new MyDate());
}
public Employee(String name, double salary, String office, MyDate dateHired) {
super(name);
this.salary = salary;
this.office = office;
this.dateHired - dateHired;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getOffice() {
return office;
}
public void setOffice (String office) {
this.office = office;
}
public MyDate getDateHired() {
return dateHired;
}
public void setDateHired(MyDate dateHired) {
this.dateHired = dateHired;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:" + this.getClass().getName();
}
}
//Defines class Faculty extends Employee
public class Faculty extends Employee {
public static String LECTURER = "lecturer";
public static String ASSISTANT_PROFESSOR = "assistant professor";
public static String ASSOCIATE_PROFESSOR + "associate professor";
public static PROFESSOR = "professor";
protected String officeHours;
protected String rank;
public Faculty(String name) {
this(name, "9-5 PM", "Employee");
}
public Faculty(String name, String officeHours, String rank) {
super(name);
this.officeHours = officeHours;
this.rank = rank;
}
public String getOfficeHours() {
return officeHours;
}
public void setOfficeHours(String officeHours) {
this.officeHours = officeHours;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank=rank;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}
//Defines class Staff extends Employee
public class Staff extends Employee {
protected String title;
public Staff(String name) {
this(name, "none");
}
public Staff(String name, String title) {
super(name);
this.title=title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}
//Define class MyDate
public class MyDate {
private int month, day, year;
public MyDate (int month, int day, int year) {
this.day=day;
this.month=month;
this.year=year;
}
}
Yes, there should be one class per file. Moreover, you are using the MyDate class in the Employee class, which you need to extend and you cannot extends more than one class, so it's better use the predefined Date class which is present java.util.Date. Import this in the Employee class.
import java.util.Date;
instead of this:
public Employee(String name, double salary, String office, MyDate dateHired)
use:
public Employee(String name, double salary, String office, Date dateHired)
There are some careless mistakes:
in Employee class
public static String ASSOCIATE_PROFESSOR + "associate professor";
change to:
public static String ASSOCIATE_PROFESSOR = "associate professor";
Similarly in faculty class
public static String ASSOCIATE_PROFESSOR + "associate professor";
put = instead of +.
Now this code will work.
Yes it is a must. One class per file. Class can have inner classes. You can define subclasses as inner classes. But I recommend putting them in separate files and don't use inner classes.
I have made this class in java
package mainpackage;
public class Users {
public char username;
public char name;
public char surname;
public char department;
public int usersCounter;
public char getUsername() {
return this.username;
}
public void setUsername(char username) {
this.username = username;
}
public char getSurname() {
return this.surname;
}
public void setSurname(char surname) {
this.surname = surname;
}
public char getName() {
return this.name;
}
public void setName(char name) {
this.name = name;
}
public char getDepartment() {
return this.department;
}
public void setDepartment(char department) {
this.department = department;
}
public int getUsersCounter() {
return this.usersCounter;
}
public void setUsersCounter(int usersCounter) {
this.usersCounter = usersCounter;
}
public Users(char userName, char Name, char surName, char Department, int UsersCounter) {
username = userName;
name = Name;
surname= surName;
department = Department;
usersCounter = UsersCounter;
}
}
and i also made a subclass in which I try to create the constructor of the subclass,but it shows me an error in super(which i put inside the constructor of the subclass) saying that the constructor Users is undefined.I am using Eclipse and the only solution to this problem is to create a constructor Users,but it already exists!!!
package mainpackage;
public class Students extends Users {
static int registrationNumber;
#SuppressWarnings("static-access")
public int getNumber() {
return this.registrationNumber;
}
#SuppressWarnings("static-access")
public void setNumber(char registrationNumber) {
this.registrationNumber = registrationNumber;
}
public Students(char userName, char Name, char surName, char Department, int UsersCounter,int RegistrationNumber) {
super(userName, Name, surName, Department, UsersCounter);
registrationNumber = RegistrationNumber;
}
}
I want to implement a test to check my sub-class. How do I test that Customer class extends the Person class at run time?
Super Class
import java.util.Scanner;
public class Person
{
private String name;
private String address;
private String number;
public Person() //No Argument constructor//
{
name= "";
address= "";
number= "" ;
}
public Person(String num, String nam, String add) //Explicit value constructor//
{
number= num;
name= nam;
address= add;
}
public String getName() //Accessor method//
{
return name;
}
public void setName(String name )//Mutator method//
{
this.name= name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address= address;
}
public String getTelephoneNumber()
{
return number;
}
public void setNumber(String number)
{
this.number= number;
}
public String toString ()
{
return name + "\n" + address + "\n" + number;
}}
Sub Class
public class Customer extends Person
{
public Customer(String num, String nam, String add)
{
super (num, nam, add);
}
public boolean checkResponse( char response)
{
if (response == 'Y'){return true;}
return false;
}
public void display()
{
System.out.println(super.toString());
}
}