Results 1 to 10 of 10
Thread: Abstract Classes and ArrayLists.
- 05-14-2011, 07:15 AM #1
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
Abstract Classes and ArrayLists.
I am working with Abstract Classes that have Arraylist, i have created a User Class, Administrator Class and An Instructor class, so i have a users.Arraylist and a requests arraylist, i also have a courses arraylist which is in the instructor class, i have read in a login.txt file, and and add a login, pass, and id to the arraylist, eg: jsmith(login) | abc123(password) | instructor(id) | cosc1020 : cosc1245.
And then i add the coursecodes to the arraylist of courses.
now i am having trouble returning the course arraylist of type string, my output looks like this:
[| cosc1020, cosc1245]
[| cosc1009]
[]
can you help me to output just the codes without the [] and |
regards
- 05-14-2011, 07:45 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The brackets are caused by printing an array list. An array lists toString appendage a [ on the front and a ] on the back. If you don't want them, just manually loop through the array printing each item. As far as the pipe(|), you may have improperly splitting the read in text file.
Would you mind posting your code via code tags rather than an attachment, to use a code tag do this, type [code] then paste your code under it, then close it with [/code] after the code.
It seems like you are creating a class and just storing all the data in an array list, which is not a great design. Is this an assignment where you need to use an array list toning the data? It would be nicer if the classes just had instance variables which represent each individual thing about the class.
- 05-14-2011, 07:57 AM #3
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
Hey i used arraylist cause its dynamic and i am not sure how many objects will be added to any of the arraylist, any help you offer would be greatly appreciated. Below I have pasted all code and the txt files.
Again any help will be appreciated.
Java Code:public class Administrator extends User { private String id; public Administrator(String login, String password) { super(login, password); this.id = "admin"; } public String getId() { return id; } }Java Code:import java.util.ArrayList; public class Instructor extends User { ArrayList<String> courses; private String id; public Instructor(String login, String password) { super(login, password); this.courses = new ArrayList<String>(); this.id = "instructor"; } public void addToCourses(String course) { courses.add(course); } public ArrayList<String> getCourses() { return courses; } public void setCourses(ArrayList<String> courses) { this.courses = courses; } public String getId() { return id; } }Java Code:public class RequestApp { private static ArrayList<User> users; private static ArrayList<Requests> requests; /** * @param args * @throws ParseException * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException, ParseException { users = new ArrayList<User>(); requests = new ArrayList<Requests>(); restore(); for (int i = 0; i < users.size(); i++) { System.out.println(users.get(i).getLogin() + users.get(i).getPassword()); } for (int i = 0; i < users.size(); i++) { if (users.get(i).getId() == "instructor") { Instructor instructor = (Instructor) users.get(i); System.out.println(instructor.getCourses()); } } for (int i = 0; i < requests.size(); i++) { System.out.println(requests.get(i).getRequestID() + ", " + requests.get(i).getStudentID() + ", " + requests.get(i).getCourseCode() + ", " + requests.get(i).getAssessment() + ", " + requests.get(i).getReason() + ", " + requests.get(i).getDays() + ", " + requests.get(i).getOutcome() + ", " + requests.get(i).getDueDate().get(Calendar.DATE) + "/" + requests.get(i).getDueDate().get(Calendar.MONTH) + "/" + requests.get(i).getDueDate().get(Calendar.YEAR)); } mainMenu(); } public static void mainMenu() { System.out.println(""); String login; Scanner scnr = new Scanner(System.in); login = scnr.nextLine(); for (int i = 0; i < users.size(); i++) { if (users.get(i).getLogin().equalsIgnoreCase(login)) { String pass = scnr.nextLine(); } } } public static void restore() throws FileNotFoundException, ParseException { String login, password, studentID, courseCode, assessment, type, reason, outcome; int requestID, days; GregorianCalendar dueDate; File fileLogins = new File("logins.txt"); Scanner fileScnr = new Scanner(fileLogins); try { while (fileScnr.hasNextLine()) { StringTokenizer inReader = new StringTokenizer(fileScnr .nextLine(), "|"); login = inReader.nextToken(); password = inReader.nextToken(); type = inReader.nextToken("|"); if (type.contains("instructor")) { Instructor instructor = new Instructor(login, password); while (inReader.hasMoreTokens()) { courseCode = inReader.nextToken(":").trim(); instructor.addToCourses(courseCode); } users.add(instructor); } else { Administrator admin = new Administrator(login, password); users.add(admin); } } fileScnr.close(); } catch (NoSuchElementException nsee) { System.out.println("NoSuchElementException"); } File fileRequest = new File("requests.txt"); Scanner requestFile = new Scanner(fileRequest); try { while (requestFile.hasNextLine()) { StringTokenizer rqToken = new StringTokenizer(requestFile .nextLine(), ","); String tempRequestID = rqToken.nextToken(); requestID = Integer.parseInt(tempRequestID); studentID = rqToken.nextToken(); courseCode = rqToken.nextToken(); assessment = rqToken.nextToken(); reason = rqToken.nextToken(); // count if there is more tokens after reason int number = rqToken.countTokens(); if (number == 0) { Requests request = new Requests(requestID, studentID, courseCode, assessment, reason); requests.add(request); } else { String tempDays = rqToken.nextToken().trim(); days = Integer.parseInt(tempDays); outcome = rqToken.nextToken(); String tempDueDate = rqToken.nextToken(); // Split string into an array of value with a "/" delimiter. String[] tempDate = tempDueDate.split("\\/"); // Convert string into an integer type variable. int day = Integer.parseInt(tempDate[0].trim()); int month = Integer.parseInt(tempDate[1].trim()); int year = Integer.parseInt(tempDate[2].trim()); // Construct an end date. dueDate = new GregorianCalendar(year, month, day); Requests request = new Requests(requestID, studentID, courseCode, assessment, reason, days, outcome, dueDate); requests.add(request); } } } catch (NoSuchElementException nsee) { System.out.println("NoSuchElementException"); } catch (NumberFormatException nfe) { System.out.println("NumberFormatException"); } } }Java Code:import java.text.ParseException; import java.util.GregorianCalendar; class Requests { // Initialize variables private int requestID; private String studentID; private String courseCode; private String assessment; private String reason; private int days; private String outcome; private GregorianCalendar dueDate; // constructor to set default for reason, days, and outcome, when those // details are missing from the hard coded details. public Requests(int requestID, String studentID, String courseCode, String assessment, String reason) throws ParseException { this.requestID = requestID; this.studentID = studentID; this.courseCode = courseCode; this.assessment = assessment; this.reason = reason; this.days = 0; this.outcome = "Pending"; this.dueDate = new GregorianCalendar(); } // constructor to set default for reason, days, and outcome, when days, and // outcome details are missing from the hardcoded details. public Requests(int requestID, String studentID, String courseCode, String assessment, String outcome, String reason) throws ParseException { this.requestID = requestID; this.studentID = studentID; this.courseCode = courseCode; this.assessment = assessment; this.reason = reason; this.days = 0; this.outcome = "Pending"; this.dueDate = new GregorianCalendar(); } // Default Constructor. public Requests(int requestID, String studentID, String courseCode, String assessment, String reason, int days, String outcome, GregorianCalendar dueDate) throws ParseException { this.requestID = requestID; this.studentID = studentID; this.courseCode = courseCode; this.assessment = assessment; this.reason = reason; this.days = days; this.outcome = outcome; this.dueDate = dueDate; } public Requests() { // TODO Auto-generated constructor stub } // read request id and set to integer public void setRequestID(int requestID) { this.requestID = requestID; } // read student id and set to string public void setStudentID(String studentID) { this.studentID = studentID; } // read courseCode and set to string public void setCourseCode(String courseCode) { this.courseCode = courseCode; } // read Assessment and set to string public void setAssessment(String assessment) { this.assessment = assessment; } // read reason and set to string public void setReason(String reason) { this.reason = reason; } // read days and set to integer public void setDays(int days) { this.days = days; } public int getDays() { return days; } // read outcome and set to string public void setOutcome(String outcome) { this.outcome = outcome; } // read dueDate and set to Gregorian Calendar public void setDueDate(GregorianCalendar dueDate) { this.dueDate = dueDate; } /* * Get Methods */ // read RequestId and return integer value public int getRequestID() { return requestID; } // read StudentID and return String value public String getStudentID() { return studentID; } // read CourseCode and return string value public String getCourseCode() { return courseCode; } // read Assessment and return string value public String getAssessment() { return assessment; } // read Reason and return string value public String getReason() { return reason; } // read Outcome and return string value public String getOutcome() { return outcome; } // read DueDate and return Gregorian Calendar value public GregorianCalendar getDueDate() { return dueDate; } }logins.txtJava Code:public abstract class User { private String login, password; private String id; public User(String login, String password) { super(); this.login = login; this.password = password; } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
jsmith | abc123 | instructor | cosc1020 : cosc1245
djones | pass | admin
clem | clem | instructor | cosc1009
teacher1 | password | instructor
requests.txt
5,3344556,COSC2135, Weblearn, Work commitment, 7, Denied, 13/09/2011
1006, 3212908, COSC1077, Assignment1, Flu, null, null, null
21450, 2345678F, COSC1117, Assignment2, Husband Sick, 3, Granted, 25/04/2011
089, 9987564X, COSC1077, Weblearn, In Hospital, 7, Pending, 24/05/2011
1245, 3145091, COSC1117, Assignment3, Carers leave, 5, Granted, 02/04/2011
- 05-14-2011, 08:21 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Is your only problem how to print the courses without the pipe and brackets?
If so, extract the courses and store it in a temp array list, then print them with a loop and remove the pipe.
Originally o thought your classes didn't have instance variables and instead used an array list to hold all the variables, that's why I questioned the design. Now i see it and it looks fine.
- 05-14-2011, 08:24 AM #5
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
thanks for the feedback, its appreciated, would you be able to look at the reading in of the file and tell me how i might be able to get the | out of the course array list
Cheers.
- 05-14-2011, 08:31 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I believe the call to nextToken(":") changes the delimiter so it goes from current position to the :. I suggest you simply extract the next token(without changing to : ), this will return a string separated by :'s, then you can split the string and store it in the arraylist with something like this
Where is is the colon delimited string.Java Code:courses.addAll(Arrays.asList(s.split(":")));
- 05-14-2011, 08:34 AM #7
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
thank you so much that is a great help.
Cheers.
- 05-14-2011, 08:36 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You are welcome, if I helped you figure it out, please mark your thread solved with the thread tools at the top of the page.
- 05-14-2011, 09:27 AM #9
Member
- Join Date
- May 2011
- Posts
- 11
- Rep Power
- 0
mostly sorted, still trying to work out how to loop thru and print each element, but i don't have the | in there anymore which is great.
Cheers.
- 05-14-2011, 09:38 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Abstract Classes.
By maknib in forum New To JavaReplies: 3Last Post: 05-12-2011, 02:30 PM -
abstract classes
By renju krishnan in forum New To JavaReplies: 1Last Post: 09-29-2010, 08:31 AM -
question about abstract classes
By TheFlying_Boy in forum New To JavaReplies: 6Last Post: 07-08-2009, 07:19 AM -
interface vs abstract classes
By rosh72851 in forum New To JavaReplies: 7Last Post: 11-16-2008, 08:22 PM -
Inverfaces vs Abstract Classes
By ravian in forum New To JavaReplies: 1Last Post: 11-28-2007, 09:53 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks