package com.imooc.colection;
/**
* 课程类
* @author Administrator
*
*/
public class Course {
private String id;
private String name;
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 Course(String id,String name){
this.id=id;
this.name=name;
}
/**
*
*/
public Course(){
}
public String toString() {
return "Course [id=" + id + ", name=" + name + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Course))
return false;
Course other = (Course) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
package com.imooc.colection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ListTest {
//创建一个list容器用于存放备选课程
private List coursesToSelect;
public List getCoursesToSelect() {
return coursesToSelect;
}
public void setCoursesToSelect(List coursesToSelect) {
this.coursesToSelect = coursesToSelect;
}
//创建构造器
public ListTest(){
this.coursesToSelect=new ArrayList();
}
public void testAdd(){
Course[] cr={new Course("1","大学英语"),new Course("1","高等数学"),new Course("3","大学语文"),new Course("4","大学物理"),new Course("5","离散数学")};
coursesToSelect.addAll(Arrays.asList(cr));
}
}
package com.imooc.colection;
import java.util.HashSet;
import java.util.Set;
/**
* 学生类
* @author Administrator
*
*/
public class Student {
private String id;
private String name;
private Set<Course> courses;
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 Set<Course> getCourses() {
return courses;
}
public void setCourses(Course courses) {
this.courses.add(courses);
}
//创建含参构造器
public Student(String id,String namen){
this.id=id;
this.name=name;
this.courses=new HashSet<Course>();
}
}
package com.imooc.colection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class SetTest {
/**
* 创建一容器coursesToSelect,用于盛放待选课程
*/
private List<Course>coursesToSelect;
public List<Course> getCoursesToSelect() {
return coursesToSelect;
}
public void setCoursesToSelect(List<Course> coursesToSelect) {
this.coursesToSelect = coursesToSelect;
}
/**
* 添加一构造器用于初始化coursesToSelect
*/
public SetTest(){
this.coursesToSelect=new ArrayList<Course>();
}
public void testAdd(){
Course[] cr={new Course("1","大学英语"),new Course("2","高等数学"),new Course("3","大学语文"),new Course("4","大学物理"),new Course("5","离散数学")};
coursesToSelect.addAll(Arrays.asList(cr));
System.out.print("现有如下课程待选:");
for(int i=0;i<cr.length;i++){
System.out.print(cr.getId()+">"+cr.getName()+" ");
}
System.out.println();
}
/**
* 创建选课方法
*/
public void testPick(Student student,SetTest it){
System.out.println("Welcome"+student.getName()+"to pick class");
Scanner input =new Scanner(System.in);
for(int i=0;i<2;i++){
i:
do{
System.out.print("请输入预选课程ID:");
String id=input.next();
for(Course cr:coursesToSelect){
if(cr.getId().equals(id)){
student.setCourses(cr);
break i;
}
}
System.out.println("输入错误");
}while(true);
}
System.out.print("你已经选择如下课程");
for(Course cr :student.getCourses()){
System.out.print(cr.getName()+" ");
}
}
public void testListContains(){
Scanner input=new Scanner(System.in);
Course[] cr2=new Course[2];
for(int i=0;i<2;i++){
cr2=new Course();
System.out.print("请输入查询的课程:");
String in=input.next();
System.out.println("你输入的是:"+in);
cr2.setName(in);
}
System.out.println("备选课程是否包括"+Arrays.toString(cr2)+":"+coursesToSelect.containsAll(Arrays.asList(cr2)));
}
public void testASetContens(Student stu){
System.out.println("检查已选课程");
Scanner input=new Scanner(System.in);
boolean check=false;
do{
System.out.println("请输入已选课程|");
Course cr=new Course(null,input.next());
boolean s=stu.getCourses().contains(cr);
System.out.println("已选课程是否有"+cr.getName()+":"+s);
System.out.println("是否继续呀?(是/不)");
if(input.next().equals("是"))
check=true;
else
check=false;
}while(check);
}
public static void main(String[] args) {
SetTest it=new SetTest();
it.testAdd();
Student stu=new Student("1","jack");
it.testPick(stu,it);
it.testListContains();
it.testASetContens(stu);
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
* 课程类
* @author Administrator
*
*/
class Course {
private String id;
private String name;
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 Course(String id,String name){
this.id=id;
this.name=name;
}
/**
*
*/
public Course(){
}
public String toString() {
return "Course [id=" + id + ", name=" + name + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Course))
return false;
Course other = (Course) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
class ListTest {
//创建一个list容器用于存放备选课程
private List coursesToSelect;
public List getCoursesToSelect() {
return coursesToSelect;
}
public void setCoursesToSelect(List coursesToSelect) {
this.coursesToSelect = coursesToSelect;
}
//创建构造器
public ListTest(){
this.coursesToSelect=new ArrayList();
}
public void testAdd(){
Course[] cr={new Course("1","大学英语"),new Course("1","高等数学"),new Course("3","大学语文"),new Course("4","大学物理"),new Course("5","离散数学")};
coursesToSelect.addAll(Arrays.asList(cr));
}
}
/**
* 学生类
* @author Administrator
*
*/
class Student {
private String id;
private String name;
private Set<Course> courses;
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 Set<Course> getCourses() {
return courses;
}
public void setCourses(Course courses) {
this.courses.add(courses);
}
//创建含参构造器
public Student(String id,String name){
this.id=id;
this.name=name;
this.courses=new HashSet<Course>();
}
}
public class SetTest {
/**
* 创建一容器coursesToSelect,用于盛放待选课程
*/
private List<Course>coursesToSelect;
public List<Course> getCoursesToSelect() {
return coursesToSelect;
}
public void setCoursesToSelect(List<Course> coursesToSelect) {
this.coursesToSelect = coursesToSelect;
}
/**
* 添加一构造器用于初始化coursesToSelect
*/
public SetTest(){
this.coursesToSelect=new ArrayList<Course>();
}
public void testAdd(){
Course[] cr={new Course("1","大学英语"),new Course("2","高等数学"),new Course("3","大学语文"),new Course("4","大学物理"),new Course("5","离散数学")};
coursesToSelect.addAll(Arrays.asList(cr));
System.out.print("现有如下课程待选:");
for(int i=0;i<cr.length;i++){
System.out.print(cr.getId()+">"+cr.getName()+" ");
}
System.out.println();
}
/**
* 创建选课方法
*/
public void testPick(Student student,SetTest it){
System.out.println("Welcome"+student.getName()+"to pick class");
Scanner input =new Scanner(System.in);
for(int i=0;i<2;i++){
i:
do{
System.out.print("请输入预选课程ID:");
String id=input.next();
for(Course cr:coursesToSelect){
if(cr.getId().equals(id)){
student.setCourses(cr);
break i;
}
}
System.out.println("输入错误");
}while(true);
}
System.out.print("你已经选择如下课程");
for(Course cr :student.getCourses()){
System.out.print(cr.getName()+" ");
}
}
public void testListContains(){
Scanner input=new Scanner(System.in);
Course[] cr2=new Course[2];
for(int i=0;i<2;i++){
cr2=new Course();
System.out.print("请输入查询的课程:");
String in=input.next();
System.out.println("你输入的是:"+in);
cr2.setName(in);
}
System.out.println("备选课程是否包括"+Arrays.toString(cr2)+":"+coursesToSelect.containsAll(Arrays.asList(cr2)));
}
public void testASetContens(Student stu){
System.out.println("检查已选课程");
Scanner input=new Scanner(System.in);
boolean check=false;
do{
System.out.println("请输入已选课程|");
Course cr=new Course(null,input.next());
boolean s=stu.getCourses().contains(cr);
System.out.println("已选课程是否有"+cr.getName()+":"+s);
System.out.println("是否继续呀?(是/不)");
if(input.next().equals("是"))
check=true;
else
check=false;
}while(check);
}
public static void main(String[] args) {
SetTest it=new SetTest();
it.testAdd();
Student stu=new Student("1","jack");
it.testPick(stu,it);
it.testListContains();
it.testASetContens(stu);
}
}
屏幕快照 2016-08-23 上午11.35.41.png (75.12 KB, 下载次数: 36)
坚持努力wbj 发表于 2016-8-23 11:55
可我现在两个电脑运行它都这样
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |