In: Computer Science
Java Program General Scenario: You are creating an interactive application that gets input from the keyboard and keeps track of a grocery store that a businessman runs. In order to develop this application, you will be using 2 interfaces, 1 abstract class, and 1 concrete class. You should use the interfaces and the abstract class to create the concrete class. Your application should have overridden toString() at appropriate places so that you could display the object state, i.e., the string representation of the objects that you create by simply using the following command: System.out.println(object). Follow the following instructions: Create 2 interfaces: Onion and Coke. Interface Onion contains only 1 abstract method: onionPrice() which does not return any data. It takes 1 double argument: onionPrice that represents the current price (per kilo) of onion. Interface Coke contains only 1 abstract method: cokePrice() which does not return any data. It takes 1 double argument: cokePrice that represents the current price (per bottle) of coke. Create an abstract class named Businsess. This class contains 1 instance variable, 1 constructor, and 1 abstract method. Instance variable: numEmp (that will take care of the number of employees in the business. An 1-argument constructor that will be called from its subclass to load 1 instance variable of this level during the creation of an object in its subclass. 1 abstract method: numEmployee(). This method does not take any arguments. When implemented in its subclass, it is supposed to return an integer that represents the number of employees in the business. In this abstract class Business, write necessary code to get the object state (i.e., the string representation) that will eventually be called from its concrete subclass. Create a concrete class named GroceryBusiness that makes use of both the interfaces: Onion and Coke that you created earlier. This concrete class also makes use of the abstract class Business that you also created earlier. a. This concrete class GroceryBusiness has 1 instance variable of its own, 1 constructor, and 1 concrete method of its own. Instance variable: groceryIncome (Use appropriate data type) that represents the current yearly income of the grocery business. Constructor: 1 constructor that constructs a GroceryBusiness object with all the its instance variables (numEmp and groceryIncome) supplied during the GroceryBusiness object creation. 1 concrete method of its own: yearEstablished() that does not return any data. It takes 1 integer argument (year) that represents the year the grocery business was established. This method displays the year the grocery business was established from within itself. Write all necessary code to make this class a concrete class. Write necessary code to get the complete string representation (i.e., the object state) of objects made from this class. [35 marks] Create a driver class named XXXMidTest (replace XXX by three letters of your name). This driver class would prompt the user to enter current number of employees in your grocery business. It would then ask how much you earn from the grocery business. It then uses these 2 user input to create a GroceryBusiness object. It would then display the string representation of the object you just created. Then it would ask for the year the grocery business was established.. It would display the year you just entered. It would then ask for the current price of onion per kilo. And finally it would ask for the current price of coke per bottle. It would then display the prices you just entered.
1)Onion interface:
//interface
public interface Onion {
//only 1 abstract method:
void onionPrice(double onionPrice);
}
2)Coke interface:
//interface
public interface Coke {
//only 1 abstract method
void cokePrice(double cokePrice);
}
3)Business class:
//an abstract class named Business.
public abstract class Business {
//Instance variable
int numEmp;
//An 1-argument constructor
public Business(int numEmp) {
this.numEmp = numEmp;
}
//1 abstract method: numEmployee(). This method does
not take any arguments.
public abstract int numEmployee();
//toString() method
@Override
public String toString() {
return "Business [Number of
employees in the business:" + numEmp + "]";
}
}
4)GroceryBusiness class:
/*This class makes use of both the interfaces: Onion and Coke
also makes use of the abstract class Business */
public class GroceryBusiness extends Business implements Onion,
Coke {
//instance variable
private double groceryIncome;
//An 2-argument constructor which calls
super(Business) class constructor
public GroceryBusiness(int numEmp, double
groceryIncome) {
//calling super(Business) class
constructor
super(numEmp);
this.groceryIncome =
groceryIncome;
}
//Cock interface abstract method
implementation
@Override
public void cokePrice(double cokePrice) {
System.out.println("Current price
(per bottle) of coke:"+cokePrice+".");
}
//Onion interface abstract method
implementation
@Override
public void onionPrice(double onionPrice) {
System.out.println("Current price
(per kilo) of onion:"+onionPrice+".");
}
//Business abstract class abstract method
implementation
@Override
public int numEmployee() {
return this.numEmp;
}
//1 concrete method
void yearEstablished(int year) {
System.out.println("Grocery
business was established in the year "+year+".");
}
//getter and setter methods for groceryIncome
public double getGroceryIncome() {
return groceryIncome;
}
public void setGroceryIncome(double groceryIncome)
{
this.groceryIncome =
groceryIncome;
}
@Override
public String toString() {
return "GroceryBusiness [Grocery
Income:" + groceryIncome + ", Number of employees in the business:"
+ numEmployee() +"]";
}
}
5)JohnMidTest class:
import java.util.Scanner;
//Driver class
public class JohnMidTest {
//main method
public static void main(String[] args) {
//Scanner object to read input from
keyboard
Scanner scan = new
Scanner(System.in);
//asking or prompting for user
input
System.out.print("Enter current
number of employees in your grocery business:");
int numEmp = scan.nextInt();
System.out.print("How much you earn
from the grocery business:");
double groceryIncome =
scan.nextDouble();
//uses these 2 user input to create
a GroceryBusiness object
GroceryBusiness groceryBusiness =
new GroceryBusiness(numEmp, groceryIncome);
//display the string representation
of the object you just created.
System.out.println(groceryBusiness);
System.out.print("Year the grocery
business was established:");
int year = scan.nextInt();
//display the year you just
entered.
groceryBusiness.yearEstablished(year);
System.out.print("Current price of
onion per kilo:");
double onionPrice =
scan.nextDouble();
System.out.print("Current price of
coke per bottle:");
double cokePrice =
scan.nextDouble();
// display the prices you just
entered.
groceryBusiness.onionPrice(onionPrice);
groceryBusiness.cokePrice(cokePrice);
}
}
Output: