In: Computer Science
Using Eclipse, create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type double. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type double. For each class, provide its getter and setter functions, a default constructor, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for the Services class is computed as numberOfHours times ratePerHour, and for the Supplies class the cost will be numberOfItems times pricePerItem. Each class should have a function toString() that will return all the required information. Next, create a class called Account with two protected attributes fullName of type String, and accountNum of type integer. Provide appropriate constructors, getter, setters, and toString methods for the class. Make classes Services and Supplies inherit class Account. Write a main() program that uses Java ArrayList to create an array of Account that will store objects of Services and objects of Supplies. Your program should use the loop structure that will ask the user which object to input into the array. The program will then ask the user to enter all the required values. Make up any values when creating each object. The user should have the options to enter one or more objects of Services or Supplies.
Account.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Account {
protected String fullName;
protected int accountNum;
public Account(String fullName, int accountNum)
{
super();
this.fullName = fullName;
this.accountNum = accountNum;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public int getAccountNum() {
return accountNum;
}
public void setAccountNum(int accountNum) {
this.accountNum = accountNum;
}
@Override
public String toString() {
return "Account [fullName=" +
fullName + ", accountNum=" + accountNum + "]";
}
//Main method for testing of the three classes
public static void main(String[] args) {
ArrayList<Account> accounts =
new ArrayList<Account>();
String choice="";
do {
BufferedReader
br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("What do you want to
add?");
System.out.println("1. Services.");
System.out.println("2. Supplies.");
int option =
Integer.parseInt(br.readLine());
String fullname;
int ac_no;
System.out.println("Enter full name: ");
fullname = br.readLine();
System.out.println("Enter account number:
");
ac_no = Integer.parseInt(br.readLine());
switch(option) {
case 1:
double numHours, rate;
System.out.println("Enter
number of hours: ");
numHours =
Double.parseDouble(br.readLine());
System.out.println("Enter
rate: ");
rate =
Double.parseDouble(br.readLine());
Services s = new
Services(fullname, ac_no, numHours, rate);
accounts.add(s);
break;
case 2:
double numItems, price;
System.out.println("Enter
number of items: ");
numItems =
Double.parseDouble(br.readLine());
System.out.println("Enter
price per item : ");
price =
Double.parseDouble(br.readLine());
Supplies sp = new
Supplies(fullname, ac_no, numItems, price);
accounts.add(sp);
break;
default:
System.exit(-1);
}
System.out.println("Do you want to add more?
yes/no");
choice = br.readLine();
}catch(IOException ioe) {
ioe.printStackTrace();
}
}while(choice.contentEquals("yes"));
}
}
Supplies.java
public class Supplies extends Account{
private double numberOfItems;
private double pricePerItem;
public Supplies(String fullName, int accountNum,
double numberOfItems, double pricePerItem) {
super(fullName, accountNum);
this.numberOfItems =
numberOfItems;
this.pricePerItem =
pricePerItem;
}
public Supplies(String fullName, int accountNum)
{
super(fullName, accountNum);
}
public double getNumberOfItems() {
return numberOfItems;
}
public void setNumberOfItems(double numberOfItems)
{
this.numberOfItems =
numberOfItems;
}
public double getPricePerItem() {
return pricePerItem;
}
public void setPricePerItem(double pricePerItem)
{
this.pricePerItem =
pricePerItem;
}
@Override
public String toString() {
return "Supplies [numberOfItems=" +
numberOfItems + ", pricePerItem=" + pricePerItem + "]";
}
public double calculateSales() {
return numberOfItems *
pricePerItem;
}
}
Services.java
public class Services extends Account{
private double numOfHours;
private double ratePerHour;
public Services(String fullName, int accountNum,
double numOfHours, double ratePerHour) {
super(fullName, accountNum);
this.numOfHours = numOfHours;
this.ratePerHour =
ratePerHour;
}
public Services(String fullName, int accountNum)
{
super(fullName, accountNum);
}
@Override
public String toString() {
return "Services [numOfHours=" +
numOfHours + ", ratePerHour=" + ratePerHour + "]";
}
public double calculateSales() {
return
numOfHours*ratePerHour;
}
}