Что не так с моим кодом Java? Я не могу точно получить желаемый результат - PullRequest
0 голосов
/ 09 июля 2020

пакет торт;

импорт java .util.ArrayList; import java .util.Arrays;

publi c class Cake {

public String name;
public String[] topping;
public String[] toppingOrder;


double priceSmall;
double priceMedium;
double priceBig;
double totalPrice;
double sizePrice;
int size;
int quantity;
String stringSize;





// Class member variables
public Cake(String name, String[] topping, String[] toppingOrder, double priceSmall, double priceMedium,
        double priceBig, double totalPrice, double sizePrice, int size, int quantity, String stringSize) {
    this.name = name;
    this.topping = topping;
    this.toppingOrder = toppingOrder;
    this.priceSmall = priceSmall;
    this.priceMedium = priceMedium;
    this.priceBig = priceBig;
    this.totalPrice = totalPrice;
    this.sizePrice = sizePrice;
    this.size = size;
    this.quantity = quantity;
    this.stringSize = stringSize;

    
    
}

public Cake() {
    name = " ";
    String[] topping = {"Lava Chocolate","Cream Cheese", "Butter Cream", "Fondant"};
    priceSmall = 45.0;
    priceMedium = 65.0;
    priceBig = 80.0;
    totalPrice = 0;
    sizePrice = 0;
    size = 0;
    quantity = 5;
    String[] stringSize = {"Small","Medium","Big"};
}

//Constructor that accept String cake’s name 
// and set the value to class member name
public Cake(String name) {
    this.name = name;
}

// void method setCake that accept four parameters
public void setCake(String[] topping, double priceSmall,
        double priceMedium, double priceBig) {
    
    this.topping = topping;
    this.priceSmall = 45.00;
    this.priceMedium = 65.00;
    this.priceBig = 80.00;
}

public String[] topping() {
    return topping;
}

public int quantity() {
    return quantity;
}

public int size() {
    return size;
}

// Void method called orderCake that accept three parameters
public void orderCake(String[] topping, int quantity, int size) {
    
    
    
    this.topping = topping;
    this.quantity = quantity;
    this.size = size;
    
    
    
    totalPrice = sizePrice * quantity + (topping.length*10);
    
    
    
    
    
}

// • Method called getSizePrice that return 
// a double data type where it checks size and return
// the price.
public double getSizePrice(int size, double sizePrice) {
    
     
    if (size == 1)
        
    {
        
        return 45.0;
        
    }
    else if(size == 2)
        
    { 
        return 65.0;
        
    }
    
    else 
        
    {
        
        return 80.0;
        
    }   
    
    
    
}

// Method called getTotalPrice 
// that return a double data type totalPrice


// Method called getSize which returns 
// a String value according to the class member size
public String getSize() {
    
        if (size == 1) {
            return "Small";
        } else if(size == 2) { 
            return "Medium";
        } else if(size == 3) {
            return "Big";
        } else {
            return null;
        }
    }




public int getQuantity() {
    
    return quantity;
}

publi c double getTotalPrice () {

    totalPrice = getSizePrice() * quantity + (topping.length*10);

    return totalPrice;
    
    
}

    class orderCake extends Cake{
        
        String[] topping;
        int size;
        int quantity;
        
    
    
    
    
    }

    public void printCake() {
        // TODO Auto-generated method stub
        
        System.out.println("------------------------------------------------");
        System.out.println("\t\tCake Menu");
        System.out.println("------------------------------------------------");
        System.out.println("\nGeneric Cake with available Toppings:\n");
        System.out.println("1) Lava Chocolate");
        System.out.println("2) Cream Cheese");
        System.out.println("3) Butter Cream");
        System.out.println("4) Fondant");
        System.out.println("\nPrice:\n");
        System.out.println("[1] Small: RM45.0");
        System.out.println("[2] Medium: RM65.0");
        System.out.println("[3] Big: RM80.0");
        System.out.println();
    }

    public void printOrder() {
        
        
        
        // TODO Auto-generated method stub
        System.out.println("Cake order detail:\n");
        System.out.println("------------------------");

        System.out.println("Topping:" + Arrays.toString(topping));
        
        
        
        System.out.println("Size: " + getSize() + "\t"); 
        
        
        
        
        System.out.println("------------------------");
        
        
        
        System.out.println("\nTotal Price:" + getTotalPrice() + "\t");
        
        System.out.println();
        
    }

}

Мой код, наконец, может работать после изменения int getSize () на string getSize (), но у меня все еще есть проблема с моей расчетной частью. Я не могу запустить свой код, и он говорит об ошибке с getSize () в publi c double getTotalPrice ()

Ответы [ 3 ]

0 голосов
/ 09 июля 2020

На вашем месте я бы изменил тип getSize() на String:

public String getSize() {
    if (size == 1) {
        return "Small";
    } else if(size == 2) { 
        return "Medium";
    } else if(size == 3) {
        return "Big";
    } else {
        return null;
    }
}

И изменил printOrder() с

-------
System.out.println("Size:" + size + "\t");
-------

на

-------
System.out.println("Size:" + getSize() + "\t");
-------
0 голосов
/ 09 июля 2020

в любом случае, кто-нибудь знает, как исправить расчет общей цены?

это мой код

public double getSizePrice(int size, double sizePrice) {
    
    if (size == 1)      
    {   
        return sizePrice = 45.0;    
    }
    else if(size == 2)  
    { 
        return sizePrice = 65.0;
    }
    else    
    {
        return sizePrice = 80.0;    
    }   
}

public double getTotalPrice() {
    
    totalPrice = getSizePrice() * quantity + (topping.length*10);
    return totalPrice;
}

он говорит об ошибке с getSizePrice ()

0 голосов
/ 09 июля 2020

Причина, по которой отображается «2» вместо «medium», заключается в том, что отображаемая переменная является целым числом, а не строкой. Значение 2 - это числовое представление «среднего», но код этого не знает. Чтобы напечатать строку, которую представляет число, вы можете использовать оператор if. Например:

String stringSize;
if (size == 1) stringSize = "Small";
if (size == 2) stringSize = "Medium";
if (size == 3) stringSize = "Large"; 
System.out.println("Size: " + stringSize + "\t"); 

Кроме того, для цены нигде вы не устанавливаете значение totalPrice равным значению желаемого торта.

...