Program to Create arraylists for Courses and Students using OOP concepts with getter and setter methods

Course.java

public class Course {
private String id;
private String name;

public Course() { }

public Course(String id, String name) {
this.id = id;
this.name = name;
}

public void setId(String id) {
this.id = id;
}

public String getId() {
return this.id;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

@Override
public String toString() {
return this.id;
}
}


Student.java

public class Student {
private int rollNo;
private String name;
private Course course;
private String address;
private int phoneNumber;

/**
* Default Constructor
*/
public Student() { }

/**
* Overloaded Constructor
*/
public Student(int rollNo, String name, String address, int phoneNumber, Course course) {
this.rollNo = rollNo;
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
this.course = course;
}

/**
* Setter for rollNo
*/
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}

/**
* Getter for rollNo
*/
public int getRollNo() {
return this.rollNo;
}

/**
* Setter for name
*/
public void setName(String name) {
this.name = name;
}

/**
* Getter for name
*/
public String getName() {
return this.name;
}

/**
* Setter for address
*/
public void setAddress(String address) {
this.address = address;
}

/**
* Getter for address
*/
public String getAddress() {
return this.address;
}

/**
* Setter for phoneNumber
*/
public void setPhoneNumber(int phoneNumber) {
this.phoneNumber = phoneNumber;
}

/**
* Getter for phoneNumber
*/
public int getPhoneNumber() {
return this.phoneNumber;
}

public void setCourse(Course course) {
this.course = course;
}

public Course getCourse() {
return this.course;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
return sb.append(this.rollNo)
.append(" ")
.append(this.name)
.append(" ")
.append(this.address)
.append(" ")
.append(this.phoneNumber)
.append(" ")
.append(this.course).toString();
}

}


Main.java

import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
ArrayList<Course> courses = new ArrayList<>();
ArrayList<Student> students = new ArrayList<>();

// Add few courses in coures ArrayList
courses.add(new Course("BCA", "Bachelors of Computer Applications"));
courses.add(new Course("BBA", "Bachelors of Business Administration"));
courses.add(new Course("B.Tech", "Bachelors of Technology"));

// Add few students to students ArrayList
students.add(new Student(1, "Ranvir Singh", "", 183268, courses.get(1)));
students.add(new Student(1001, "Satkar Singh", "", 123612, courses.get(0)));
students.add(new Student(900101, "Khatta Singh", "Jabalpur", 1237612, courses.get(2)));
students.add(new Student(10023, "Shyam Lal", "", 12281182, courses.get(0)));
students.add(new Student(51212, "Rukhmani Kumari", "", 9237849, courses.get(1)));
students.add(new Student(21321, "Kavitha Krishnamurthi", "", 83472, courses.get(0)));
students.add(new Student(11232, "John Doe", "New Jersy", 289732, courses.get(0)));

// Iterate students
for (Student s : students) {
System.out.println(s);
}
}

}

Comments

Popular posts from this blog

Zeller's Congruence

Property Event-Delegation

Method with variable arguments