-2

This is the code that I have so far. I am using string methods to represent the taco names and double to represent the price. However, I have no clue how to start the array of taco names and prices. I need the array to produce an output that looks like this:

Sorted Tacos are
Taco Prices Crispy Potato Soft Taco 0.99
Taco Prices Crunchy Taco 1.19
Taco Prices Soft Taco 1.19
Taco Prices Doritos Locos Taco (Nacho Cheese) 1.49
Taco Prices Crunchy Taco Supreme 1.59
Taco Prices Soft Taco Supreme 1.59
Taco Prices Chicken Soft Taco 1.79
Taco Prices Double Decker Taco 1.89
Taco Prices Doritos Locs Tacos(Fiery) Supreme 1.89
Taco Prices Double Decker Taco Supreme 2.29

Someone please help.

import java.util.Scanner;
public class TacoSortProgram {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Write a program that sorts prices of 10 tacos in ascending order based on the price, using arrays

        System.out.println ("Welcome to the taco price sorter! Enter 10 taco names and prices and I'll sort it!");
        Scanner keyboard = new Scanner(System.in);

           //       System.out.printf("$%4.2f for each %s ", price, item);
           //       System.out.printf("\nThe total is: $%4.2f ", total);

            //process for item one
            System.out.println("Enter the name of taco 1");
            String taco = keyboard.nextLine();
            System.out.println("Enter taco's price");
            double price = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 2");
            String taco2 = keyboard.nextLine();
            System.out.print("Enter taco's price\n");
            double price2 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 3");
            String taco3 = keyboard.nextLine();
            System.out.println("Enter taco's price");
            double price3 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 4");
            String taco4 = keyboard.nextLine();
            System.out.println("Enter taco's price");
            double price4 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 5");
            String taco5 = keyboard.nextLine();
            System.out.print("Enter taco's price\n");
            double price5 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 6");
            String taco6 = keyboard.nextLine();
            System.out.println("Enter taco's price");
            double price6 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 7");
            String taco7 = keyboard.nextLine();
            System.out.println("Enter taco's price");
            double price7 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 8");
            String taco8 = keyboard.nextLine();
            System.out.print("Enter taco's price\n");
            double price8 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 9");
            String taco9 = keyboard.nextLine();
            System.out.println("Enter taco's price");
            double price9 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 10");
            String taco10 = keyboard.nextLine();
            System.out.println("Enter taco's price");
            double price10 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Sorted tacos are");
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
PaulL
  • 45
  • 1
  • 8
  • Use an array, or a List, containing instances of a Taco class. Create that Taco class, with 2 fields inside: the name, and the price. The google on how to sort a list/array of objects. Whenever you have the same code repeated a number of times like in the above, you must think: "That can't be right. Let's use a loop.". The teacher was kind by asking for 10 tacos. What if he had asked for 1000 tacos. You wouldn't create 1000 variables and repeat the same 4 lines of code 1000 times, right? You know you must use an array, but I don't see any array in the posted code. – JB Nizet Sep 20 '15 at 13:51

4 Answers4

2

You should create a class to represent your tacos, like so:

public class Taco {
public String name;
public double price;

public Taco() {}

Taco(String name, double price){
    this.name  = name;
        this.price = price;
    }
}

You can then sort them using Collections.sort(), passing it a comparator:

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Test {
public static void main(String[] args) {
    // Write a program that sorts prices of 10 tacos in ascending order
    // based on the price, using arrays

    ArrayList<Taco> tacos = new ArrayList<>();
    Scanner keyboard = new Scanner(System.in);

    System.out.println("Welcome to the taco price sorter! Enter 10 taco names and prices and I'll sort it!");

    // Loop 10 times.
    for( int i = 0; i < 10; i++ ){
        // Get the name for each new item
        System.out.println("Enter the name of taco " + (i + 1) );
        String taco = keyboard.nextLine();
        System.out.println("Enter taco's price");
        double price = Double.parseDouble(keyboard.nextLine());
        Taco t = new Taco(taco, price);

        // Add it to our array list.
        tacos.add(t);
    }

    //Sort the list:
    Collections.sort(tacos, new Comparator<Taco>(){
         public int compare(Taco o1, Taco o2){
             if(o1.price == o2.price)
                 return 0;
             return o1.price < o2.price ? -1 : 1;
         }
    });


    System.out.println("Sorted tacos are:");
    for( Taco t : tacos ){
        System.out.println("Taco Prices: " + t.name + " " + t.price );
    }

    keyboard.close();
}
}
jste89
  • 426
  • 4
  • 15
  • The comparator should not be the Taco. You shouldn't create a Taco to sort other tacos. – JB Nizet Sep 20 '15 at 14:49
  • Agreed, I've now updated my code to provide a better answer. – jste89 Sep 20 '15 at 14:54
  • Much better. You could now remove the no-arg constructor of Taco. You could also use `return Double.compare(o1.getPrice(), o2.getPrice());` instead of reimplementing it. You should definitely make the two fields private, and not public. – JB Nizet Sep 20 '15 at 14:57
0

You can create a class called Taco.

Once a user has entered the price and name, you can initiate an object of class Taco and assign the values.

Keep all the objects in an array.

And use a sorting algorithm.

Shamal Perera
  • 1,611
  • 1
  • 18
  • 21
0

Create a separate class for taco. Then create the name and price as its member variables. Now create instance of the taco as an array. Like taco tacoInst[10]. Now you can refer to each taco's price be tacoInst[0].price. Now when you sort the price, the names automatically gets sorted. Or vice versa.

S Praveen Kumar
  • 341
  • 1
  • 13
0

To add to people's answers above, and yeah - you should develop a Taco class, why not try implementing Comparable? I'm not sure how advanced your java is and whether you've done interfaces yet, but looking at the level of the problem you've been set, it seems like it would get you some solid extra credit/showing off cred.

You can find a good example in Enno Shioji's answer here (the first one). Basically what you'd be doing is declaring that Taco implements Comparable<Taco> and then overriding the compareTo method. This will allow you to automatically sort the tacos using Collections.sort(tacos); (here the name of the array is tacos, if you called it, say, tacoArray the command would be Collections.sort(tacoArray);)

EDIT: Further to JB Nizet's comment below this answer, what I was trying to do here was to signpost new Java techniques you might not have been familiar with. He's perfectly correct in it not being the single greatest implementation the world has ever seen, my logic was that if you weren't aware of this stuff then it might provide an interesting challenge.

Community
  • 1
  • 1
MrB
  • 818
  • 8
  • 28
  • 1
    There are two very good reasons to NOT make Taco Comparable. First reason: Comparable should be consistent with equals(). That means equals() and hashCode() should also be implemented if Comparable, and that means that two tacos with the same price but not the same name would become equal, which is not very realistic. Second reason: Comparable defines a natural order for a class. Why would the price define a natural order? Why not the name? Sorting by price is a specific use-case, and that should thus be implemented by a specific Comparator, not by making the price define the natural order. – JB Nizet Sep 20 '15 at 14:22
  • Absolutely agreed, and I glossed over a lot. My answer (and, yes, I should have made that clearer - I'll edit following this comment) was aimed more at introducing new concepts and techniques to what seems very much like a homework assignment. However, I'm not sure about the equals() and hashcode() point in general terms, there's nothing preventing his compareTo method taking account of name to sort Large Taco 1.99 after Breakfast Tac 1.99, it's just a little more advanced. However, as I say, you're right that I should have been clearer about the intent of my answer. – MrB Sep 20 '15 at 14:25
  • No problem in making Taco Comparable per se, provided it's consistent with equals, and doesn't just compare by price. But since that's what the user wants, a Comparator is more appropriate. – JB Nizet Sep 20 '15 at 14:32