new semester here we go

This commit is contained in:
Medium Fries
2019-03-15 01:25:45 -07:00
parent c319e77f12
commit fa12687849
80 changed files with 7781 additions and 4 deletions

78
338/homework5/App.java Normal file
View File

@@ -0,0 +1,78 @@
/*
Author: Alejandro Santillana
Description: Builds a basic school data tracker
keeps track of students and what course they are enrolled in as well as what courses
are offered by the school.
Course information is also available
Input is read from file
*/
public class App {
public static void main(String[] args) {
String dataFile1 = "data1.txt";
String dataFile2 = "data2.txt";
// course reference for demo purposes
Course course1;
Student student1;
// first we'll read in some data
System.out.println("===== Read Data 1 =====");
School SCD = new School("CSUMB");
// get all basic info about the SCD
SCD.readData(dataFile1);
System.out.println("\n===== School Info 1 =====");
SCD.schoolInfo();
// round 2 of adding info to the SCD
System.out.println("\n===== Read Data 2 =====");
SCD.readData(dataFile2);
System.out.println("\n===== School Info 2 =====");
SCD.schoolInfo();
// we'll now add some new instructors
SCD.addInstructor(700, "E. Tao", "tao@csumb.edu", "777-777-1234");
SCD.addCourse(300, "CST300 ProSem", 700, "BIT110");
SCD.addCourse(231, "CST231 Intro C++", 100, "BIT104");
// examples of bad courses to add
System.out.println("\n===== Failed Course Addition =====");
SCD.addCourse(306, "CST306 GUI Dev", 250, "BIT120");
SCD.addCourse(499, "CST499 iOS Dev", 150, "BIT104");
System.out.println("\n===== Detailed Course Info =====");
SCD.courseInfo(306);
// updateing a courses location
course1 = SCD.getCourse(205);
course1.updateLocation("Library 104");
// checking some courses' information
System.out.println("\n===== Detailed Course Info 2 =====");
SCD.courseInfo(205);
System.out.println("\n===== Detailed Course Info 4 =====");
SCD.courseInfo();
// adding a student (valid this time)
SCD.addStudent(5555, "Chris Watson", 205, 85.50f, "B");
System.out.println("\n===== Detailed Course Info 5 =====");
SCD.courseInfo(205);
student1 = SCD.getStudentInfo(7777);
// student info
System.out.println("\n===== Detailed Student Info =====");
System.out.println(student1);
System.out.println("\n===== Detailed Student Info 2 =====");
System.out.println(SCD.getStudentInfo(7777));
SCD.graduateStudent(5555);
System.out.println("\n===== Detailed Course Info 6 =====");
SCD.courseInfo(205);
SCD.graduateStudent(5555);
System.out.println("\n===== Good Job! Bye! =====");
}
}

25
338/homework5/Course.java Normal file
View File

@@ -0,0 +1,25 @@
public class Course {
public int id;
public String title;
public int instructorID;
public String location;
public int studentCount;
public Course (int id, String title, int instructorID, String location) {
this.id = id;
this.title = title;
this.instructorID = instructorID;
this.location = location;
this.studentCount = 0;
}
// updates the room location of a given course ex. BIT-104 to BIT-110
public void updateLocation(String newLocation) {
this.location = newLocation;
}
public String toString() {
return this.id + " - " + this.title;
}
}

8
338/homework5/Grade.java Normal file
View File

@@ -0,0 +1,8 @@
public class Grade {
public float num_grade;
public String str_grade;
public Grade(float num_grade, String str_grade) {
this.num_grade = num_grade;
this.str_grade = str_grade;
}
}

View File

@@ -0,0 +1,15 @@
public class Instructor {
public int id;
public String name;
public String email;
public String phone;
public Instructor(int id , String name, String email, String phone) {
this.id = id;
this.name = name;
this.email = email;
this.phone = phone;
}
public String toString() {
return this.name;
}
}

279
338/homework5/School.java Normal file
View File

@@ -0,0 +1,279 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
// NOTE: there will probably only be one instance of a school object ever
public class School {
private ArrayList<Instructor> instructorList;
private ArrayList<Course> courseList;
private ArrayList<Student> studentList;
private String name;
public School(/*String file,*/ String schoolName) {
this.instructorList = new ArrayList<Instructor>();
this.courseList = new ArrayList<Course>();
this.studentList = new ArrayList<Student>();
this.name = schoolName;
//readData(file);
}
public void readData(String filename) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
// first line is how many instructors we want to add
int inst_count = Integer.parseInt(reader.readLine());
for(int i =0;i<inst_count;i++) {
// now we shold begin to populate out instructor list
String[] data = reader.readLine().split(",");
int tmp_id = Integer.parseInt(data[0]);
// ignoring the return value as those custom messages are in the method itself
this.addInstructor(tmp_id, data[1], data[2],data[3]);
}
// next line should be number of courses to add
int course_count = Integer.parseInt(reader.readLine());
for(int i =0;i<course_count;i++) {
String[] data = reader.readLine().split(",");
int tmp_id = Integer.parseInt(data[0]);
int inst_id = Integer.parseInt(data[2]);
this.addCourse(tmp_id, data[1], inst_id, data[3]);
}
// finally the amount of students we want to add is next
int student_count = Integer.parseInt(reader.readLine());
for(int i =0;i<student_count;i++) {
// parse items from the current line
String[] data = reader.readLine().split(",");
int stud_id = Integer.parseInt(data[0]);
int cour_id = Integer.parseInt(data[2]);
float fl_grade = Float.parseFloat(data[3]);
// attempt to add the student
this.addStudent(stud_id, data[1], cour_id, fl_grade, data[4]);
}
System.out.println("Done.");
}
catch(IOException e) {
e.printStackTrace();
}
}
public boolean addInstructor(int id, String name, String email, String phone) {
// make sure that the new instructor isn't already in the system
for(Instructor i : this.instructorList) {
if(id == i.id) {
System.out.println("Instructor ID already in use");
return false;
}
}
Instructor new_inst = new Instructor(id, name, email, phone);
instructorList.add(new_inst);
return true;
}
public boolean addCourse(int id, String title, int instructorID, String location) {
// make sure we don't the same course twice
for(Course c : this.courseList) {
if(c.id == id) {
System.out.println("Course addition failed Course number already used.");
return false;
}
}
// check if the instructor exists
boolean instructorExists = false;
for(Instructor i : this.instructorList) {
if(i.id == instructorID) {
instructorExists = true;
}
}
if(!instructorExists) {System.out.println("Course addition failed Non-existing instructor.");}
// check the instructor
Course new_course = new Course(id, title, instructorID, location);
courseList.add(new_course);
return true;
}
public int courseEnrolled(int id) {
// Helper function which simply counts up how many people are enrolled in a given cuorse
int count = 0;
for(Course i : this.courseList) {
if(i.id == id) {
count++;
}
}
return count;
}
public String getInstructor(int id) {
// find the instructor name given an id
for(Instructor i : this.instructorList) {
if(i.id == id) {
return i.name;
}
}
return "Not Found";
}
public float courseAverage(int id) {
// gets average of all the students grades for a given course by id
float sum = 0;
int count = 0;
for(Student s : this.studentList) {
for(Integer i : s.grades.keySet()) {
if(i == id) {
sum += s.grades.get(id).num_grade;
count++;
}
}
}
if(count == 0) {
return 0.00f;
}
return sum / (float)count;
}
public void courseInfo(int id) {
boolean found = false;
// look for the course given by the id
for(Course i : this.courseList) {
found = true;
if(i.id == id) {
System.out.println("Course number: " + i.id);
System.out.println("Instructor: "+ this.getInstructor(i.instructorID));
System.out.println("Course title: " + i.title);
System.out.println("Room: " + i.location);
System.out.println("Total enrolled: " + this.courseEnrolled(id));
System.out.println("Course average: " + this.courseAverage(id));
}
}
if(!found) {
System.out.println("Course not found: [id = " + id + "]");
}
}
public void courseInfo() {
System.out.println("Number of courses: " + this.courseList.size());
for(Course c : this.courseList) {
System.out.println(c.id + ": " + this.courseEnrolled(c.id) + " enrolled");
}
}
public Course getCourse(int id) {
for(int i =0;i<this.courseList.size();i++) {
if(this.courseList.get(i).id == id) {
return this.courseList.get(i);
}
}
return null;
}
public boolean courseEmpty(int courseID) {
for(Student s : this.studentList) {
if(s.grades.get(courseID) != null) {
return true;
}
}
return true;
}
public boolean deleteCourse(int id) {
for(int i =0;i<this.courseList.size();i++) {
if(this.courseEmpty(this.courseList.get(i).id)) {
if(this.courseList.get(i).id == id) {
this.courseList.remove(i);
return true;
}
}
}
return false;
}
public boolean addStudent(int id, String name, int courseID, float floatGrade, String letterGrade) {
for(Student i : this.studentList) {
if(i.id == id) {
// attempt to regitser the student in case they're trying to add a new course
// this method should also give us proper error msgs about registering
if(this.register(id, courseID, floatGrade, letterGrade)) {
return true;
}
return false;
}
}
// check if the course even exists at all
boolean cFound = false;
for(Course c : this.courseList) {
if(c.id == courseID) {
cFound = true;
}
}
if(!cFound) {
System.out.println("Course does not exist");
return false;
}
// finally we can add the student into the system
// this is assuming we didn't register them earlier
Student new_student = new Student(id, name, courseID, floatGrade, letterGrade);
studentList.add(new_student);
return true;
}
public Student getStudentInfo(int id) {
for(Student s : this.studentList) {
if(s.id == id) {
return s;
}
}
return null;
}
public boolean graduateStudent(int id) {
// remove the target id from all the courses
Student tmp = getStudentInfo(id);
if(tmp == null) {
System.out.println("Student not found");
return false;
}
tmp.graduate();
return true;
}
public boolean register(int studentID, int courseID, float floatGrade, String letterGrade) {
Student tmp = this.getStudentInfo(studentID);
if(tmp.grades.keySet().contains(courseID)) {
System.out.println("Student already enrolled");
return false;
}
// make sure we don't try to regitster for a course that doesn't exist
if(this.getCourse(courseID) == null) {
return false;
}
// if the student is there but not enrolled in that course then just create a new student with new data
tmp.enroll(courseID, floatGrade, letterGrade);
return true;
}
public void drop(int studentID, int courseID) {
}
public void assign(int instructorID, int courseID) {
}
public void unassign(int instructorID, int courseID) {
}
// basic printout of school infor
public void schoolInfo() {
System.out.println("School name: " + this.name);
// instructors
System.out.println("Instructor info");
for(Instructor i : this.instructorList) {
System.out.println(i.name);
}
// courses
System.out.println("Course Information");
for(Course c : this.courseList) {
System.out.println(c.title);
}
// students
System.out.println("Student Information");
for(Student s : this.studentList) {
// check what courses that student is in
for(Integer i : s.grades.keySet()) {
Course c_rep = this.getCourse(i);
System.out.println(s.name + ": " + c_rep.title);
}
}
}
}

View File

@@ -0,0 +1,43 @@
import java.util.HashMap;
import java.util.ArrayList;
public class Student {
public int id;
public String name;
// courseID, grade for that course
public HashMap<Integer, Grade> grades;
public String status;
public Student(int id, String name, int courseId, float floatGrade, String letterGrade) {
this.name = name;
this.id = id;
grades = new HashMap<Integer, Grade>();
grades.put(courseId, new Grade(floatGrade, letterGrade));
// default student status
this.status = "Enrolled";
}
public String toString() {
return "Student number: " + this.id + "\n" +
"Name: " + this.name + "\n" +
"Status: " + this.status;
}
public float averageGrade() {
float sum = 0;
int count = 0;
for(Grade g : this.grades.values()) {
sum += g.num_grade;
count++;
}
return sum/(float)count;
}
public void graduate() {
for(Integer i : this.grades.keySet()) {
// remove from all the courses
this.grades.remove(i);
}
this.status = "Graduated";
}
public void enroll(int courseId, float num_grade, String l_grade) {
grades.put(courseId, new Grade(num_grade, l_grade));
}
}

13
338/homework5/data1.txt Normal file
View File

@@ -0,0 +1,13 @@
4
100,Y. Byun,ybyun@csumb.edu,111-111-1111
200,S. Narayanan,sathya@csumb.edu,222-222-2222
300,M. Lara,lara@csumb.edu,333-333-3333
250,S. Bude,bude@csumb.edu,444-123-4567
3
338,CST338 - Software Design,100,BIT 104
205,CST205 - Multimedia Design and Programming,200,BIT 118
306,CST306 - Game Engine Programming,100,BIT 104
3
7777,Alice Otter,338,89.50,B
8888,Bob Otter,205,75.00,C
7777,Alice Otter,306,98.00,A

10
338/homework5/data2.txt Normal file
View File

@@ -0,0 +1,10 @@
2
500,G. Bruns,bruns@csumb.edu,555-222-2222
300,O. Doe,doe@csumb.edu,444-333-3333
1
336,CST336 - Internet Programming,300,Library 1010
4
7777,Alice Otter,205,71.00,C
7777,Unknown Otter,205,71.00,C
7777,Alice Otter,310,81.55,B
9999,John Doe,338,89.50,B

8
338/homework5/makefile Normal file
View File

@@ -0,0 +1,8 @@
all:
javac App.java Course.java Instructor.java School.java Student.java
run: all
@java App
clean:
rm -f *class