Advertisement
Guest User

EventPlanner

a guest
Feb 10th, 2012
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.util.Arrays;
  4.  
  5. public class Main
  6. {
  7.  
  8.     private class Event implements Comparable<Event>
  9.     {
  10.         public int time;
  11.         public String event;
  12.         public Event(int time, String event){
  13.             this.time = time;
  14.             this.event = event;
  15.         }
  16.        
  17.         public String toString(){
  18.             return time+":00 - "+event;
  19.         }
  20.        
  21.         public int compareTo(Event e){
  22.             if(this.time == e.time){
  23.                 return 0;
  24.             }
  25.             return (this.time < e.time ? -1 : 1);
  26.         }
  27.     }
  28.     private class EventPlanner
  29.     {
  30.         ArrayList<Event> events = new ArrayList<>();
  31.        
  32.         public void addEvent(int time,String event){
  33.             if(time > 0 && time < 25){
  34.                 Event e = new Event(time,event);
  35.                 events.add(e);
  36.             } else {
  37.                 System.out.println("Time has to be between 0 and 25");
  38.             }
  39.         }
  40.        
  41.         public void removeEvent(int time){
  42.             for(int a = 0;a < events.size();++a){
  43.                 if(events.get(a).time == time){
  44.                     events.remove(a);
  45.                     break;
  46.                 }
  47.             }
  48.         }
  49.        
  50.         public void removeEvent(String event){
  51.             for(int a = 0;a < events.size();++a){
  52.                 if(events.get(a).event.equalsIgnoreCase(event)){
  53.                     events.remove(a);
  54.                 }
  55.             }
  56.         }
  57.        
  58.         public String toString(){
  59.             StringBuilder sb = new StringBuilder(events.size()*20);
  60.             Event[] array = events.toArray(new Event[0]);
  61.             Arrays.sort(array);
  62.             for(Event e : array){
  63.                 sb.append(e+"\n");
  64.             }
  65.             return sb.toString();
  66.         }
  67.        
  68.     }
  69.     public void run(){
  70.         System.out.println("For help, type 'help'");
  71.         Scanner sc = new Scanner(System.in);
  72.         EventPlanner ep = new EventPlanner();
  73.         String input = null;
  74.         String[] split = null;
  75.         while(!"q".equals(input)){
  76.             input = sc.nextLine();
  77.             split = input.split(" ");
  78.             System.out.println("");
  79.             switch(split[0].toLowerCase()) {
  80.                 case "add":{
  81.                     ep.addEvent(Integer.parseInt(split[1]),split[2]);
  82.                     break;
  83.                 }
  84.                 case "view":{
  85.                     System.out.println(ep);
  86.                     break;
  87.                 }
  88.                 case "remove":{
  89.                     if(split[1].matches("[0-2]?[0-9]")){
  90.                         ep.removeEvent(Integer.parseInt(split[1]));
  91.                     } else {
  92.                         ep.removeEvent(split[1]);
  93.                     }
  94.                     break;
  95.                 }
  96.                 case "help":{
  97.                     System.out.println("Command:\tUsage:\t\t\t\tDescription:\nAdd\t\tadd <hour> <description>\tAdd a new event\nremove\t\tremove <hour>\t\t\tRemoves the event\nremove\t\tremove <description>\t\tRemoves the event\nview\t\tview\t\t\t\tShows current tasks\nhelp\t\thelp\t\t\t\tPrints this screen");
  98.                     break;
  99.                 }
  100.             }
  101.         }
  102.     }
  103.     public static void main(String[] args){
  104.         new Main().run();
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement