Class – Procedure In Java Write a project and package that unit tests the Proced
ID: 3756506 • Letter: C
Question
Class – Procedure
In Java Write a project and package that unit tests the Procedure classes. You will have to import the package that has the definitions of the Procedure class.
Store in project/package
Member Variables (all private)
Data Type
Name of the procedure.
Price of the procedure.
Member Method Signatures and Descirptions (all public)
Default Constructor
Default constructor. Sets the values of each member variable to a default value.
Constructor – Two Parameters
Constructor that sets the values of the member variables. There should be a corresponding parameter for each member variable.
Get/set methods for all member variables
void Write(PrintStream ps)
Write the contents of all member variables to the given instance of PrintStream. Assume the PrintStream is already open and ready to use. Data should be written on to separate lines. DO NOT ADD ANY DESCRIPTIVE TEXT IN THE OUTPUT. JUST PRINT THE VALUES.
IMPORTANT - Whatever data is written out should be readable by the Read method of this class. If descriptive text is added then Read will not work.
void Read(Scanner s)
Read the contents of all member variables from the given instance of Scanner. Assume the following:
Scanner is already open.
Member variable values are on separate lines.
This method should return a string using JSON formatting (www.json.org). Here is the format:
{ “variable name” : value, … }
Each variable name should be surrounded by quotes. If the variable data type is String then the value should be surrounded by double quotes. There should be a comma between each <variable name, value> pair (no comma after the last <variable name, value> pair).
Example :
{"name" : "Rabies Shot", "price" : 25.5}
This method should return a String instance (not print on the screen) that contains the values of member variables with descriptive text. Here is an example of a string that gets returned from the method.
For example:
Name: Rabies Shot
Price: $25.50
Class – Main
Store in project/package:
Member Variables (all private)
Data Type
No member variables
Member Method Signatures and Descirptions (all public)
public static void main(String args[])
Write unit testing and other code in main. See below for details.
Automated Test
Create an automated test in main.
There should be unit tests of ALL get/set methods of both classes. GetJSON is NOT included here.
You need to write code that runs the other methods that are not being unit tested on each class (this includes GetJSON, constructors, Read, Write, toString etc…)
Hint: To truly test if the output produced by the Write method of either class works you need to try and read in the file that it class generates with its Read method. For example, assume you write data to a file named “abc.txt” using the write method. You should now use “abc.txt” file as input to the read method. If the program fails on this read there is an error.
Procedure File Format
ProcedureName
ProcedureCost
Procedure Sample Input File (Procedure.txt)
Rabies Shot
25.50
Data Type
Name of the procedure.
Price of the procedure.
Explanation / Answer
package File;
import java.io.*;
import java.util.*;
// Defines a class Procedure to read and write data to file
class Procedure
{
// Instance variables to store data
String name;
double price;
// Default constructor definition
Procedure()
{
name = "";
price = 0.0;
}// End of default constructor
// Parameterized constructor to assign parameter values to instance variable
Procedure(String name, double price)
{
this.name = name;
this.price = price;
}// End of parameterized constructor
// Method to set name
void setName(String na)
{
name = na;
}// End of method
// Method to set price
void setPrice(double pr)
{
price = pr;
}// End of method
// Method to return name
String getName()
{
return name;
}// End of method
// Method to return price
double getPrice()
{
return price;
}// End of method
// Overloads toString() method to return procedure information
public String toString()
{
return " Name: " + getName() + " Price: " + getPrice();
}// End of method
// Method to write procedure data to file
void Write(PrintStream ps)
{
// Writes new line character
ps.println();
// Calls the method getName() to return current object instance variable name
// writes the name to file
ps.println(getName());
// Calls the method getPrice() to return current object instance variable price
// writes the name to file
ps.print(getPrice());
// Close the file
ps.close();
}// End of method
// Method to read file contents and stores it in current object instance variable
void Read(Scanner s)
{
// s.nextLine() to read the name from file
// Calls the method to setName() to assign name to instance variable of current object
setName(s.nextLine());
// s.nextDouble() to read the price from file
// Calls the method to setPrice() to assign price to instance variable of current object
setPrice(s.nextDouble());
}// End of method
}// End of class
// Driver class FileReadWriteProcedure definition
public class FileReadWriteProcedure
{
// Crates a Scanner class object to accept data from console
static Scanner keyboard = new Scanner(System.in);
// Static method to display menu, accept user choice and returns user choice
static int menu()
{
// To store user choice
int ch;
// Displays menu
System.out.print(" 1. Create a Procedure.");
System.out.print(" 2. Read Procedure.");
System.out.print(" 3. Write Procedure.");
System.out.print(" 4. Display Procedure.");
System.out.print(" 5. Exit.");
// Accepts user choice
System.out.print(" Enter your choice: ");
ch = keyboard.nextInt();
// Returns user choice
return ch;
}// End of method
// Static method to accept procedure data and returns Procedure class object
static Procedure acceptData()
{
// Crates a temporary object of Procedure class using default constructor
Procedure temp = new Procedure();
// To skip new line character
keyboard.nextLine();
// Accepts name from the user and assigns it to instance variable name of temp object
// by calling setName() method
System.out.print(" Enter Procedure Name: ");
temp.setName(keyboard.nextLine());
// Accepts price from the user and assigns it to instance variable price of temp object
// by calling setPrice() method
System.out.print(" Enter Procedure Price: ");
temp.setPrice(keyboard.nextDouble());
// Returns the object
return temp;
}// End of method
// main method definition
public static void main(String[] args)
{
// Declares an array of object of class Procedure
Procedure pro[] = new Procedure[100];
// Variable to point to current index position
int counter = 0;
// Scanner class object declared to read file contents
Scanner fileRead = null;
// PrintStream class object declared to write data to file
PrintStream printWrite = null;
// try block begins
try
{
// Opens the file for reading
fileRead = new Scanner(new File("procedure.txt"));
// Opens the file for writing
File fileWrite = new File("procedure.txt");
printWrite = new PrintStream(new BufferedOutputStream(new FileOutputStream(fileWrite, true)));
}// End of try
// Catch block to handle FileNotFoundException
catch (FileNotFoundException e)
{
e.printStackTrace();
System.err.println("Unable to open the file");
}// End of catch
// Loops till user choice is not 4
do
{
// Calls the method menu() to accept user choice
// based on the return value calls the appropriate method
switch(menu())
{
case 1:
// Initializes the object at counter index position by calling default constructor
pro[counter] = new Procedure();
// Calls the method to accept data
// Stores the return object at counter index position of array of object pro
// increase the counter by one
pro[counter++] = acceptData();
break;
case 2:
// Loops till end of the file
while(fileRead.hasNextLine())
{
// Initializes the object at counter index position by calling default constructor
pro[counter] = new Procedure();
// Calls the method to read data from file
// Stores the object at counter index position of array of object pro
// increase the counter by one
pro[counter++].Read(fileRead);
// Checks whether any more data
if(fileRead.hasNextLine())
// Skips the new line character
fileRead.nextLine();
}// End of while loop
break;
case 3:
// Calls the method to write data at counter -1 index position data to file
pro[counter-1].Write(printWrite);
break;
case 4:
// Loops till number of records and displays information using toString() method
for(int x = 0; x < counter; x++)
System.out.println(pro[x]);
break;
case 5:
// Close the program
System.exit(0);
default:
System.out.println("Invalid choice!");
}// End of switch case
}while(true);// End of do while loop
}// End of main method
}// End of driver class
Sample Output:
1. Create a Procedure.
2. Read Procedure.
3. Write Procedure.
4. Display Procedure.
5. Exit.
Enter your choice: 2
1. Create a Procedure.
2. Read Procedure.
3. Write Procedure.
4. Display Procedure.
5. Exit.
Enter your choice: 4
Name: Rabies Shot Price: 25.5
Name: Arnab Sahu Price: 32.52
Name: Binod Raut Price: 77.39
1. Create a Procedure.
2. Read Procedure.
3. Write Procedure.
4. Display Procedure.
5. Exit.
Enter your choice: 1
Enter Procedure Name: Sunit Panda
Enter Procedure Price: 56.45
1. Create a Procedure.
2. Read Procedure.
3. Write Procedure.
4. Display Procedure.
5. Exit.
Enter your choice: 3
1. Create a Procedure.
2. Read Procedure.
3. Write Procedure.
4. Display Procedure.
5. Exit.
Enter your choice: 2
1. Create a Procedure.
2. Read Procedure.
3. Write Procedure.
4. Display Procedure.
5. Exit.
Enter your choice: 4
Name: Rabies Shot Price: 25.5
Name: Arnab Sahu Price: 32.52
Name: Binod Raut Price: 77.39
Name: Sunit Panda Price: 56.45
1. Create a Procedure.
2. Read Procedure.
3. Write Procedure.
4. Display Procedure.
5. Exit.
Enter your choice: 9
Invalid choice!
1. Create a Procedure.
2. Read Procedure.
3. Write Procedure.
4. Display Procedure.
5. Exit.
Enter your choice: 5
procedure.txt file contents
Rabies Shot
25.50
Arnab Sahu
32.52
Binod Raut
77.39
Sunit Panda
56.45