Reputation: 625
I have a Project class and I want that to hold projects that I want to be doing. I'm a jewelry and want to essentially have a file from each project I have going on. Each file has, right now, a name, date started, deadline date, and description. I want to call the printPro() I just don't know how.
The Project class looks like this:
public class Project {
String name;
int date;//start date
int fDate;//finish date
String description;
public Project(String gName, int gDate, int gFDate, String gDescription){
// mean they are the ones that are given.
}
public void receivedName(String receivedName) {
name= receivedName;
}
public void receivedDate(int receivedDate) {
date= receivedDate;
}
public void receivedFDate(int receivedDate) {
fDate= receivedDate;
}
public String returnName(){
return name;
}
public int returnDate(){
return date;
}
public int returnFDate(){
return fDate;
}
public String returnDescription(){
return description;
}
public String printPro(int index){
String tempString = ("Project Name: " + name +
"\nStart Date: " + date +
"\nFinish Date: " + fDate + "\nDescription: " + description);
return tempString;
}
public void printProject(int index){
System.out.println("Project Name: " + name +
"\nStart Date: " + date +
"\nFinish Date: " + fDate + "\nDescription: " + description);
}
}
Right now all I have is everything running though a console. I was going to build a Window later when I learn how to create and import a text file. Anyway here is the code for the console:
public class Top{
Math help = new Math();//Creates a Math object
People person = new People();//Creates a People object
// Project nProject= new Project();
ArrayList<Project> mProject = new ArrayList<Project>();
Project[] storeProject; //Tried to create an array of Projects I could just add to.
Project[] storeTempProject;
StringTest sTest = new StringTest(); // This is my class to use the console.
String choice; //Temporary strings
String redo;
public static void main (String [] args){
// ============Project Menu =========================
private void project() {
System.out.println("ADD, DELETE, REVIEW, or BACK?");
sTest.stringReader();
choice = sTest.getString();
if (sTest.test(choice, "add")){
System.out.println("What would you like to call this project?");
sTest.stringReader();
String name = sTest.getString();
System.out.println("When would you like it to start?");
sTest.stringReader();
String startDate = sTest.getString();
int sDate = Integer.parseInt(startDate);
System.out.println("When would you like it to end?");
sTest.stringReader();
String finishDate = sTest.getString();
int fDate = Integer.parseInt(finishDate);
System.out.println("What is the description of your project?");
sTest.stringReader();
String projectDescription = sTest.getString();
mProject.add(new Project(name, sDate, fDate, projectDescription));
}
}
I took out some unnecessary stuff out of my code so it doesn't bog down this question. If you need more info let me know. I have been searching for an hour now so I'm assuming this doesn't work and I need to do it another way.
Upvotes: 1
Views: 147
Reputation: 3454
I'm not entirely sure on your question, but maybe this what you are after:
You can call methods on the elements of your ArrayList like this:
for (Project proj : mProject) {
proj.printPro();
}
To display the first one just do:
mProject.get(0)
Will return the first Project in the ArrayList. To return second just change the argument to 'get' to 1. You should be able to work out how to get the rest.
Upvotes: 3
Reputation: 2164
Yes the answer given by joeslice is the one I would go for, you just need to go through the ArrayList of Project objects and call the printPro method on each Project object.
Also you should make your instance variables in Project private, if not specified Java defaults them to public. The reason for this is that good OOP code should encapsulate the state of the object so the state can only be altered by calling methods on it.
I also don't think it make sense to have printPro and printProject methods. I would just have the printPro method and if you need to print anything just do:
System.out.println(proj.printPro());
Otherwise you will just have duplicate code and printPro is probably more useful as it returns a string which can be used external to the Project class. Printing out to the console or to a window should not be part of the classes that store data. You should delegate the task of displaying output to another class e.g one named ConsoleView.
It is not clear what you want to do, so if you need anymore help just ask.
Hope this helps.
Upvotes: 0