I am trying to put together a small test application that takes inputs via scanner and puts them in-memory via hashmap and tree set.
Later on I'll search, edit, and delete them (So basically a CRUD), it requires 2 classes an employee class and a company one. I'm trying to take the input for the employee with all the information for the employee including the company which is a foreign custom class object in the POJO.
It won't let me class cast it, what should I do?
Here is the POJO
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
private String ssn;
private Integer salary;
private String birthDate;
private String jobTitle;
#ManyToOne
private Company companyName;
public Employee (String fN, String lN, String SSN, Integer sal, String birth, String jobT, Company compName) {
lastName = lN;
firstName = fN;
SSN = ssn;
sal = salary;
birth = birthDate;
jobT = jobTitle;
compName = companyName;
}
public String toString()
{
return "Employee[Last Name= " + lastName + ", First Name= " + firstName + " SSN= " + ssn + ","
+ "Salary= " + salary + ", Birth Date= " + birthDate + ", Job Title= " + jobTitle + ",]" ;
}
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 String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
public String getBirthDate() {
return birthDate;
}
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public Company getCompanyName() {
return companyName;
}
public void setCompanyName(Company companyName) {
this.companyName = companyName;
}
}
Here is part of the class the scanner is in
System.out.println("Enter Company Name : ");
String val7 = input1.nextLine();
...
Employee newEmp = new Employee(str1,str2, str3, val4, str5, str6, val7);
If I pass the str7 in, it obviously creates an error as the method takes the CompanyName.
Any idea what I need to be doing as ClassCast doesn't work here.
EDIT here is the company class
public class Company {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
String companyName;
private String description;
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
you need to create instance of Company, don't pass it as string:
System.out.println("Enter Company Name : ");
String val7 = input1.nextLine();
Company company = new Company();
company.setCompanyName(val7);
and use company instead of val7 when you create Employee
Employee newEmp = new Employee(str1,str2, str3, val4, str5, str6, company);
ALSO you have a logical error in the constructor, this
compName = companyName;
should be the other way around ... like this
companyName = compName;
because you want to assign the value from param to the member,
There is only the way you already half took:
you use the scanner and ask for the values that you need to create a new instance of that class
you validate all those values
you use them to create the object
Like:
while (something) {
String newFirstName = scanner.next();
...
int newSalary = scanner.nextInt();
...
Employee newGuy = new Employee(newFirstName, ...
In other words: Scanner only knows how to provide those specific "built-in" types such as String, int, float, ...
Thus: if you need an aggregate of such types, you have collect them "manually".
In your case: simply ask for companies first. Meaning: have the user first enter the companies; and your code create company objects. You could put those into a Map<String, Company> for example. Now, when entering employees, you ask for the company name last; and then your code retrieves the Company object for the name entered.
Related
package week3;
import java.util.ArrayList;
public abstract class TaxPayer {
private final String TFN;
private String firstName;
private String lastName;
private double income;
private Address address(int streetNumber, String streetName, String suburb,
String city, String state, int postcode);
public TaxPayer(String TFN, String firstName, String lastName, double income,
Address address) {
this.TFN = TFN;
this.firstName = firstName;
this.lastName = lastName;
this.income = income;
this.address = address;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public double getIncome() {
return income;
}
public Address getAddress() {
return address;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setIncome(double income) {
this.income = income;
}
public void setAddress(Address i) {
}
public String getFullName() {
return getFirstName() + " " + getLastName();
}
#Override
public String toString() {
return TFN + " " + firstName + " " + lastName + " "
+ income;
}
public static double calcTax(double income) {
return 1;
}
public double calcTax() {
return 1;
}
public static void printArrayListToConsole(ArrayList<TaxPayer> Array) {
for (TaxPayer d : Array) {
System.out.println(d);
}
}
}
package week3;
public class Address {
int streetNumber;
String streetName;
String suburb;
String city;
String state;
int postcode;
public Address(int streetNumber, String streetName, String suburb,
String city, String state, int postcode) {
this.streetNumber = streetNumber;
this.streetName = streetName;
this.suburb = suburb;
this.city = city;
this.state = state;
this.postcode = postcode;
}
#Override
public String toString() {
return streetNumber + " " + streetName + " " + suburb + " " + city + " "
+ state + " " + postcode;
}
}
So, my problem is in creating the address attribute, which must be private. I think I've made the other class correctly yet it still isn't working. As well as this, the attribute isn't working with the fields I try to put in it (streetNumber, streetName, etc), it's coming up with the error 'missing method body, or declare abstract' but I'm unsure why. Any help is appreciated!
private Address address(int streetNumber, String streetName, String suburb,
String city, String state, int postcode);// this is a method not a attribute
Make private attribute of Address Class.
public abstract class TaxPayer {
.
.
.
private Address address; // this is attribute.
.
.
.
//One way is passing the all address attributes in constructor
public TaxPayer(String TFN, String firstName, String lastName, double income,int streetNumber, String streetName, String suburb,
String city, String state, int postcode) {
this.TFN = TFN;
this.firstName = firstName;
this.lastName = lastName;
this.income = income;
this.address = new Address(streetNumber, streetName, String suburb,city, state, postcode);
}
}
second option is use same constructor and make a Address objectin main class and pass into that constructor like :
public TaxPayer(String TFN, String firstName, String lastName, double income,Address address) {
this.TFN = TFN;
this.firstName = firstName;
this.lastName = lastName;
this.income = income;
this.address = address;
}
And while creating Tax Payer Object first create Address Object and pass it.
Address address= new Address(streetNumber, streetName, String suburb,city, state, postcode);
TaxPayer tp= new TaxPayer(TFN, firstName, lastName, income,address);
private Address address(int streetNumber, String streetName, String suburb,
String city, String state, int postcode);
You have to declaring a variable of Address type but this is not the way to declare a variable.
Replace the above code of TaxPayer class with just this
private Address address;
So within this class, I need to create a Equals method that will check to determine if the two objects have the same name. I tried creating the two objects within the class and just initialize it with "" for the constructor, but it gave an error on the created objects
Person.Java
public class Person
{
String firstName = "";
String lastName = "";
String age = "";
public Person (String firstName, String lastName, String age){
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String getAge(){
return age;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public void setAge(String age){
this.age = age;
}
public String toString(){
return firstName + " " + lastName + ", " + age + " years old";
}
}
Here is my driver, so basically I need a method that sees both have the same name and prints out a message saying that they have the same name. My lab states it has to be in the class NOT the driver, which is why I'm lost considering I could easily make an if/else statement within the driver.
public class PersonDriver
{
public static void main(String[] args)
{
Person p1 = new Person("John","Doe", "42");
Person p2 = new Person("John","Doe", "43");
System.out.println(p1);
System.out.println(p2);
}
}
I am implementing a payroll system in which I need to implement a PayrollSystem class to add an employee to an ArrayList of employees and create checks for each of those said employees. I've written up the Employee and Paycheck classes, but I'm having trouble with the PayrollSystem class.
How do I create the addEmployee method? Do I pass an Employee object to it along with the information on that employee or is there another way?
Employee:
package payrollSystem;
public class Employee {
private String firstName;
private String lastName;
private int ID;
private double hourlyWage;
private double hoursWorked;
public Employee(String first, String last, int id, double wage, double hours) {
firstName = first;
lastName = last;
ID = id;
hourlyWage = wage;
hoursWorked = hours;
}
public void setFirstName(String first) {
this.firstName = first;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String last) {
this.lastName = last;
}
public String getLastName() {
return lastName;
}
public void setID(int ID) {
this.ID = ID;
}
public int getID() {
return ID;
}
public void setHourlyWage(double hourlyWage) {
this.hourlyWage = hourlyWage;
}
public double getHourlyWage() {
return hourlyWage;
}
public void setHoursWorked(double hoursWorked) {
this.hoursWorked = hoursWorked;
}
public double getHoursWorked() {
return hoursWorked;
}
public double calcPay(double wage, double hours) {
wage = getHourlyWage();
hours = getHoursWorked();
return wage * hours;
}
}
PayCheck:
package payrollSystem;
public class PayCheck {
private String firstName;
private String lastName;
private int ID;
private double netAmount;
public PayCheck(String first, String last, int id, double wage, double hours) {
firstName = first;
lastName = last;
ID = id;
netAmount = wage * hours;
}
public String toString() {
return "Paycheck issued for " + netAmount + "to " + firstName + ", "+ lastName + ", employee ID " + ID;
}
}
PayrollSystem:
package payrollSystem;
import java.util.List;
import java.util.ArrayList;
public class PayrollSystem {
public List<Employee> employees = new ArrayList<Employee>();
public String companyName;
PayrollSystem(String company) {
companyName = company;
}
void addEmployee(Employee a) {
employees.add(a);
}
void getHoursWorked(double hrs) {
this.a.getHoursWorked();
}
void issueCheck() {
double checkAmount = this.a.calcPay(a.getHoursWorked(), a.getHourlyWage());
PayCheck check = new PayCheck(a.getFirstName(), a.getLastName(), a.getID(), a.getHoursWorked(), a.getHourlyWage());
check.toString();
}
}
You can make an addEmployee() method. As long as somewhere you create the employee, and then pass it into your payRoll class.
Would look like this, assuming you have a List of Employees :
public void addEmployee(Employee employee){
employees.add(employee);
}
In your main you could then just go (assuming myEmployee is an Employee Object):
PayrollSystem payrollSystem = new PayRollSystem();
payrollSystem.addEmployee(new Employee("John", "Smith", 1, wage, hours)); // Way 1
payrollSystem.addEmployee(myEmployee); // Way 2
The Employee Object will contain all the information of the Employee. So when that Object is passed in, all the information will come with it. That allows you to fetch all the Employees from the payrollSystem, or use internal methods to perform actions on them.
Please help me find errors from this code. I'm still new and I don't know if this is correct or not.
I do have one error.
This is the error:
constructor Person in class Person cannot be applied to given types;
super();
^
required: String,String,String
found: no arguments
reason: actual and formal argument lists differ in length
This is my code:
import java.util.*;
public class Person {
//Data fields
private String lastName;
private String middleInitial;
private String firstName;
//Constructors
public Person(String lastName, String middleInitial, String firstName) {
this.lastName = lastName;
this.middleInitial = middleInitial;
this.lastName = lastName;
}
//Accessor methods
public String getlastName() {
return lastName;
}
public String getmiddleInitial() {
return middleInitial;
}
public String getfirstName() {
return firstName;
}
//Mutator methods
public void setlastName(String lastName) {
lastName = lastName;
}
public void setmiddleInitial(String middleInitial) {
middleInitial = middleInitial;
}
public void setfirstName(String firstName) {
firstName = firstName;
}
public String getName() {
String studentName = this.lastName + ", " + this.firstName +
this.middleInitial + ".";
return studentName;
}
} //end Person class
class Address {
//Data fields
private String streetName;
private int zipCode;
private String state;
private String country;
//Constructors
public Address(String streetName, int zipCode, String state,
String country) {
this.streetName = streetName;
this.zipCode = zipCode;
this.state = state;
this.country = country;
}
//Accessor methods
public String getstreetName() {
return streetName;
}
public int getzipCode() {
return zipCode;
}
public String getstate() {
return state;
}
public String getcountry() {
return country;
}
//Mutator methods
public void setstreetName(String streetName) {
streetName = streetName;
}
public void setzipCode(int zipCode) {
zipCode = zipCode;
//Integer.toString(zipCode);
}
public void setstate(String state) {
state = state;
}
public void setcountry(String country) {
country = country;
}
public String getAddress() {
String studentAddress = streetName + "\n" + state + ", " + country +
"\n" + zipCode;
return studentAddress;
}
} //end Address class
class Student extends Person {
private String dateOfBirth;
//Constructors
public Student (String studentName, String dateOfBirth) {
super();
dateOfBirth = dateOfBirth;
}
//Accessor methods
public String getdateOfBirth() {
return dateOfBirth;
}
//Mutator methods
public void setdateOfBirth() {
this.dateOfBirth = dateOfBirth;
}
public String toString() {
return ("Date of Birth: " + dateOfBirth);
}
} //end Student subclass
Edited: If I do so for both the Person and Address class. I can only have three-arg constructors. How can I call a one-arg constructor?
For example, I have
public Student (String firstName, String lastName, String middleInitial, String dateOfBirth) {
super(firstName, lastName, middleInitial); and
public Student (String streetName, String state, String country) {
super(streetName, state, country);
How can I get zipcode separately?
Class Person has a constructor, therefore the default no-arg constructor is not created for you. Therefore you can't call super() in Student's constructor, you have to call super(lastName, middleInitial, firstName);.
Or you could create a new Person no-arg constuctor.
Try this
In student class
public Student ( String lastName, String middleInitial, String firstName,String studentName, String dateOfBirth) {
super( lastName, middleInitial,firstName);
this.dateOfBirth = dateOfBirth;
}
Or
In Person class create no arg consructor. Eg:
public Person(){}
Person Class has a constructor with arguments. So default constructor will not be created. So you have to pass 3 String parameters in super(3 String parameters) or you have to create a constructor which does not take any parameter in person class.
Ye've been very helpful so far even though I havn't been great at wording my questions. I think I almost know what I'm doing but I'm trying to get my head around the relationship between getters, setters and constructors. I have two classes as follows
Student and Name and I'm confused about the relationship between the parameters in the getters and setters and constructor
If I remove the parameters from the Name constructor, i.e. this
public Name (){
this.firstName = firstName;
this.lastName = lastName;
and aslo edited the Student constructor to reflect this
public Student(int id, String firstName, String lastName, String street, String area, String city, String country, int age, char gender, String college, String course, int level, int gradePointAverage){ //This is the list of variables called from the Student class
this.id = id;
this.name = new Name(); //I have removed the parameters here
this.name.setFirstName(firstName);
this.name.setLastName(lastName);
this.address = new Address(street, area, city, country);
this.age = age;
this.gender = gender;
this.college = college;
this.course = course;
this.level = level;
this.gradePointAverage = gradePointAverage;
}
I'm not sure what impact it would have. I would be very greatful of someone could explain to me where I should/shouldn't have parameters and why? Understand this concept is what is holding me back from moving any further with coding at the moment.
public class Student {
private Name name; // This is calling from the Name class, giving it the name 'name'
private Address address; // This calls from Address, giving it the name 'address'
private char gender;
private String course, college;
private int gradePointAverage, id, age, level;
public Student(int id, String firstName, String lastName, String street, String area, String city, String country, int age, char gender, String college, String course, int level, int gradePointAverage){ //This is the list of variables called from the Student class
this.id = id;
this.name = new Name(firstName, lastName);
//this.name = new Name();
this.name.setFirstName(firstName);
this.name.setLastName(lastName);
this.address = new Address(street, area, city, country);
this.age = age;
this.gender = gender;
this.college = college;
this.course = course;
this.level = level;
this.gradePointAverage = gradePointAverage;
}
public int getId(){
return id;
}
public String getName(){
return name.toString();
}
public String getAddress(){
return address.toString();
}
public int getAge(){
return age;
}
public char getGender(){
return gender;
}
public String getCollege(){
return college;
}
public int getLevel() {
return level;
}
public String getCourse() {
return course;
}
public int getGradePointAverage() {
return gradePointAverage;
}
public void printStudent() {
System.out.println("The Student " + name.toString() + " is logged under the student ID number " + id + ".");
System.out.println("They live at " + address.toString() + " and their age is " + age + ".");
System.out.println("Their gender is " + gender + ".");
System.out.println("The student studies at " + college + " attending classes in " + course + ".");
System.out.println("Their level is " + level + " and the student grade average in points is " + gradePointAverage + ".");
System.out.println();
}
}
and Name
public class Name{
private String firstName, lastName;
public Name (String firstName, String lastName){
//public Name (){
this.firstName = firstName;
this.lastName = lastName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
//public String getFirstName() {
// return firstName;
//}
//public String getLastName() {
// return lastName;
//}
///** Returns first name concatenated to last name */
//public String toString() {
// return firstName + " " + lastName;
//}
}
The constructors are for initialization of an instance of an Object to ensure that all the minimum amount of data required for a valid object state are provided at creation time.
In your case Name should have a constructor that requires firstName and lastName because those things are what make the Name object fully initialized. This Object should work just like your Address object you have shown as well.
Otherwise if you use setXXX methods, the Name object is incomplete with the two String objects initialized to null or some other undefined state.