All pastes #250617 Raw Edit

Something

public text v1 · immutable
#250617 ·published 2006-11-17 03:30 UTC
rendered paste body
import java.lang.Comparable;

/**
Creates a class Appointment
**/
public class Appointment implements Comparable<Appointment>{
	public Appointment() {
	}
	/**
			Appointment Cons. with String day (yyyy/mm/dd) String descripition, double start(7.00) and double end(8.00)
	**/
	public Appointment(String d, String de, double st, double et) {
		day = d;
		description = de;
		start = st;
		end = et;
	}
	/**
	Sets Day		
	**/
	public void setDay(String d) {
		day = d;
	}
	/**
	gets Day
	**/
	public String getDay() {
		return day;
	}
	/**
	set Descripition
	**/
	public void setDescription(String de) {
		description = de;
	}
	/**
	gets Descripition		
	**/
	public String getDescription() {
		return description;
	}
	/**
	sets Start time
	**/
	public void setStart(int st) {
		start = st;
	}
	/**
	gets Start time
	**/
	public double getStart() {
		return start;
	}
	/**
	sets End time
	**/
	public void setEnd(int et) {
		end = et;
	}
	/**
	get End time
	**/
	public double getEnd() {
		return end;
	}
	/**
	returns a toString for Appointment		
	**/
	public String toString() {
		return "Appointment[Day: " + day + " Description: " + description
				+ " Start Time: " + start + " End Time: " + end + "]";
	}
	/**
	compares two Appointments and returns 1 if a is before b, -1 if a is after b and 0 if they overlap.
	**/
	public int compareTo(Appointment b) {
		String aday = this.getDay();
		String bday = b.getDay();
		if (aday.compareTo(bday) == 0) {
			double ast = this.getStart();
			double ae = this.getEnd();
			double bst = b.getStart();
			double be = b.getEnd();
			if (ast == bst) {
				return 0;
			} else if (ast < bst) {
				if (ae <= bst) {
					return 1;
				} else {
					return 0;
				}
			} else if (ast > be) {
				return -1;
			} else {
				return 0;
			}
		} else {
			return aday.compareTo(bday);
		}
	}

	private String day, description;

	private double start;

	private double end;
}




import java.util.*;
/**
AppointmentBook Class
**/
public class AppointmentBook{
	/**
	Empty cons. Appointmentbook
	**/
	public AppointmentBook(){
	}
	/**
	Cons. AppointmentBook with ArrayList holding Appointments
	**/
	public AppointmentBook(ArrayList<Appointment> an){
		anArray = an;
		i = 0;
	}
	/**
	Adds a appointment to the List but checks to make sure it is not overlapping first		
	**/
	public String add(Appointment a){
		BinarySearcher searcher = new BinarySearcher(anArray);
		int sea = searcher.search(a);
		if (sea == -1 && i > 0){
			return "Error: Overlapping Appointments!";
		}
		else{
			i++;
			anArray.add(sea, a);
			return "Appointment Added!";
		}
			
	}
	/**
	Prints a day of appointments
	**/
	public String print(String a){
		
		String rvalue = "";
		for (int n = 0; n < anArray.size(); n++){
			x = anArray.get(n);
			String c = x.getDay();
			if (c.equals(a)){
				rvalue = rvalue + x.toString() + "\n";
			}
		}
		return rvalue;
	}
	
	private Appointment x;
	private ArrayList<Appointment> anArray;
	private int i;
}





import java.util.ArrayList;
/**
   A class for executing binary searches through an array.
*/
public class BinarySearcher
{  
   /**
      Constructs a BinarySearcher.
      @param anArray a sorted array of integers
   */
   public BinarySearcher(ArrayList<Appointment> anArray)
   {
      a = anArray;
   }
   
   /**
      Finds a value in a sorted array, using the binary
      search algorithm.
      @param v the value to search
      @return the index at which the value occurs, or -1
      if it does not occur in the array
   */
   public int search(Appointment v)
   {  
	   int midp = 0;
      int low = 0;
      int high = a.size() - 1;
      while (low <= high)
      {
         int mid = (low + high) / 2;
         midp = Math.round(mid);
         int diff = a.get(mid).compareTo(v);
      
         if (diff == 0) // a[mid] == v
            return -1;
         else if (diff < 0) // a[mid] < v 
            low = mid + 1;
         else
            high = mid - 1;
      }
      return midp;
   }

   private ArrayList<Appointment> a;
}





import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

import java.io.*;
import java.util.*;

public class AppointmentBookTester {
	
	private static final int FWIDTH = 400;
	private static final int FHEIGHT  = 300;
	
	public static void main(String[] args){

		JFrame frame = new JFrame();
		
		final int WIDTH = 10;
		final int COLUMNS = 30;
		final int ROWS = 10;
		
		final JTextArea text = new JTextArea(ROWS, COLUMNS);
		text.setEditable(false);
		JScrollPane sPane = new JScrollPane(text);
		
		JButton Add = new JButton("Add");
		JButton Print = new JButton("Print");
		
		JLabel day = new JLabel("Day:");
		JLabel desc = new JLabel("Description:");
		JLabel st = new JLabel("Start Time:");
		JLabel et = new JLabel("End Time");
		
		final JTextField dayField = new JTextField(WIDTH);
		dayField.setText("");
		final JTextField descField = new JTextField(WIDTH);
		descField.setText("");
		final JTextField stField = new JTextField(WIDTH);
		stField.setText("");
		final JTextField etField = new JTextField(WIDTH);
		etField.setText("");
		
		JPanel panelleft = new JPanel();
		JPanel panelright = new JPanel();
		
		panelleft.setLayout(new GridLayout(2,1));
		panelleft.add(Add);
		panelleft.add(Print);
		
		panelright.setLayout(new GridLayout(2,4));
		panelright.add(day);
		panelright.add(dayField);
		panelright.add(desc);
		panelright.add(descField);
		panelright.add(st);
		panelright.add(stField);
		panelright.add(et);
		panelright.add(etField);
		
		frame.add(panelright, BorderLayout.NORTH);
		frame.add(panelleft, BorderLayout.CENTER);
		frame.add(sPane, BorderLayout.SOUTH);
		
		ArrayList<Appointment> anArray = new ArrayList<Appointment>();
		final AppointmentBook Book = new AppointmentBook(anArray);
		
		class AddActionListener implements ActionListener{
			public void actionPerformed(ActionEvent event){
				String DaY, DesC;
				double St, Et;
				DaY = dayField.getText();
				DesC = descField.getText();
				St = Double.parseDouble(stField.getText());
				Et = Double.parseDouble(etField.getText());
				Appointment appoint = new Appointment(DaY, DesC, St, Et);
				text.append(Book.add(appoint)+ "\n");
				
			}
		}
		class PrintActionListener implements ActionListener{
			public void actionPerformed(ActionEvent event){
				String DaY = dayField.getText();
				text.append(Book.print(DaY) + "\n");
			}
		}
		
		ActionListener AddActionListener = new AddActionListener();
		Add.addActionListener(AddActionListener);
		
		ActionListener PrintActionListener = new PrintActionListener();
		Print.addActionListener(PrintActionListener);
		
		frame.setSize(FWIDTH, FHEIGHT);
		frame.setTitle("Appointment Book");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
	}

}