Solved

write a Java application

Posted on 2011-09-07
19
1,121 Views
Last Modified: 2012-05-12
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
Comment
Question by:Jenn0101
  • 9
  • 9
19 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36500237
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
 

Author Comment

by:Jenn0101
ID: 36500288
I have one do I post it here
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500291
Sure, post it here
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36500323
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
 

Author Comment

by:Jenn0101
ID: 36500352
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
 

Author Comment

by:Jenn0101
ID: 36500370
This is just my rough draft
0
 

Author Comment

by:Jenn0101
ID: 36500383
my first class works fine and the output is correct I just need help with the putting together the second class
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500387
Well, it is almost working

Where is CoffeDriver ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500397
So waht is your problem in writing the second class ?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Jenn0101
ID: 36500398
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36500412
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36500420
You don't have any constructore in your currently named CoffeShop, so you don't need to rename that
0
 

Author Comment

by:Jenn0101
ID: 36500437
ok here's the thing my first out put is correct but I'm not getting the names only the prices
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500463
Yes, and your application does not sort anything by the way.

Can we use ArrayList 's ?
0
 

Author Comment

by:Jenn0101
ID: 36500491
yes I believe so
0
 

Author Comment

by:Jenn0101
ID: 36500494
I am pretty new to this so it is still a little confusing
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36500599

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
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36500606
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
 

Author Closing Comment

by:Jenn0101
ID: 36507211
Excellent help and very quick with the responses! Thank you for the help!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
This is about my first experience with programming Arduino.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now