Как я могу улучшить мою программу с фруктами, используя регистр игнорирования? - PullRequest
0 голосов
/ 08 ноября 2018

Программа работает, но вам нужно набрать заглавные или строчные буквы в зависимости от слова. Кроме того, когда вы выбираете 2 или более стандартных / роскошных начинок, он отображает только последний вход каждого из них. Определенно жду помощи: D. Спасибо!

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);
 }
}

Программа для водителя

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.setStandardToppingList(input.nextLine());

        if ((sundae.getStandardToppingList().equals("Whipped cream"))||
                (sundae.getStandardToppingList().equals("Syrup"))||
                (sundae.getStandardToppingList().equals("Multi colored sprinkles"))||
                (sundae.getStandardToppingList().equals("Cherry")))

        {
            sundae.getStandardToppingList();

        }

        else  

        {
            sundae.setStandardToppingList("Whipped cream, hot fudge, multi colored sprinkles and cherry");
        }

    }
    System.out.println("Which syrup do you want?");
    sundae.setFreeSyrupChoice(input.nextLine());

    if ((sundae.getFreeSyrupChoice().equals("Hot fudge"))||
        (sundae.getFreeSyrupChoice().equals("Chocolate"))||
        (sundae.getFreeSyrupChoice().equals("Caramel")))
    {

    }

    else

    {
        sundae.setFreeSyrupChoice("");
    }

    System.out.println("How many deluxe toppings do you want?");
    numberD = input.nextInt();
    sundae.setCounterD(numberD);
    sundae.getCounterD();

    input.nextLine();

    for(int j = 0; j < numberD; j++)
    {
        System.out.println((j + 1) + " deluxe toppings do you want?");
        sundae.setDeluxeToppingList(input.nextLine());

        if((sundae.getDeluxeToppingList().equals("M&Ms"))||
                (sundae.getDeluxeToppingList().equals("Crushed oreos"))||
                (sundae.getDeluxeToppingList().equals("Reeses")||
                (sundae.getDeluxeToppingList().equals("Chocolate chips"))||
                (sundae.getDeluxeToppingList().equals("KitKats"))||
                (sundae.getDeluxeToppingList().equals("Gummy Bears"))||
                (sundae.getDeluxeToppingList().equals("Cookie Dough Bits"))||
                (sundae.getDeluxeToppingList().equals("Marshmallows"))||
                (sundae.getDeluxeToppingList().equals("Peanuts"))||
                (sundae.getDeluxeToppingList().equals("Walnuts"))))

                {
                    sundae.getDeluxeToppingList();
                }
        else

        {
            sundae.setDeluxeToppingList("");
        }

    }

    sundae.Print();
    input.close();
 }
}

Ответы [ 2 ]

0 голосов
/ 08 ноября 2018

использование:

private List<String> standardToppingList;
private List<String> deluxToppingList;

с конструктором, содержащим:

standardToppingList = new Arraylist<>();
deluxToppingList = new Arraylist<>();

, чтобы добавить в список, используйте:

standardToppingList.add(standardTopping);
deluxToppingList.add(deluxTopping);

и отображение с помощью:

for(String s : deluxToppingList) {
    System.out.println(s);
}
for(String s : standardToppingList) {
    System.out.println(s);
}
0 голосов
/ 08 ноября 2018

Это выглядит как идеальное время для использования enum (s). Я приведу пример для первого случая. Ароматизаторы для мороженого.

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;
    }
}

Тогда вы можете назвать это как

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

И таким образом ваша логика для проверки аромата, который он сохранил, - в перечислении.

...