2015-10-30 3 views
0
import java.util.Scanner; 
import java.util.ArrayList; 
public class OnlineStore { 
    String[] books = { 
     "Intro to Java", "Intro to C++", "Intro to C++", "Perl" 
    }; 
    String[] dvds = { 
     "Snow White", "Cinderella", "Dumbo", "Bambi" 
    }; 
    double[] booksPrices = { 
     45.99, 89.34, 100.00, 25.0 
    }; 
    double[] dvdsPrices = { 
     19.99, 24.99, 17.99, 21.99 
    }; 
    ArrayList <String> cartItems = new ArrayList < >(); 
    ArrayList <Double> prices = new ArrayList < >(); 
    java.util.Scanner input = new Scanner(System. in); 
    int userChoice; 
    public void displayMenu() { 
     System.out.printf("**Welcome to the Mustangs Books and DVDs Store**/n/n" + "Choose from the following options:/n1 - Browse books inventory" + "and add a book to the cart/n2 - Browse DVDs inventory and add" + "a DVD to the cart/n3 - View Cart/n4 - Checkout/n9 - Canel order" + "and exit store"); 
    } 
    public static void displayArrays(String[] itemsArray, double[] pricesArray, String itemtype) { 
     System.out.printf("Inventory Number itemType  Prices" + "------------------------------------------" + " 1" + itemsArray[0] + pricesArray[0] + " 2" + itemsArray[1] + pricesArray[1] + " 3" + itemsArray[2] + pricesArray[2] + " 4" + itemsArray[3] + pricesArray[3]); 
    } 
    public void getTotal(ArrayList <Double> prices) { 
     double total = 0; 
     double taxTotal; 
     double tax; 
     int i; 
     for (i = 0; i < prices.size(); i++) { 
      total += prices.get(i); 
     } 
     tax = total * .825; 
     taxTotal = tax + total; 
     System.out.print("Your total is " + taxTotal); 
    } 
    public int getInventoryNumber() { 
     System.out.print("Enter the inventory number you wish to purchase or enter -1 if you wish to see the menu: "); 
     userChoice = input.nextInt(); 
     if (userChoice == -1) { 
      displayMenu(); 
     } 
     int correctNumber = userChoice - 1; 
     return correctNumber; 
    } 
    public String displayArrays(ArrayList <String> items, ArrayList <Double> prices) { 
     int i; 
     System.out.print("Items   Prices" + "----------------------"); 
     for (i = 0; i < items.size(); i++) 
     System.out.printf("%d%s%f", i, items.get(i), prices.get(i)); 
     System.out.print("---------------------- \n" + "Total + tax" + getTotal(prices)); 
    } 
    public void addToCart(int inventoryNumber, ArrayList <String> cart, String[] inventory) { 
     cart.add(inventory[inventoryNumber]); 
    } 
    public void cancelOrder(ArrayList <String> cartItems, ArrayList <Double> prices) { 
     cartItems.clear(); 
     prices.clear(); 
    } 
    public void main(String[] args) { 
     int userInput; 
     do { 
      displayMenu(); 
      userInput = input.nextInt(); 
      if (userInput != 1 && userInput != 2 && userInput != 3 && userInput != 4 && userInput != 9) { 
       System.out.println("This option is not acceptable."); 
      } else if (userInput == 1) { 
       displayArrays(books, booksPrices, "books"); 
       int correctNumber = getInventoryNumber(); 
       addToCart(correctNumber, cartItems, books); 
      } else if (userInput == 2) { 
       displayArrays(dvds, dvdsPrices, "dvds"); 
       getInventoryNumber(); 
       int correctNumber = getInventoryNumber(); 
       addToCart(correctNumber, cartItems, dvds); 
      } else if (userInput == 3) { 
       displayArrays(cartItems, prices); 
      } else if (userInput == 4) { 
       getTotal(prices); 
      } else { 
       cancelOrder(cartItems, prices); 
      } 
     } 
     while (userInput != 9); 
    } 
} 

В строке 59, я получаю сообщение об ошибке «'void' type not allowed". Я не совсем уверен, как это исправить, и я был бы рад помочь. Я понимаю, как у пустоты не может быть возврата, но я немного потерял, почему это происходит.Тип 'Void' не разрешен здесь error

+4

Какая строка линии 59? Обычно это означает, что вы пытаетесь напечатать метод, который ничего не возвращает: 'System.out.println (someMethod());' where someMethod объявлен для возврата 'void'. Решение: не делайте этого. Либо метод возвращает String или объект, либо не пытайтесь распечатать его. –

+0

Это не может быть пример _minimal_, который все еще показывает проблему. –

+0

Вы пытаетесь напечатать результат 'getTotal (prices)', который возвращает 'void', так что это не имеет большого смысла. – misko321

ответ

2

В этом проблема. getTotal метод типа void, и вы пытаетесь его распечатать. Либо измените тип возврата, либо удалите оператор печати.

System.out.print("---------------------- \n" + 
       "Total + tax" + getTotal(prices)); 
3

линия в вопросе это одна:

System.out.print("---------------------- \n" + 
       "Total + tax" + getTotal(prices)); 

Причина вы получаете сообщение об ошибке, что getTotal это ничего не возвращает.

public void getTotal(ArrayList <Double> prices) { 

Если вы хотите getTotal вернуть что-то, вы можете изменить подпись ..

public Double getTotal(ArrayList <Double> prices) { 

, а затем настроить конец функции надлежащим образом. (добавить оператор возврата)

+0

Рассмотрите добавление: Кроме того, 'getTotals()' не должно содержать никаких 'println' или аналогичных операторов внутри него. Методы Getter должны просто возвращать значение и не иметь побочных эффектов. –

Смежные вопросы