Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is a class for which I want to write JUnit test cases. Can anyone help me in writing test cases for the class? The

Below is a class for which I want to write JUnit test cases. Can anyone help me in writing test cases for the class?

The application is about the online parking garage. There are many classes. This is one of them which is used for Transaction after the reservation is done.

I am also pasting the reservation class. But I want test cases only for Transaction class

I am new with Junit, kindly help.

package comp.design.parkingsystem;

import java.math.BigInteger;

import java.sql.Date;

import java.sql.SQLException;

import java.util.Calendar;

public class Transaction {

BigInteger id;

public Date time;

public Money amount;

public double tax;

public BigInteger accountNumber, routingNumber;

public Money total() {

return new Money();

}

private static final float RATE = 4; // Dollars per hour

private static final float MAX_DAY = 30;

// Purpose: Takes two calendar objects and returns the difference in time

public static float getTimeDifference(Calendar calFirst, Calendar calSecond) {

if (calFirst.after(calSecond)) {

return 0;

}

float time_in_milli = calFirst.getTimeInMillis();

float time_out_milli = calSecond.getTimeInMillis();

float time_total_milli = time_out_milli - time_in_milli;

float time_total_hour = time_total_milli / 1000 / 60 / 60; // convert milliseconds to hour

return time_total_hour;

}

public static float getPrice(Reservation r) {

Calendar reservedTimeIn = r.reservationInformation().get(0);

Calendar reservedTimeOut = r.reservationInformation().get(1);

Calendar actualTimeIn = r.reservationInformation().get(2);

Calendar actualTimeOut = r.reservationInformation().get(3);

float earlyTime = getTimeDifference(actualTimeIn, reservedTimeIn);

float reservedTime = getTimeDifference(reservedTimeIn, reservedTimeOut);

float extraTime = getTimeDifference(reservedTimeOut, actualTimeOut);

float totalCost = (RATE * earlyTime) + (RATE * reservedTime);

if (totalCost > MAX_DAY) {

totalCost = MAX_DAY;

}

return totalCost;

}

public static float chargeCustomer(Reservation r) throws ClassNotFoundException, SQLException {

// Calculate price

double price = Transaction.getPrice(r);

// update Database

/* update Reservation Object

/*

* update Reservation Object update Reservation Database

*/

return (float) price;

}

}

Reservation Class

package comp.design.parkingsystem;

import java.math.BigInteger;

import java.sql.Date;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.Calendar;

public class Reservation implements Comparable {

public BigInteger id;

public Date arrival, departure;

public Driver driver;

public Transaction transaction;

private int reservationID;

private int customerID;

private int parkingId;

private Calendar reservationTimeIn;

private Calendar reservationTimeOut;

private Calendar actualTimeIn;

private Calendar actualTimeOut;

/**

*

* @param resId

* @param custId

* @param spotId

* @param in

* @param out

* @param ain

* @param aout

*/

public Reservation(int resId, int custId, int spotId, Calendar in,

Calendar out, Calendar ain, Calendar aout) {

}

public int compareTo(Reservation reserve) {

if (reservationTimeIn.before(reserve.reservationTimeIn)){

return -1;

}

else if (reservationTimeIn.after(reserve.reservationTimeIn)){

return 1;

}

else{

return 0;

}

}

/**

* Make a reservation

* Output: Reservation Object if successful

* no parking spots: -1 for reservationID

* too many reservation: -2 for reservationID

* else make reservation

* @throws ClassNotFoundException

* @throws SQLException

*/

public void reserveParking(int parkingId, int resID, Calendar start, Calendar finish)

throws ClassNotFoundException, SQLException {

}

/*

* When a driver has confirmed the reservation, this function is called

*/

public void confirmArrival(Calendar in) {}

/*

* Call when car is leaving

*/

public void confirmDeparture() {}

/*purpose: returns ArrayList of Calendar objects:

* 1 reservationTimeIn;

* 2 reservationTimeOut

* 3 actualTimeIn

* 4 actualTimeOut

* */

public ArrayList reservationInformation(){

ArrayList output = new ArrayList();

output.add(reservationTimeIn);

output.add(reservationTimeOut);

output.add(actualTimeIn);

output.add(actualTimeOut);

return output;

}

/*

* Helper Functions

*/

public int getReservationID(){

return reservationID;

}

public int getCustomerID(){

return customerID;

}

public int getParkingId(){

return parkingId;

}

public void setActualTimeIn(Calendar c){

actualTimeIn = c;

}

public void setActualTimeOut(Calendar c){

actualTimeOut = c;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions

Question

How do we do subnetting in IPv6?Explain with a suitable example.

Answered: 1 week ago

Question

Explain the guideline for job description.

Answered: 1 week ago

Question

What is job description ? State the uses of job description.

Answered: 1 week ago

Question

What are the objectives of job evaluation ?

Answered: 1 week ago

Question

Write a note on job design.

Answered: 1 week ago

Question

2. How will the team select a leader?

Answered: 1 week ago