From 1d66bba2cc3568e83128eed2cd245b9943dcf392 Mon Sep 17 00:00:00 2001 From: tokugawa Date: Fri, 24 Jan 2014 12:07:59 -0500 Subject: [PATCH] Functioning Homework 1 Part 1. Creates a treelist of all new users and removes all users that were in old users, leaving only the unique, non-duplicated new users not in old users. --- HW1/src/edu/uncc/itcs4180/PartOne.java | 201 ++++++++++++++++++++++++- 1 file changed, 194 insertions(+), 7 deletions(-) diff --git a/HW1/src/edu/uncc/itcs4180/PartOne.java b/HW1/src/edu/uncc/itcs4180/PartOne.java index 2f6fee7..53973db 100644 --- a/HW1/src/edu/uncc/itcs4180/PartOne.java +++ b/HW1/src/edu/uncc/itcs4180/PartOne.java @@ -1,12 +1,199 @@ +/* + * Bradlee Speice, Brandon Rodenmayer + * ITIS 4180 + * Homework 1 + * PartOne.java + */ package edu.uncc.itcs4180; -/* Assignment: Homework 1 - * File name: PartOne.java - * Group: - * Bradlee Speice - * Brandon Rodenmayer - */ +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.*; -public class PartOne { +public class PartOne +{ + public static void main(String[] args) + { + SortedSet uniqueUsers = new TreeSet(new UserComparator()); + + //Add all new users to uniqueUsers assuming they are not old users + String filePath = "userListNew.txt"; + //Using modified code provided in assignment instructions + // Lets make sure the file path is not empty or null + if (filePath == null || filePath.isEmpty()) + { + System.out.println("Invalid File Path"); + return; + } + + BufferedReader inputStream = null; + //We need a try catch block so we can handle any potential IO errors + try + { + //Try block so we can use efinallyf and close BufferedReader + try + { + inputStream = new BufferedReader(new FileReader(filePath)); + String lineContent = null; + //Loop will iterate over each line within the file. + //It will stop when no new lines are found. + while ((lineContent = inputStream.readLine()) != null) + { + //Add user to the list of users + String[] resultingTokens = lineContent.split(","); + User tempUser= new User(resultingTokens[0].trim(), resultingTokens[1].trim().charAt(0), resultingTokens[2].trim(), Integer.parseInt(resultingTokens[3].trim())); + uniqueUsers.add(tempUser); + } + } + //Make sure we close the buffered reader. + finally + { + if (inputStream != null) + inputStream.close(); + } + } + catch (IOException e) + { + e.printStackTrace(); + } + + //Remove all of the old users from uniqueUsers, leaving only new users + filePath = "userListOld.txt"; + //Using modified code provided in assignment instructions + // Lets make sure the file path is not empty or null + if (filePath == null || filePath.isEmpty()) + { + System.out.println("Invalid File Path"); + return; + } + + inputStream = null; + //We need a try catch block so we can handle any potential IO errors + try + { + //Try block so we can use efinallyf and close BufferedReader + try + { + inputStream = new BufferedReader(new FileReader(filePath)); + String lineContent = null; + //Loop will iterate over each line within the file. + //It will stop when no new lines are found. + while ((lineContent = inputStream.readLine()) != null) + { + //Remove the user if they exist + String[] resultingTokens = lineContent.split(","); + User tempUser= new User(resultingTokens[0].trim(), resultingTokens[1].trim().charAt(0), resultingTokens[2].trim(), Integer.parseInt(resultingTokens[3].trim())); + /*Print users as they are removed + * if(uniqueUsers.remove(tempUser)); + * System.out.println("REMOVED USER: "+tempUser); + */ + uniqueUsers.remove(tempUser); + } + } + //Make sure we close the buffered reader. + finally + { + if (inputStream != null) + inputStream.close(); + } + } + catch (IOException e) + { + e.printStackTrace(); + } + //iterator to print out all users in uniqueUsers (Sorted by age) + for (User user : uniqueUsers) + { + System.out.print(user.getFname()+ " "); + System.out.print(user.getMinit()+ " "); + System.out.print(user.getLname()+ " "); + System.out.println(user.getAge()); + } + } } + +class User +{ + private String fname; + private char minit; + private String lname; + private int age; + + public User() + { + fname = "Default"; + minit = 'D'; + lname = "User"; + age = 0; + } + + public User(String fname, char minit, String lname, int age) + { + this.fname = fname; + this.minit = minit; + this.lname = lname; + this.age = age; + } + + public String getFname() + { + return this.fname; + } + + public char getMinit() + { + return this.minit; + } + + public String getLname() + { + return this.lname; + } + + public int getAge() + { + return this.age; + } + + public boolean equals(User user) + { + if(this.fname.equals(user.getFname()) && this.minit==user.getMinit() && this.lname.equals(user.getLname()) && this.age==user.getAge()) + { + return true; + } + else + { + return false; + } + } + + public String toString() + { + String userString = fname+" "+minit+" "+lname+" "+age; + return userString; + } +} + +class UserComparator implements Comparator +{ + public int compare(User a, User b) + { + if(a.getFname().equals(b.getFname()) && a.getMinit()==b.getMinit() && a.getLname().equals(b.getLname()) && a.getAge()==b.getAge()) + { + return 0; + } + else + { + if(a.getAge()>b.getAge()) + { + return 1; + } + else + { + return -1; + } + } + } +} \ No newline at end of file