Assignment8.java(More code need to be added) Project.java(given by the instructo
ID: 3680522 • 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
package com.home.chegg1;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
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 exception) {
System.out.println("Exception occured : " + exception);
}
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 (Exception exception) {
System.out.println("Exception occured : " + exception);
}
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 (Exception exception) {
System.out.println("Exception occured : " + exception);
}
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();
try {
File file = new File(filename);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(manage1.listProjects());
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
case 'V': // Read Text from a File
System.out.print("Please enter a file name to read: ");
filename = stdin.readLine().trim();
/************************************************************************************
*** ADD your code to read a text (string) from the
* specified file. Catch exceptions.
************************************************************************************/
break;
case 'W': // Serialize ProjectManagement to a File
System.out.print("Please enter a file name to write: ");
filename = stdin.readLine().trim();
try {
File yourFile = new File(filename);
if (!yourFile.exists()) {
yourFile.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(yourFile);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(manage1);
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException :>>" + e);
} catch (IOException e) {
System.out.println("IOException :>>" + e);
}
break;
case 'X': // Deserialize ProjectManagement from a File
System.out.print("Please enter a file name to read: ");
filename = stdin.readLine().trim();
Object object = null;
try {
FileInputStream fileInputStream = new FileInputStream(new File(filename));
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
object = objectInputStream.readObject();
System.out.println(object);
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException :>>" + e.getStackTrace());
} catch (IOException e) {
System.out.println("IOException :>>" + e.getStackTrace());
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException :>>" + e.getStackTrace());
}
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 ");
}
}
package com.home.chegg1;
import java.io.Serializable;
import java.text.NumberFormat;
public class Budget implements Serializable {
private static final long serialVersionUID = 1L;
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;
}
}
package com.home.chegg1;
import java.io.Serializable;
public class Project implements Serializable{
private static final long serialVersionUID = 1L;
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;
}
}
package com.home.chegg1;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class ProjectManagement implements Serializable {
private static final long serialVersionUID = 1L;
List<Project> list;
public ProjectManagement() {
this.list = new ArrayList<Project>();
}
int projNumberExists(int projNumber) {
if (this.list != null) {
for (int i = 0; i < list.size(); i++) {
Project project = list.get(i);
if (project.getNumber() == projNumber) {
return i;
}
}
}
return -1;
}
int nameLocationExists(String name, String location) {
if (this.list != null) {
for (int i = 0; i < list.size(); i++) {
Project project = list.get(i);
if (project.getName().endsWith(name) && project.getLocation().equalsIgnoreCase(location)) {
return i;
}
}
}
return -1;
}
boolean addProject(String name, String location, int projNumber, double initialFund) {
int result = nameLocationExists(name, location);
if (result == -1) {
result = projNumberExists(projNumber);
if (result == -1) {
Project project = new Project(initialFund);
project.setLocation(location);
project.setName(name);
project.setNumber(projNumber);
this.list.add(project);
return true;
}
}
return false;
}
boolean removeProjNumber(int projNumber) {
if (this.list != null) {
Iterator<Project> iterator = this.list.iterator();
while (iterator.hasNext()) {
Project project = iterator.next();
if (project.getNumber() == projNumber) {
iterator.remove();
return true;
}
}
}
return false;
}
boolean removeNameLocation(String name, String location) {
if (this.list != null) {
Iterator<Project> iterator = this.list.iterator();
while (iterator.hasNext()) {
Project project = iterator.next();
if (project.getName().endsWith(name) && project.getLocation().equalsIgnoreCase(location)) {
iterator.remove();
return true;
}
}
}
return false;
}
void sortByProjNumber() {
Collections.sort(this.list, new ProjNumberComparator());
}
void sortByNameLocation() {
Collections.sort(this.list, new ProjNameLocationComparator());
}
String listProjects() {
return this.list.toString();
}
void closeProjectManagement() {
this.list.clear();
}
public List<Project> getList() {
return list;
}
public void setList(List<Project> list) {
this.list = list;
}
}
package com.home.chegg1;
import java.util.Comparator;
public class ProjNameComparator implements Comparator<Project> {
@Override
public int compare(Project o1, Project o2) {
return o1.getName().compareTo(o2.getName());
}
}
package com.home.chegg1;
import java.util.Comparator;
public class ProjNameLocationComparator implements Comparator<Project> {
@Override
public int compare(Project o1, Project o2) {
int result = o1.getName().compareTo(o2.getName());
if(result == 0){
return o1.getLocation().compareTo(o2.getLocation());
}
return result;
}
}
package com.home.chegg1;
import java.util.Comparator;
public class ProjNumberComparator implements Comparator<Project> {
@Override
public int compare(Project o1, Project o2) {
return ((Integer) o1.getNumber()).compareTo(o2.getNumber());
}
}
package com.home.chegg1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Sorts {
public static void sort(ArrayList<Project> objects, Comparator<Project> comparator) {
Collections.sort(objects, comparator);
}
}
I have left read from file I hope you can complete that. Please let me know if you need modification.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.