when i run my coffe driver i get null for program print

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ANTHONYSEAGER
    New Member
    • Jul 2012
    • 1

    #1

    when i run my coffe driver i get null for program print

    got rid of all errors but it will not print what it is suposed to it runs and prints out

    Welcome to Coffee Driver! Do you want to see our menu sorted by item name or price?/nEnter 'p' for price and 'n' for item name:
    p
    null $0.0

    null $0.0

    null $0.0

    null $0.0

    null $0.0

    null $0.0

    null $0.0

    null $0.0

    null $0.0

    null $0.0

    null $0.0

    null $0.0

    null $0.0

    null $0.0

    here are my codes

    ///"ITEM.JAVA"



    public class Item{

    //delcare instance fields
    private String itemName;
    private double itemPrice;

    //declare constructor
    public Item(String itemName){
    itemName = itemName;
    }
    public Item(double itemPrice){
    itemPrice = itemPrice;

    }

    //delcare setters
    public void setItemName (String itemName){
    itemName = itemName;
    }
    public void setItemPrice (double itemPrice){
    itemPrice = itemPrice;
    }

    //declare getters
    public String getItemName(){
    return itemName;
    }
    public double getItemPrice(){
    return itemPrice;
    }


    }//ends class


    ////"COFFEEDRIVER.J AVA"
    import java.util.*;

    public class CoffeeDriver{

    public static void main(String[] args){

    Scanner keyboard = new Scanner(System. in);

    Item[] item = new Item[5];
    Item[] price = new Item[5];
    String answer;
    String itemName;
    double itemPrice;


    //load array

    item[0] = new Item("Coffee");
    item[1] = new Item("Water");
    item[2] = new Item("Milk");
    item[3] = new Item("Bagel");
    item[4] = new Item("Donut");

    price[0] = new Item(1.00);
    price[1] = new Item(2.00);
    price[2] = new Item(1.50);
    price[3] = new Item(1.25);
    price[4] = new Item(0.75);
    //get user input
    System.out.prin tln("Welcome to Coffee Driver! Do you want to see our menu sorted by item name or price?" +
    "/nEnter 'p' for price and 'n' for item name: ");
    answer = keyboard.nextLi ne();

    if(answer.equal sIgnoreCase("p" )){
    sortPrice(item) ;
    displayItems(it em);
    } else {
    displayItems(it em);
    }//ends if..else

    displayItems(it em);
    sortPrice(item) ;
    displayItems(it em);
    }//ends main
    public static void sortPrice(Item[] p){
    Item temp;
    for(int a = 0; a < p.length-1; a++){
    for(int b = 0; b < p.length-1; b++){
    if(p[b].getItemPrice() >= p[b+1].getItemPrice() ){
    temp=p[b];
    p[b]=p[b+1];
    p[b+1]=temp;
    }//end if
    }//end for
    }//end for
    }//end sortUnits method

    public static void displayItems(It em[] n){
    for(int i=0; i < n.length; i++){
    System.out.prin tln(n[i].getItemName() + "\t" + "$" + n[i].getItemPrice() );
    System.out.prin tln();
    }//end for
    }//end displayItems method

    }//ends class
Working...