NOTE:
I can’t use any advanced methods or features java might have to solve this. I’m limited to whatever’s in the code mostly and I’m aware the code could be shorter and cleaner but for me, as a beginner the shorter the code is the harder for me to understand hence the reason so many methods and non-advanced for loops, etc…
For my newEnrolment()
method I figured out how to go with the double loop to loop for what I needed which was "finding a specific attribute from an object from a list of objects in an array e.g students, subjects, enrolments"
now the next thing is validity check in which I am supposed to create a new enrolment which takes a student number and subject code from user checks against the attributes of those objects from the ArrayList and only enroll them if the student no and subject code already exists in the ArrayList’s objects attributes… I know I’m making it complicated cause I don’t know any other way to explain that’s why I added my whole class…
for (int i=0 ; i<subjects.size() ; i++){
if (subjects.get(i).getCode().equals(newsub) )
{
enrolments.add(enrolment1);
System.out.println("A new enrolment for the student "+newstu+" on the subject "+newsub+" has been n" +
"added to the list.");
}
else if (i == subjects.size()-1)
{
System.out.println("Invalid subject code");
}
}
This loop does the validity check for subjects or for students but doesn’t work for both because both have different amounts of values in their ArrayList.
Here I tried putting both my required conditions inside the if() statement but the outer loop can only take its conditions from one ArrayList so I’m stuck at this
for (int i=0 ; i<subjects.size() ; i++){
if (subjects.get(i).getCode().equals(newsub) && students.get(i).getNumber() == newstu)
{
enrolments.add(enrolment1);
System.out.println("A new enrolment for the student "+newstu+" on the subject "+newsub+" has been n" +
"added to the list.");
}
else if (i == subjects.size()-1)
{
System.out.println("Invalid subject code");
}
}
I have the same issue in switch() statement where i want to do sth like if the user inputs something other than 1-8 it says "input invalid" but again not sure what kinda statemnet to attach with switch() to make it work.
Been stuck on this for a while now tried variations of loops i could think of no luck yet
So two questions
One : is regarding the validity check of both inputs while doing exactly what the for loop is doing now
Two : using validity check on switch() incase user input isn’t 1-8
Here is the code of my Main class so far Everythink works fine except the problem i mentioned with validity check
THANKS HEAPS!!!
import java.sql.Array;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.*;
public class StudentEnrolmentSystem {
private ArrayList<Student> students;
private ArrayList<Subject> subjects;
private ArrayList<Enrolment> enrolments;
public StudentEnrolmentSystem() {
students = new ArrayList<Student>();
subjects = new ArrayList<Subject>();
enrolments = new ArrayList<Enrolment>();
}
public static void main(String[] args) {
StudentEnrolmentSystem ses = new StudentEnrolmentSystem();
ses.data();
while (true) {
ses.displaymenu();
}
}
public void displaySTU() {
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i).toString());
}
}
public void displaySUB() {
for (int i = 0; i < subjects.size(); i++) {
System.out.println(subjects.get(i).toString());
}
}
public void displayENR() {
for (int i = 0; i < enrolments.size(); i++) {
System.out.println(enrolments.get(i).toString());
}
}
public void searchSTU() {
Scanner Input = new Scanner(System.in);
System.out.println("Enter Student number :");
int input = Input.nextInt();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getNumber() == input) {
System.out.println(students.get(i).toString());
}
}
}
public void searchSUB(){
Scanner Input = new Scanner(System.in);
System.out.println("Enter Subject code :");
String input = Input.nextLine();
for (int i=0 ; i<subjects.size() ; i++){
if (subjects.get(i).getCode().equals(input)){
System.out.println(subjects.get(i).toString());
}
}
}
public void findEnrolment(){
Scanner Input = new Scanner(System.in);
System.out.println("Enter Student number :");
int input = Input.nextInt();
for (int i = 0; i < enrolments.size(); i++) {
if (enrolments.get(i).getNumber() == input) {
System.out.println(enrolments.get(i).toString());
}
}
}
public void newEnrolment(){
Scanner Input = new Scanner(System.in);
System.out.println("Input a student number:");
int newstu = Input.nextInt();
Scanner Input2 = new Scanner(System.in);
System.out.println("Input a subject code:");
String newsub = Input2.nextLine();
Enrolment enrolment1 = new Enrolment(newstu,newsub,"26/07/2022");
for (int i=0 ; i<subjects.size() ; i++){
if (subjects.get(i).getCode().equals(newsub) )
{
enrolments.add(enrolment1);
System.out.println("A new enrolment for the student "+newstu+" on the subject "+newsub+" has been n" +
"added to the list.");
}
else if (i == subjects.size()-1)
{
System.out.println("Invalid subject code");
}
}
}
public void displaymenu(){
Scanner Input = new Scanner(System.in);
System.out.println("1. Display all students");
System.out.println("2. Display all subjects");
System.out.println("3. Display all enrolments");
System.out.println("4. Find a student");
System.out.println("5. Find a subject");
System.out.println("6. Find a student's enrolments");
System.out.println("7. Add an enrolment");
System.out.println("8. Exit");
int input = Input.nextInt();
switch (input) {
case 1:
displaySTU();
break;
case 2:
displaySUB();
break;
case 3:
displayENR();
break;
case 4:
searchSTU();
break;
case 5:
searchSUB();
break;
case 6:
findEnrolment();
break;
case 7:
newEnrolment();
break;
case 8:
System.exit(-1);
break;
}
}
public void data () {
Subject CSCI213 = new Subject("CSCI213", "Java Programming & Object Oriented Design", 6,
"This subject provides an introduction to the Java language and some of its standard " +
"class libraries, you will have experience with object oriented design " +
"and implementation techniques",
"CSCI124 or CSCI121 or CSCI192", 3, 2);
Subject CSCI124 = new Subject("CSCI124", "Applied Programming", 6,
"This subject develops a thorough understanding of program design using data structures. " +
"It extends CSCI114 and presents pointers, dynamic memory management and exception handling",
"CSCI114 & CSCI103 or CSCI111 & CSCI103", 4, 2);
Subject CSCI235 = new Subject("CSCI235", "Database system", 6,
"This subject investigates three major areas of modern database systems:" +
" 1. design of relational databases 2. programming of relational databases",
"CSIT115", 3, 2);
Subject CSIT115 = new Subject("CSIT115", "Data management and security", 6,
"The subject investigates three major areas of modern data management systems:" +
" data modelling, data processing, and data security.",
"", 2, 2);
Subject CSIT111 = new Subject("CSIT111", "Programming Fundamentals", 6,
"The broad aim of this subject is to develop in students an understanding " +
"of the fundamental principles of programming.",
"", 2, 2);
Subject CSIT121 = new Subject("CSIT121", "Object oriented design and programming", 6,
"The aims of this subject are to consolidate and extend student's knowledge and skills" +
" in structured programming and to develop their understanding and " +
"practice of object oriented programming.",
"CSIT111 OR ENGG100", 2, 2);
Subject CSCI251 = new Subject("CSCI251", "Advanced programming", 6,
"This subject develops a thorough understanding of advanced programming features, " +
"and how to implement them in modern C++.",
"CSIT121", 3, 2);
subjects.add(CSCI213);
subjects.add(CSCI124);
subjects.add(CSCI235);
subjects.add(CSIT115);
subjects.add(CSIT111);
subjects.add(CSIT121);
subjects.add(CSCI251);
ArrayList<String> majors = new ArrayList<String>();
majors.add("Artificial Intelligence and Bing Data");
majors.add("Cyber Security");
Undergraduate s1 = new Undergraduate(100100, "Albert", "13/10/1965", "[email protected]",
"12 Robert street Woonona NSW 2517", "12345678", "Bachelor of CS", majors);
Undergraduate s2 = new Undergraduate(100110, "Alvin", "13/10/1977", "[email protected]",
"56 Marlo road Wollongong NSW 2500", "11223344", "Bachelor of CS", majors);
Undergraduate s3 = new Undergraduate(100120, "Alice", "17/06/1973", "[email protected]",
"43 Collaery road Russell Vale NSW 2517", "12345677", "Bachelor of CS", majors);
Undergraduate s4 = new Undergraduate(100150, "Bob", "02/07/1960", "[email protected]",
"23 Kendall street Wollongong NSW 2500", "12345688", "Bachelor of CS", majors);
Undergraduate s5 = new Undergraduate(100200, "Carl", "02/02/1967", "[email protected]",
"44 Mount Keira road West Wollongong NSW 2500", "21345687", "Bachelor of IT", majors);
Undergraduate s6 = new Undergraduate(100250, "Douglass", "14/04/1983", "[email protected]",
"78 Uralba street West Wollongong NSW 2500", "010123456", "Bachelor of CS", majors);
Undergraduate s7 = new Undergraduate(100101, "Peter", "13/11/1976", "[email protected]",
"77 Gipps road Wollongong NSW 2500", "0102123456", "Bachelor of IT", majors);
Undergraduate s8 = new Undergraduate(100103, "Ami", "12/09/1985", "[email protected]",
"51 Mackie street Coniston NSW 2500", "0242211234", "Bachelor of IT", majors);
Postgraduate s9 = new Postgraduate(100107, "Wendy", "12/09/1988", "[email protected]",
"41 Wall street Wollongong NSW 2500", "0281234567", "Master of CS", Type.CourseWork);
Postgraduate s10 = new Postgraduate(100109, "Michael", "12/09/1990", "[email protected]",
"112 Smith road Wollongong NSW 2500", "0242201234", "Master of CS", Type.CourseWork);
Postgraduate s11 = new Postgraduate(100125, "Angela", "20/11/1990", "[email protected]",
"23 Gibsons road Figtree NSW 2525", "0201123456", "Master of IT", Type.CourseWork);
Postgraduate s12 = new Postgraduate(100105, "Robert", "15/01/1986", "[email protected]",
"66 Risely road Figtree NSW 2525", "0202213123", "Master of IT", Type.CourseWork);
Postgraduate s13 = new Postgraduate(100136, "Aban", "15/01/1990", "[email protected]",
"187 Princes Highway Wollongong NSW 2500", "0103123456", "Bachelor of IT", Type.Reserch);
Postgraduate s14 = new Postgraduate(100187, "Eadger", "07/04/1986", "[email protected]",
"73 Ocean street Wollongong NSW 2500", "0104123321", "Master of Philosophy", Type.Reserch);
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
students.add(s5);
students.add(s6);
students.add(s7);
students.add(s8);
students.add(s9);
students.add(s10);
students.add(s11);
students.add(s12);
students.add(s13);
students.add(s14);
}
}
3
Answers
Thank you for all the comments and answers but my question was limited to the methods I could use to solve this and this is the double loop that worked for me thanks to one of the comments!
To summarize from my understanding this loop goes through a specific attribute of an object comparing it to user input and it does that for every object in the arraylist findind the one that matches.. Hope this helps someone else too
Cheers!
I think, you can use streams and filter on that something like this
change ArrayList to Map<Integer,Student>
set key by student number :
get student by key -> student number :