2015-11-04 67 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())或類似的東西。