I have been provided a piece of code and need to write JUnit Test Case for it. I am stuck regarding the loops and user input.
import java.util.Scanner;
import java.text.*;
public class hotelOccupancy
{
public void calcRate()
{
Scanner scan = new Scanner(System.in);
DecimalFormat fmt = new DecimalFormat();
int occupied = 0
int totalOccupied = 0
// Get and validate occupancy information for each floor
System.out.println("Enter the number of occupied suites on each of the following floors.\n");
for (int floor = 1; floor <= 10; floor++)
{
System.out.println("\nFloor " + floor+ ": ");
occupied=scan.nextInt();
while (occupied < 0 || occupied > 120)
{
System.out.println("\nThe number of occupied suites must be between 0 and " + 120 );
System.out.println("\n Re-enter the number of occupied suites on floor " + floor + ": ");
occupied = scan.nextInt();
}
// Add occupied suites on this floor to the total
totalOccupied += occupied;
}
// Display results
System.out.println("\n\nThe hotel has a total of " + 120 + " suites.\n");
System.out.println(totalOccupied+ " are currently occupied.\n");
System.out.println("This is an occupancy rate of " + fmt.format(occupancyRate)+ "% \n");
} }
How would I write a JUnit test case that can test for user input and multiple loops? Preferable if I don't have to make changes to the source code.
Thank you.