I am following a question but I am getting this error. I am not sure how to fix it, I have looked up this error but i can't relate it to my code. Would appreciate it any help given!
import java.util.Scanner;
public class Employee {
private static String name;
private static String job;
private static float salary;
private static int id;
public Employee() {
Scanner scan = new Scanner(System.in);
name = scan.nextLine();
job = scan.nextLine();
salary = scan.nextFloat();
id = scan.nextInt();
}
public Employee(String name, String job, float salary, int id) {
name = name;
job = job;
salary = salary;
id = id;
}
public String getName(name) {
return name;
}
public String getJobTitle(job) {
return job;
}
public float salary(salary){
return salary;
}
public static void showEmp() {
System.out.println("Employee is called: " + name);
System.out.println("They are a " +job);
System.out.println("Their salary is " + salary);
System.out.println("Their Employee ID number is " + id);
}
}
Your getter should not have a paramter:
public String getName() {
return name;
}
The variables should not be static also the showEmp method.
Full code should look like:
import java.util.Scanner;
public class Employee {
private String name;
private String job;
private float salary;
private int id;
public Employee() {
Scanner scan = new Scanner(System.in);
name = scan.nextLine();
job = scan.nextLine();
salary = scan.nextFloat();
id = scan.nextInt();
}
public Employee(String name, String job, float salary, int id) {
this.name = name;
this.job = job;
this.salary = salary;
this.id = id;
}
public String getName() {
return name;
}
public String getJobTitle() {
return job;
}
public float salary() {
return salary;
}
public void showEmp() {
System.out.println("Employee is called: " + name);
System.out.println("They are a " + job);
System.out.println("Their salary is " + salary);
System.out.println("Their Employee ID number is " + id);
}
}
Related
Create a class that represents an employee. This class will have three constructors to initialize variables. If the constructor doesn't provide a parameter for a field, make it either "(not set)" or "0" as appropriate.
Name:
Employee
Fields:
name : String
idNumber : int
department : String
position : String
Methods:
Employee()
Employee(name : String, idNumber : int)
Employee(name : String, idNumber : int, department : String, position : String)
getName() : String
getDepartment() : String
getPosition() : String
getIdNumber() : int
My issue is that I don't know if the Employee() field is a void
Also not sure why public class Employee is included in the comment rather than the code.
public class Employee {
private String name;
private int idNumber;
private string department;
private string position;
/* public void setEmployee (String n)
{
name = n;
if (n == null)
{
n = "(not set)";
}
}
public void Employee (string n, int id)
{
name = n;
idNumber =id;
}
public void Employee (string n, int id, string d, string p)
{
name = n;
idNumber = id;
department = d;
position = p;
} */
public String getName ()
{
return name;
}
public string getDepartment ()
{
return department;
}
public string getPosition ()
{
return position;
}
public int getIdNumber ()
{
return idNumber;
}
}
Also having an issue with this second part
Not sure where to go from here
import java.util.Scanner;
public class EmployeeDem {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String option;
do {
System.out.println("-- Employee ID Menu --\n Enter 'exit' to quit");
option = keyboard.nextLine();
System.out.println("What is your name?");
String name = keyboard.nextLine();
System.out.println("What is your ID number?");
int idNumber = keyboard.nextInt();
System.out.println("What is your department?");
String department = keyboard.nextLine();
System.out.println("What is your position?");
String position = keyboard.nextLine();
} while (option != "exit");
//class obj instance
Employee myEmployee = new Employee();
myEmployee.
}
}
You can initialize the variables first when creating the class, if you don't change them using setters or constructors they will have the initial value that you setted. I initialized them with "(not set)" and 0
public class Employee {
private String name = "(not set)";
private int idNumber = 0;
private String department = "(not set)";
private String position = "(not set)";
public Employee() {}
public Employee(String name, int idNumber) {
this.name = name;
this.idNumber = idNumber;
}
public Employee(String name, int idNumber, String position) {
this.name = name;
this.idNumber = idNumber;
this.position = position;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIdNumber() {
return idNumber;
}
public void setIdNumber(int idNumber) {
this.idNumber = idNumber;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
Hope this is what you want :)
My instructions are:
The third constructor should have one formal parameter for an Account object. This constructor makes a duplicate account EXCEPT for the account number.
public class Account
{
private String first;
private String last;
private int acctNum;
private double balance;
public Account()
{
first = "";
last = "";
acctNum = (int)Math.random()* 1000;
}
public Account(String f, String l, double b)
{
first = f;
last = l;
balance = b;
}
public Account()
{
}
}
public class Account
{
private String first;
private String last;
private int acctNum;
private double balance;
public Account()
{
first = "";
last = "";
acctNum = (int)Math.random()* 1000;
}
public Account(String f, String l, double b)
{
first = f;
last = l;
balance = b;
}
public Account(Account acc)
{
this.first = acc.first;
this.last = acc.last;
this.balance = acc.balance;
// Account num deliberately omitted
}
}
Right way :
public class Account {
private String firstName;
private String lastName;
private int accountNumber;
private double balance;
public Account()
{
Random random = new Random();
this.accountNumber = random.nextInt(1000);
}
public Account(String firstName, String lastName, double balance)
{
Random random = new Random();
this.accountNumber = random.nextInt(1000);
this.firstName = firstName;
this.lastName = lastName;
this.balance = balance;
}
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 getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
#Override
public String toString() {
return "Account{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", accountNumber=" + accountNumber +
", balance=" + balance +
'}';
}
}
run example:
public class Main {
public static void main(String [] args){
//fist Option
Account firstAccount = new Account();
firstAccount.setBalance(100);
firstAccount.setFirstName("jhon");
firstAccount.setLastName("ker");
System.out.println(firstAccount.toString());
//second Option
Account secondAccount = new Account("alex","alex",1000);
System.out.println(secondAccount.toString());
}
}
I'm a beginner in Java and I need help with how I can declare a Manager class that inherits from the Employee class and adds a bonus instance variable to store a salary bonus. I have this code that I can work around and a test program that I can supply to test it. Any suggestions on how to implement the constructors and methods here
public class Demo {
class Employee {
int id;
public Employee() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
class Manager extends Employee {
double bonus;
public Manager() {
super();
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Manager m = new Manager();
m.setId(1);
m.setBonus(100);
System.out.println("Manger has an ID of " + m.getId() + " with a bonus of " + m.getBonus());
}
}
It will look like below. Study it, and see if you can understand it.
class Employee {
int id;
public Employee() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
class Manager extends Employee {
double bonus;
public Manager() {
super();
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
}
public class Demo {
public static void main(String[] args) {
Manager m = new Manager();
m.setId(1);
m.setBonus(100);
System.out.println("Manger has an ID of " + m.getId() + " with a bonus of " + m.getBonus());
}
}
Lets start with 3 fields id,name and Salary for Employee and manager gets an additional field bonus.The salary for Manager will have bonus component added to the salary.
For Employee class create an argument constructor and set all the fields.Create getters for the fields required.
public class Employee {
protected int id;
protected String Name;
protected double Salary;
public Employee(int id, String name, double salary) {
this.id = id;
this.Name = name;
this.Salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return Name;
}
public double getSalary() {
return Salary;
}
}
The Manager class now will extend Employee class.Based on the requirement the only difference between employee and manager is the salary component.Since all the fields in Employee class is set as protected and we extend the Employee class the state of the field can be changed in the child class.
In the Constructor of Manager class the salary will be calculated as salary + bonus.
public class Manager extends Employee {
private double bonus;
public Manager(int id, String name, long salary,double bonus) {
super(id, name, salary);
this.bonus = bonus;
this.Salary = salary + this.bonus;
}
public static void main(String[] args)
{
Manager mng = new Manager(1,"Manager",10000,100);
Employee emp = new Employee(2,"Employer",10000);
System.out.println("MANAGER SALARY " + mng.getSalary()+" "+mng.getId());
System.out.println("EMPLOYER SALARY " + emp.getSalary()+" "+emp.getId());
}
}
I have a problem with my JPA.
Basically my Couriers are created by the program and the customer and parcels are created by user while running. When I try to add a new parcel I add the object to parcel list in Customer and parcel list in Courier. But it crashes when tries to add to Courier parcel list. I create a courier object before calling my menu in the main class
And it throws the following error:
During synchronization a new object was found through a relationship that was not marked cascade PERSIST: Courier:
Id: 0
Name: null
Vehicle: null.
This is my code:
#Entity
#SequenceGenerator(name="cou_seq", initialValue=1, allocationSize=1)
#SuppressWarnings("SerializableClass")
public class Courier implements Serializable{
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator="cou_seq")
private int couId;
private String name;
private String vehicle;
#OneToMany(mappedBy = "courier", cascade = CascadeType.PERSIST)
private List<Parcel> plist = new ArrayList<>();
public Courier(){
}
public Courier(String nameIn, String vehicleIn){
name = nameIn;
vehicle = vehicleIn;
}
public void addParcel(Parcel p1){
plist.add(p1);
p1.setCo(this);
}
public int getCouId() {
return couId;
}
public String getName() {
return name;
}
public String getVehicle() {
return vehicle;
}
public void setCouId(int couId) {
this.couId = couId;
}
public void setName(String name) {
this.name = name;
}
public void setVehicle(String vehicle) {
this.vehicle = vehicle;
}
public List<Parcel> getParcel(){
return plist;
}
public void setParcel(List<Parcel> parcels) {
plist = parcels;
}
#Override
public String toString(){
return "Courier: \nId: " + couId + "\nName: " + name + "\nVehicle: " + vehicle;
}
//CUSTOMER CLASS
#Entity
#SequenceGenerator(name="cus_seq", initialValue=1, allocationSize=1)
#SuppressWarnings("SeralizableClass")
public class Customer implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator="cus_seq")
private int cusId;
private String login;
private String password;
private String fname;
private String lname;
private String dob;
private String address;
private String phoneNo;
private String email;
#OneToMany(mappedBy = "customer")
private List<Parcel> plist = new ArrayList<>();
public Customer(){
}
public Customer(String loginIn, String passwordIn, String fnameIn, String lnameIn, String dobIn, String addressIn, String phoneNoIn, String emailIn){
login = loginIn;
password = passwordIn;
fname = fnameIn;
lname = lnameIn;
dob = dobIn;
address = addressIn;
phoneNo = phoneNoIn;
email = emailIn;
}
public void addParcel(Parcel p) {
plist.add(p);
p.setC(this);
}
public String getFname() {
return fname;
}
public String getLname() {
return lname;
}
public String getDob() {
return dob;
}
public String getAddress() {
return address;
}
public String getPhoneNo() {
return phoneNo;
}
public String getEmail() {
return email;
}
public void setFname(String fname) {
this.fname = fname;
}
public void setLname(String lname) {
this.lname = lname;
}
public void setDob(String dob) {
this.dob = dob;
}
public void setAddress(String address) {
this.address = address;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public void setEmail(String email) {
this.email = email;
}
public List<Parcel> getParcel(){
return plist;
}
public void setParcel(List<Parcel> parcels) {
plist = parcels;
}
public String toString(){
return "Customer: " + "\nID: " + cusId + "\nLogin: " + login + "\nFirst Name: " + fname + "\nSecond Name: " + lname + "\nDOB: " + dob + "\nAddress: " + address + "\nPhone No: " + phoneNo;
}
}
//PARCEL CLASS
#Entity
#Inheritance(strategy = InheritanceType.JOINED)
#DiscriminatorColumn(name = "type")
#SequenceGenerator(name = "par_seq", initialValue = 1, allocationSize = 1)
#SuppressWarnings("SerializableClass")
public class Parcel {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "par_seq")
private int parId;
private double height;
private double width;
private double length;
private double weight;
private String receiver;
#ManyToOne()
#JoinColumn(name = "cuId")
private Customer customer;
#ManyToOne()
#JoinColumn(name = "coId")
private Courier courier;
private static double price;
public Parcel() {
}
public Parcel(double heightIn, double widthIn, double lengthIn, double weightIn, String receiverIn) {
height = heightIn;
width = widthIn;
length = lengthIn;
weight = weightIn;
receiver = receiverIn;
}
public double getHeight() {
return height;
}
public double getWidth() {
return width;
}
public double getLength() {
return length;
}
public double getWeight() {
return weight;
}
public double getPrice() {
return price;
}
public void setHeight(double height) {
this.height = height;
}
public void setWidth(double width) {
this.width = width;
}
public void setLength(double length) {
this.length = length;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void setPrice(double price) {
this.price = price;
}
public double calcSize(double height, double width, double length) {
return height * width * length;
}
public void setC(Customer c) {
this.customer = c;
}
public Customer getC() {
return customer;
}
public void setCo(Courier c1) {
this.courier = c1;
}
public Courier getCo() {
return courier;
}
#Override
public String toString() {
return "Parcel:\nHeight: " + height + "\nWidth: " + width + "\nLength: " + length + "\nWeight: " + weight;
}
}
//JPA method that adds a new parcel
public Parcel createParcel(double heightAdd, double widthAdd, double lengthAdd, double weightAdd, String receiverAdd,String owner,String type){
int id = findCustomerIdByLogin(owner);
Customer c = em.find(Customer.class, id);
Courier co = new Courier();
em.getTransaction().begin();
if(type.equals("INT")){
System.out.println("Inside here");
InternationalParcel int1 = new InternationalParcel(heightAdd, widthAdd, lengthAdd, weightAdd, receiverAdd);
em.persist(int1);
c.addParcel(int1);
//em.persist(int1);
co.addParcel(int1);
em.getTransaction().commit();
return int1;
} else {
NationalParcel nat1 = new NationalParcel(heightAdd, widthAdd, lengthAdd, weightAdd,receiverAdd);
em.persist(nat1);
c.addParcel(nat1);
em.persist(nat1);
co.addParcel(nat1);
em.getTransaction().commit();
return nat1;
}
}
You are adding your Parcel to a Courier that is not persisted in the Database yet.
You have to Persist your Courier Object as well.
Since you told your Courier Object that it should cascade persist its Parcel objects it should actually be enough to just persist the Courer without persisting each parcel on its own:
Parcel parcel;
Customer c = em.find(Customer.class, id);
Courier co = new Courier();
em.getTransaction().begin();
if(type.equals("INT")){
parcel = new InternationalParcel(heightAdd, widthAdd, lengthAdd, weightAdd, receiverAdd);
} else {
parcel = new NationalParcel(heightAdd, widthAdd, lengthAdd, weightAdd,receiverAdd);
}
co.addParcel(parcel);
em.persist(co); // <- This is what you are currently not doing!
em.persist(parcel); // <- this might not be necessary because of cascade persist
c.addParcel(parcel);
em.getTransaction().commit();
return parcel;
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)
}