import java.util.ArrayList; import java.util.List; public class Student { private String id; private String name; public List<kecheng> yxkc; public Student (String id,String name){ this.id=id; this.name=name; this.yxkc=new ArrayList<kecheng>(); } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
public class kecheng {
private String id;
private String name;
public kecheng(String id,String name)
{this.id=id;
this.name=name;
}
public kecheng(){}//为了继承能实现,所以手动增加 一个无参构造器;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Test {
public List<kecheng> allkc;
public Test(){
this.allkc=new ArrayList<kecheng>();
}
/*public void testadd(){
kecheng k1=new kecheng("1","数据结构");
allkc.add(k1);
kecheng s1=(kecheng)allkc.get(0);
System.out.println("课程《"+s1.getName()+"》添加成功,课程号为"+s1.getId());
}*/
public void testadd(){
System.out.println("请输入要添加的课程序号和课程名");
Scanner s=new Scanner(System.in);
String a,b;
a=s.next();
b=s.next();
kecheng k1=new kecheng(a,b);
allkc.add(k1);
System.out.println("课程添加成功!");
}
public void foreach()
{
for(kecheng c:allkc)
{System.out.println("课程《"+c.getName()+"》,课程号为"+c.getId());}
}
public void choose()
{
System.out.println("请输入你的学号和姓名。");
Scanner nn=new Scanner(System.in);
String num=nn.next();
String name=nn.next();
Student s=new Student(num,name);
for(int i=0;i<2;i++){
System.out.println("请输入你要选择的课程的课序号:");
String n=nn.next();
for(kecheng c:allkc)
{
if(c.getId().equals(n))
{
s.yxkc.add(c);
}
}
}
Iterator <kecheng> ic= s.yxkc.iterator();
System.out.println("你选取了如下课程:");
while(ic.hasNext())
{
kecheng syx=ic.next();
System.out.println("课程《"+syx.getName()+"》,课程号为"+syx.getId());
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t=new Test();
System.out.println("请选择:0 教师,1 学生");
Scanner in=new Scanner(System.in);
int bool=in.nextInt();
if(bool==0){
System.out.println("请输入要添加的课程的数量:");
Scanner sadmin=new Scanner(System.in);
int i=sadmin.nextInt();
for(int k=0;k<i;k++)
{
t.testadd();
}
System.out.println("供选择的课程有:");
t.foreach();
t.choose();
}
else if(bool==1){
t.choose();
}else
{System.out.println("输入异常 请检查后重新输入!");}
}//main函数的
}
如果感觉不错,可以放到“ 我的社区->作品”那里哦