payroll system in java - java

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.

Related

How do I create an boolean equals method compares objects in an array

My programming assignment tasked me with writing an increase/decreasePay abstract method that must be put in my abstract employee class. I can't seem to get the the method correct in HourlyWorker so that it will take increase or decrease the pay by a "percentage". My math is sound (monthly pay - or + (monthly pay * the percentage), but my output in my test class is coming out the same after increasing/decreasing pay. Any help?
Employee class:
abstract public class Employee
{
private String lastName;
private String firstName;
private String ID;
public abstract void increasePay(double percentage);
public abstract void decreasePay(double percentage);
public abstract double getMonthlyPay();
public Employee(String last, String first, String ID)
{
lastName = last;
firstName = first;
this.ID = ID;
}
public void setLast(String last)
{
lastName = last;
}
public void setFirst(String first)
{
firstName = first;
}
public void setIdNumber(String ID)
{
this.ID = ID;
}
public String getLastName()
{
return lastName;
}
public String getFirstName()
{
return firstName;
}
public String getName()
{
return firstName + lastName;
}
public String getIdNumber()
{
return ID;
}
}
HourlyWorkerClass
public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;
public HourlyWorker(String last, String first, String ID, double rate)
{
super(last, first, ID);
hourlyRate = rate;
}
public void setHours(int hours)
{
this.hours = hours;
}
public int getHours()
{
return hours;
}
public void setHourlyRate(double rate)
{
if ( hours > 160 )
this.hourlyRate = hourlyRate * 1.5;
else
this.hourlyRate = rate;
}
public double getHourlyRate()
{
return hourlyRate;
}
public void setMonthlyPay(double monthlyPay)
{
monthlyPay = hourlyRate * hours;
}
public double getMonthlyPay()
{
return hourlyRate * hours;
}
public void increasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
public void decreasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
public String toString()
{
String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
+ getIdNumber() + " \nHourly Rate: " + hourlyRate;
return result;
}
}
Testing class (currently testing increase
public class TestEmployee2
{
public static void main(String[] args)
{
Employee [] staff = new Employee[3];
Supervisor sup = new Supervisor("Boss", "Jim", "JB7865", 54000);
HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 11.95);
hw1.setHours(200);
staff[0] = sup;
staff[1] = hw1;
System.out.println(staff[0].getMonthlyPay());
staff[0].increasePay(5);
System.out.println(staff[0].getMonthlyPay());
System.out.println(staff[1].getMonthlyPay());
staff[1].increasePay(10);
System.out.println(staff[1].getMonthlyPay());
}
}
Supervisor class:
public class Supervisor extends Employee
{
private double annualSalary;
private double monthlyPay;
public Supervisor(String last, String first, String ID, double salary)
{
super(last, first, ID);
annualSalary = salary;
}
public void setAnnualSalary(double salary)
{
annualSalary = salary;
}
public double getAnnualSalary()
{
return annualSalary;
}
public double getMonthlyPay()
{
return ((annualSalary + (annualSalary * .02)) / 12);
}
public void increasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
public void decreasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
public String toString()
{
String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
+ getIdNumber() + "\nAnnual Salary: " + annualSalary;
return result;
}
}
Output is:
4590.0 4590.0 2390.0 2390.0
Doesn't appear to be modifying getMonthlyPay()
Should be:
4590.00 4819.50 2390.00 2629.00
Generally, when implementing equals(), you compare “key” fields whose values don’t change for the entity, and don’t compare “state” fields whose values change from time to time.
You are comparing sharePrice, when I believe you should be comparing symbol.
When you do list.indexOf(temp), what that does, right now, is look for a Stock that is equals to the argument passed to it -- so it looks for a Stock with price zero, not caring about the symbol at all. That's what the code does right now.
Honestly, using indexOf and equals is not really appropriate for this problem. indexOf is really only useful when you have something that's totally equal to the target you're looking for.
The best way to do something like this is
Optional<Stock> foundStock = list.stream().filter(stock -> stock.getName().equals(symbol)).findAny();
if (foundStock.isPresent()) {
// do something with foundStock.get()
} else {
// no found stock
}
indexOf() is a method return the index of the first occurrence of the specified element in the returned list. If the list does not contain this element, value -1 is returned.
More formally, return the lowest index i that meets the following conditions:
if(o==null? get(i)==null :o.equals(get(i))){
return i;
}
return -1;
If there is no such index, return -1.
And you have override the equals method, I guess you just want to focus on the same price Stock?:
#Override
public boolean equals(Object obj){
if (obj instanceof Stock){
Stock other = (Stock) obj;
return getPrice() == other.getPrice();
}
return false;
}
As my opinion, you have use List<Stock> list so the Object in the list is all Stock. Maybe it could be simplifed:
#Override
public boolean equals(Object obj){
Stock other = (Stock) obj;
return getPrice() == other.getPrice();
}

some params not being passed to super() construtor

I have a problem passing params into a super constructor. I'm implementing Abstraction with Inheritance. I created three classes that extends the abstract class Employee. But when I created the objects of these three classes, only one class passes the arguments correctly to the super() constructor, while the rest will return null.
Here is the abstract class
public abstract class Employee {
private String firstName,lastName,SSN;
public Employee(String fName, String lName, String SSN){
this.SSN = SSN;
this.firstName = fName;
this.lastName = lName;
}
public void setFirstName(String fName){
this.firstName = firstName;
}
public void setLastName(String lName){
this.lastName = lName;
}
public void setSSN(String SSN){
this.SSN = SSN;
}
public String getSSN(){
return SSN;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public abstract double getEarnings();
#Override
public String toString(){
return String.format("First Name: %-2s \n Last Name: %-2s \n Social Security Number: %-2s", getFirstName(), getLastName(),getSSN());
}
And here are the classes:
public class SalariedEmployee extends Employee {
private String firstName, lastName, SSN;
double weeklyPay;
public SalariedEmployee(String fName, String lName, String SSN, double wPay) {
super(fName, lName, SSN);
this.weeklyPay = wPay;
}
public void setWeeklyPay(double wPay) {
this.weeklyPay = wPay;
}
public double getWeeklyPay() {
return weeklyPay;
}
#Override
public double getEarnings() {
return getWeeklyPay();
}
#Override
public String toString() {
return String.format("%s \n Weekly Pay: %2s", super.toString(), getEarnings());
}
}
public class HourlyEmployee extends Employee{
private String firstName, lastName, SSN;
private double hourlyPay,wage;
private int hours;
public HourlyEmployee(String fName, String lName, String SSN, int hour, double hourlyPay ){
super(fName, lName, SSN);
this.hours = hour;
this.hourlyPay = hourlyPay;
}
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 double getHourlyPay() {
return hourlyPay;
}
public void setHourlyPay(double hourlyPay) {
this.hourlyPay = hourlyPay;
}
public double getWage() {
return wage;
}
public void setWage(double wage) {
this.wage = wage;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
#Override
public double getEarnings(){
double earnings;
if(hours> 40)
earnings = hourlyPay * 40 + ((hours-40) *hourlyPay *1.5);
else
earnings = hourlyPay * hours;
return earnings;
}
#Override
public String toString(){
return String.format("%s \n Hours worked: %s \n Salary: %s \n ", super.toString(), getHours(), getEarnings());
}
}
public class CommissionEmployee extends Employee {
private String firstName, lastName, SSN;
private double sales,commission;
public CommissionEmployee(String fName, String lName, String SSN, double sales, double commission){
super(fName, lName, SSN);
this.commission = commission;
this.sales = sales;
}
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 double getSales() {
return sales;
}
public void setSales(double sales) {
this.sales = sales;
}
public double getCommission() {
double commissionRate;
if(commission >0 && commission<1)
return commission;
else
return 0;
}
public void setCommission(double commission) {
this.commission = commission;
}
#Override
public double getEarnings(){
return getCommission() * getSales();
}
#Override
public String toString(){
return String.format("%s \n Commission Rate: %s \n Salary: %s \n", super.toString(), getCommission(), getEarnings());
}
}
And Here is the TEST Class
public class EmployeeTest {
public static void main(String[] args) {
Employee salariedEmp,hourlyEmp,commissionEmp;
salariedEmp = new SalariedEmployee("Aminu", "Bishir", "11-22-33-33", 300.47);
System.out.println(printDetails(salariedEmp));
hourlyEmp = new HourlyEmployee("Musa","Sale","22-33-44-55",50,20.4);
System.out.println(printDetails(hourlyEmp));
commissionEmp = new CommissionEmployee("Muhammad","Ibrahim","33-44-55-66",20000,0.3);
System.out.print( printDetails(commissionEmp));
}
public static String printDetails(Employee emp){
return String.format("%s \n %s",emp.getClass(),emp.toString());
}
}
And this is the output I get whenever I run it:
class Abstraction.SalariedEmployee
First Name: Aminu
Last Name: Bishir
Social Security Number: 11-22-33-33
Weekly Pay: 300.47
class Abstraction.HourlyEmployee
First Name: null
Last Name: null
Social Security Number: null
Hours worked: 50
Salary: 1122.0
class Abstraction.CommissionEmployee
First Name: null
Last Name: null
Social Security Number: null
Commission Rate: 0.3
Salary: 6000.0
You should delete line
private String firstName, lastName, SSN;
In child classes CommissionEmployee, HourlyEmployee and SalariedEmployee. Because these properties are declared in your abstract class Employee, use it.
You have a problem with declaring variables multiple times. By declaring them in a super class, they also get declared in a subclass. When you declare the variables with the same name in a subclass, you can address the superclass' variable via super.variable. But I would recommend you to simply delete the variable declarations in your subclasses, that were already declared in the superclass.
However this is not the root of your problem. A similar issue occures if you override methods, in your case the subclass that prints correctly does not override the getters, which are used to read out the information stored in your variables. In the class that prints out correctly the getters are found in the superclass and hence the getters are addressing the superclass variables. In the other 2 classes the overridden getters are found, that are using the variables found in the subclasses. Since you set only the variables in the superclass, the subclass variables are always null and so is your result.
I would recommend you to delete the overriden getters and the variables in the subclasses. You don't need them.
Variables
private String firstName,lastName,SSN;
are already defined in your employee class, no need to repeat them. That's why of those nulls

Java working with superclasses, but I do not get the output I expect

Hi I have written this program that Implements a superclass Employee that has the following fields and methods.
Fields:
String firstName
String lastName
int employeeID
double salary
Methods:
Constructor(): initialize balance field to null and zero.
Setters and getters for firstName, lastName, and employeeID
EmployeeSummary() – prints all account attributes
Part 2: Implement a Manager class that inherits from the Employee class.
Has a department attribute
Methods:
EmployeeSummary() – prints all superclass and subclass attributes
The problem is I expected to see:
Employee Name: Charles Dickens Employee Id : 34599 salary: 6500.0
Department : Accounts
as the output but I get nothing....
Any help is greatly appreciated.
here is the code:
package week1john_huber;
import java.util.*;
import java.lang.*;
import java.io.*;
class Employee {
//attributes of Employee class
private String firstName;
private String lastName;
private int employeeID;
private double salary;
public Employee() { //default constructor
firstName = null;
lastName = null;
employeeID = 0;
salary = 0.0;
}
public void setFirstName(String fname) { //set and get methods for all attributes
firstName = fname;
}
public String getFirstname() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lname) {
lastName = lname;
}
public double getEmployeeID() {
return employeeID;
}
public void setEmployeeID(int empId) {
employeeID = empId;
}
public double getSalary() {
return salary;
}
public void setSalary(double s) {
salary = s;
}
public void EmployeeSummary() { //display all attributes of Employee
System.out.println("Employee Name: " + firstName + " " + lastName + " Employee Id :" + employeeID + " salary: " + salary);
}
}
class Manager extends Employee {
private String department;
public Manager() { //default constructor
super(); //calling superor base class default constructor
department = null;
}
public String getDepartment() {
return department;
}
public void setDepartment(String dept) { //set and get methods for department
department = dept;
}
public void EmployeeSummary() {
super.EmployeeSummary(); //calling super class method with same name
System.out.println("Department : " + department);
}
}
class TestEmployee {
public static void main(String[] args) {
Manager mgr = new Manager();
mgr.setFirstName("Charles"); //all set methods of super class are available to derived class
mgr.setLastName("Dickens");
mgr.setEmployeeID(34599);
mgr.setSalary(6500);
mgr.setDepartment("Accounts");
mgr.EmployeeSummary();
}
}
Ok, I believe the problem is the fact that you have the whole thing in a single file.
Try moving the TestEmployee class to its own file and rename the class to
public class TestEmployee
It should work that way.

How Can I Input A Custom Class Value Via Scanner?

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.

unable to access account number

import java.text.DecimalFormat;
import java.util.ArrayList;
public class AccountOwner {
private Account account;
private String firstname;
private String lastname;
private String address;
private double currentBalance;
private ArrayList<Integer> withdrawAmount = new ArrayList<Integer>(5);
private ArrayList<Double> deposits = new ArrayList<Double>();
private ArrayList<Double> purchases = new ArrayList<Double>(5);
private DecimalFormat formatter = new DecimalFormat("##0.00");
public AccountOwner(String firstname, String lastname, String address) {
this.firstname = firstname;
this.lastname = lastname;
this.address = address;
}
public String getFirstName() {
return firstname;
}
public String getLatName() {
return lastname;
}
public String getAddress() {
return address;
}
public String checkBalance() {
for (Double deposit : deposits) {
this.currentBalance += deposit;
}
return formatter.format(currentBalance);
}
public void makeDeposit(double amount) {
deposits.add(amount);
}
public void viewAllDeposits() {
double allDeposits = 0.0;
System.out.println("All deposits till today:");
for (int i = 0; i < deposits.size(); i++) {
allDeposits = deposits.get(i);
System.out.print("\t"+"$"+allDeposits);
}
System.out.println();
}
public void withdrawMoney(int amount) {
withdrawAmount.add(amount);
currentBalance -= amount;
}
public String getActualBalance() {
return formatter.format(currentBalance);
}
public void withdrawAmounts(){
System.out.println("All Withdrawls till today");
for(int i = 0; i < withdrawAmount.size(); i++){
System.out.print("\t"+"$"+withdrawAmount.get(i));
}
System.out.println();
}
public void makePurchase(double itemPrice){
purchases.add(itemPrice);
this.currentBalance -= itemPrice;
}
public void viewAllmadePurchases() {
double purchase = 0.0;
System.out.println("All purchases made till today:");
for (int i = 0; i < purchases.size(); i++) {
purchase = purchases.get(i);
System.out.print("\t"+"$"+purchase);
}
}
public void viewMyPersonalInformation(){
System.out.println("Firstname:" + this.firstname);
System.out.println("LastName:" +this.lastname);
System.out.println("Address:" +this.address);
System.out.println("Balance:" +this.checkBalance());
viewAllDeposits();
withdrawAmounts();
viewAllmadePurchases();
}
public class Account {
private AccountOwner customer;
private int accountNumber;
public Account(){
customer = null;
accountNumber = 0000000;
}
public Account(int accountNumber, AccountOwner owner){
this.accountNumber = accountNumber;
customer = owner;
}
public int accountNumberIs(){
return accountNumber;
}
public class BankManager {
private Account account;
private AccountOwner accountOwner;
private int accountNumber;
public void createNewAccount(int accountNumber, AccountOwner owner){
account = new Account(accountNumber, owner);
this.accountNumber = accountNumber;
this.accountOwner = owner;
}
public int getaccountNumber(){
return accountNumber;
}
public void setAccountNumber(int newaccountnumber){
accountNumber = newaccountnumber;
}
public void customerInformation(){
accountOwner.viewMyPersonalInformation();
}
public class MainProgram {
/**
* #param args
*/
public static void main(String[] args) {
BankManager manager = new BankManager();
AccountOwner owner = new AccountOwner("Tom", "Smith", "208 W 119th St");
manager.createNewAccount(389745, owner);
Account acc = new Account();
owner.makeDeposit(550);
owner.makeDeposit(700);
owner.makeDeposit(122.93);
owner.makeDeposit(195.93);
owner.withdrawMoney(200);
owner.makePurchase(200);
owner.makeDeposit(100);
owner.makePurchase(1220);
owner.withdrawMoney(30);
owner.viewMyPersonalInformation();
System.out.println();
System.out.println();
System.out.println();
System.out.println(acc.accountNumberIs());
The problem that i have is i'm trying to access the account number from the accountowner without involving a reference to the bankmanager class.. how can i get it to work. I have
been trying to using the account class itself cause i created a constructor and assign those
paramaters to the fields in the account class but seems not to work
This code contains a bug :
public Account(int accountNumber, AccountOwner owner){
AccountOwner cstomer = owner;
int acctNumber = accountNumber;
accountNumber = acctNumber;
//System.out.println(accountNumber);
}
The accountNumber parameter you're passing into your constructor is taking precedence over your class's accountNumber field. your Account's accountNumber field is never actually getting set.
This is equivalent to :
public Account(int accountNumber, AccountOwner owner){
AccountOwner cstomer = owner;
accountNumber = accountNumber;
}
to make sure that you're accessing the field use the this keyword as in: this.accountNumber
public Account(int accountNumber, AccountOwner owner){
AccountOwner cstomer = owner;
this.accountNumber = accountNumber;
//System.out.println(accountNumber);
}
There is another bug, the Account.customer is not assigned :
public Account(int accountNumber, AccountOwner owner){
customer = owner;
this.accountNumber = accountNumber;
}
I recommend you to learn to use a debugger or still better write unit tests (first).
class AccountOwner {
//...
private Account account;
//...
public Integer getAccountNumber() {
return this.account != null ? this.account.accountNumberIs() : null;
}
}
You have set your accountNumber type to be int and set it to 0000000 (which is equal to just 0 for an int type) therefore you are getting 0 when you call getAccountNumber() method.
Instead, change accountNumber type to String and initialize it to '0000000'. You will see 0000000 being printed then.

Categories

Resources