0

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.

dxb_hasan
  • 1
  • 2
  • 1
    Possible duplicate of [how to accept input from user in Junit console](http://stackoverflow.com/questions/13212568/how-to-accept-input-from-user-in-junit-console) – DaoWen Oct 05 '16 at 23:02
  • Not sure if your method qualifies for testing, here have a look at this http://stackoverflow.com/questions/1244541/how-to-test-void-method-with-junit-testing-tools question on how to junit test void method – Raf Oct 06 '16 at 00:36

0 Answers0