Results 1 to 10 of 10
  1. #1
    Hooky75 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default 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
    Attached Files Attached Files

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    Hooky75 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    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;
    	}
    
    }
    Java 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;
    	}
    }
    logins.txt

    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

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  5. #5
    Hooky75 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    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.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Java Code:
    courses.addAll(Arrays.asList(s.split(":")));
    Where is is the colon delimited string.

  7. #7
    Hooky75 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    thank you so much that is a great help.
    Cheers.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  9. #9
    Hooky75 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    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.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by Hooky75 View Post
    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.
    Think how you would loop through an array of strings and print each element. Now use the get method instead of subscript([]) operators.

Similar Threads

  1. Abstract Classes.
    By maknib in forum New To Java
    Replies: 3
    Last Post: 05-12-2011, 03:30 PM
  2. abstract classes
    By renju krishnan in forum New To Java
    Replies: 1
    Last Post: 09-29-2010, 09:31 AM
  3. question about abstract classes
    By TheFlying_Boy in forum New To Java
    Replies: 6
    Last Post: 07-08-2009, 08:19 AM
  4. interface vs abstract classes
    By rosh72851 in forum New To Java
    Replies: 7
    Last Post: 11-16-2008, 09:22 PM
  5. Inverfaces vs Abstract Classes
    By ravian in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 10:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •