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.Scanner; import flower.lib.*; public class Flower_store { public static void main(String[] args) throws IOException { Scanner s = new Scanner(System.in); int count = 0; Flower[] bouquest = new Flower[1000]; 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 current bouquet"); String input = s.next(); //System.out.println(input); if (input.equalsIgnoreCase("s")) { bouquest[count] = new Sunflower(); count++; //Sunflower f1 = new Sunflower(1); //count = (count + f1.getPrice()); }else if (input.equalsIgnoreCase("t")){ bouquest[count] = new Tulip(); count++; //Tulip f2 = new Tulip(5); //count = (count + f2.getPrice()); }else if (input.equalsIgnoreCase("o")){ bouquest[count] = 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<count; i++) { System.out.print((i+1)+" "); bouquest[i].printDetail(); cost = cost + bouquest[i].getPrice(); } 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; } System.out.println("Reset current bouquet Complete."); count = 0; } if (input.equalsIgnoreCase("q")) { System.out.println("Bye Bye!"); System.exit(0); } } } } |