Classes needed for completion: Assignment8.java(More code need to be added) Proj
ID: 3678232 • Letter: C
Question
Classes needed for completion:
Assignment8.java(More code need to be added)
Project.java(given by the instructor, it needs to be modified for this assignment)
Budget.java(given by the instructor, it needs to be modified for this assignment)
ProjNumberComparator.java
ProjNameComparator.java
Sorts.java
ProjectManagement.java
------------------------------------------------------------------------------------------------------------
Assignment8.java:
------------------------------------------------------------------------------------------------------------
Project.java:
------------------------------------------------------------------------------------------------------------
Budget.java:
------------------------------------------------------------------------------------------------------------
ProjNumberComparator.java:
The ProjNumberComparator class implements the "Comparator" interface (The Comparator interface is in "java.util" package.). It needs to define the following method that was an inherited abstract method from Comparator interface:
public int compare(Object first, Object second)
(Note that you can also define:
public int compare(Project first, Project second)
instead by making the class implements Comparator.
If the first argument object has a smaller projNumber than that of the second argument, an int less than zero is returned. If the first argument object has a larger projNumber than that of the second argument, an int greater than zero is returned. If the both are same, 0 should be returned.
The Budget class also implements the "Serializable" interface so that its object can be stored.
------------------------------------------------------------------------------------------------------------
ProjNameComparator.java:
The ProjNameComparator class implements the "Comparator" interface (The Comparator interface is in "java.util" package.). It needs to define the following method that was an inherited abstract method from Comparator interface:
public int compare(Object first, Object second)
(Note that you can also define:
public int compare(Project first, Project second)
instead by making the class implements Comparator.
If the first argument object has a project name lexicographically less than that of the second argument, an int less than zero is returned. If the first argument object has a project name lexicographically larger than that of the second argument, an int greater than zero is returned. If the both project names are same, their locations should be compared. If they have same project name and location, then 0 should be returned.
------------------------------------------------------------------------------------------------------------
Sorts.java:
The Sorts class is a utility class that will be used to sort a list of Project objects. Sorting algorithms are described in the algorithm note posted under Notes section of the course web site. These algorithms sort numbers stored in an array. It is your job to modify it to sort objects stored in an array list (or a vector).
The Sorts class object will never be instantiated. It must have the following methods:
public static void sort(ArrayList objects, Comparator)
Your sort method utilizes the compare method of the parameter Comparator object to sort. You can use one of Selection sort or Insertion Sort.
------------------------------------------------------------------------------------------------------------
ProjectManagement.java:
The ProjectManagement class has a list of Project objects that can be organized at the project management system. The project management system will be a fully encapsulated object. The ProjectManagement class implements the Serializable interface.
It has the following attributes:
A list of Projects in the object management system
The following public methods should be provided to interact with the project management system:
No input/output should occur in the project management system. User interaction should be handled only by the driver class.
You may add other methods to the class in order to make your life easier.
Attribute name Attribute type Description projectList ArrayList or VectorA list of Projects in the object management system
Explanation / Answer
Answer:
Note:Code is modified as per user requirements
import java.io.*;
public class Assignment8
{
public static void main (String[] args)
{
char input1;
String name, location, projNumStr, budgetStr;
int projNumber;
double budget;
boolean operation = false;
int operation2 = 0;
String line;
String filename;
// create a ProjectManagement object. This is used throughout this class.
ProjectManagement manage1 = new ProjectManagement();
try
{
// print out the menu
printMenu();
// create a BufferedReader object to read input from a keyboard
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (isr);
do
{
System.out.print("What action would you like to perform? ");
line = stdin.readLine().trim(); //read a line
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1) //check if a user entered only one character
{
switch (input1)
{
case 'A': //Add Project
try
{
System.out.print("Please enter a project name to add: ");
name = stdin.readLine().trim();
System.out.print("Please enter its projNumber to add: ");
projNumStr = stdin.readLine().trim();
projNumber = Integer.parseInt(projNumStr);
System.out.print("Please enter its location to add: ");
location = stdin.readLine().trim();
System.out.print("Please enter its initial budget to add: ");
budgetStr = stdin.readLine().trim();
budget = Double.parseDouble(budgetStr);
operation = manage1.addProject(name, location, projNumber, budget);
if (operation == true)
System.out.print("project added ");
else
System.out.print("project exists ");
}
catch(Exception e )
{
e.printStackTrace();
}
break;
case 'D': //Search by projNumber
try
{
System.out.print("Please enter projNumber to search: ");
projNumStr = stdin.readLine().trim();
projNumber = Integer.parseInt(projNumStr);
operation2=manage1.projNumberExists(projNumber);
if (operation2 > -1)
System.out.print("projNumber found ");
else
System.out.print("projNumber not found ");
}
catch( InputMismatchException ie )
{
ie.printStackTrace();
}
break;
case 'E': //Search by name and location
System.out.print("Please enter a name to search: ");
name = stdin.readLine().trim();
System.out.print("Please enter a location to search: ");
location = stdin.readLine().trim();
operation2=manage1.nameLocationExists(name, location);
if (operation2 > -1)
System.out.print("project name and location found ");
else
System.out.print("project name and location not found ");
break;
case 'L': //List projects
System.out.print(manage1.listProjects());
break;
case 'O': // Sort by projNumber
manage1.sortByProjNumber();
System.out.print("sorted by projNumber ");
break;
case 'P': // Sort by locations and project names
manage1.sortByNameLocation();
System.out.print("sorted by project names and locations ");
break;
case 'Q': //Quit
break;
case 'R': //Remove by projNumber
try
{
System.out.print("Please enter projNumber to remove: ");
projNumStr = stdin.readLine().trim();
projNumber = Integer.parseInt(projNumStr);
operation=manage1.removeProjNumber(projNumber);
if (operation == true)
System.out.print("projNumber removed ");
else
System.out.print("projNumber not found ");
}
catch(InputMismatchException ie )
{
ie.printStackTrace();
}
break;
case 'S': //Remove by location and name
System.out.print("Please enter a name to remove: ");
name = stdin.readLine().trim();
System.out.print("Please enter a location to remove: ");
location = stdin.readLine().trim();
operation=manage1.removeNameLocation(name, location);
if (operation == true)
System.out.print("project name and location removed ");
else
System.out.print("project name and location not found ");
break;
case 'T': //Close ProjectManagement
manage1.closeProjectManagement();
System.out.print("project management system closed ");
break;
case 'U': //Write Text to a File
System.out.print("Please enter a file name to write: ");
filename = stdin.readLine().trim();
System.out.println("Enter text");
String text=stdin.readLine();
BufferedWriter bwr;
try
{
bwr=new BufferedWriter(new FileWriter(new File(filename)));
bwr.write(text);
bwr.close();
}
catch(FileNotFoundException fe)
{
fe.printStackTrace();
bwr.close();
}
break;
case 'V': //Read Text from a File
System.out.print("Please enter a file name to read: ");
filename = stdin.readLine().trim();
BufferedReader bwr;
try
{
bwr=new BufferedReader(new FileReader(new File(filename)));
String text=bwr.readLine();
System.out.print("String read from file:"+text);
bwr.close();
}
catch(FileNotFoundException fe)
{
fe.printStackTrace();
bwr.close();
}
break;
case 'W': //Serialize ProjectManagement to a File
System.out.print("Please enter a file name to write: ");
filename = stdin.readLine().trim();
String text="";
BufferedWriter bwr;
try
{
bwr=new BufferedWriter(new FileWriter(new File(filename)));
text=manage1.listProjects();
bwr.write(text);
bwr.close();
}
catch(FileNotFoundException fe)
{
fe.printStackTrace();
bwr.close();
}
break;
case 'X': //Deserialize ProjectManagement from a File
System.out.print("Please enter a file name to read: ");
filename = stdin.readLine().trim();
BufferedReader bwr;
try
{
bwr=new BufferedReader(new FileReader(new File(filename)));
String text;
while((text=bwr.readLine())!=-1)
System.out.println(text);
bwr.close();
}
catch(FileNotFoundException fe)
{
fe.printStackTrace();
bwr.close();
}
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action ");
break;
}
}
else
{
System.out.print("Unknown action ");
}
} while (input1 != 'Q' || line.length() != 1);
}
catch (IOException exception)
{
System.out.print("IO Exception ");
}
}
/** The method printMenu displays the menu to a user **/
public static void printMenu()
{
System.out.print("Choice Action " +
"------ ------ " +
"A Add Project " +
"D Search for ProjNumber " +
"E Search for Name and Location " +
"L List Projects " +
"O Sort by ProjNumber " +
"P Sort by Name and Location " +
"Q Quit " +
"R Remove by ProjNumber " +
"S Remove by Name and Location " +
"T Close ProjectManagement " +
"U Write Text to File " +
"V Read Text from File " +
"W Serialize ProjectManagement to File " +
"X Deserialize ProjectManagement from File " +
"? Display Help ");
}
} // end of Assignment8 class
//Project.java:
public class Project implements Serializable
{
private String projName;
private int projNumber;
private String projLocation;
private Budget projBudget;
//Constructor to initialize all member variables
public Project(double funding)
{
projName = "?";
projNumber = 0;
projLocation = "?";
projBudget = new Budget(funding);
}
//Accessor methods
public String getName()
{
return projName;
}
public int getNumber()
{
return projNumber;
}
public String getLocation()
{
return projLocation;
}
public Budget getBudget()
{
return projBudget;
}
//Mutator methods
public void setName(String aName)
{
projName = aName;
}
public void setNumber(int aNumber)
{
projNumber = aNumber;
}
public void setLocation(String aLocation)
{
projLocation = aLocation;
}
//Add a new expenditure to the budget
public boolean addExpenditure(double amount)
{
boolean success = projBudget.addSpending(amount);
return success;
}
//toString() method returns a string containg its name, number, location and budget
public String toString()
{
String result = " Project Name: " + projName
+ " Project Number: " + projNumber
+ " Project Location: " + projLocation
+ " "
+ projBudget.toString() + " ";
return result;
}
//Budget.java:
import java.text.NumberFormat;
public class Budget implements Serializable
{
private double initialFunding;
private double spending;
private double currentBalance;
//Constructor to initialize all member variables
public Budget(double funding)
{
initialFunding = funding;
spending = 0.0;
currentBalance = initialFunding - spending;
}
//add some additional spending
public boolean addSpending(double additionalSpending)
{
if (additionalSpending > 0 && additionalSpending <= currentBalance)
{
spending += additionalSpending;
currentBalance = initialFunding - spending;
return true;
}
else
return false;
}
//toString() method returns a string containg its initial funding
//spending and current balance
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String result = "Budget:"
+ " Initial Funding " + fmt.format(initialFunding)
+ " Spending " + fmt.format(spending)
+ " Current Balance " + fmt.format(currentBalance);
return result;
}
}
//ProjNumberComparator.java
public class ProjNumberComparator implements Comparator
{
public int compare(Object first, Object second)
{
Project p1=(Project)first;
Project p2=(Project) second;
if(p1.getNumber()<p2.getNumber())
return -1;
else if(p1.getNumber()>p2.getNumber())
return 1;
else if(p1.getNumber()==p2.getNumber())
return 0;
}
}
//ProjNameComparator.java
public class ProjNameComparator implements Comparator
{
public int compare(Object first, Object second)
{
Project p1=(Project)first;
Project p2=(Project) second;
if(p1.getName()==null||p2.getName()==null)
return -1;
return p1.getName().compareTo(p2.getName);
}
}
//Sorts.java
public class Sorts
{
public static void sort(ArrayList<Project> objects, Comparator c)
{
for(int k1=1;k1<objects.size();k1++)
{
int k2=k1;
while((k2>0)&&(c.compare(objects.get(k2-1),objects.get(k1))>1)
{
objects.set(k2,ojects.get(k2-1));
k2--;
}
objects.set(k2,ojects.get(k1));
}
}
}
//ProjectManagement.java
public class ProjectManagement implements Serializable
{
ArrayList<Project> projectList;
public ProjectManagement()
{
projectList=new ArrayList<Project>();
}
public int projNumberExists(int projNumber)
{
for(int k1=0;k1<projectList.size();k1++)
{
if(projectList.get(k1).getNumber()==projNumber)
{
return k1;
}
}
return -1;
}
public int nameLocationExists(String name, String location)
{
for(int k1=0;k1<projectList.size();k1++)
{
if(projectList.get(k1).getName().equals(name)&&projectList.get(k1).getLocation().equlas(location))
{
return k1;
}
}
return -1;
}
public boolean addProject(String name, String location, int projNumber, double initialFund)
{
Project newProject=new Project(name, location, projNumber, initialFund);
int projNo=newProject.getNumber();
int t=projNumberExists(projNo);
if(t==-1)
{
projectList.add(newProject);
return true;
}
return false;
}
public boolean removeProjNumber(int projNumber)
{
int t=projNumberExists(projNo);
if(t!=-1)
{
projectList.remove(t);
return true;
}
return false;
}
public boolean removeNameLocation(String name, String location)
{
int t=nameLocationExists(name, location);
if(t!=-1)
{
projectList.remove(t);
return true;
}
return false;
}
public void sortByProjNumber()
{
Sorts.sort(projectList, new ProjNumberComparator());
}
public void void sortByNameLocation()
{
Sorts.sort(projectList, new ProjNameComparator());
}
public String listProjects()
{
String projectIn="";
for(int k1=0;k1<projectList.size();k1++)
{
projectIn+=projectList.get(k1).toString();
}
return projectIn;
}
public void closeProjectManagement()
{
projectList.clear();
}
}