- import java.util.Scanner;
- import java.io.*;
- //=============================================================================
- public class UseForest {
- //-----------------------------------------------------------------------------
- private static Scanner keyboard = new Scanner (System.in);
- private static final int ZERO = 0;
- //-----------------------------------------------------------------------------
- public static void main (String[] args) {
- //---variables-----------------------------------------------------------------
- String name;
- int index;
- char choice;
- Forest currentForest;
- Forest newForest;
- int reapHeight;
- //-------user input------------------------------------------------------------
- System.out.println();
- do {
- System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave," +
- " (L)oad, e(X)it : ");
- choice = keyboard.next().charAt(0);
- choice = Character.toUpperCase(choice);
- //------Choices---------------------------------------------------------------
- switch (menu) {
- case 'D' :
- if (currentForest == null) {
- System.out.println("No forest");
- break;
- } else {
- currentForest.display();
- }
- break;
- case 'N' :
- System.out.print("What is the forest name : ");
- name = keyboard.nextLine();
- newForest = new Forest(name);
- break;
- case 'Y' :
- if (currentForest == null) {
- System.out.println("No forest");
- } else {
- currentForest.year();
- }
- break;
- case 'R' :
- if (currentForest == null) {
- System.out.println("No forest");
- } else {
- System.out.print("What height to reap at : ");
- reaper = keyboard.nextDouble();
- currentForest.reap(reaper);
- }
- keyboard.nextLine();
- break;
- case 'S' :
- if (currentForest == null) {
- System.out.println("No forest");
- } else {
- //newForest.save(newForest);
- currentForest.save(name);
- }
- break;
- case 'L' :
- System.out.print("What is the forest name : ");
- forestName = keyboard.nextLine();
- //newForest.load(forestName);
- currentForest=load(forestName);
- break;
- case 'X' :
- System.out.println("Goodbye");
- System.out.println();
- break;
- default :
- System.out.println("ERROR: Invalid option");
- break;
- }
- }
- while (menu != 'X');
- }
- //-----------------------------------------------------------------------------
- public Forest load(String forestName) throws IOException {
- try {
- ObjectInputStream fromStream;
- Forest local;
- fromStream = new ObjectInputStream(new FileInputStream(forestName));
- local = (Forest)fromStream.readObject();
- fromStream.close();
- return (local);
- } catch (Exception e) {
- System.out.println("Error");
- }
- //-----------------------------------------------------------------------------
- }
- }
- //==================================================================
- import java.io.*;
- //=============================================================================
- public class Forest implements Serializable {
- //-----------------------------------------------------------------------------
- private Tree[] trees;
- private static final int TREES = 10;
- String name;
- int index;
- //-----------------------------------------------------------------------------
- public Forest(String treeName) {
- trees = new Tree[TREES];
- name = treeName;
- for(index = 0; index < trees.length; index++){
- trees[index] = new Tree();
- }
- }
- //-----------------------------------------------------------------------------
- public void display() {
- for(index = 0; index < trees.length; index++) {
- System.out.printf("%2d", (index+1));
- System.out.print(trees[index]);
- }
- }
- //-----------------------------------------------------------------------------
- public void reapForest(int reapHeight) {
- for(index = 0; index < trees.length; index++) {
- if(trees[index].getHeight <= 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 String getName() {
- return(name);
- }
- //-----------------------------------------------------------------------------
- public void year() {
- for(index = 0; index < trees.length; index++) {
- trees[index].year();
- }
- }
- //-----------------------------------------------------------------------------
- public void save(Forest newForest) throws IOException {
- ObjectOutputStream toStream;
- toStream = new ObjectOutputStream(new FileOutputStream
- (newForest.getName()));
- toStream.writeObject(newForest);
- toStream.close();
- }
- //-----------------------------------------------------------------------------
- }
- //=============================================================================
- 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() {
- return(" : Math.Round(height) (" + growthrate + ")%");
- }
- //-----------------------------------------------------------------------------
- public void growth() {
- height = height * ((100.0 + growthRate)/100.0);
- }
- //-----------------------------------------------------------------------------
- }
- //=============================================================================