In: Computer Science
Chapter 9 Programming Project (Inheritance, Abstract Class/Methods, Overriding Methods)
-For all classes, you need to provide the accessor and mutator methods for all instance variables, and provide/override the toString methods.
-Create a Customer class that has the attributes of name and age. Provide a method named importanceLevel. Based on the requirements below, I would make this method abstract.
-Extend Customer to two subclasses: FlightCustomer, and RetailCustomer
-FlightCustomer attributes: ticketPrice, seatNumber (The seat number should be a randomly generated number between 1 to 200)
-RetailCustomer attributes: itemsPurchased, totalSpent.
-For both FlighCustomer and RetailCustomer, you need to provide the implementation for the importanceLevel method. There are four levels: gold, silver, bronze, regular. For FlighCustomer, the level is based on the ticketPrice; for RetailCustomer, the level is based on the average price of each item.
-Instantiate three Customers for each class (six allotter) and tests ALL methods in a meaningful/informative way.
-Programming Language is Java
Java Code:
import java.util.Random;
abstract class Customer{
String name;
int age;
Customer(String name,int age){
this.name=name;
this.age=age;
}
//getter method for name
public String getName() {
return name;
}
//getter method for age
public int getAge() {
return age;
}
// Setter method for name
public void setName(String Name) {
this.name = Name;
}
//setter method for age
public void setAge(int Age) {
this.age = Age;
}
// abstract method
abstract void importanceLevel();
//toString method
public String toString()
{
return ("The Persons's name is : "+name + " & age is : " + age
);
}
}
class FlightCustomer extends Customer{
int ticketPrice, seatNumber;
Random rand = new Random();
// Generate random integers in range 0 to 200
int rand1 = rand.nextInt(200);
int rand2 = rand.nextInt(200);
FlightCustomer(String name,int age,int ticketPrice){
super(name,age);
this.ticketPrice= ticketPrice;
this.seatNumber=rand1;
}
//getter method
public int getTicketPrice() {
return ticketPrice;
}
//getter method
public int getSeatNumber() {
return seatNumber;
}
// Setter method
public void setTicketPrice(int ticketPrice) {
this.ticketPrice=ticketPrice;
}
//setter method
public void setSeatNumber() {
this.seatNumber = rand2;
}
//toString method
public String toString()
{
return ("The Persons's name is : "+name + " & age is : " + age
+ " & TicketPrice is " +ticketPrice +" & seatnumber is : "
+seatNumber );
}
//here we implementing abstract method of customer class
public void importanceLevel(){
if(ticketPrice>500){
System.out.println("Level is gold");
}
if(ticketPrice>=400 && ticketPrice<500){
System.out.println("Level is silver");
}
if(ticketPrice>=300 && ticketPrice<400){
System.out.println("Level is bronze");
}
if(ticketPrice<300){
System.out.println("Level is regular");
}
}
}
class RetailCustomer extends Customer{
int itemsPurchased, totalSpent;
RetailCustomer(String name,int age,int itemsPurchased,int
totalSpent){
super(name,age);
this.itemsPurchased=itemsPurchased;
this.totalSpent=totalSpent;
}
//getter method
public int getItemsPurchased() {
return itemsPurchased;
}
//getter method
public int getTotalSpent() {
return totalSpent;
}
// Setter method
public void setItemsPurchased(int itemsPurchased) {
this.itemsPurchased=itemsPurchased;
}
//setter method
public void setTotalSpent(int totalSpent) {
this.totalSpent=totalSpent;
}
//toString method
public String toString()
{
return ("The Persons's name is : "+name + " & age is : " + age
+" & itemPurchased is : "+ itemsPurchased+" & totalspent is
: " +totalSpent);
}
//here we implementing abstract method of customer class
public void importanceLevel(){
int avgPrice;
//if itemPurchased is zero.then avgPrice will be undefined.so we
handel that case using the following try catch block
try {
avgPrice = totalSpent / itemsPurchased;
}
catch(ArithmeticException e){
System.out.println("Item purchased can not be zero");
return;
}
if(avgPrice>500){
System.out.println("Level is gold");
}
if(avgPrice>=400 && avgPrice<500){
System.out.println("Level is silver");
}
if(avgPrice>=300 && avgPrice<400){
System.out.println("Level is bronze");
}
if(avgPrice<300){
System.out.println("Level is regular");
}
}
}
public class HelloWorld{
public static void main(String []args){
// here we create 3 FlightCustomer objects
FlightCustomer f1=new FlightCustomer("Ram",80,9000);
FlightCustomer f2=new FlightCustomer("sam",29,199);
FlightCustomer f3=new FlightCustomer("jodu",26,400);
System.out.println(f1.toString());
f1.importanceLevel();
f1.setName("Ravan");
f1.setSeatNumber();
f2.setAge(36);
System.out.println(f1.toString());
System.out.println(f2.toString());
f2.importanceLevel();
System.out.println(f3.toString());
f3.importanceLevel();
// here we create 3 RetailedCustomer objects
RetailCustomer r1=new RetailCustomer("sanu",20,50,600);
RetailCustomer r2=new RetailCustomer("hanu",30,10,7000);
RetailCustomer r3=new RetailCustomer("bonu",40,15,300);
System.out.println(r1.toString());
r1.importanceLevel();
r1.setName("shreya");
r1.setAge(45);
r1.setItemsPurchased(5);
r1.setTotalSpent(700);
System.out.println(r1.toString());
System.out.println(r2.toString());
r2.importanceLevel();
System.out.println("honu's age is : "+r2.getAge());
System.out.println(r3.toString());
System.out.println("bonu's totalspent is :
"+r3.getTotalSpent());
r3.importanceLevel();
}
}
OUTPUT:
Code in my Complier: