⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 agenda.java

📁 采用面向对象方法和Java语言开发一个基于命令行交互方式的议程(agenda)管理系统
💻 JAVA
字号:

 
package agenda;
/**
 *@author haha
 */
import java.io.*;
import java.util.StringTokenizer;



public class Agenda
{
	//这是会议数组,最多能保存100个会议
	private Meeting[] meetingarray=new Meeting[100];
	//这是用户数组,最多能保存100名用户
	private User[] userarray=new User[100];
	
	//这个参数记录会议数组最后一个非空元素的下标
	private int lastofmeeting=-1;
	//这个参数记录用户数组最后一个非空元素的下标
	private int lastofuser=-1;
	
	/**
	 *This is the constructer.
	 */
	public Agenda()
	{
		for(int i=0;i<100;i++)
		{
			meetingarray[i]=new Meeting(null,null,null,null,null);
		}
		for(int j=0;j<100;j++)
		{
			userarray[j]=new User(null,null);
		}
	}
	
	
	public boolean haveUser(String newusername)
	{
		int i;
		//如果已存在此用户,返回true
		for(i=0;i<=lastofuser;i++)      
		{
			if(userarray[i].getUserName().equals(newusername))
				return true;
		}
		return false;
	}
	
	
	public boolean haveMeeting(String newmeetingid)
	{
		int i;
		//如果已存在此会议,返回true
		for(i=0;i<=lastofmeeting;i++)
		{
			if(meetingarray[i].getmeetingid().equals(newmeetingid)) 
				return true;
		}
		return false;
	}
	
	
	public boolean pwIsRight(String newusername,String newpassword)
	{
		int i;
		if(haveUser(newusername))
		{
			//取出用户名在数组中下标
			for(i=0;i<=lastofuser;i++)
			{
				if(userarray[i].getUserName().equals(newusername))
					break;
			}
			//如果密码正确,返回true
			if(userarray[i].getPassword().equals(newpassword))
				return true;	
		}
		return false;
	}
	
	
	public boolean timeCompare(String timeone,String timetwo)
	{
		StringTokenizer st1=new StringTokenizer(timeone,":");
		StringTokenizer st2=new StringTokenizer(timetwo,":");
		
		//将参数timeone和timetwo改为int型
		int stOne=Integer.valueOf(st1.nextToken()).intValue();
		int stTwo=Integer.valueOf(st2.nextToken()).intValue();
		
		//如果timeone是在timetwo之后,返回true,否则返回false
		if(stOne>stTwo) return true;
		if(stOne<stTwo) return false;
		if(stOne==stTwo)
		{
			stOne=Integer.valueOf(st1.nextToken()).intValue();
			stTwo=Integer.valueOf(st2.nextToken()).intValue();
			if(stOne>=stTwo) return true;
			if(stOne<stTwo) return false;
		}
		return true;
	}
	
	
	public boolean IsTimeForm(String time)
	{
		StringTokenizer st=new StringTokenizer(time,":");
		//转换time格式为整型.
		int stOne=Integer.valueOf(st.nextToken()).intValue();
		int stTwo=Integer.valueOf(st.nextToken()).intValue();
		//如果time不是24小时制,返回false.
		if(stOne<0||stOne>23) return false;
		if(stTwo<0||stTwo>59) return false;
		//如果time是24小时制,返回true.
		return true;
	}
	
	
	public void Register(String newusername,String newpassword)
	{
		
		//如果此用户已注册,返回false
		if(haveUser(newusername)) {
			System.out.println("This user has been registered!");
			System.exit(1);
		}
		lastofuser++;
		//因为最多只能注册100个用户,如果再注册则返回false
		if(lastofuser==100) 
		{
			lastofuser--;
			System.out.println("There have been 100 users!");
			System.exit(1);
		}
		//新建一个用户和保存
		User us=new User(newusername,newpassword);
		userarray[lastofuser]=(User)us.clone();
		System.out.println("Register successfully!");
	}
	
	
	public void Add(String newusername,String newpassword,String other,
					String newstart,String newend,String title)
	{
		if(!this.IsTimeForm(newstart))
		{
			System.out.println("The time you input is not in correct form!");
			System.out.println("The form of time should be 24:00.");
			System.exit(1);
		}
		
		if(!this.IsTimeForm(newend))
		{
			System.out.println("The time you input is not in correct form!");
			System.out.println("The form of time should be 24:00.");
			System.exit(1);
		}
		
		if(!haveUser(newusername)) {
			System.out.println("This user hasn't been registered!");
			System.exit(1);
		}
		if(!pwIsRight(newusername,newpassword)) {
			System.out.println("Your password is not correct!");
			System.exit(1);
		}
		if(!haveUser(other)) {
			System.out.println("Your responder is not a legality user!");
			System.exit(1);
		}
		if(haveMeeting(title)){
			System.out.println("The name of your meeting has existed!");
			System.exit(1);
		}
		if(timeCompare(newstart,newend)) {
			System.out.println("Your meeting time is not correct!");
			System.exit(1);
		}
		
		int i;
		for(i=0;i<=lastofmeeting;i++)
		{
			//如果发起会议的用户和响应会议的用户在约定时间期间已安排了其他会议
			//则返回false
			if(meetingarray[i].getsponsor().equals(newusername)
				||meetingarray[i].getresponder().equals(newusername)
				||meetingarray[i].getsponsor().equals(other)
				||meetingarray[i].getresponder().equals(other))
			{
				if(timeCompare(meetingarray[i].getstart(),newstart))
				{
					if(timeCompare(newend,meetingarray[i].getstart()))
					{
						System.out.println("There have been 100 meetings!");
						System.exit(1);
					}		
				}else{
					if(timeCompare(meetingarray[i].getend(),newstart))
					{
						System.out.println("Your meeting time is conflicting with others!");
						System.exit(1);
					}
				}
			}
		}
		
				
		lastofmeeting++;
		if(lastofmeeting==100)
		{
			lastofmeeting--;
			System.out.println("There have been 100 meetings!");
			System.exit(1);
		}
		
		//新建一个会议并将它保存
		Meeting mt=new Meeting(newstart,newend,title,newusername,other);
		meetingarray[lastofmeeting]=(Meeting)mt.clone();
		System.out.println("Your meeting has been saved!");
		
	}
	
	
	public void Query(String username,String password,String newstart,String newend)
	{
		if(!this.haveUser(username)) 
		{
			System.out.println("This user hasn't been registered!");
			System.exit(1);
		}
		
		if(!this.pwIsRight(username,password))
		{
			System.out.println("Your password is not correct!");
			System.exit(1);
		}	
		
		if(!this.IsTimeForm(newstart))
		{
			System.out.println("The time you input is not in correct form!");
			System.out.println("The form of time should be 24:00.");
			System.exit(1);
		}
		
		if(!this.IsTimeForm(newend))
		{
			System.out.println("The time you input is not in correct form!");
			System.out.println("The form of time should be 24:00.");
			System.exit(1);
		}		
		
		//如果查询的时间与该会议的进行时间不相交,则返回false
		int i;
		int count=1;
		for(i=0;i<=this.lastofmeeting;i++)
		{
			if(this.meetingarray[i].getsponsor().equals(username)
				||this.meetingarray[i].getresponder().equals(username))
			{
				
             if(!((timeCompare(meetingarray[i].getstart(),newstart)&&timeCompare(meetingarray[i].getstart(),newend))||timeCompare(newstart,meetingarray[i].getend())))
	{
					System.out.println("meeting:");
					System.out.println("("+count+"):title="+this.meetingarray[i].getmeetingid()
								+",starttime="+this.meetingarray[i].getstart()
								+",endtime="+this.meetingarray[i].getend());
				}
			}
			count++;
		}
		if(count==1) 
			System.out.println("There is no meeting you want.");
		
	}
		
	
	
	
	public void Delete(String newusername,String newpassword,String title)
	{
		if(!haveUser(newusername)) {
			System.out.println("This user hasn't been registered!");;
			System.exit(1);
		}
		if(!pwIsRight(newusername,newpassword))  {
			System.out.println("Your password is not correct!");;
			System.exit(1);
		}
		if(!haveMeeting(title))  {
			System.out.println("This meeting doesn't exist!");;
			System.exit(1);
		}
		
		int i;
		//找到用户所要删除的会议,并将会议数组最后一个元组覆盖该会议
		//然后lastofmeeting减1,说明会议数组元素个数减少1
		for(i=0;i<=lastofmeeting;i++)
		{
			if(meetingarray[i].getmeetingid().equals(title))  break;
		}
		Meeting mt=new Meeting(meetingarray[lastofmeeting].getstart(),
								meetingarray[lastofmeeting].getend(),
								meetingarray[lastofmeeting].getmeetingid(),
								meetingarray[lastofmeeting].getsponsor(),
								meetingarray[lastofmeeting].getresponder());
		meetingarray[i]=(Meeting)mt.clone();
		this.lastofmeeting--;
		System.out.println("Delete successfully!");
		
	}
				
	
	
	
	public void Clear(String newusername,String newpassword)
	{
		if(!haveUser(newusername)){
			System.out.println("This user hasn't been registered!");
			System.exit(1);
		}
		if(!pwIsRight(newusername,newpassword)) {
			System.out.println("Your password is not correct!");
			System.exit(1);
		}
		
		int i;
		//找到与该用户名符合的会议,并将其所有属性置为null
		for(i=0;i<=lastofmeeting;i++)
		{
			if(meetingarray[i].getsponsor().equals(newusername)
				||meetingarray[i].getresponder().equals(newusername))
			{
				Meeting mt=new Meeting(null,null,null,null,null);
				meetingarray[i]=(Meeting)mt.clone();
			}
		}
		Meeting[] another=new Meeting[100];
		//count用来计算原数组中不为null的元素个数
		int count=0;
		//使用新一个数组,保存原数组中不为null的元素
		for(i=0;i<=lastofmeeting;i++)
		{
			if(meetingarray[i].getmeetingid()!=null)
			{
				Meeting mt=new Meeting(meetingarray[i].getstart(),
										meetingarray[i].getend(),
										meetingarray[i].getmeetingid(),
										meetingarray[i].getsponsor(),
										meetingarray[i].getresponder());
				another[count]=(Meeting)mt.clone();
				count++;
			}
		}
		//从新计算lastofmeeting
		lastofmeeting=count-1;
		//将新数组中的元素复制回到原数组中
		for(i=0;i<=lastofmeeting;i++)
		{
			Meeting mt=new Meeting(another[i].getstart(),
									another[i].getend(),
									another[i].getmeetingid(),
									another[i].getsponsor(),
									another[i].getresponder());
			meetingarray[i]=(Meeting)mt.clone();
		}
		System.out.println("Clear successfully!");
	}
	
	
	
	
	
			
		
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -