2015-11-04 57 views
0

我想看看如何获​​取客户对象的名称和食物,当它已被添加到队列?所以说,我想打印一个字符串,使用第一个客户对象的名称和食物元素添加到队列后?队列窥视方法是占位符,因为我不确定如何在将队列添加到队列中后访问对象的名称和食物。如果我打印peek方法,它只是给我的内存位置,而不是对象的食物或名称。对象添加到队列后访问对象变量?

结果会是这样的:

“你想要什么工艺做?!披萨或沙拉

沙拉

詹姆斯的沙拉做”

代码:

主要类:

import java.util.Scanner; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.LinkedList; 
import java.util.Queue; 

public class Main { 

    public static void main(String[] args) throws FileNotFoundException { 
     File customerTxt = new File("customer.txt"); 
     Queue<Customer> pizza = new LinkedList<Customer>(); 
     Queue<Customer> salad = new LinkedList<Customer>(); 
     try { 
      Scanner readCus = new Scanner(customerTxt); 
      Scanner readFood = new Scanner(System.in); 
      while (readCus.hasNextLine()) { 
       String line = readCus.nextLine(); 
       String[] strArray = line.split(","); 
       String customerName = strArray[0]; 
       String customerFood = strArray[1]; 
       Customer cus = new Customer(customerName, customerFood); 
       if (customerFood.equalsIgnoreCase("salad")) { 
        salad.add(cus); 
       } 
       if (customerFood.equalsIgnoreCase("pizza")) { 
        pizza.add(cus); 
       } 
      } 
      if (pizza.isEmpty() == false && salad.isEmpty() == false) { 
       System.out.println("What kind of food would you like to make?"); 
       String foodChoice = readFood.nextLine(); 
       if (foodChoice.equalsIgnoreCase("salad")) { 
        System.out.println(salad.peek()); 
       } 
       if (foodChoice.equalsIgnoreCase("pizza")) { 
        System.out.println(salad.peek()); 
       } 
      } 
      if (pizza.isEmpty() == true && salad.isEmpty() == false) { 
       System.out.println("There are no Pizzas left to process. I will just finish the rest of the Salads"); 
       while (salad.isEmpty() == false) { 
        System.out.println(salad.peek()); 
       } 
      } 
      if (pizza.isEmpty() == false && salad.isEmpty() == true) { 
       System.out.println("There are no Salads left to process. I will just finish the rest of the Pizzas"); 
       while (pizza.isEmpty() == false) { 
        System.out.println(pizza.peek()); 
       } 
      } 
     } 

     catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

Customer类:

public class Customer { 

    public String name = ""; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String food = ""; 

    public String getFood() { 
     return food; 
    } 

    public void setFood(String food) { 
     this.food = food; 
    } 

    public Customer(String customerName, String customerFood) { 
     this.name = customerName; 
     this.food = customerFood; 
    } 



    } 
+2

覆盖Customer类中的'toString()'以包含食物和名称值 –

+1

在附注中,当!!<!布尔值> = false时很难找到< <布尔值>'会很好。也许这只是我,但我认为你应该把'pizza.isEmpty()== false && salad.isEmpty()== false'改成'! pizza.isEmpty()&&! salad.isEmpty()'或'!(pizza.isEmpty()|| salad.isEmpty())' – Aaron

回答

1

LinkedList.peek(),它返回正确的对象。我相信你只是看到该对象的哈希,因为你打印Customer对象,它并没有重新定义.toString():您正在使用Object.toString()返回你所看到的哈希值。

要么重新定义Customer.toString()由扎克·麦康伯的建议,如果你总是希望代表你Customer正如它的名字和食物的选择,或者转而选择System.out.println(queue.peek().getName() + " choosed " + queue.peek().getFood())或类似的东西。