1

I am trying to make a menu like this for a userenter image description here

to select a wine type (riesling, chardonnay) and then from that, it shows them the variations of the column the selected. The loop is supposed to cycle at most 16 times, or until the user indicates they're done. This is just one method that I am trying to complete right now.

I wanted to know if I was heading in the right direction. I have made the main method but I wanted to know how to make the method for gathering the input(user enters 1 for riesling, 1 for dry, then it totals it) all inside of a loop.

I would really appreciate it if someone could just help me figure this out. Thank you so much.

import javax.swing.JOptionPane;

public class WineCalc{

public static void main(String[] args){

  String[][]wineTypes = {
                        {"Riesling", "Chardonnay", "Sauvignon Blanc", "Merlot"},
                        {"Dry- $4.50", "Apple- $6.00", "Lime-$4.50", "Plum- $5.00"},
                        {"Off Dry-$4.00", "Lemon-$5.50", "Lemongrass- $6.50", "Black Cherry- $7.50"},
                        {"Sweet- $5.00", "Vanilla- $6.00", "Coconut- $7.00", "Chocolate- $6.00"},
                        };
  double[][]prices = {
                     {4.50, 6.00, 4.50, 5.00},
                     {4.00, 5.50, 6.50, 7.50},
                     {5.00, 6.00, 7.00, 6.00},
                     };

  int[][]counter = {
                   {0,0,0,0},
                   {0,0,0,0},
                   {0,0,0,0},
                   };

}

public static int getWineType(String wineTypes[][]){





return wineTypes[][];
}
}
Hello
  • 113
  • 8
  • You are heading in the right direction. I suggest you make the code you have compile next. – Peter Lawrey Dec 11 '15 at 23:51
  • Cool. May I ask, what is wrong with this code? Was I supposed to put quotation marks around the words in the wineTypes array? – Hello Dec 11 '15 at 23:54
  • I changed all of my wine types and variations to have quotes around them, and then I added commas after the prices array rows. Something definitely appears to be wrong with my parameters for the method calls. – Hello Dec 11 '15 at 23:57
  • All Strings must appear between two `"` I suggest you read up on basic Java syntax. – Peter Lawrey Dec 11 '15 at 23:57
  • There is nothing wrong with the parameters. The problem you have is you must define the body of those methods, make them `static` and chose the right return type i.e. everything else to make it a method. The methods have to be defined outside the scope of `main` as they cannot appear inside another method. – Peter Lawrey Dec 11 '15 at 23:58
  • Can you update the code in the question to avoid confusing anyone new to reading your question? – Peter Lawrey Dec 11 '15 at 23:59
  • Can you attempt the other suggestions? – Peter Lawrey Dec 12 '15 at 00:05
  • Yes, and I changed my code again. I am going to make one method for selecting the column, then another for the row. I need to return an int, but how do I do that with the wineTypes string array? – Hello Dec 12 '15 at 00:08
  • That depends on which int value you need to return. If you can explain in detail what you need to do in English it makes it easier to write the same in Java. – Peter Lawrey Dec 12 '15 at 00:13
  • So, I think the easiest way for this menu to work is to have a user enter a number, which will correspond with the columns, to decide what type of wine they want. within that loop, there'd be another loop, which will ask the user to enter a number 1 through 3 to determine which wine variation they want. The problem for me is, this array is of strings, and I want to do this with integers. – Hello Dec 12 '15 at 00:15

2 Answers2

1

you need to add qoutation marks areound the worrds in the winetypes array.

String[][]wineTypes = {
                    {"Riesling", "Chardonnay", "Sauvignon Blanc", "Merlot"},
                    {"Dry- $4.50", "Apple- $6.00", "Lime-$4.50", "Plum- $5.00"},
                    {"Off Dry-$4.00", "Lemon-$5.50", "Lemongrass- $6.50", "Black Cherry- $7.50"},
                    {"Sweet- $5.00", "Vanilla- $6.00", "Coconut- $7.00", "Chocolate- $6.00"},
                    };

also you need to add commas between the entries in the prices array

double[][]prices = {
                 {4.50, 6.00, 4.50, 5.00},
                 {4.00, 5.50, 6.50, 7.50},
                 {5.00, 6.00, 7.00, 6.00}
                 };

finaly you need to create the methods getWineType(int wineTypes[][]); and getMostOrdered(int wineTypes[][]); and getCombo(int wineTypes[][]); and printReport(int wineTypes[][]);

man-r
  • 208
  • 3
  • 14
  • Fixed that. Now, I want to make the method for the menu, letting the user type 1 to get the column, then typing 1-3 to get the specific row. – Hello Dec 12 '15 at 00:05
  • simplest way to get the user input from the command line is to use the Scanner object have a look at this post http://stackoverflow.com/questions/5287538/how-can-i-get-the-user-input-in-java – man-r Dec 12 '15 at 00:10
  • I'd use scanner but this is for my class and they require me to use JOptionPane – Hello Dec 12 '15 at 00:11
  • i recommend that you read the documentation for JOptionPane https://docs.oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html – man-r Dec 12 '15 at 00:13
  • this link is also useful https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html – man-r Dec 12 '15 at 00:14
  • I'll check these out thank you! The problem for me is how to actually equate a user entering a number with the column they want, same with the row they want, since this is an array of strings. – Hello Dec 12 '15 at 00:18
  • I'm not sure what you mean but an example for 2d array selection is wineTypes[1][2] will get you "Chardonnay" – man-r Dec 12 '15 at 00:28
1

Just to put you more on the right track I will show you Java's Object Oriented way of doing things.

public class Wine{
    private final String name;
    private final List<Type> types = new ArrayList<>();
    private final Map<String, Type> typeMap = new HashMap<>();

    public Wine(String name){
        this.name = name;
    }

    public Wine addType(String name, double price){
        addType(new Type(name, price));
        return this;
    }

    public Wine addType(Type type){
        types.add(type);
        typeMap.put(type.getName(), type);
        return this;
    }

    public Wine.Type get(int index){
        return types.get(index);
    }

    public Wine.Type get(String type){
        return typeMap.get(type);
    }

    public boolean hasType(String type){
        return typeMap.containsKey(type);
    }

    public int totalWineTypes(){
        return types.size();
    }

    @Override
    public String toString(){
        return new StringBuilder().append("[Wine = ").append(name).append(" ").append(types).toString();
    }


    public class Type{
        private final String name;
        private double price;
        public Type(String name, double price){
            this.name = name;
            this.price = price;
        }

        public String getName(){
            return name;
        }

        public double getPrice(){
            return price;
        }

        @Override
        public String toString(){
            return Wine.this.name + "[" + name + ", " + price + "]";
        }
    }
}

main method

public static void main(String[] args) {
    Wine[] wines = new Wine[]{
            new Wine("Riesling").addType("Dry", 4.5).addType("Off Dry", 4.0).addType("Sweet", 5.0),
            new Wine("Chardonnay").addType("Apple", 6.0).addType("Lemon", 5.5).addType("Vanilla", 6.0),
    };
    for (Wine w : wines) {
        System.out.println(w);
        if(w.hasType("Apple")){
            Wine.Type t = w.get("Apple");
            System.out.println();
            System.out.println(t.getName() + " -> " + t.getPrice());
            System.out.println(t);
        }

    }
}

It will display this when run

[Wine = Riesling [Riesling[Dry, 4.5], Riesling[Off Dry, 4.0], Riesling[Sweet, 5.0]]
[Wine = Chardonnay [Chardonnay[Apple, 6.0], Chardonnay[Lemon, 5.5], Chardonnay[Vanilla, 6.0]]

Apple -> 6.0
Chardonnay[Apple, 6.0]
John E.
  • 418
  • 2
  • 10