Assignment8.java(More code need to be added) Project.java(given by the instructo
ID: 3680521 • Letter: A
Question
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.
Attribute name Attribute type Description projectList ArrayList or VectorA list of Projects in the object management system
Explanation / Answer
Assignment8.java:
import java.io.*;
import java.lang.*;
import java.util.*;
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;
ProjectManagement manage1 = new ProjectManagement();
try
{
printMenu();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.print("What action would you like to perform? ");
line = br.readLine().trim();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1)
{
switch (input1)
{
case 'A':
try
{
System.out.print("Please enter a project name to add: ");
name = br.readLine().trim();
System.out.print("Please enter its projNumber to add: ");
projNumStr = br.readLine().trim();
projNumber = Integer.parseInt(projNumStr);
System.out.print("Please enter its location to add: ");
location = br.readLine().trim();
System.out.print("Please enter its initial budget to add: ");
budgetStr = br.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( IOException i)
{
i.printStackTrace();
}
break;
case 'D':
try
{
System.out.print("Please enter projNumber to search: ");
projNumStr = br.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(IndexOutOfBound e )
{
e.printStackTrace();
}
break;
case 'E':
System.out.print("Please enter a name to search: ");
name = br.readLine().trim();
System.out.print("Please enter a location to search: ");
location = br.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':
System.out.print(manage1.listProjects());
break;
case 'O':
manage1.sortByProjNumber();
System.out.print("sorted by projNumber ");
break;
case 'P':
manage1.sortByNameLocation();
System.out.print("sorted by project names and locations ");
break;
case 'Q':
break;
case 'R':
try
{
System.out.print("Please enter projNumber to remove: ");
projNumStr = br.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(ClassCastException c )
{
c.printStackTrace();
}
break;
case 'S':
System.out.print("Please enter a name to remove: ");
name = br.readLine().trim();
System.out.print("Please enter a location to remove: ");
location = br.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':
manage1.closeProjectManagement();
System.out.print("project management system closed ");
break;
case 'U'
System.out.print("Please enter a file name to write: ");
filename = br.readLine().trim();
FileWriter fw = new FileWriter(filename);
BufferedWriter bw = new BufferedWriter(fw);
String data;
System.out.println(“Enter String:”);
data = br.readLine();
bw.write(data);
bw.close();
break;
case 'V':
System.out.print("Please enter a file name to read: ");
filename = br.readLine().trim();
FileReader fr = new FileReader(filename);
BufferedReader reader = new BufferedReader(fr);
StringBuffer s = new StringBuffer();
int num;
char a[] = new char[1024];
while(num == fr.read(a)) >0) {
s.append(a,0,num); }
fr.close();
System.out.println(s.toString());
break;
case 'W':
System.out.print("Please enter a file name to write: ");
filename = br.readLine().trim();
FileWriter fw = new FileWriter(filename);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(manage1);
bw.close();
break;
case 'X':
System.out.print("Please enter a file name to read: ");
filename = br.readLine().trim();
FileReader fr = new FileReader(filename);
BufferedReader reader = new BufferedReader(fr);
StringBuffer s = new StringBuffer();
s.append(manage1);
fr.close();
System.out.println(s.toString());
break;
case '?':
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 ");
}
}
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 ");
}
}
Project.java:
import java.io.*;
import java.lang.*;
import java.util.*;
public class Project
{
public static void main(String args[]) throws Exceptions {
private String projName;
private int projNumber;
private String projLocation;
private Budget projBudget;
BufferedReader br = new BufferedReader (newI(nputStreamReader(System.in));
System.out.println(“Enter Project name”);
projName = br.readLine();
System.out.println(“Enter project number”);
projNumber = br.readLine();
System.out.println(“Enter project Location”);
projLocation = br.readLine();
System.out.println(“Enter project budget”);
projBudget = br.readLine();
funding = Integer.parseInt(projBudget);
public Project(double funding)
{
projName = "?";
projNumber = 0;
projLocation = "?";
projBudget = new Budget(funding);
}
public String getName()
{
return this.projName;
}
public int getNumber()
{
return this.projNumber;
}
public String getLocation()
{
return this.projLocation;
}
public Budget getBudget()
{
return projBudget;
}
public void setName(String projName)
{
this.projName =projName;
}
public void setNumber(int projNumber)
{
this.projNumber = projNumber;
}
public void setLocation(String projLocation)
{
this.projLocation = projLocation;
}
public boolean addExpenditure(double amount)
{
boolean success = projBudget.addSpending(amount);
return success;
}
public String toString()
{
String result = " Project Name: " + this.projName
+ " Project Number: " + this.projNumber
+ " Project Location: " + this.projLocation
+ " "
+ projBudget.toString() + " ";
return result;
}
}
}
Budget.java:
import java.text.NumberFormat;
public class Budget
{
public static void main (String args[]) throws Exception
private double initialFunding;
private double spending;
private double currentBalance;
public Budget(double funding)
{
initialFunding = funding;
spending = 0.0;
currentBalance = initialFunding - spending;
}
public boolean addSpending(double as)
{
if (as > 0 && as <= currentBalance)
{
spending += as;
currentBalance = initialFunding - spending;
return true;
}
else
return false;
}
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.