访问对象的对象变量?

时间:2015-11-03 20:22:38

标签: java string queue

我正在尝试了解如何获取Customer对象名称和食物,当它已添加到队列中时?所以说我想在第一个客户对象的名称和食物元素添加到队列后使用它来打印字符串?队列偷看方法是占位符,因为我不确定在将对象的名称和食物添加到队列后如何访问它。

结果将是这样的:

“你想要处理什么:披萨或沙拉?

沙拉

詹姆斯的沙拉已经完成!“

代码:

主要课程:

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

客户类:

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



    }

1 个答案:

答案 0 :(得分:2)

您的课程有getset个方法,用于访问课程的属性。

如此简单:

String food = cus.getFood(); //food now contains what is contained in the food variable of your cus object
cus.setName("Bob"); //The name of your customer is now Bob

将允许您获取/设置食物字符串和客户名称。