All pastes #44103 Raw Edit

Someone

public text v1 · immutable
#44103 ·published 2006-03-01 15:21 UTC
rendered paste body
public class HW5 {
	public static void main(String[] args) {
		PetStore pet_store = new PetStore(9, 17, "Pets R Us");
		FoodStore food_store = new FoodStore(0, 25, "Foodmart");
		//why is this 25 allowed? why doesn't it throw an exception that stops the program?
		BookStore book_store = new BookStore(12, 21, "Ninja Books");
		System.out.println(pet_store.printHours());
		System.out.println(pet_store.printObjects());
		System.out.println(food_store.printHours());
		System.out.println(food_store.printObjects());
		System.out.println(book_store.printHours());
		System.out.println(book_store.printObjects());
	}
}

abstract class Store {
	//constants
	protected final int DEFAULTHOUR = 9;
	protected final String DEFAULTNAME = "Unknown";
	public final static int SMALLESTHOUR = 0;
	public final static int LARGESTHOUR = 23;

	//protected data
	protected static int numObjects;
	protected int openHour;
	protected int closeHour;
	protected String storeName;
	
	//abstract methods
	public abstract String printHours();
	public abstract String printObjects();
	
	//common methods
	public boolean isOpen(int hour) {
		if(hour >= openHour || hour <= closeHour)
			return true;
		return false;
	}
	
	//mutators
	public void setOpen(int openHour) {
		checkHour(openHour);
		this.openHour = openHour;
	}

	public void setClose(int closeHour) {
		checkHour(openHour);
		this.closeHour = closeHour;
	}
	
	public void setName(String storeName) {
		this.storeName = storeName;
	}
	
	//accessors
	public int getOpen() {
		return openHour;
	}
	
	public int getClose() {
		return closeHour;
	}
	public String getName() {
		return storeName;
	}
	
	//utility
	private void checkHour(int n) {
		if(n < SMALLESTHOUR || n > LARGESTHOUR)
			throw new NumberFormatException("Hour outside of acceptable range");
	}
	
}

class PetStore extends Store {
	private boolean sellsCats;
	private boolean sellsDogs;
	private boolean sellsFish;
	public PetStore() {
		numObjects++;
		setOpen(DEFAULTHOUR);
		setClose(DEFAULTHOUR);
		setName(DEFAULTNAME);
	}
	public PetStore(int openHour, int closeHour, String storeName) {
		numObjects++;
		setOpen(openHour);
		setClose(closeHour);
		setName(storeName);
	}
	public String printHours() {
		return "Pet Store \""+storeName+"\" opens at " + openHour + " closes at " + closeHour;
	}
	public String printObjects() {
		return "PetStore sees " + numObjects + " objects";
	}
	public boolean getSellsCats() {
		return sellsCats;
	}
	public boolean getSellsDogs() {
		return sellsDogs;
	}
	public boolean getSellsFish() {
		return sellsFish;
	}
	public void setSellsCats(boolean sellsCats) {
		this.sellsCats = sellsCats;
	}
	public void setSellsDogs(boolean sellsDogs) {
		this.sellsDogs = sellsDogs;
	}
	public void setSellsFish(boolean sellsFish) {
		this.sellsFish = sellsFish;
	}
}

class FoodStore extends Store {
	private boolean sellsLiquor;
	public FoodStore() {
		numObjects++;
		setOpen(DEFAULTHOUR);
		setClose(DEFAULTHOUR);
		setName(DEFAULTNAME);
	}
	public FoodStore(int openHour, int closeHour, String storeName) {
		numObjects++;
		setOpen(openHour);
		setClose(closeHour);
		setName(storeName);
	}
	public String printHours() {
		return "Food Store \""+storeName+"\" opens at " + openHour + " closes at " + closeHour;
	}
	public String printObjects() {
		return "Food Store sees " + numObjects + " objects";
	}
	public boolean getSellsLiquor() {
		return sellsLiquor;
	}
	public void setSellsLiquor(boolean sellsLiquor) {
		this.sellsLiquor = sellsLiquor;
	}
}

class BookStore extends Store {
	private boolean sellsCoffee;
	public BookStore() {
		numObjects++;
		setOpen(DEFAULTHOUR);
		setClose(DEFAULTHOUR);
		setName(DEFAULTNAME);
	}
	public BookStore(int openHour, int closeHour, String storeName) {
		numObjects++;
		setOpen(openHour);
		setClose(closeHour);
		setName(storeName);
	}
	public String printHours() {
		return "Book Store \""+storeName+"\" opens at " + openHour + " closes at " + closeHour;
	}
	public String printObjects() {
		return "Book Store sees " + numObjects + " objects";
	}
	public boolean getSellsCoffee() {
		return sellsCoffee;
	}
	public void setSellsCoffee(boolean sellsCoffee) {
		this.sellsCoffee = sellsCoffee;
	}
}