- import java.util.*;
- import java.io.*;
- //=============================================================================
- public class UseForest {
- //-----------------------------------------------------------------------------
- private static Scanner keyboard = new Scanner(System.in);
- private static final double MAX_TREES = 10.0;
- //-----------------------------------------------------------------------------
- public static void main(String[] args) {
- char menu;
- double reapHeight;
- Tree newTree;
- String name, forestName;
- Forest newForest = null;
- do {
- System.out.println();
- System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, "
- + "e(X)it : ");
- menu = Character.toUpperCase(keyboard.nextLine().charAt(0));
- switch (menu) {
- case 'D' :
- if (newForest == null) {
- System.out.println("No forest");
- break;
- } else {
- newForest.display();
- }
- break;
- case 'N' :
- System.out.print("What is the forest name : ");
- name = keyboard.nextLine();
- newForest = new Forest(name);
- break;
- case 'Y' :
- if (newForest == null) {
- System.out.println("No forest");
- } else {
- newForest.year();
- }
- break;
- case 'R' :
- if (newForest == null) {
- System.out.println("No forest");
- } else {
- try {
- System.out.print("What height to reap at : ");
- reapHeight = keyboard.nextDouble();
- newForest.reap(reapHeight);
- } catch (InputMismatchException e) {
- System.out.println("ERROR: Invalid height");
- }
- keyboard.nextLine();
- }
- break;
- case 'S' :
- if (newForest == null) {
- System.out.println("No forest");
- } else {
- try {
- newForest.save(newForest);
- } catch (Exception e) {
- System.out.println("ERROR saving");
- break;
- }
- }
- break;
- case 'L' :
- try{
- System.out.print("What is the forest name : ");
- forestName = keyboard.nextLine();
- if (newForest == null) {
- newForest = new Forest("");
- newForest = newForest.load(forestName);
- } else {
- newForest = newForest.load(forestName);
- }
- } catch (Exception e) {
- System.out.println("ERROR loading");
- }
- break;
- case 'X' :
- System.out.println("Goodbye");
- System.out.println();
- break;
- default :
- System.out.println("ERROR: Invalid option");
- break;
- }
- }
- while (menu != 'X');
- }
- //-----------------------------------------------------------------------------
- }
- //=============================================================================
- import java.util.*;
- import java.io.*;
- //=============================================================================
- public class Forest implements Serializable{
- //-----------------------------------------------------------------------------
- private Tree[] trees;
- private static final int MAX_TREES = 10;
- private String name;
- private int index;
- //-----------------------------------------------------------------------------
- public Forest(String treeName) {
- trees = new Tree[MAX_TREES];
- name = treeName;
- for (index = 0; index < trees.length; index++) {
- trees[index] = new Tree();
- }
- }
- //-----------------------------------------------------------------------------
- public void display () {
- System.out.println(name);
- for (index = 0; index < MAX_TREES; index++) {
- System.out.printf("%2d ", (index+1));
- System.out.print(trees[index]);
- }
- }
- //-----------------------------------------------------------------------------
- public void year() {
- for(index = 0; index < trees.length; index++) {
- trees[index].growth();
- }
- }
- //-----------------------------------------------------------------------------
- public void reap (double reapHeight) {
- for( index=0; index< trees.length; index++) {
- if(trees[index].heightGenerator() >= reapHeight){
- System.out.print("Cut ");
- System.out.printf("%2d ", (index+1));
- System.out.print(trees[index]);
- trees[index]= new Tree();
- System.out.print("New ");
- System.out.printf("%2d ", (index+1));
- System.out.print(trees[index]);
- }
- }
- }
- //-----------------------------------------------------------------------------
- public void save(Forest newForest) throws Exception {
- ObjectOutputStream toStream;
- toStream = new ObjectOutputStream(new FileOutputStream
- (newForest.getName()));
- toStream.writeObject(newForest);
- toStream.close();
- }
- //-----------------------------------------------------------------------------
- public Forest load(String forestName) throws Exception {
- ObjectInputStream fromStream;
- Forest local;
- fromStream = new ObjectInputStream(new FileInputStream(forestName));
- local = (Forest)fromStream.readObject();
- fromStream.close();
- return (local);
- }
- //-----------------------------------------------------------------------------
- public String getName() {
- return(name);
- }
- //-----------------------------------------------------------------------------
- }
- //=============================================================================
- import java.util.*;
- import java.io.*;
- //=============================================================================
- public class Tree implements Serializable{
- //-----------------------------------------------------------------------------
- private double height;
- private int growthRate;
- //-----------------------------------------------------------------------------
- private static final double MAX_HEIGHT = 5.0;
- private static final int RATE = 50;
- //-----------------------------------------------------------------------------
- public Tree() {
- height = MAX_HEIGHT * Math.random();
- growthRate = RATE + (int)(Math.random()*RATE);
- }
- //-----------------------------------------------------------------------------
- public double heightGenerator() {
- return(height);
- }
- //-----------------------------------------------------------------------------
- public String toString() {
- String printable = String.format(" : %6.2f (%d%% pa)\n", height, growthRate);
- return printable;
- }
- //-----------------------------------------------------------------------------
- public void growth() {
- height = height * ((100.0 + growthRate)/100.0);
- }
- //-----------------------------------------------------------------------------
- }
- //=============================================================================