Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


richard phillips
Coffee shop
1 Answer(s)      3 years and 10 months ago
Posted in : Java Beginners


How would i write a program that says's..Welcome to Wings Coffee Shop. We have a great list items on our menu.Would you like to see items sorted by name or price? (n/p):n Item Name Price Bagel $1.25 Water $2.00 Coffee $1.00 Donut $0.75 Milk $1.50


Advertisement
View Answers

March 11, 2011 at 12:44 PM


import java.util.*;

class Item {
        String name;
        double price;
        Item(String name,double price){
        this.name=name;
        this.price=price;
        }
        public void setName(String name){
                this.name = name;
        }
        public String getName() {
                return name;
        }
        public void setPrice(double price) {
                this.price = price;
        }
        public double getPrice(){
                return price;
        }
       }
class NameComparator implements Comparator{
    public int compare(Object o1, Object o2){    
       String name1 = ((Item)o1).getName();        
       String name2 = ((Item)o2).getName();
       return name1.compareTo(name2);
 }
}
class PriceComparator implements Comparator{
  public int compare(Object o1, Object o2){
 if (((Item)o1).getPrice() < ((Item)o2).getPrice()) return -1;
        if (((Item)o1).getPrice() > ((Item)o2).getPrice()) return 1;
        return 0;
    } 
}
class  CoffeeDriver
{
    static ArrayList<Item> list=new ArrayList<Item>();
    public static void sortName(){
    Collections.sort(list,new NameComparator());
                for(Item data: list){
               System.out.println(data.getName()+"\t "+data.getPrice());
    }
    }
    public static void sortPrice(){
    Collections.sort(list,new PriceComparator());
                for(Item data: list){
               System.out.println(data.getName()+"\t "+data.getPrice());
    }
    }
    public static void main(String[] args) 
    {
        Scanner input=new Scanner(System.in);

               list.add(new Item("Bagel",1.25));
               list.add(new Item("Water",2.00));
               list.add(new Item("Coffee",1.00));
               list.add(new Item("Donut",0.75));
               list.add(new Item("Milk",1.50));
               System.out.println("Sorted By Name");
               System.out.println("Sorted By Price");
               System.out.println("Exit");
                boolean exit=false;
               do{
               System.out.print("Enter your choice: ");
               int choice=input.nextInt();

             switch(choice){
             case 1:
                 sortName();
                 break;
             case 2: 
                 sortPrice();
             break;
             case 3:
                 exit=true;
             break;
             default:
                 System.out.println("Invalid");
         }
               }
         while(!exit);
    }
    }


Related Tutorials/Questions & Answers:
Coffee shop
Coffee shop  How would i write a program that says's..Welcome to Wings Coffee Shop. We have a great list items on our menu.Would you like to see... $2.00 Coffee $1.00 Donut $0.75 Milk $1.50   
cofee shop
)); list.add(new Item("Coffee",1.00)); list.add(new
Advertisements
Flower Shop Ordering To Go
Flower Shop Ordering To Go   Create a flower shop application which deals in flower objects and use those flower objects in a bouquet which can then be sold. The flower object has a flower name, color, quantity. Adding
Computer shop mangerment
Computer shop mangerment   Part A You are supposed to write a class Computer to stores the computer id, description, brand, price, speed and another... of it. Part B Develop array based java program to help a local computer shop
Shop and win campaign drive web project in strictly HTML in addition to PHP languages only.(it should have both languages)
Shop and win campaign drive web project in strictly HTML in addition to PHP languages only.(it should have both languages)  Shop N win campaign... a campaign drive dubbed ?shop and win . In this drive, a customer wins a particular
Ajax - An Introduction
Ajax - An Introduction         Ajax-an introduction Developing web application has now been a cup of coffee for developers having the knowledge of Ajax
Open Source e-commerce
Online Shop E-Commerce Solutions Open source Commerce is an Open Source based online shop e-commerce solution that is available for free under.... The software is being developed by group of like-minded shop owners, programmers, designers
Shopping cart
Shopping cart Overview A web based shopping cart is something like the original grocery shop shopping cart that is used by the customer in selecting certain products. Finally after selection the customer confirms orders
Coorg Hill Station in India
; incredibly scenic beauty, verdant valleys, eye catching coffee plantations
Flex Outsourcing, Outsourcing Flex development work
Flex Outsourcing Flex is a new but powerful open-source framework that allows users to create highly interactively visually rich and customize solutions. Due to being new and complex technology it is not a cup of coffee for everyone
Web Search Applications ? Find What You Want Anytime, Anywhere.
into a buzzing network for people to shop, research, publish, and network
Spring framework implementation
at the coffee shop. Create a technology solution for this problem to automate the coffee shop problem using springs? Thanks... someone help me in implementing the below case in spring? Coffee Take out problem
java - Java Beginners
java  why java is called so?  Hi Friend, It was named after the name of coffee from java island which was so famous and coffee is itself called Java in US slang. Thanks
Java Class Question?
to make a choice for a Coffee cup size, S for Small, T for Tall, G for Grande and V... if they would like to add a flavor to the coffee and add an additional $1.00 to the price of the coffee, display the total price of the cup of coffee after
Codings - Java Beginners
Codings  Can anyone help me out in doing a java program for grocery shop maintenance
Java Class Question?
to make a choice for a Coffee cup size, S for Small, T for Tall, G for Grande... if they would like to add a flavor to the coffee and add an additional $1.00 to the price of the coffee, display the total price of the cup of coffee after
Java Class Question?
to make a choice for a Coffee cup size, S for Small, T for Tall, G for Grande and V... if they would like to add a flavor to the coffee and add an additional $1.00 to the price of the coffee, display the total price of the cup of coffee after
java icon in control panel - Java Beginners
java icon in control panel  i have a folder icon in the contrl panel instead of a coffee cup when i click on it says not to be found but i have java 6 with 7 downloaded i have windows vista preium
Inheritance and Composition - Java Beginners
object. For example, it might be useful if the coffee cup object of your program could contain coffee. Coffee itself could be a distinct class, which your program could instantiate. You would award coffee with a type if it exhibits
Unable looping using JOptionPane - Java Beginners
class Shop{ public static void main(String [] args){ Shop shop = new Shop..., Try the following code: import javax.swing.*; public class Shop{ public static void main(String [] args){ Shop shop = new Shop(); String mainMenu
PaintShopProTutorials
Paint Shop Pro Tutorials   ... NNTP forums for Paint Shop Pro, Photo Album and Studio are now available... Shop Pro Tutorial The only thing better than getting new software
Use if, if else,for,while,do wile ad switch in same Java Class
..   import java.util.*; import java.text.*; class Shop{ public String... double total; public Shop(){} public Shop(String item,int quantity, double...-(d1*p1))*q1; list.add(new Shop("T Shirt",q1,p1,d1,t1)); break; case 2
using if and switch stmt - Java Beginners
: import java.util.*; import java.text.*; class Shop{ public String item; public... total; public Shop(){} public Shop(String item,int quantity, double price,double...; double t1=(p1-(d1*p1))*q1; list.add(new Shop("T Shirt",q1,p1,d1,t1)); break
VoIP Palm
.    VoIP Palm Your Hand Alan sits alone at a table in a coffee shop slowly sipping a large cappuccino. His advanced smart phone sits
Joomla Starter Package
unlimited numbers of pages or web components online shop or online directory
facecart
online shop quickly and cost effectivly. It is apropriate for large stores
dvd rental system - Java Beginners
record of a customer, and a vedio in the video shop. and save this to a cvs file
Shopping in Agra
it with the best you can get from any shop. Below are listed some of the most popular places and things one can shop at Agra. Shopping Places of Agra: Shopping... to shop at Agra. It is close to Agra Cantt Railway Station. It is convenient
Java Showroom Application
*; import java.text.*; class Shop { public String item; public int...; public Shop() { } public Shop(String item, int quantity, double price...)) * q1; list.add(new Shop("T Shirt", q1, p1, d1, t1)); break
Advertisements
 

 

 

DMCA.com