***JAVA PROGRAMMING*** Write a Menu Driven program * for the student to enter in
ID: 3721868 • Letter: #
Question
***JAVA PROGRAMMING*** Write a Menu Driven program * for the student to enter information and enroll fro m the list of available courses 1. Ask student for first name, last name, id, address and instantiate a student object. 2. Create a menu from the sections returned from the DataStore class (an array of section objects) 3. Prompt the student to enroll in sections available from the menu 4. Handle the custom exceptions and let the student know if they try to enroll in a duplicate section or if they have enrolled in too many 5. Provide a "Quit" option in your menu 6. When the user chooses "Quit", automatically print their information and schedule.
---ADDRESS CLASS---
---PERSON CLASS---
---STUDENT CLASS---
---COURSESECTION CLASS---
---DataStore Class---
***HERE IS THE SECTIONS.TXT FILE***
Explanation / Answer
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Paths;
import java.util.Scanner;
class Rextester {
public static void main(String args[]) {
Map<Integer, CourseSection> store = new HashMap<Integer, CourseSection>();
DataStore ds = DataStore.getInstance();
CourseSection[] srcs = ds.getSections();
int i = 0;
for (CourseSection courseSection : srcs) {
store.put(++i, courseSection);
}
List<Student> all = new ArrayList<Student>();
Scanner in = new Scanner(System.in);
boolean quit = false;
int menuItem;
Student stud = null;
do {
System.out.print("Choose menu item: ");
System.out.println("press 1 for adding student info.");
System.out.println("press 2 choosing a course to that student");
System.out.println("press 0 to QUIT");
try {
menuItem = in.nextInt();
} catch (Exception e) {
System.out.println("Invalid choice!!! Choose correctly ..start again");
break;
}
switch (menuItem) {
case 1:
System.out.println("Enter the student first name");
String fname = in.next();
System.out.println("Enter the student last name");
String lname = in.next();
System.out.println("Enter the student ID");
String id = in.next();
System.out.println("Enter the student address in street ,city ,state,zip");
String street = in.next();
String city = in.next();
String state = in.next();
String zip = in.next();
Address add = new Address(street, city, state, zip);
stud = new Student(id, fname, lname, add);
all.add(stud);
System.out.println("Student added successfully");
break;
case 2:
System.out.println("Enter the student ID");
String idGiven = in.next();
Student entered = new Student();
entered.setId(idGiven);
Student found = null;
try {
found = all.get(all.indexOf(entered));
System.out.println(found.toString());
} catch (Exception e) {
System.out.println(
"This student id not available !!! You need to add a student first..so choose 1 or input correct student id");
continue;
}
int j = 0;
for (CourseSection courseSection : srcs) {
System.out.println((++j) + " : " + courseSection.toString());
}
j = 0;
System.out.println("Enter any section to add");
try {
int course = in.nextInt();
if (course > 15 || course < 0) {
System.out.println("choose between 1 to 15 available courses only..try again");
continue;
}
found.addCourse(store.get(course));
System.out.println("Enrolled for the courses successfully");
} catch (MaximumCoursesExceeded e) {
//handled in MaximumCoursesExceeded class
} catch (DuplicateCourseException e2) {
//handled in DuplicateCourseException class
} catch (Exception e3) {
System.out.println(e3.getMessage());
}
break;
case 0:
quit = true;
System.out.println("All student & course details");
for (Student student : all) {
System.out.println(student.toString());
System.out.println("Corresponding courses Enrolled:");
if (student.getSections().length == 0)
continue;
for (CourseSection course : student.getSections()) {
if (course == null || course.getId() == null)
continue;
System.out.println(course.toString());
}
System.out.println("#########################################################");
}
break;
default:
System.out.println("Invalid choice");
}
} while (!quit);
System.out.println("Thank you for your time!!!");
}
}
class DataStore {
// Static attribute
private static DataStore singleton = null;
// Private default constructor
private DataStore() {
}
// Private section array attribute
private static CourseSection[] sections = new CourseSection[15];
private static File sectionFile = Paths.get(".", "resources", "Sections.txt").normalize().toFile();
public static DataStore getInstance() {
if (singleton == null) {
singleton = new DataStore();
Scanner fs;
// Try/catch
try {
fs = new Scanner(sectionFile);
for (int i = 0; i < sections.length; i++) {
sections[i] = new CourseSection(fs.next(), fs.next(), fs.next(), fs.next(), fs.next(), fs.next(),
fs.next(), fs.next());
}
} catch (FileNotFoundException e) {
System.out.print(e.getMessage());
}
}
return singleton;
}
// Getter
public static CourseSection[] getSections() {
return sections;
}
}
class Address extends Object {
// Attributes
private String street;
private String city;
private String state;
private String zip;
// Default constructor
public Address() {
}
// Designated constructor
public Address(String street, String city, String state, String zip) {
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
}
// Constructor
public Address(String state, String zip) {
this("", "", state, zip);
}
// Getter for street
public String getStreet() {
return street;
}
// Setter for street
public void setStreet(String street) {
this.street = street;
}
// Getter for city
public String getCity() {
return city;
}
// Setter for city
public void setCity(String city) {
this.city = city;
}
// Getter for state
public String getState() {
return state;
}
// Setter for state
public void setState(String state) {
this.state = state;
}
// Getter for zip
public String getZip() {
return zip;
}
// Setter for zip
public void setZip(String zip) {
this.zip = zip;
}
// toString method
@Override
public String toString() {
return "Address: " + street + " " + city + ", " + state + " " + zip;
}
}
// ---PERSON CLASS---
class Person {
// Attributes
private String firstName;
private String lastName;
private Address address;
// Default constructor
public Person() {
this("", "", new Address());
}
// Designated constructor
public Person(String firstName, String lastName, Address address) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}
// Constructor
public Person(String firstName, String lastName) {
this(firstName, lastName, new Address());
}
// Getter and Setter methods
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
// toString Method
@Override
public String toString() {
return firstName + " " + lastName + " ";
}
}
class Student extends Person {
// Attributes
String id;
CourseSection[] sections = new CourseSection[5];
private int numberEnrolled = 0;
// Default constructor
public Student() {
}
// Designated constructor
public Student(String id, String firstName, String lastName, Address address) {
super(firstName, lastName, address);
this.id = id;
for (int i = 0; i < sections.length; i++) {
sections[i] = new CourseSection();
}
}
// Getter and setter for ID
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
// Method to add a course
public void addCourse(CourseSection section) throws MaximumCoursesExceeded, DuplicateCourseException {
try {
for (int i = 0; i < numberEnrolled; i++) {
if (sections[i].getSubject().equals(section.getSubject())
&& sections[i].getNumber().equals(section.getNumber())) {
throw new DuplicateCourseException();
}
}
sections[numberEnrolled] = section;
numberEnrolled++;
} catch (ArrayIndexOutOfBoundsException e) {
throw new MaximumCoursesExceeded();
}
}
public CourseSection[] getSections() {
return sections;
}
// toString method
@Override
public String toString() {
return "Student:" + super.toString() + " " + "Student ID: " + id + " ";
}
@Override
public boolean equals(Object obj) {
// If the object is compared with itself then return true
if (obj == this) {
return true;
}
/*
* Check if obj is an instance of Student or not "null instanceof [type]" also
* returns false
*/
if (!(obj instanceof Student)) {
return false;
}
// typecast o to Student so that we can compare data members
Student stud = (Student) obj;
return this.getId().equalsIgnoreCase(stud.getId());
}
}
class MaximumCoursesExceeded extends Exception {
public MaximumCoursesExceeded() {
System.out.println("Maximum Courses Exceeded");
}
}
class DuplicateCourseException extends Exception {
public DuplicateCourseException() {
System.out.println("Duplicate Course Found");
}
}
class Course {
// Attributes
String subject;
String number;
String name;
// Default constructor
public Course() {
}
// Designated constructor
public Course(String subject, String number, String name) {
this.subject = subject;
this.number = number;
this.name = name;
}
// Constructor
public Course(String subject, String name) {
this(subject, "", name);
}
// Getter and setter methods
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// toString method
@Override
public String toString() {
return " " + subject + " " + number + " " + name + " ";
}
}
class CourseSection extends Course {
// Attributes
String id;
String days;
String startTime;
String building;
String room;
// Default Constructor
public CourseSection() {
}
// Constructor with all attributes
public CourseSection(String subject, String number, String name, String id, String days, String startTime,
String building, String room) {
super(subject, number, name);
this.id = id;
this.days = days;
this.startTime = startTime;
this.building = building;
this.room = room;
}
public CourseSection(Course c, String id, String days, String startTime, String building, String room) {
super(c.getSubject(), c.getNumber(), c.getName());
this.id = id;
this.days = days;
this.startTime = startTime;
this.building = building;
this.room = room;
}
// Getters and Setters
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDays() {
return days;
}
public void setDays(String days) {
this.days = days;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getBuilding() {
return building;
}
public void setBuilding(String building) {
this.building = building;
}
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
@Override
public String toString() {
return " CourseSection: " + super.toString() + id + " " + days + " " + startTime + " " + building + " " + room
+ " " + " ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.