public class Drink { private String name; private String brand; private double calories; public Drink() { name = ""; brand = ""; calories = 0; } public void setName(String s) { name = s; } public String getName() { return name; } public void setBrand(String s) { brand = s; } public String getBrand() { return brand; } public void setCal(double i) { calories = i; } public double getCal() { return calories; } public void printInfo() { System.out.println("Name: " + name); System.out.println("Brand: " + brand); System.out.println("Calories: " + calories); } }
public class FruitJuice extends Drink { private double fruitPercentage; private String fruit; public FruitJuice() { super(); fruitPercentage = 0; fruit = ""; } public void setFruitPercentage(double fruit) { fruitPercentage = fruit; } public double getFruitPercentage() { return fruitPercentage; } public void setFruit(String s) { fruit = s; } public String getFruit() { return fruit; } public void printInfo() { super.printInfo(); System.out.println("Fruit Percentage: " + fruitPercentage + "%"); System.out.println("Fruit: " + fruit); } }
public class Soda extends Drink { private double caffinePercent; public Soda() { super(); caffinePercent = 0; } public void setCaffinePercent(double caffine) { caffinePercent = caffine; } public double getCaffinePercent() { return caffinePercent; } public void printInfo() { super.printInfo(); System.out.println("Caffine Percent: " + caffinePercent + "%"); } }
import java.util.Scanner; public class DrinkList { public static String lowestCalorie(Drink[] drinks) { Drink drink = drinks[0]; for(int i = 0; i < drinks.length; i++) { if(drinks[i].getCal() <= drink.getCal()) { drink = drinks[i]; } } String lowest = drink.getName(); return lowest; } public static void printAll(Drink[] drinks) { for(int i = 0; i < drinks.length; i++) { drinks[i].printInfo(); System.out.println(); } } public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); boolean run = true; System.out.println("How many drinks will you be entering?"); int num = keyboard.nextInt(); System.out.println(); Drink[] drinks = new Drink[num]; for(int i = 1; i <= num; i++) { keyboard.nextLine(); System.out.println("Information on Drink " + i + ": "); System.out.print("Type of Drink (Soda or Fruit) "); String type = keyboard.nextLine(); System.out.print("Enter " + type + "'s name: "); String name = keyboard.nextLine(); System.out.print("Enter " + name + "'s brand: "); String brand = keyboard.nextLine(); System.out.print("Enter " + name + "'s calories: "); double calories = keyboard.nextDouble(); keyboard.nextLine(); if(type.equalsIgnoreCase("Soda")) { System.out.print("Enter the percentage of caffine in " + name + ": "); double caffine = keyboard.nextDouble(); Soda soda = new Soda(); soda.setName(name); soda.setBrand(brand); soda.setCal(calories); soda.setCaffinePercent(caffine); drinks[i - 1] = soda; } else if(type.equalsIgnoreCase("Fruit")) { System.out.print("Enter the fruit in " + name + ": "); String fruit = keyboard.nextLine(); System.out.print("Enter the percentage vitamin (Daily value) in " + name + ": "); double vitamin = keyboard.nextDouble(); FruitJuice juice = new FruitJuice(); juice.setName(name); juice.setBrand(brand); juice.setCal(calories); juice.setFruit(fruit); juice.setFruitPercentage(vitamin); drinks[i - 1] = juice; } else { System.out.println("Sorry, but I didn't recognize the kind of drink."); System.out.println("Next time put the type in right."); i--; } System.out.println(); } String option = keyboard.nextLine(); do { System.out.println("Enter choice (\"Lowest Calorie\", \"PrintAll\", or \"exit\" ): "); option = keyboard.nextLine(); if(option.equalsIgnoreCase("Lowest Calorie")) { System.out.println(lowestCalorie(drinks)); } else if(option.equalsIgnoreCase("PrintAll")) { printAll(drinks); } else { System.out.println("Please enter one of the available commands."); } }while(!option.equalsIgnoreCase("Exit")); System.out.println("Thanks for using the program. :)"); } }
No comments:
Post a Comment