To Do in Java: VACATION AND VACATIONDRIVER In this exercise, you will write a Ja
ID: 3908215 • Letter: T
Question
To Do in Java: VACATION AND VACATIONDRIVER
In this exercise, you will write a Java class for keeping track of sales for a company that sells vacation packages. Write the Vacation class with a vacation object being described by three instance variables: vacationName (a String), numSold (an integer that represents the number of those vacation packages sold), and priceEach (a double that is the price of the vacation package). I only want three instance variables!! The class should have the following methods:
A constructor that has two parameter – a String containing the name of the vacation and a double containing its price. The constructor should set the other instance variable to zero.
An empty constructor
All getters and setters
A method updateSales() that takes a single integer parameter representing the number of additional vacations of that type sold. The method should add this number to the numSold instance variable and return nothing
A method totalValue() that takes no parameters and returns the total value of that vacation inventory
A toString() method that returns a string containing the name of the vacation and the number sold, the price each, and the total value
Write a VacationDriver program that uses Vacation objects to track the sales.
Ask the user for the number of different types of vacations and how many sales people are selling them.
Using a nested for loop, get the information for the vacations (see outline below). After each prompt, call the updateSales() method to update the number of vacations sold.
After all the data has been read in for a given type of vacation, call the toString() method for this to print out their information. Then loop to get the next vacation.
Here is an outline for you
public static void main(String[] args)
{
// ask for the number of vacations and read it into numVacations
// ask for the number of sales people and read it into numPeople
// beginning of loop for number of vacations
for(int i = 0; i < numVacations; i++)
{
//ask for the name and price and read these into variables
// create a Vacation instance using the data obtained above
for(int j = 0; j < numPeople; j++) // loop through the sales people {
// ask for the sales for the current vacation and read it in
// call updateSales with this number
} //end of inner loop
//print out this vacation info
} //end of outer for loop
} //end of main
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
Vacation.java
------
public class Vacation {
private String vacationName;
private int numSold;
private double priceEach;
public Vacation(){
vacationName = "";
numSold = 0;
priceEach = 0;
}
public Vacation(String name1, double price1){
vacationName = name1;
numSold = 0;
priceEach = price1;
}
public String getVacationName() {
return vacationName;
}
public void setVacationName(String vacationName) {
this.vacationName = vacationName;
}
public int getNumSold() {
return numSold;
}
public void setNumSold(int numSold) {
this.numSold = numSold;
}
public double getPriceEach() {
return priceEach;
}
public void setPriceEach(double priceEach) {
this.priceEach = priceEach;
}
public double totalValue(){
return numSold * priceEach;
}
public void updateSales(int sold){
numSold += sold;
}
public String toString(){
return "Vacation name: " + vacationName + ", sold: " + numSold + ", price: " + priceEach + ", total: " + totalValue();
}
}
VacationDriver.java
------
import java.util.Scanner;
public class VacationDriver {
public static void main(String[] args)
{
int numVacations, numPeople;
Scanner keyboard = new Scanner(System.in);
String name;
double price;
int sold;
Vacation vac;
// ask for the number of vacations and read it into numVacations
System.out.println("Enter the number of vacations: ");
numVacations = keyboard.nextInt();
// ask for the number of sales people and read it into numPeople
System.out.println("Enter the number of sales people: ");
numPeople = keyboard.nextInt();
// beginning of loop for number of vacations
for(int i = 0; i < numVacations; i++)
{
keyboard.nextLine() ;//get rid of newline
//ask for the name and price and read these into variables
System.out.print("Enter vacation name: ");
name = keyboard.nextLine();
System.out.print("Enter vacation price: ");
price = keyboard.nextDouble();
// create a Vacation instance using the data obtained above
vac = new Vacation(name, price);
for(int j = 0; j < numPeople; j++) // loop through the sales people {
{
// ask for the sales for the current vacation and read it in
System.out.print("How many sold by salesperson " + (j+1) +" ? ");
sold = keyboard.nextInt();
// call updateSales with this number
vac.updateSales(sold);
} //end of inner loop
//print out this vacation info
System.out.println(vac);
} //end of outer for loop
} //end of main
}
output
-----
Enter the number of vacations:
2
Enter the number of sales people:
3
Enter vacation name: The Holidays..
Enter vacation price: 999
How many sold by salesperson 1 ? 2
How many sold by salesperson 2 ? 1
How many sold by salesperson 3 ? 4
Vacation name: The Holidays.., sold: 7, price: 999.0, total: 6993.0
Enter vacation name: Vacation2
Enter vacation price: 123
How many sold by salesperson 1 ? 1
How many sold by salesperson 2 ? 3
How many sold by salesperson 3 ? 5
Vacation name: Vacation2, sold: 9, price: 123.0, total: 1107.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.