Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1136
  • Last Modified:

write a Java application

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 initialize the instance variables * A get and set method for each instance variable
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. After initializing the array, prompt the user for how they want to see the list – sorted by name or price. Then call the appropriate method.
Be sure your program demonstrates good programming style (appropriate comments, identifier names, indenting, etc).
Although it is not required to look this way, your output might look something like the following:
(appropriate comments, identifier names, indenting, etc).
Although your output is not required to look this way, it might look something like:
Please enter a value:1.23
 Please enter a value: 2.34
 Please enter a value: 3.45
Please enter a value: 4.56
Please enter a value: 5.00
The sum of these values is $16.58
The average of these values is: $3.32
The values higher than the average are: $3.45
$4.56 $5.67
0
Jenn0101
Asked:
Jenn0101
  • 9
  • 9
1 Solution
 
for_yanCommented:
Accirding to EE rules experts cannot write applications for hoemwork assignemnts
If you write a general framework of the application, we can help you to correct and debug your application.

0
 
Jenn0101Author Commented:
I have one do I post it here
0
 
for_yanCommented:
Sure, post it here
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TommySzalapskiCommented:
Put code tags around it and it will be easier for us to read.
Like this (except without the spaces
[ co d e ]
int i = 5;
etc etc
[ / c o d e ]
0
 
Jenn0101Author Commented:
import java.util.Scanner;

public class CoffeeShop  { 

//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;
    }
}

Open in new window

0
 
Jenn0101Author Commented:
This is just my rough draft
0
 
Jenn0101Author Commented:
my first class works fine and the output is correct I just need help with the putting together the second class
0
 
for_yanCommented:
Well, it is almost working

Where is CoffeDriver ?
0
 
for_yanCommented:
So waht is your problem in writing the second class ?
0
 
Jenn0101Author Commented:
ok I think I may have figured out my own problem I just need help in correcting it. My first class is supposed to be named "Item" and My second class is supposed to be named " CoffeeDriver" can you look at my code and tell me if I have the second class listed as Item and I named the first one CoffeeShop? How do I fix this mess
0
 
for_yanCommented:
You split them into two files (maybe they are).

Then you rebame file to CoffeDriver.java
and another one to Item.java

and that's  more or les how it would be
0
 
for_yanCommented:
You don't have any constructore in your currently named CoffeShop, so you don't need to rename that
0
 
Jenn0101Author Commented:
ok here's the thing my first out put is correct but I'm not getting the names only the prices
0
 
for_yanCommented:
Yes, and your application does not sort anything by the way.

Can we use ArrayList 's ?
0
 
Jenn0101Author Commented:
yes I believe so
0
 
Jenn0101Author Commented:
I am pretty new to this so it is still a little confusing
0
 
for_yanCommented:

So this is ordering as requested
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class CoffeShop  {

     static Item[] items;
//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};

    items = new Item[itemName.length];
    for(int j=0; j<itemName.length; j++){
         items[j] = new Item(itemName[j], itemPrice[j]);
    }
     


        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.trim().equals("n")){
               
                sortName(items);
                for(int j=0; j<items.length; j++){
                    System.out.println(items[j].getName() + " " + items[j].getPrice());

                }

                }
            else {
                sortPrice(items);
                     for(int j=0; j<items.length; j++){
                    System.out.println(items[j].getName() + " " + items[j].getPrice());

                }
                }
    }

//method to sort by item name and display
    public static void sortPrice (Item[] array){
    Arrays.sort(array, new Comparator<Item>(){
       public int compare(Item o1, Item o2){
           if(o1.getPrice()>o2.getPrice())return 1;
           else return -1;

        }

    });


       // 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 sortName (Item [] array){
       Arrays.sort(array, new Comparator<Item>(){
       public int compare(Item o1, Item o2){
         return o1.getName().compareTo(o2.getName());

        }

    });
    }
}


 class Item {
    private String name;
    private double price;

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

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

    public double getPrice(){
    return price;
    }

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

Open in new window


Output:
Welcome to Wings Coffee Shop
We have a great list of tasty items on our menu.
Would you like to see these items sorted by
name or by price? (n/p): 
p
Donut 0.75
Coffee 1.0
Bagel 1.25
Milk 1.5
Water 2.0

Open in new window

0
 
for_yanCommented:
But,
0) First try an d compile and make sure it works for you

Then

1) You need to change the name to CoffeeDriver

2) To have everything in the main() method  it is a bad practice
    You should move it to constructor of the class and use main just to instantiate
the instance of the class

So that is left to you - so this is not the final code of your homework

3) let me know if yiou have any quiestions
0
 
Jenn0101Author Commented:
Excellent help and very quick with the responses! Thank you for the help!
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now