package aa;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.stream.IIOByteBuffer;
/*例外:周一第一节大课为a同学课程:具体时间为7-15周,只在单周上课。
周五第四节大课为a同学课程:具体时间为1-6周,只在双周上课。
周五第四节大课为b同学课程:具体时间为4-14周,只在双周上课。
但在dis(去除重复时),程序出现错误,合并不了。
例如:周一 第一大节people应为b。*/
/*有三个同学,分别为abc.
方法 : 利用容器思想,用户输入非数字异常处理。
注:table为原课表,table2为无课课表,有五大节课课,输入1代表1-2节,输入2代表3-5节,输入3代表6-7节,输入4代表8-10节,输入5代表11-12节。
0代表单双周,1代表单周,2代表双周。
步骤:
1.原课表保存在table中,先让用户选择要查询的周数,分单双周,单双周的课程有所不同,将本周的课表的无课课表保存在table2中,并将table2打印出来
2.在table2的基础上让用户选择要查询的星期数,将此保存在table3中,并将table3打印出来
3.在table3的基础上让用户选择要查询的具体时间,将此保存在table4中,并将table4打印出来
4.循环1,2,3步骤,在此之前需将每个容器清空。退出程序为0
*/
public class query{
public static void main(String args[]){
while(true){
try{
course.selectWeek();
}catch(InputMismatchException e){ //用户输入非数字处理
System.out.println("您输入的不是数字,请重新输入:");
continue;
}
}
}
}
class course{
static ArrayList<course> table=new ArrayList<course>();//原所有课表
static ArrayList<course> table2=new ArrayList<course>();//原所有无课课表
static ArrayList<course> table5=new ArrayList<course>(); //table5:合并之前 原 本周 无课课表
static ArrayList<course> table6=new ArrayList<course>(); //table6:合并之后
static ArrayList<course> table3=new ArrayList<course>(); //table3:用来存储查出的星期
static ArrayList<course> table4=new ArrayList<course>(); //table4:用来存储查出的确切课程
String week;
String time;
String people;
static int cStart;//课程开始周
static int cEnd;//课程结束周
int calcu; //1:单周上课,2:双周上课 ,0:单双周
course(String w, String t,String p){
week = w;
time=t;
people=p;
cStart=1;
cEnd=17;
calcu=0;
}
course(String w, String t,String p,int cs,int ce,int cal){
week = w;
time=t;
people=p;
cStart=cs;
cEnd=ce;
calcu=cal;
}
static void init1(){
table.add(new course("周一","1-2","c"));
table.add(new course("周一","1-2","a",7,15,1));
table.add(new course("周一","3-5","abc"));
table.add(new course("周一","11-12","b"));
table.add(new course("周二","1-2","b"));
table.add(new course("周二","3-5","ac"));
table.add(new course("周二","6-7","a"));
table.add(new course("周三","1-2","ac"));
table.add(new course("周三","6-7","ab"));
table.add(new course("周三","8-10","bc"));
table.add(new course("周三","11-12","c"));
table.add(new course("周四","1-2","c"));
table.add(new course("周四","3-5","a"));
table.add(new course("周四","6-7","bc"));
table.add(new course("周四","8-10","ab"));
table.add(new course("周四","11-12","ac"));
table.add(new course("周五","1-2","ac"));
table.add(new course("周五","3-5","c"));
table.add(new course("周五","6-7","abc"));
table.add(new course("周五","8-10","a",1,6,2));
table.add(new course("周五","8-10","b",4,15,2));
table.add(new course("周五","11-12","b"));
}
static void show(ArrayList<course> tab){
Iterator<course> i=tab.iterator();
while(i.hasNext()){
course j=i.next();
System.out.printf("%-6s %-5s %-5s\n",j.week,j.time,j.people);
}
}
static void revise(ArrayList<course> ttable,ArrayList<course> utable){ //将ttable的无课课表保存到utable中
Iterator<course>m=ttable.iterator();
while(m.hasNext()){
course n=m.next();
switch(n.people){
case "a":
n.people="bc";
utable.add(n);
//System.out.printf("%-6s %-5s %-5s\n",n.week,n.time,n.people);
break;
case "b":
n.people="ac";
utable.add(n);
//System.out.printf("%-6s %-5s %-5s\n",n.week,n.time,n.people);
break;
case "c":
n.people="ab";
utable.add(n);
///System.out.printf("%-6s %-5s %-5s\n",n.week,n.time,n.people);
break;
case "ab":
n.people="c";
utable.add(n);
//System.out.printf("%-6s %-5s %-5s\n",n.week,n.time,n.people);
break;
case "ac":
n.people="b";
//System.out.printf("%-6s %-5s %-5s\n",n.week,n.time,n.people);
break;
case "bc":
n.people="a";
utable.add(n);
//System.out.printf("%-6s %-5s %-5s\n",n.week,n.time,n.people);
break;
case "abc":
n.people="nobody";
utable.add(n);
//System.out.printf("%-6s %-5s %-5s\n",n.week,n.time,n.people);
break;
}
}
}
static void selectWeek(){ //按周数查询
course.init1();
course.revise(table,table2);
int read = 0;
while(true) {
try{//异常处理
System.out.println("请输入要查询的周(若要退出请输入0):");
Scanner reader=new Scanner(System.in);//用户输入
read=(int)reader.nextInt();
if(read<0||read>18){ //用户输入周数越界处理:重新输入。
System.out.println("您输入周数越界,请重新输入:");
continue;
}
}catch(InputMismatchException e){ //用户输入非数字处理
System.out.println("您输入的不是数字,请重新输入:");
continue;
}
if (read==0) System.exit(0);
else if(read%2!=0&&(read>=course.cStart&&read<=course.cEnd)){ //单周
System.out.println("第"+read+"周无课课表(注:未查到为abc):");
Iterator<course> ii=table2.iterator();
while(ii.hasNext()){
course jj=ii.next();
if(jj.calcu==0||jj.calcu==1){
table5.add(jj);
}
}
dis(table5, table6);
}
else if(read%2==0&&(read>=course.cStart&&read<=course.cEnd)){ //双周
System.out.println("第"+read+"周无课课表(注:未查到为abc):");
Iterator<course> ii=table2.iterator();
while(ii.hasNext()){
course jj=ii.next();
if(jj.calcu==0||jj.calcu==2){
table5.add(jj);
}
}
dis(table5, table6);
}
break;
}
selectTime();
table.removeAll(table);
table2.removeAll(table2);
table3.removeAll(table3);
table4.removeAll(table4);
table5.removeAll(table5);
table6.removeAll(table6);
}
static void dis(ArrayList<course> tab,ArrayList<course> tab1){//两个课程表作比较合并(当在同一时间段时)
Iterator<course> i=tab.iterator();
Iterator<course> j=tab.iterator();
while(i.hasNext()){
while(j.hasNext()){
course ii=i.next();
course jj=j.next();
if(ii.week.equals(jj.week)&&ii.time.equals(jj.time)){// 两表中相同内容,直接添加在tab1中
if(ii.people.equals(jj.people)){
tab1.add(ii);
}
else{
for(int m=0;m<jj.people.length();m++){//两表中星期相同,具体时间相同,将people改正,合并。
char c=jj.people.charAt(m);//第二个表jj的每个字符,转化为字符串。
String s=Character.toString(c);
Pattern p=Pattern.compile(s);
Matcher mat=p.matcher(ii.people);//在ii.people对字符串进行模式匹配.
if(mat.find()) ii.people=s;
tab1.add(ii); //匹配改正people并添加在tab1中
}
}
}
else table.add(ii);
}
}
show(tab1);//将合并之后的表打印出来。
}
static void selectTime(){ //按星期查询
int read=0;
while(true) { //异常处理
try{
System.out.println("请输入要查询的星期数(若要退出请输入0):");
Scanner reader=new Scanner(System.in);//用户输入
read=(int)reader.nextInt();
if(read<0||read>7){ //用户输入周数越界处理:重新输入。
System.out.println("您输入星期数越界,请重新输入(若要退出请输入0):");
continue;
}
}catch(InputMismatchException e){ //用户输入非数字处理
System.out.println("您输入的不是数字,请重新输入:");
continue;
}
Iterator<course> i=table5.iterator();
if(read>0&&read<8) System.out.println("星期"+read+"无课课表为:");
switch(read){
case 1: //1代表周一
while(i.hasNext()){
course readw=i.next();
if(readw.week.equals("周一")) table3.add(readw);
}
break;
case 2:
while(i.hasNext()){
course readw=i.next();
if(readw.week.equals("周二")) table3.add(readw);
}
break;
case 3:
while(i.hasNext()){
course readw=i.next();
if(readw.week.equals("周三")) table3.add(readw);
}
break;
case 4:
while(i.hasNext()){
course readw=i.next();
if(readw.week.equals("周四")) table3.add(readw);
}
break;
case 5:
while(i.hasNext()){
course readw=i.next();
if(readw.week.equals("周五")) table3.add(readw);
}
break;
case 6:
while(i.hasNext()){
course readw=i.next();
if(readw.week.equals("周六")) table3.add(readw);
}
break;
case 7:
while(i.hasNext()){
course readw=i.next();
if(readw.week.equals("周日")) table3.add(readw);
}
break;
case 0:
System.exit(0);
break;
}
System.out.println("注:未查到为“abc”");
show(table3);
break;
}
selectCourse(); //在某天查询具体第几节课课程表
}
static void selectCourse(){
int read = 0;
while(true) {
try{//异常处理
System.out.println("要查询第几大节课?(若要退出请输入0):");
Scanner reader=new Scanner(System.in);//用户输入
read=(int)reader.nextInt();
if(read<0||read>6){ //用户输入具体时间数越界处理:重新输入。
System.out.println("您没有本节课,请重新输入:");
continue;
}
}catch(InputMismatchException e){ //用户输入非数字处理
System.out.println("您输入的不是数字,请重新输入:");
continue;
}
Iterator<course> i=table3.iterator();
if(read>0&&read<6) System.out.println("第"+read+"大节课无课课表为:");
switch(read){
case 1: //1-2为第一大节,...
while(i.hasNext()){
course readw=i.next();
if(readw.time.equals("1-2")) table4.add(readw);
}
break;
case 2:
while(i.hasNext()){
course readw=i.next();
if(readw.time.equals("3-5")) table4.add(readw);
}
break;
case 3:
while(i.hasNext()){
course readw=i.next();
if(readw.time.equals("6-7")) table4.add(readw);
}
break;
case 4:
while(i.hasNext()){
course readw=i.next();
if(readw.time.equals("8-10")) table4.add(readw);
}
break;
case 5:
while(i.hasNext()){
course readw=i.next();
if(readw.time.equals("11-12")) table4.add(readw);
}
break;
case 0:
System.exit(0);
break;
}
System.out.println("注:未查到为“abc”");
show(table4);
break;
}
}
}