Fill in the blanks in the Java code below as per UML class diagram and description below:
UML Diagram
Class:
- LocalTour
Fields:
- numberOfTourist : int
- destination : String[]
- destinationCode : int
Methods:
- Tour()
- getInformation(Scanner scan) : void
- retrieveDestination() : String
- totalPrice() : double
- finalPrice() : double
- display() : String
The UML class diagram is based on LocalTour class.
A multi-location Tour company conducts sightseeing one day trips for groups from its home base.
For booking, their application accepts tour data, that includes:
- a numeric code that represents the destination
- The number of travelers taking the tour.
As data is entered for each tour, verify that destination code is valid. Continue to prompt the user until valid code is entered. Below are the valid code and an associated destination:
Code | Destination |
1 | Chicago |
2 | Miami |
3 | Hawaii |
Ask for number of travelers after valid code is entered. Your program must be able to retrieve destination name using code. The company also offers discount on total price based on how many travelers are in a group. Discounts are calculated as:
Number of Tourists | Discount per group |
1-2 | 0 |
3-6 | 100 |
7-12 | 200 |
13-20 | 400 |
21-50 | 500 |
Once processed, print destination code, destination name, number of travelers, gross total price for the tour and final price for the tour after discount.
Below are two sample output screen shots:
a) This one is for no discounts
Enter the numeric code for the destination: 5
Enter the numeric code for the destination: -1
Enter the numeric code for the destination: 2
Enter the number of travelers: 2
Destination name: Miami
Destination code: 2
Number of Travelers: 2
Gross total price for the tour: $1000.00
Final price for the tour after discount: $1000.00
b) This one is with discount
Enter the numeric code for the destination: 1
Enter the number of travelers: 10
Destination name: Chicago
Destination code: 1
Number of Travelers: 10
Gross total price for the tour: $5000.00
Final price for the tour after discount: $4800.00
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Amrit
*/
public class LocalTour {
//attributes or fields
BLANK 1;
BLANK 2;
BLANK 3;
//For simplicity, assume that price per person for every destination is a constant.
//set value of the constant to 500
BLANK 4;
public LocalTour() {
//store three destinations in order inside the array
BLANK 5;
}
public void getInformation(Scanner scan) {
/*write while or do-while loop to prompt for numeric code. Valid codes are 1,2 and 3. Initialize the field
named destination code with entered number.
if code is valid, prompt for number of travelers and save the input in corresponding field
do not exit loop until a valid code is not entered
*/
BLANK 6;
BLANK 7
BLANK 8
BLANK 9
BLANK 10
BLANK 11
BLANK 12
BLANK 13
}
public Blank 14 retrieveDestination() {
//retrieve destination name using code
String myDestination = “”;
for (int i = 0; i < destinations.length; i++) {
//look into the array for a match, if found save destination name into returned variable.
Blank 15
Blank 16;
Blank 17
Blank 18
return myDestination;
}
//calculate and return total price of the trip
public double totalPrice() {
Blank 19;
}
//calculate and return final price after discount
public double finalPrice() {
double discount = 0;
if (Blank 20) {
discount = 100;
} else if (Blank 21) {
discount = 200;
} else if (Blank 22) {
discount = 400;
} else if (Blank 23) {
discount = 500;
}
return Blank 24;
}
//display output text
public Blank 25 display() {
String myOutput = “Destination name: ” + Blank 26;
myOutput += “\nDestination code: ” + Blank 27;
Blank 28 += “\nNumber of Travelers: ” + numberOfTourist;
myOutput += “\n” + String.format(“Gross total price for the tour: $%.2f”, Blank 29);
myOutput += “\n” + String.format(“Final price for the tour after discount: $%.2f”, Blank 30);
Blank 31;
}
}
public class LocalTourTester {
public static void main(String args) {
//Declare a reference variable of LocalTour class
Blank 32;
tour = new LocalTour();
Scanner scan = new Scanner(System.in);
//call methods from LocalTour class to show desired results
Blank 33;
Blank 34;
}
}
ANSWERS Below:
- BLANK-1 is private int numberOfTourist
- BLANK-2 are private String<, >, and destinations
- BLANK-3 is private int destinationCode
- BLANK-4 is private double PRICE_PER_PERSON = 500
- BLANK-5 are destinations = new String<, >, and {“Chicago”, “Miami”, “Hawaii”}
- BLANK-6 is do {
- BLANK-7 is System.out.print(“Enter the numeric code for the destination: “)
- BLANK-8 is destinationCode = scan.nextInt()
- BLANK-9 are if (destinationCode >, 0 &, &, destinationCode <, and 4) {
- BLANK-10 is System.out.print(“Enter the number of travelers: “)
- BLANK-11 is numberOfTourist = scan.nextInt()
- BLANK-12 is }
- BLANK-13 are } while (destinationCode <, 1 || destinationCode >, and 3)
- BLANK-14 is String
- BLANK-15 is if (destinationCode – 1 == i) {
- BLANK-16 are myDestination = destinations< and i>
- BLANK-17 is }
- BLANK-18 is }
- BLANK-19 is return PRICE_PER_PERSON * numberOfTourist
- BLANK-20 are numberOfTourist >, = 3 &, &, numberOfTourist <, and = 6
- BLANK-21 are numberOfTourist >, = 7 &, &, numberOfTourist <, and = 12
- BLANK-22 are numberOfTourist >, = 13 &, &, numberOfTourist <, and = 20
- BLANK-23 are numberOfTourist >, = 21 &, &, numberOfTourist <, and = 50
- BLANK-24 is totalPrice() – discount
- BLANK-25 is String
- BLANK-26 is retrieveDestination()
- BLANK-27 is destinationCode
- BLANK-28 is myOutput
- BLANK-29 is totalPrice()
- BLANK-30 is finalPrice()
- BLANK-31 is return myOutput
- BLANK-32 is LocalTour tour
- BLANK-33 is tour.getInformation(scan)
- BLANK-34 is System.out.println(tour.display())