Unit 1 Primitives
Avinh Huynh
public class TimeInterval {
// returns true if interval overlaps with this TimeInterval;
// otherwise, returns false
public boolean overlapsWith(TimeInterval interval){
}
}
public class Appointment {
// returns the time interval of this Appointment
public TimeInterval getTime(){
}
// returns true if the time interval of this Appointment
// overlaps with the time interval of other;
// otherwise, returns false
public boolean conflictsWith(Appointment other){ // 1a
if this.getTime.overlapsWith(other.getTime()){
return true;
} else {
return false;
}
}
}
public class DailySchedule {
// contains Appointment objects, no two Appointments overlap
private ArrayList apptList;
public DailySchedule(){
apptList = new ArrayList(); }
// removes all appointments that overlap the given Appointment
// postcondition: all appointments that have a time conflict with
// appt have been removed from this DailySchedule
public void clearConflicts(Appointment appt){ // 1b
for (Appointment test : apptList) {
if this.conflictsWith(appt) {
apptList.remove(Integer.valueOf(appt));
}
}
}
// if emergency is true, clears any overlapping appointments and adds
// appt to this DailySchedule; otherwise, if there are no conflicting
// appointments, adds appt to this DailySchedule;
// returns true if the appointment was added;
// otherwise, returns false
public boolean addAppt(Appointment appt, boolean emergency){
// if its an emergency clear conflicts and return true
if (emergency == true) {
clearConflicts(appt);
apptList.add(appt);
return true;
}
// if its not an emergency check if there are conflicts
for (Appointment test : apptList) {
// if no conflicts remove the conflicting appointment
// add the new one and return true
if (test.conflictsWith(appt)) {
apptList.add(appt);
return true
}
}
// if there is a conflict
return false;
}
// There may be fields, constructors, and methods that are not shown.
}
public interface Item {
double purchasePrice();
}
public abstract class TaxableItem implements Item {
private double taxRate;
public abstract double getListPrice();
public TaxableItem(double rate) {
taxRate = rate;
}
// returns the price of the item including the tax
public double purchasePrice() {
int tax = this.purchasePrice * (1+this.taxRate);
return tax + this.purchasePrice
}
}
public class Customer {
// constructs a Customer with given name and ID number
public Customer(String name, int idNum)
{ /* implementation not shown */ }
// returns the customer's name
public String getName()
{ /* implementation not shown */ }
// returns the customer's id
public int getID()
{ /* implementation not shown */ }
// returns 0 when this customer is equal to other;
// a positive integer when this customer is greater than other;
// a negative integer when this customer is less than other
public int compareCustomer(Customer other) {
if (this.name.compareTo(other.getName) == 0) {
if (this.id > other.getID) {
return 1;
} else {
return -1;
}
}
return this.name.compareTo(other.getName);
}
// There may be fields, constructors, and methods that are not shown.
}