Write a Java program to use classes and objects to implement Movie Ticketing Sys
ID: 3823701 • Letter: W
Question
Write a Java program to use classes and objects to implement Movie Ticketing System problem in PA05/Q2 Create a class Customer with the following instance variables:
- String: customerName ie. First name Last name
- String: memberID // this could be null for non-members
- String movieName
- double: memberDiscount // as %
- double: movieTicketCost
- double: discountAmount // calculated value from above values
- double: salesTaxAmount // calculated value from above values
- double: netMovieTicketCost // calculated value from above values
The class shall have a final instance variable of type double, named SALES_TAX_PERCENTAGE and initialized to 7.4%.
Define 2 overloaded constructors to create objects, as follows:
Customer(customerName,movieName, movieTicketCost)
Customer(customerName,movieName, memberID, movieTicketCost)
When the first constructor gets invoked ie. non-member customers, set the memberID and memberDiscount to null and 0%.
When the second constructor with memberID gets invoked, set the memberID and initialize the memberDiscount to a default 5%.
Implement getter and setter methods for all the instance variables. The class shall have the following class methods:
- CalculateDiscount() // discount % will be scratch card discount + memberdiscount
- CalculateSalesTax () - CalculateNetMovieTicketCost()
This method will call CalculateDiscount() and CalculateSalesTax() and set the discountAmount and salesTax instance variables. Then calculate net movie ticket cost as :
netMovieTicketCost = movieTicketCost - discountAmount + salesTaxAmount
- toString() This method must display the output in the following format: Customer Name: Movie Ticket Cost: Total Discount: Sales Tax: Net Movie Ticket Cost Amount: Write a test class TestCustomer with main() method to test the Customer class implementation. Instantiate 3 Customer class objects as follows:
cust1 = Customer(“John Doe”, “Movie 1’’, 12.99)
cust2 = Customer(“David Fields”, “Movie 2”, “8877”, 13.99)
cust3 = Customer(“Carol smith”, “Movie 3”, 5.75)
Calculate and display the net movie ticket cost for each of the above customer objects. Then make the cust3 to become a member by setting the memberID using the corresponding setter method. i.e. cust3.setMemberID(“9250”) Again calculate and display the net movie ticket cost for each of the above customer objects. Evaluation Criteria:
1. You must use the class template in your program classes
2. The program must not have any compilation or runtime errors
Explanation / Answer
public class TestCustomer {
public static void main(String args[]) {
Customer cust1 = new Customer("John Doe", "Movie 1", 12.99);
Customer cust2 = new Customer("David Fields", "Movie 2", "8877", 13.99);
Customer cust3 = new Customer("Carol smith", "Movie 3", 5.75);
cust1.calculateNetMovieTicketCost();
cust2.calculateNetMovieTicketCost();
cust3.calculateNetMovieTicketCost();
System.out.println(" Customer-1 " + cust1.toString());
System.out.println(" Customer-2 " + cust2.toString());
System.out.println(" Customer-3 " + cust3.toString());
cust3.setMemberID("9250");
cust3.setMemberDiscount(5);
System.out.println(" Customer-3 " + cust3.toString());
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------
public class Customer {
public final static double SALES_TAX_PERCENTAGE = 7.4;
private String customerName;
private String memberID;
private String movieName;
private double memberDiscount;
private double movieTicketCost;
private double discountAmount;
private double salesTaxAmount;
private double netMovieTicketCost;
public Customer(String customerName, String movieName, double movieTicketCost) {
this.customerName = customerName;
this.movieName = movieName;
this.movieTicketCost = movieTicketCost;
this.memberDiscount = 0.0;
}
public Customer(String customerName, String movieName, String memberID, double movieTicketCost) {
this.customerName = customerName;
this.movieName = movieName;
this.memberID = memberID;
this.movieTicketCost = movieTicketCost;
this.memberDiscount = 5.0;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getMemberID() {
return memberID;
}
public void setMemberID(String memberID) {
this.memberID = memberID;
}
public String getMovieName() {
return movieName;
}
public void setMovieName(String movieName) {
this.movieName = movieName;
}
public double getMemberDiscount() {
return memberDiscount;
}
public void setMemberDiscount(double memberDiscount) {
this.memberDiscount = memberDiscount;
}
public double getMovieTicketCost() {
return movieTicketCost;
}
public void setMovieTicketCost(double movieTicketCost) {
this.movieTicketCost = movieTicketCost;
}
public double getDiscountAmount() {
return discountAmount;
}
public void setDiscountAmount(double discountAmount) {
this.discountAmount = discountAmount;
}
public double getSalesTaxAmount() {
return salesTaxAmount;
}
public void setSalesTaxAmount(double salesTaxAmount) {
this.salesTaxAmount = salesTaxAmount;
}
public double getNetMovieTicketCost() {
return netMovieTicketCost;
}
public void setNetMovieTicketCost(double netMovieTicketCost) {
this.netMovieTicketCost = netMovieTicketCost;
}
public double calculateDiscount() {
/*
* TODO
* No information provided about scratch card discount
*/
double scratchCardDiscount = 2.0;
return this.memberDiscount + scratchCardDiscount;
}
public double calculateSalesTax() {
return ((movieTicketCost-discountAmount) * SALES_TAX_PERCENTAGE/100.0);
}
public double calculateNetMovieTicketCost() {
this.discountAmount = (movieTicketCost * calculateDiscount()/100.0);
this.salesTaxAmount = calculateSalesTax();
this.netMovieTicketCost = (movieTicketCost - discountAmount) + salesTaxAmount;
return netMovieTicketCost;
}
@Override
public String toString() {
return "Customer Name : " + this.customerName + " "
+ "Movie Name : " + this.movieName + " "
+ "Ticket Cost : " + this.movieTicketCost + " "
+ "Discount Amount : " + this.discountAmount + " "
+ "Sales Tax Amount : " + this.salesTaxAmount + " "
+ "Net Movie Ticket Cost : " + this.netMovieTicketCost + " ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.