Create classes named Book, Course
and Student
to perform simple course-selecting system (CSS). Student
has at least three overloaded constructors with different parameter list. Student
may select one or more Course
, and one Course
also has some members of Book
.
Examples:
import java.util.List;
import java.util.ArrayList;
//import java.util.Iterator;
public class CSS {
public static void main(String[] args) {
ArrayList<String> k = new ArrayList();
int n = args.length;
if(n == 0)
return ;
for(int i = 0; i < args.length; i++)
if(i != 0)
k.add(args[i]);
Student now = new Student(args[0], k);
now.print();
}
class Student{
public String sid;
public String sname;
public List<Course> courses;
public Student() {
}
public Student(String sid,ArrayList<String> cs){
this.sid=sid;
courses=new ArrayList();
for(String i:cs)
courses.add(new Course(i));
//System.out.println(this.sid+"select"+this.courses+"with book");
}
public Student(String sid,String sname,List<Course> courses) {
this.sid=sid;
this.sname=sname;
this.courses=courses;
}
public String getSname() {
return sname;
}
public void setSname(String s) {
this.sname=s;
}
public String getSid() {
return this.sid;
}
public void setSid(String s) {
this.sid=s;
}
public List<Course> getCourses(){
return courses;
}
public void setCourses(List<Course> s) {
this.courses=s;
}
public void print(){
System.out.print(sid);
if(!courses.isEmpty()){
System.out.print(" select ");
for(int i = 0; i < courses.size(); i++){
if(i != 0)
System.out.print("; ");
courses.get(i).print();
}
}
System.out.print(".");
}
}
class Course{
public String cname;
public List<Book> books;
public Course() {
}
public Course(String cname,List<Book> books) {
this.cname=cname;
this.books=books;
}
public Course(String cname) {
this.cname=cname;
}
public String getCname() {
return this.cname;
}
public void setCname(String s) {
this.cname=s;
}
public List<Book> getBooks(){
return this.books;
}
public void setBooks(List<Book> b) {
this.books=b;
}
public void print(){
System.out.print(cname);
if(!books.isEmpty()){
System.out.print(" with ");
for(int i = 0; i < books.size(); i++){
if(i != 0)
System.out.print(", ");
books.get(i).print();
}
}
}
}
class Book{
public String bname;
public Book() {
}
public Book(String s) {
this.bname=s;
}
public String getBname() {
return this.bname;
}
public void setBname(String s) {
this.bname=s;
}
void print(){
System.out.print(bname);
}
}
}
|