You need to write a set of four Java classes. The structures for these Java clas
ID: 3686801 • Letter: Y
Question
You need to write a set of four Java classes. The structures for these Java classes are described here:
A parent class of businesses (Business)
o This is an abstract class, so this class will have no concrete instance.
o All instance variables must be private. This class needs to keep track of at least four (4) pieces of information:
account number – an integer (int)
business name – a string (String)
the primary income – a floating point value (double)
the secondary income – a floating point value (double)
o This class must have the following API:
public Business (int accountNumber, String name); // this is the constructor, it creates and registers a business
public int getAccountNumber (); // return the account number of the business
public void setAccountNumber (int accountNumber); // set the account number of the business
public String getName (); // return the name of the business
public void setName (String name); // set the name of the business
public double getPrimaryIncome (); // return the accumulated primary income
public double getSecondaryIncome (); // return the accumulated secondary income
public void addPrimaryIncome (double amount); // add the amount to the accumulated primary income
public void addSecondaryIncome (double amount); // add the amount to the accumulated secondary income
public abstract double getTaxDue (); // abstract method for calculating tax (this method needs to be abstract since every type of business has a different tax calculating scheme)
public String report (); // return a string that lists the income and tax due (see below for details)
public static Business [] getRegisteredBusinesses (); // return an array of (registered) businesses1 , return an empty array (array of length zero) if no business has been registered yet
public static Business findBusiness (int accountNumber); // find (registered) business by account number, return null if none found
o Each type of businesses will have two types of incomes – primary income and secondary income. Each of the primary income and secondary income are defined based on the type of business.
o The report () method must return a string that looks like the following (see example output files provided):
The first line shows the account number and name of business
The second line reports the type of this business
The third and the fourth lines display the primary and secondary income, respectively
The fifth line displays the total income
The sixth line shows the calculated tax due.
All floating point values (incomes and taxes due) should be output with two (2) digits after the decimal points. (Hint: use the format method of the String class. See API for details)
Three subclasses, one each for restaurants, hotels, and convenience stores.
o These subclasses cannot have instance variables – private static constants are ok.
o These subclasses should have a constructor that takes an account number and a name.
o These subclasses should also implement the getTaxDue () and the report () methods to make the subclasses concrete.
The tax due for a restaurant is calculated as follow:
o Primary income is the (non-alcoholic) food income and will be charged a tax of 6%.
o Secondary income is the alcoholic income and will be charged a tax of 12%.
o If the secondary income is greater than primary income, a surtax of 5% will be charged. This surtax is calculated on the total income.
The tax due for a hotel is calculated as follow:
o Primary income is for room income.
o Secondary income is for suite income.
o The larger of the two types of income will be charged a tax of 15%, while the smaller one will be charged a tax of 12%.
The tax due for a convenience store is calculated as follow:
o Primary income is income for everything except newspaper and will be charged a tax of 7%.
o Secondary income is income for newspaper and will not be charged of any tax.
All methods, except for those defined in the API described above, must be private.
There cannot be any import statement in your file, except for the following (which is necessary for using the ArrayList class): import java.util.ArrayList;
Explanation / Answer
Hello there,
Please find below code and o/p.
import java.util.ArrayList;
import java.util.List;
/**
* It is an abstract class represents a business.
*
* @author dipal.prajapati
*
*/
public abstract class Business {
static List<Business> listOfBusinesses = new ArrayList<Business>();
private int accountNumber;
private String name;
private double primaryIncome;
private double secondaryIncome;
public Business(int accountNumber, String name) {
this.accountNumber = accountNumber;
this.name = name;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrimaryIncome() {
return primaryIncome;
}
public void addPrimaryIncome(double primaryIncome) {
this.primaryIncome = primaryIncome;
}
public double getSecondaryIncome() {
return secondaryIncome;
}
public void addSecondaryIncome(double secondaryIncome) {
this.secondaryIncome = secondaryIncome;
}
public abstract double getTaxDue();
public abstract String report();
@Override
public String toString() {
return "Business [accountNumber=" + accountNumber + ", name=" + name + ", primaryIncome=" + primaryIncome + ", secondaryIncome="
+ secondaryIncome + "]";
}
public static Business[] getRegisteredBusinesses() {
Business[] businessArray = new Business[listOfBusinesses.size()];
return listOfBusinesses.toArray(businessArray);
}
public static Business findBusiness(int accountNumber, List<Business> list) {
for (int index = 0; index < list.size(); index++) {
if (list.get(index).getAccountNumber() == accountNumber)
return list.get(index);
}
System.out.println("No business found for this " + accountNumber + " account number.");
return null;
}
}
/**
* It is a restaurant business class.
* @author dipal.prajapati
*
*/
public class Restaurants extends Business {
public Restaurants(int accountNumber, String name) {
super(accountNumber, name);
}
/**
* It calculates tax for Restaurants.
*/
@Override
public double getTaxDue() {
double taxDue = 0;
taxDue = ((getPrimaryIncome() * 6) / 100) + ((getSecondaryIncome() * 12) / 100);
if (getSecondaryIncome() > getPrimaryIncome()) {
taxDue = taxDue + (((getPrimaryIncome() + getSecondaryIncome()) * 5) / 100);
}
return taxDue;
}
/**
* It reports the business detail.
*/
@Override
public String report() {
StringBuffer buffer=new StringBuffer();
buffer.append(getAccountNumber()+" "+getName()+" ");
buffer.append("Type:Restaurants"+" ");
buffer.append("Primary Income:"+getPrimaryIncome()+" ");
buffer.append("Secondary Income:"+getSecondaryIncome()+" ");
buffer.append("Total Income:"+String.format("%.2f",(getPrimaryIncome()+getSecondaryIncome()))+" ");
buffer.append("Tax due:"+String.format("%.2f",getTaxDue())+" ");
return buffer.toString();
}
}
public class Hotels extends Business {
public Hotels(int accountNumber, String name) {
super(accountNumber, name);
}
/**
* It calculates tax for Hotels.
*/
@Override
public double getTaxDue() {
double taxDue = 0;
if (getSecondaryIncome() > getPrimaryIncome()) {
taxDue = ((getPrimaryIncome() * 12) / 100) + ((getSecondaryIncome() * 15) / 100);
}else{
taxDue = ((getPrimaryIncome() * 15) / 100) + ((getSecondaryIncome() * 12) / 100);
}
return taxDue;
}
/**
* It reports the business detail.
*/
@Override
public String report() {
StringBuffer buffer=new StringBuffer();
buffer.append(getAccountNumber()+" "+getName()+" ");
buffer.append("Type:Hotels"+" ");
buffer.append("Primary Income:"+getPrimaryIncome()+" ");
buffer.append("Secondary Income:"+getSecondaryIncome()+" ");
buffer.append("Total Income:"+String.format("%.2f",(getPrimaryIncome()+getSecondaryIncome()))+" ");
buffer.append("Tax due:"+String.format("%.2f",getTaxDue())+" ");
return buffer.toString();
}
}
public class ConvenienceStores extends Business {
public ConvenienceStores(int accountNumber, String name) {
super(accountNumber, name);
}
/**
* It calculates tax for ConvenienceStores.
*/
@Override
public double getTaxDue() {
double taxDue = 0;
taxDue = (getPrimaryIncome() * 7) / 100;
return taxDue;
}
/**
* It reports the business detail.
*/
@Override
public String report() {
StringBuffer buffer=new StringBuffer();
buffer.append(getAccountNumber()+" "+getName()+" ");
buffer.append("Type:Convenience Stores"+" ");
buffer.append("Primary Income:"+getPrimaryIncome()+" ");
buffer.append("Secondary Income:"+getSecondaryIncome()+" ");
buffer.append("Total Income:"+String.format("%.2f",(getPrimaryIncome()+getSecondaryIncome()))+" ");
buffer.append("Tax due:"+String.format("%.2f",getTaxDue())+" ");
return buffer.toString();
}
}
/**
* Main App.
* @author dipal.prajapati
*
*/
public class BusinessApp {
public static void main(String[] args) {
Business restaurant = new Restaurants(23231, "My Restaurant");
restaurant.addPrimaryIncome(1500000);
restaurant.addSecondaryIncome(100000);
Business hotel = new Hotels(34561, "My Hotel");
hotel.addPrimaryIncome(2000000);
hotel.addSecondaryIncome(400000);
Business store = new ConvenienceStores(42211, "My Store");
store.addPrimaryIncome(1000000);
store.addSecondaryIncome(100000);
Business.listOfBusinesses.add(restaurant);
Business.listOfBusinesses.add(hotel);
Business.listOfBusinesses.add(store);
for (int index = 0; index < Business.listOfBusinesses.size(); index++) {
System.out.println(Business.listOfBusinesses.get(index).report());
}
System.out.println(Business.findBusiness(23231, Business.listOfBusinesses).toString());
Business[] businesses = Business.getRegisteredBusinesses();
for (int i = 0; i < businesses.length; i++) {
System.out.println(businesses[i]);
}
}
}
====O/P====
23231 My Restaurant
Type:Restaurants
Primary Income:1500000.0
Secondary Income:100000.0
Total Income:1600000.00
Tax due:102000.00
34561 My Hotel
Type:Hotels
Primary Income:2000000.0
Secondary Income:400000.0
Total Income:2400000.00
Tax due:348000.00
42211 My Store
Type:Convenience Stores
Primary Income:1000000.0
Secondary Income:100000.0
Total Income:1100000.00
Tax due:70000.00
Business [accountNumber=23231, name=My Restaurant, primaryIncome=1500000.0, secondaryIncome=100000.0]
Business [accountNumber=23231, name=My Restaurant, primaryIncome=1500000.0, secondaryIncome=100000.0]
Business [accountNumber=34561, name=My Hotel, primaryIncome=2000000.0, secondaryIncome=400000.0]
Business [accountNumber=42211, name=My Store, primaryIncome=1000000.0, secondaryIncome=100000.0]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.