How can I sort my products by price and name?

So, I have a question, what to do next. I am working on this program and your requirements.

Wings coffee shop

The local coffee shop sells the various products shown below to its customers. You will be asked to write a Java application that can be used to track these elements. In addition, this program provides a way to print a list of items.

Item Name            Price
Coffee               $1.00
Water                $2.00
Milk                 $1.50
Bagel                $1.25
Donut                $0.75

Your program will create a class called Item. This class has the following:

  • String instance variable to hold item name
  • Double instance variable to hold price
  • A constructor that accepts String and double to ini

After creating this class, you write a second class called CoffeeDriver. This class has the following methods:

  • sortName - , .
  • sortPrice - , .
  • main - Item, , .
  • get set

, . , . , 2 , . , , coffee = $1, .

import java.util.Scanner;

public class CoffeeDriver {

//main method
    public static void main (String[] args){
        String[] itemName = {"Coffee, Water, Milk, Donut, Bagel"};
        double[] itemPrice = {1.00, 2.00, 1.50, 0.75, 1.25};
        Scanner input = new Scanner(System.in);
        String decision;

        System.out.println ("Welcome to Wings Coffee Shop");
        System.out.println ("We have a great list of tasty items on our menu.");
        System.out.println ("Would you like to see these items sorted by");
        System.out.println ("name or by price? (n/p): ");
        decision = input.nextLine();
            if (decision == "n"){
                sortName(itemName);
                }
            else {
                sortPrice(itemPrice);
                }
    }

//method to sort by item name and display
    public static void sortName (String[] array){
        for (int i = 0; i < array.length; i++){
            System.out.println (array[i].toString());
            }
    }

//method to sort by item price and display
    public static void sortPrice (double[] array){
        for (int i = 0; i < array.length; i++){
            System.out.println (array[i]);
            }
    }
}


public class Item {
    private String name;
    private double price;

    Item(String itemName, double itemPrice){
        itemName = name;
        itemPrice= price;
    }

    public String getName(){
        return name;
    }
    public void setName(String itemName){
        itemName = name;
    }

    public double getPrice(){
    return price;
    }

    public void setPrice(double itemPrice){
        itemPrice = price;
    }
}
+3
5

.

( OP )

Comparator .

   List<Item> items=new ArrayList<Item>();
   items.add(new Item("A",1.2));
   items.add(new Item("Z",10.2));
   items.add(new Item("B",2.3));
   items.add(new Item("Y",5.4));
   items.add(new Item("B",1.3));


  java.util.Collections.sort(items,new Comparator<Item>()
   {
       public int compare(Item a,Item b)
        {
          if(a.getName().compareTo(b.getName())>0)
            return 1;
          else
          if(a.getName().compareTo(b.getName())<0)
            return -1;
          else
             return 0;            
        }
        public boolean equals(Object a)
         {
            return false;
         }
    });
+3

" , "

java.util.Collections. , . , "".

EDIT: , . .

, , , , , .

0

, , .

0

( , Java API , , ), Item ( ) , / ( , TreeSet).

java.util.Comparator java.util.Collections.sort ( java.util.Arrays.sort).

0

Java ( , , (Double) double, , compareTo, .)

class Item {
 private final String name;
 private final Double cost;
 :
 : //Getters and setters
 :
};

public static void sortByName(List<Item> items) {
 Collections.sort(items, new Comparator<Item>() {
     public int compare(Object o1, Object o2) {
        return (Item)o1.getName().compareTo(o2);
     }

     public boolean equals(Object o1) {
           return ((Item)o1).getName().equals((Item)o2).getName()) )
     } 

 }); 

public static void sortByPrice(List<Item> items) {
 Collections.sort(items, new Comparator<Item>() {
     public int compare(Object o1, Object o2) {
        return (Item)o1.getCost().compareTo(o2.getCost());
     }

     public boolean equals(Object o1) {
           return ((Item)o1).getCost().equals((Item)o2).getCost()) )
     } 

 }); 
0

Source: https://habr.com/ru/post/1791763/


All Articles