Coffee shop

Ads

 
 

Share on Google+Share on Google+

richard phillips
Coffee shop
1 Answer(s)      6 years and 6 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

Ads
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);
    }
    }

Ads









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
Version of ca.genovese>coffee-cats dependency
List of Version of ca.genovese>coffee-cats dependency
Version of com.theoryinpractise>coffee-maven-plugin dependency
List of Version of com.theoryinpractise>coffee-maven-plugin dependency
Maven Dependency coffee-cats >> 0.1
You should include the dependency code given in this page to add Maven Dependency of ca.genovese >> coffee-cats version0.1 in your project
Maven Dependency coffee-maven-plugin >> 1.1.1
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.1.1 in your project
Maven Dependency coffee-maven-plugin >> 1.4.0
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.0 in your project
Maven Dependency coffee-maven-plugin >> 1.4.11
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.11 in your project
Maven Dependency coffee-maven-plugin >> 1.4.14
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.14 in your project
Maven Dependency coffee-maven-plugin >> 1.4.2
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.2 in your project
Maven Dependency coffee-maven-plugin >> 1.4.5
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.5 in your project
Maven Dependency coffee-maven-plugin >> 1.4.8
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.8 in your project
Maven Dependency coffee-maven-plugin >> 1.1.0
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.1.0 in your project
Maven Dependency coffee-maven-plugin >> 1.1.2
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.1.2 in your project
Maven Dependency coffee-maven-plugin >> 1.1.3
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.1.3 in your project
Maven Dependency coffee-maven-plugin >> 1.4.1
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.1 in your project
Maven Dependency coffee-maven-plugin >> 1.4.10
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.10 in your project
Maven Dependency coffee-maven-plugin >> 1.4.12
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.12 in your project
Maven Dependency coffee-maven-plugin >> 1.4.13
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.13 in your project
Maven Dependency coffee-maven-plugin >> 1.4.15
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.15 in your project
Maven Dependency coffee-maven-plugin >> 1.4.16
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.16 in your project
Maven Dependency coffee-maven-plugin >> 1.4.3
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.3 in your project
Maven Dependency coffee-maven-plugin >> 1.4.4
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.4 in your project
Maven Dependency coffee-maven-plugin >> 1.4.6
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.6 in your project
Maven Dependency coffee-maven-plugin >> 1.4.7
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.7 in your project
Maven Dependency coffee-maven-plugin >> 1.4.9
You should include the dependency code given in this page to add Maven Dependency of com.theoryinpractise >> coffee-maven-plugin version1.4.9 in your project
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
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