Write, test and document (internally) a Java program, and required class definit
ID: 3721992 • Letter: W
Question
Write, test and document (internally) a Java program, and required class definitions, to solve the following problem:
A) Class Ticket has the following attributes and operations
attributes
name of purchaser
address of purchaser
date of purchase
operations
constructor with parameters for each attribute
accessor and mutator methods for each attribute
a toString method
B) In addition to Ticket attributes and operations, class SeasonTicket has the following attributes and operations:
attributes
total number of games in the season
total price for the season
operations
a constructor with parameters for the Ticket and SeasonTicket attributes
accessor and mutator methods for the total number of games and the total price
a toString method
C) In addition to Ticket attributes and operations, class PerGameTicket has the following attributes and operations:
attributes
date of game
ticket price
operations
a constructor with parameters for the Ticket and PerGameTicket attributes
accessor and mutator methods for the date and price attributes
a toString method
D) In addition to Ticket attributes and operations, class Package has the following attributes and operations:
attributes
number of games in the package
the date of each game
package price
operations
a constructor with parameters for the Ticket and Package attributes
accessor and mutator methods for the number of games, the date of each game and the package price
a toString method
E) Write a Java program (with a main method) which creates and initializes a 1-dimensional array of Ticket objects. (This should include objects of all four class types.) Output the attributes for each object. In addition, determine and output the following:
the total price of all tickets
the name of each season ticket holder
the total number of season tickets
the total number of per game tickets
the total number of packages
Create and use a text file to store your input data. Read the contents of the text file as input to your program. Include appropriate error checking in your program.
F) Write an interface definition called Amount which has one method:
and which is implemented by each of class SeasonTicket, PerGameTicket and Package. Method getPrice() should return the average price per game.
Explanation / Answer
******************Save file as "Main.java"*****************
import java.io.File;
import java.io.IOException;
import java.util.*;
class Main {
public static final String TICKET_STRING = "TICKET";
public static final String SEASON_TICKET_STRING = "SEASON_TICKET";
public static final String PER_GAME_TICKET_STRING = "PER_GAME_TICKET";
public static final String PACKAGE_TICKET_STRING = "PACKAGE_TICKET";
public static void main(String args[]) throws IOException {
String inputFileName = "input.txt";
Scanner sc = new Scanner(new File(inputFileName));
// Number of tickets i.e. length of tickers Array
int lenTicketArr = sc.nextInt();
Ticket ticketArr[] = new Ticket[lenTicketArr]; // Array of Tickets
for (int i = 0; i < lenTicketArr; i++) {
int ticketType = sc.nextInt();
// Details of Ticket
String purchaserName = sc.next();
String purchaserAddress = sc.next();
String purchaseDate = sc.next();
if (ticketType == 1){ // Ticket
ticketArr[i] = new Ticket(purchaserName, purchaserAddress, purchaseDate ,TICKET_STRING);
} else if (ticketType == 2){ // Season Ticket
int totalGames = sc.nextInt();
int seasonPrice = sc.nextInt();
ticketArr[i] = new SeasonTicket(purchaserName, purchaserAddress, purchaseDate,
SEASON_TICKET_STRING, totalGames, seasonPrice);
} else if (ticketType == 3){ // Per Game Ticket
int ticketPrice = sc.nextInt();
String gameDate = sc.next();
ticketArr[i] = new PerGameTicket(purchaserName, purchaserAddress, purchaseDate,
PER_GAME_TICKET_STRING, gameDate, ticketPrice);
} else if (ticketType == 4){ // Package Ticket
int numberOfGames = sc.nextInt();
String dateArr[] = new String[numberOfGames];
int priceArr[] = new int[numberOfGames];
for (int j = 0; j <numberOfGames; j++) { // dates Array input
dateArr[j] = sc.next();
}
for (int j = 0; j < numberOfGames; j++) { // prices Array input
priceArr[j] = sc.nextInt();
}
ticketArr[i] = new Package(purchaserName, purchaserAddress, purchaseDate,
PACKAGE_TICKET_STRING, numberOfGames, dateArr, priceArr);
}
}
// Print all input data
for (int i = 0; i < lenTicketArr; i++) {
String ticketType = ticketArr[i].getTicketType();
System.out.println("Type : "+ticketType);
if (ticketType.equals(TICKET_STRING)){
Ticket ticket = ticketArr[i];
System.out.println(ticket.toString());
} else if (ticketType.equals(SEASON_TICKET_STRING)){
SeasonTicket seasonTicket = (SeasonTicket) ticketArr[i];
System.out.println(seasonTicket.toString());
} else if (ticketType.equals(PER_GAME_TICKET_STRING)){
PerGameTicket perGameTicket = (PerGameTicket) ticketArr[i];
System.out.println(perGameTicket.toString());
} else if (ticketType.equals(PACKAGE_TICKET_STRING)){
Package packageTicket = (Package) ticketArr[i];
System.out.println(packageTicket.toString());
}
}
// Get total PRice of all tickets
int totalPrice = 0;
for (int i = 0; i < ticketArr.length; i++) {
String ticketType = ticketArr[i].getTicketType();
if (ticketType.equals(TICKET_STRING) == false){
Amount amount = (Amount) ticketArr[i];
totalPrice += amount.getPrice();
}
}
System.out.println("Total Price of all tickets: "+totalPrice);
// the name of each season ticket holder
System.out.print("Names of Season Ticket Holders : ");
for (int i = 0; i < ticketArr.length; i++) {
String ticketType = ticketArr[i].getTicketType();
if (ticketType.equals(SEASON_TICKET_STRING) ){
SeasonTicket seasonTicket = (SeasonTicket) ticketArr[i];
System.out.print(seasonTicket.getPurchaserName());
}
}
System.out.println();
//the total number of season tickets
int totalSeasonTickets = 0;
for (int i = 0; i < ticketArr.length; i++) {
String ticketType = ticketArr[i].getTicketType();
if (ticketType.equals(SEASON_TICKET_STRING) ){ // find Season ticket
totalSeasonTickets++;
}
}
System.out.println("Total Number of Season Tickets : "+totalSeasonTickets);
// the total number of per game tickets
int totalPerGameTickets = 0;
for (int i = 0; i < ticketArr.length; i++) {
String ticketType = ticketArr[i].getTicketType();
if (ticketType.equals(PER_GAME_TICKET_STRING) ){ // find per game ticket
totalPerGameTickets++;
}
}
System.out.println("Total Number of Season Tickets : "+totalPerGameTickets);
// the total number of packages
int totalPackageTickets = 0;
for (int i = 0; i < ticketArr.length; i++) {
String ticketType = ticketArr[i].getTicketType();
if (ticketType.equals(PACKAGE_TICKET_STRING) ){ // find package ticket
totalPackageTickets++;
}
}
System.out.println("Total Number of Package Tickets : "+totalPackageTickets);
sc.close();
}
}
******************"Ticket.java"********************************
public class Ticket{
private String purchaserName;
private String purchaserAddress;
private String purchaseDate;
private String ticketType;
// Constructor
public Ticket(String name, String address, String date, String ticketType){
this.purchaserName = name;
this.purchaserAddress = address;
this.purchaseDate = date;
this.ticketType = ticketType;
}
// Getter and Mutators
public String getPurchaserName() {
return purchaserName;
}
public void setPurchaserName(String purchaserName) {
this.purchaserName = purchaserName;
}
public String getPurchaserAddress() {
return purchaserAddress;
}
public void setPurchaserAddress(String purchaserAddress) {
this.purchaserAddress = purchaserAddress;
}
public String getPurchaseDate() {
return purchaseDate;
}
public void setPurchaseDate(String purchaseDate) {
this.purchaseDate = purchaseDate;
}
public String getTicketType() {
return ticketType;
}
public void setTicketType(String ticketType) {
this.ticketType = ticketType;
}
@Override
public String toString() {
return "Ticket [purchaserName=" + purchaserName + ", purchaserAddress="
+ purchaserAddress + ", purchaseDate=" + purchaseDate
+ ", ticketType=" + ticketType + "]";
}
}
********************"SeasonTicket.java"****************************
public class SeasonTicket extends Ticket implements Amount{
private int totalGames;
private int seasonPrice;
//Constructor
public SeasonTicket(String name, String address, String date, String ticketType, int games, int price) {
super(name, address, date, ticketType);
this.totalGames = games;
this.seasonPrice = price;
}
// Getter and Mutators
public int getTotalGames() {
return totalGames;
}
public void setTotalGames(int totalGames) {
this.totalGames = totalGames;
}
public int getSeasonPrice() {
return seasonPrice;
}
public void setSeasonPrice(int seasonPrice) {
this.seasonPrice = seasonPrice;
}
@Override
public String toString() {
return "SeasonTicket [totalGames=" + totalGames + ", seasonPrice="
+ seasonPrice + ", getPurchaserName()=" + getPurchaserName()
+ ", getPurchaserAddress()=" + getPurchaserAddress()
+ ", getPurchaseDate()=" + getPurchaseDate()
+ ", getTicketType()=" + getTicketType() + "]";
}
@Override
public double getPrice() {
return getSeasonPrice();
}
}
*******************"PerGameTicket.java"**********************************
public class PerGameTicket extends Ticket implements Amount{
String gameDate;
int ticketPrice;
//Constructor
public PerGameTicket(String name, String address, String date, String ticketType, String gameDate, int ticketPrice) {
super(name, address, date, ticketType);
this.gameDate = gameDate;
this.ticketPrice = ticketPrice;
}
// Getters and Mutators
public String getGameDate() {
return gameDate;
}
public void setGameDate(String gameDate) {
this.gameDate = gameDate;
}
public int getTicketPrice() {
return ticketPrice;
}
public void setTicketPrice(int ticketPrice) {
this.ticketPrice = ticketPrice;
}
@Override
public String toString() {
return "PerGameTicket [gameDate=" + gameDate + ", ticketPrice="
+ ticketPrice + ", getPurchaserName()=" + getPurchaserName()
+ ", getPurchaserAddress()=" + getPurchaserAddress()
+ ", getPurchaseDate()=" + getPurchaseDate()
+ ", getTicketType()=" + getTicketType() + "]";
}
@Override
public double getPrice() {
return getTicketPrice();
}
}
**********************"Package.java"************************
import java.util.Arrays;
public class Package extends Ticket implements Amount{
private int numberOfGames;
private String dateArr[];
private int priceArr[];
// Constructor
public Package(String name, String address, String date, String ticketType,
int numberOfGames, String dateArr[], int priceArr[]) {
super(name, address, date, ticketType);
this.numberOfGames = numberOfGames;
this.dateArr = dateArr;
this.priceArr = priceArr;
}
// Getters and Mutators
public int getNumberOfGames() {
return numberOfGames;
}
public void setNumberOfGames(int numberOfGames) {
this.numberOfGames = numberOfGames;
}
public String[] getDateArr() {
return dateArr;
}
public void setDateArr(String[] dateArr) {
this.dateArr = dateArr;
}
public int[] getPriceArr() {
return priceArr;
}
public void setPriceArr(int[] priceArr) {
this.priceArr = priceArr;
}
@Override
public String toString() {
return "Package [numberOfGames=" + numberOfGames + ", dateArr="
+ Arrays.toString(dateArr) + ", priceArr="
+ Arrays.toString(priceArr) + ", getPurchaserName()="
+ getPurchaserName() + ", getPurchaserAddress()="
+ getPurchaserAddress() + ", getPurchaseDate()="
+ getPurchaseDate() + ", getTicketType()=" + getTicketType()
+ "]";
}
@Override
public double getPrice() {
double totalPrice = 0;
for (int i = 0; i < priceArr.length; i++) {
totalPrice += priceArr[i];
}
return totalPrice/priceArr.length;
}
}
**********************"Amount.java"************************
// Amount interface
public interface Amount {
public double getPrice();
}
*************************************************
Sample input given below can be explained as:
First line of input is number of Tickets (tickets).
Then in each subsequent line first we have ticket type:
1. Ticket
2. Season Ticket
3. PerGameTicket
4. Package
Next three Strings in each line are Purchaser name, Address and Date.
Depending on ticket type we have next inputs in each line
Season Ticket:
Total Number of Games and Price of Season
Per Game Ticket:
Price of Ticket and Game Date
Package:
Total number of Games and date of each game and then price of each game
Note: Name of purchaser should be only one word.
**********************"input.txt"*************************
4
1 Ravi Delhi 01/03/2018
2 Mani Mumbai 02/03/2018 2 1000
3 Suri Kolkata 03/03/2018 500 10/03/2018
4 Raj Chennai 04/03/2018 3 12/03/2018 13/03/2018 15/03/2018 500 600 700
*****************************************
Sample output:
Type : TICKET
Ticket [purchaserName=Ravi, purchaserAddress=Delhi, purchaseDate=01/03/2018, ticketType=TICKET]
Type : SEASON_TICKET
SeasonTicket [totalGames=2, seasonPrice=1000, getPurchaserName()=Mani, getPurchaserAddress()=Mumbai, getPurchaseDate()=02/03/2018, getTicketType()=SEASON_TICKET]
Type : PER_GAME_TICKET
PerGameTicket [gameDate=10/03/2018, ticketPrice=500, getPurchaserName()=Suri, getPurchaserAddress()=Kolkata, getPurchaseDate()=03/03/2018, getTicketType()=PER_GAME_TICKET]
Type : PACKAGE_TICKET
Package [numberOfGames=3, dateArr=[12/03/2018, 13/03/2018, 15/03/2018], priceArr=[500, 600, 700], getPurchaserName()=Raj, getPurchaserAddress()=Chennai, getPurchaseDate()=04/03/2018, getTicketType()=PACKAGE_TICKET]
Total Price of all tickets: 2100
Names of Season Ticket Holders : Mani
Total Number of Season Tickets : 1
Total Number of Season Tickets : 1
Total Number of Package Tickets : 1
*******************************************************
Note: Keep all the files in the same folder
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.