In: Computer Science
R.R.Word.file.R.R
(Main Focus of Cheg-Study!)
// this is the question below
You will create specific customer classes with specialized behaviors for a regular subscription and platinum
subscription. This will focus on the concepts of inheritance and polymorphism.
Main objective. This will consist of four classes: Customer, RegularCustomer, PlatinumCustomer, and
Tester (or any name you’d prefer for the main method file). The Customer class will have a
name and type. The RegularCustomer and PlatinumCustomer classes will inherit both of these
fields from the Customer class. The PlatinumCustomer class will also have a discount field. The
methods for each of these classes will be the following:
Customer
* Customer (constructor)
* setName
* setType
* getName
* getType
* toString
RegularCustomer:
* RegularCustomer (constructor)
* toString
PlatinumCustomer
* PlatinumCustomer (constructor)
* setDiscount
* getDiscount
* toString
For the RegularCustomer and PlatinumCustomer constructors, the type of subscription should
be set to “regular” or “platinum". Each of these two constructors should also take
advantage of inheritance when setting the fields. Given a name of “David” and a type of
“regular”, the toString method for Customer and RegularCustomer should print the following:
David has a regular subscription.
The toString method for PlatinumCustomer should also include the discount. Assume a discount
of 7% for the following:
David has a platinum subscription. The current discount on purchases for this subscription is
7.0%.
In the Tester class, make a main method where you create a Customer reference variable. Ask
the user to input a type. If the user enters “regular” for the type, ask for the name and construct
a RegularCustomer object. If the user enters “platinum” for the type, ask for the name and a
discount and then construct a PlatinumCustomer object. Finally, print this created object
to the console.
Take note of one issue with this design. RegularCustomer and
PlatinumCustomer both inherit setType from Customer, but the user should never be able to
change the type for either of these subclasses. Implement an overriding setType for each of
these two subclasses that prevents the type from being changed.
The workflow of the main method should be similar to the following:
For a Regular Subscription;
run:
Please enter a type: regular
Please enter a name: David
David has a regular subscription.
Build successful (total time: 6 seconds)
For a Platinum Subscription;
Please enter a type: platinum
Please enter a name: David
Please enter a discount: 7
David has a platinum subscription.
The current discount on purchases for this subscription is 7.0%
Build successful (total time: 6 seconds)
(End of Work Cheg-Study!)
/******************************Customer.java******************/
/**
* The Class Customer.
*/
public class Customer {
/** The name. */
private String name;
/** The type. */
private String type;
/**
* Instantiates a new customer.
*/
public Customer() {
this.name = "";
this.type = "";
}
/**
* Instantiates a new customer.
*
* @param name the name
* @param type the type
*/
public Customer(String name, String type) {
super();
this.name = name;
this.type = type;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets the name.
*
* @param name the new name
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the type.
*
* @return the type
*/
public String getType() {
return type;
}
/**
* Sets the type.
*
* @param type the new type
*/
public void setType(String type) {
this.type = type;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Customer [name=" + name +
", type=" + type + "]";
}
}
/************************RegularCustomer.java**********************/
/**
* The Class RegularCustomer.
*/
public class RegularCustomer extends Customer {
/**
* Instantiates a new regular customer.
*
* @param name the name
* @param type the type
*/
public RegularCustomer(String name, String type) {
super(name, type);
}
/*
* (non-Javadoc)
*
* @see Customer#toString()
*/
@Override
public String toString() {
return getName() + " has a regular
subscription.";
}
}
/***********************************PlatinumCustomer.java***********************/
/**
* The Class PlatinumCustomer.
*/
public class PlatinumCustomer extends Customer {
/** The discount. */
private double discount;
/**
* Instantiates a new platinum customer.
*
* @param name the name
* @param type the type
*/
public PlatinumCustomer(String name, String type)
{
super(name, type);
}
/**
* Instantiates a new platinum customer.
*
* @param name the name
* @param type the type
* @param discount the discount
*/
public PlatinumCustomer(String name, String type,
double discount) {
super(name, type);
this.discount = discount;
}
/**
* Gets the discount.
*
* @return the discount
*/
public double getDiscount() {
return discount;
}
/**
* Sets the discount.
*
* @param discount the new discount
*/
public void setDiscount(double discount) {
this.discount = discount;
}
/*
* (non-Javadoc)
*
* @see Customer#toString()
*/
@Override
public String toString() {
return getName() + " has a platinum
subscription.\nThe current discount on purchases for this
subscription is "
+ discount + "%";
}
}
/***********************************Tester.java******************/
import java.util.Scanner;
/**
* The Class Tester.
*/
public class Tester {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Please enter a
type: ");
String type =
scan.nextLine();
System.out.print("Please enter a
name: ");
String name =
scan.nextLine();
Customer customer;
if
(type.equalsIgnoreCase("regular")) {
customer =
new RegularCustomer(name, type);
} else {
System.out.print("Please enter a discount: ");
double discount
= scan.nextDouble();
customer = new
PlatinumCustomer(name, type);
((PlatinumCustomer) customer).setDiscount(discount);
}
System.out.println(customer.toString());
scan.close();
}
}
/***************output******************/
Please enter a type: regular
Please enter a name: David
David has a regular subscription.
Please enter a type: platinum
Please enter a name: David
Please enter a discount: 7
David has a platinum subscription.
The current discount on purchases for this subscription is
7.0%
Please let me know if you have any doubt or modify the answer, Thanks :)