How can I improve my sundae program, using equals ignore case?

4328 views java
-1

The program works but you need to type in Upper case or Lower case depending on the word. As well, when you choose 2 or more standard/deluxe toppings, it displays only the last input of each one. Definitely waiting for help :D. Thank you!

public class Sundae {

private final double SALES_TAX = .08625;
private final double DELUXE_TOPPING = 1.25;
private String iceCreamFlavor;
private int numberOfScoops;
private double costForScoops;
private String standardToppingList;
private String freeSyrupChoice;
private String deluxeToppingList;
private double costOfDeluxeToppings;
private int counterD;
private double costOfSundae;
private double tax;
private double total;

public Sundae() 

{

}

public String getIceCreamFlavor() 

{
    return iceCreamFlavor;
}

public int getNumberOfScoops()

{
    return numberOfScoops;
}

public double getCostForScoops()

{
    return costForScoops;
}

public String getStandardToppingList()

{
    return standardToppingList;
}

public String getFreeSyrupChoice()
{
    return freeSyrupChoice;
}

public String getDeluxeToppingList()

{
    return deluxeToppingList;
}

public double getCostOfDeluxeToppings()

{
    return costOfDeluxeToppings;
}

public int getCounterD()

{
    return counterD;
}

public double getCostOfSundae()

{
    return costOfSundae;
}

public void setIceCreamFlavor(String choice)

{
    iceCreamFlavor = choice;
}

public void setNumberOfScoops(int numberScoops)

{
    numberOfScoops = numberScoops;
}

public void setCostForScoops()

{
    costForScoops = numberOfScoops + 1.79;
}

public void setCounterD(int numberD)

{

    counterD = numberD;
}

public void setStandardToppingList(String standardTopping)

{
    standardToppingList = standardTopping;

}

public void setFreeSyrupChoice(String syrup)

{
    freeSyrupChoice = syrup;
}

public void setDeluxeToppingList(String deluxeTopping)

{
    deluxeToppingList = deluxeTopping;
}

public void setDefault()

{
    iceCreamFlavor = "Vanilla";
    numberOfScoops = 2;
    costForScoops = 1.79;
    standardToppingList = "Whipped Cream, Hot Fudge, Multi-Colored Sprinkles, and a cherry.";

}

public void Print()

{   

    costForScoops = numberOfScoops * 1.79;

    costOfDeluxeToppings = DELUXE_TOPPING * counterD;

    costOfSundae = costForScoops + costOfDeluxeToppings;

    tax = SALES_TAX * costOfSundae; 

    total = costOfSundae + tax;

    System.out.println("Flavor: " + iceCreamFlavor + "\nNumber of scoops: " + numberOfScoops + "\nCost for scoops: " + costForScoops 
            + "\nStandard toppings: " + standardToppingList + "\nSyrup:" + freeSyrupChoice + "\nDeluxe toppings: " + deluxeToppingList 
            + "\nCost of deluxe toppings: " + costOfDeluxeToppings);

    System.out.printf("Subtotal: $%.2f \nSale Tax: $%.2f \nTotal: $%.2f", costOfSundae, tax, total);
 }
}

Driver Program

import java.util.Scanner;

public class SundaeDriver {

public static void main(String[]args) {

    Sundae sundae = new Sundae();
    int numberS;
    int numberD;
    Scanner input = new Scanner(System.in);

    System.out.println("Which sundae flavor do you want? ");
    sundae.setIceCreamFlavor(input.nextLine());

    if ((sundae.getIceCreamFlavor().equalsIgnoreCase("Vanilla"))||
        (sundae.getIceCreamFlavor().equalsIgnoreCase("French Vanilla"))|| 
        (sundae.getIceCreamFlavor().equalsIgnoreCase("Peanut Butter"))||
        (sundae.getIceCreamFlavor().equalsIgnoreCase("Chocolate"))||
        (sundae.getIceCreamFlavor().equalsIgnoreCase("Chocolate Chip"))||
        (sundae.getIceCreamFlavor().equalsIgnoreCase("Chocolate Chip Cookie"))||
        (sundae.getIceCreamFlavor().equalsIgnoreCase("Cookie Dough"))||
        (sundae.getIceCreamFlavor().equalsIgnoreCase("Ice Cream Cake"))||
        (sundae.getIceCreamFlavor().equalsIgnoreCase("American Dream"))|| 
        (sundae.getIceCreamFlavor().equalsIgnoreCase("Vanilla Chocolate Swirl"))||
        (sundae.getIceCreamFlavor().equalsIgnoreCase("Strawberry"))||
        (sundae.getIceCreamFlavor().equalsIgnoreCase("Mint Chocolate Chip"))||
        (sundae.getIceCreamFlavor().equalsIgnoreCase("Oreo Cookies and Cream")))

        {

        }

        else

        {   
            sundae.setIceCreamFlavor("Vanilla");
        }

    System.out.println("How many scoops do you want?");
    sundae.setNumberOfScoops(input.nextInt());


    if ((sundae.getNumberOfScoops() == 1)||
        (sundae.getNumberOfScoops() == 2)||
        (sundae.getNumberOfScoops() == 3)||
        (sundae.getNumberOfScoops() == 4)||
        (sundae.getNumberOfScoops() == 5)||
        (sundae.getNumberOfScoops() == 6))
    {

    }

    else 

    {
        sundae.setNumberOfScoops(2);
    }

    System.out.println("How many free toppings do you want?(free)");
    numberS = input.nextInt();

    input.nextLine();
    for (int i = 0; i < numberS;i++)
    {
        System.out.println((i + 1) + " free toppings do you want?");
        sundae.setStandardToppi

answered question

Since you claim that the code is working, you will want to look at codereview.stackexchange.com/tour

1 Answer

12

This looks like a perfect time to use enum(s). I'll provide an example for the first case. Ice cream flavors.

public enum IceCreamFlavors {
    VANILLA, FRENCH_VANILLA, PEANUT_BUTTER, CHOCOLATE, CHOCOLATE_CHIP, 
    CHOCOLATE_CHIP_COOKIE, COOKIE_DOUGH, ICE_CREAM_CAKE, AMERICAN_DREAM,
    VANILLA_CHOCOLATE_SWIRL, STRAWBERRY, MINT_CHOCOLATE_CHIP, OREOS_COOKIES_AND_CREAM;

    public static IceCreamFlavors fromName(String name) {
        for (IceCreamFlavors f : values()) {
            if (f.name().replace('_', ' ').equalsIgnoreCase(name)) {
                return f;
            }
        }
        return IceCreamFlavors.VANILLA;
    }
}

Then you can call it like

System.out.println("Which ice cream flavor do you want? ");
IceCreamFlavors flavor =  IceCreamFlavors.fromName(input.nextLine());

And that way your logic for validation of the flavor it kept with-in the enum.

posted this

Have an answer?

JD

Please login first before posting an answer.