Untitled
public class Company { private int companyID; private String companyName; private int numOfEmployees; private final int MAX_EMPLOYEES = 500; private static int numOfCompanies; public Company() { this(0, "STC", 50); } public Company(int companyID, String companyName) { this(companyID, companyName, 50); } public Company(int companyID, String companyName, int numOfEmployees) { setCompanyID(companyID); setCompanyName(companyName); this.numOfEmployees = numOfEmployees; numOfCompanies++; // Increment the company counter } public int getCompanyID() { return companyID; } public String getCompanyName() { return companyName; } public int getNumOfEmployees() { return numOfEmployees; } public static int getNumOfCompanies() { return numOfCompanies; } public void setCompanyID(int companyID) { if (companyID > 0) { this.companyID = companyID; } else { this.companyID = 0; } } public void setCompanyName(String companyName) { if (companyName != null !companyName.isEmpty()) { this.companyName = companyName; } else { this.companyName = ""; } } public void setNumOfEmployees(int numOfEmployees) { this.numOfEmployees = numOfEmployees; } public void printCompanyInfo() { System.out.println("Company ID: " + companyID); System.out.println("Company Name: " + companyName); System.out.println("Number of Employees: " + numOfEmployees); System.out.println("MAX_EMPLOYEES: " + MAX_EMPLOYEES); } public void calculateRevenue() { int revenue; if (numOfEmployees <= 100) { revenue = numOfEmployees * 10000; } else { revenue = numOfEmployees * 12000; } System.out.println("Revenue: " + revenue); } } class Main { public static void main(String[] args) { System.out.println("Number of companies: " + Company.getNumOfCompanies()); Company c1 = new Company(); System.out.println("After creating company1, Number of companies: " + Company.getNumOfCompanies()); Company c2 = new Company(1, "Apple"); System.out.println("After creating company2, Number of companies: " + Company.getNumOfCompanies()); Company c3 = new Company(2, "Microsoft", 150); System.out.println("After creating company3, Number of companies: " + Company.getNumOfCompanies()); c3.setNumOfEmployees(200); System.out.println("Updated information of third company:"); c3.calculateRevenue(); c3.printCompanyInfo(); } }
Leave a Comment