AOOP Lab Day 3 (ArrayList,LinkedList,Iterator)

November 21, 2013 3:39 pm Homework, Java

Package flower.lib

Flower.java

package flower.lib;
 
public class Flower {
 
	public Flower(int p) {
		this.price = p;
	}
 
	public Flower() {
		this.price = 0;
	}
 
 
	protected int price;
 
	public void printDetail() {
		System.out.println("Flower");
	}
 
	public int getPrice() {
		return price;
	}
}

Orchid.java

package flower.lib;
 
public class Orchid extends Flower{
 
	public Orchid(int p) {
		this.price = p;
	}
 
	public Orchid() {
		this.price = 6;
	}
 
	public void printDetail() {
		System.out.println("Orchid");
	}
 
	public int getPrice() {
		return price;
	}
}

Sunflower.java

package flower.lib;
 
public class Sunflower extends Flower{
	public Sunflower(int p) {
		this.price = p;
	}
 
	public Sunflower() {
		this.price = 1;
	}
 
	public void printDetail() {
		System.out.println("Sunflower");
	}
 
	public int getPrice() {
		return price;
	}
}

Tulip.java

package flower.lib;
 
public class Tulip extends Flower{
	public Tulip(int p) {
		this.price = p;
	}
 
	public Tulip() {
		this.price = 5;
	}
 
	public void printDetail() {
		System.out.println("Tulip");
	}
 
	public int getPrice() {
		return price;
	}
}

Package flower.program

Flower_store.java

package flower.program;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Iterator;
 
 
 
 
import flower.lib.*;
 
public class Flower_store {
 
	public static void main(String[] args) throws IOException {
		Scanner s = new Scanner(System.in);
		int rm;
		//int count = 0;
		//Flower[] bouquest = new Flower[1000];
		//ArrayList<Flower> b = new ArrayList<Flower>();
		LinkedList<Flower> c = new LinkedList<Flower>();
		System.out.println("Welcome to flower program!!");
		while(true) {
		System.out.println("What type of flower would you like to add in to your bouquet?");
		System.out.println("[s]unflower, [t]ulip, [o]rchid, [q]uit, [v]iew, [r]eset, re[m]ove current bouquet");
		String input = s.next();
		//System.out.println(input);
 
		if (input.equalsIgnoreCase("s")) {
			//bouquest[count] = new Sunflower();
			//b.add(new Sunflower());
			c.add(new Sunflower());
			//count++;
 
			//Sunflower f1 = new Sunflower(1);
			//count = (count + f1.getPrice());
		}else if (input.equalsIgnoreCase("t")){
			//bouquest[count] = new Tulip();
			c.add(new Tulip());
			//b.add(new Tulip());
			//count++;
 
			//Tulip f2 = new Tulip(5);
			//count = (count + f2.getPrice());
 
		}else if (input.equalsIgnoreCase("o")){
			//bouquest[count] = new Orchid();
			c.add(new Orchid());
			//b.add(new Orchid());
			//count++;
 
			//Orchid f3 = new Orchid(6);
			//count = (count + f3.getPrice());
 
		}else if (input.equalsIgnoreCase("v")) {
			System.out.println("----- Your Bouquet -----");
			int cost=0;
			/*for (int i=0; i<c.size(); i++) {
			//for (int i=0; i<b.size(); i++) {
				System.out.print((i+1)+". ");
				//bouquest[i].printDetail();
				c.get(i).printDetail();
				//b.get(i).printDetail();
				cost += c.get(i).getPrice();
				//cost += b.get(i).getPrice();
				//cost = cost + bouquest[i].getPrice();
			}
			*/
			/*int i = 0;
			while(i<c.size()) {
				c.get(i).printDetail();
				cost += c.get(i).getPrice();
				i++;
			}
			*/
 
			Iterator <Flower> itr = c.iterator();
			while(itr.hasNext()) {
				Flower e = itr.next();
				e.printDetail();
				cost += e.getPrice();			
			}
			//System.out.println(b.size());
			System.out.println("------------------------");
			System.out.println("Total cost is $"+cost);
			//System.out.println("Total cost is $"+total);
			System.out.println("------------------------");
		}else if (input.equalsIgnoreCase("r")) {
			/*for (int i=0; i<count; i++) {
				//bouquest[i] = null;
			}*/
			c.clear();
			//b.clear();
			System.out.println("Reset current bouquet Complete.");
			//count = 0;
		}else if (input.equalsIgnoreCase("m")) {
			System.out.print("Enter Your item to remove? : ");
			rm = s.nextInt();
			try {
			c.remove(rm-1);
			System.out.println("Complete!");
			}catch (Exception e){
				System.out.println("Error to Remove, Try again");
			}
		}
 
		if (input.equalsIgnoreCase("q")) {
			System.out.println("Bye Bye!");
			System.exit(0);
			}
		}
	}
 
}