Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3949976
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:33:26+00:00 2026-05-20T01:33:26+00:00

So I have a question on what to do next. I am working on

  • 0

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

Wings Coffee Shop

A local coffee shop sells a variety of different items shown below to their customers. You are asked to write a Java application that can be used to keep track of these items. Additionally, this program provides a way to print out a listing of the 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, named Item. This class has the following:

  • A String instance variable to hold the item name
  • A double instance variable to hold the price
  • A constructor that takes a String and double to ini

Once you have this class created, you write a second class named CoffeeDriver. This class has the following methods:

  • sortName – this method sorts the array of items by item name and then displays the name and price of all items on the screen
  • sortPrice – this method sorts the array of items by item price and then displays the name and price of all items on the screen
  • main – It creates an array of Item objects using the data above to set each Item’s information.
  • A get and set method for each instance variable

This is all I have so far. I am having a hard time thinking how to sort an array. So if I sort 2 different arrays how do I keep them in proper order. Like for instance coffee = $1 how do I sort it and keep those together.

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:33:26+00:00Added an answer on May 20, 2026 at 1:33 am

    Correction to the Item class definition.

    You need to assign parameters to the fields (In OP you’ve assign field to parameter)

    Use Comparator object parameter to implement sorting for individual field.

       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;
             }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a card game iPhone app and have a question about
I'm back with another similar question. I am currently working on a Java program
I'm a CS master student, and next semester I will have to start working
i have a question on a problem i am working on. I have to
this is a follow up on my question on paypal integration. I'm working ona
I have a question about scanner please;I working at a small company; we have
I have question quite much related to this one I asked a while ago
This question is next to the question I had asked here . I am
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have question regarding the SQLAlchemy. How can I add into my mapped class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.