厨师做饭和如何回去?

时间:2016-06-21 06:42:45

标签: java

我正在做一个烹饪计划,我坚持做厨师做饭,以及如何计算两位厨师都做过多少餐。

import java.util.Scanner;

public class Cooking {

 int count = 0;
 String chefName;
 String foodName;   
 Scanner scanner = new Scanner(System.in);

 public void chefName() {
    System.out.println("What is the name of chef 1?");
    scanner.next();
    System.out.println("What is the name of chef 2?");
    scanner.next();
    count++;
    //System.out.println("This week you made " + count + " deliveries!");
    //System.out.println("Bye, " + driverName + "!");
}

  public void foodName() {
    Scanner console = new Scanner(System.in);
    System.out.println("Meal 1: What would you like to eat?");
    foodName = scanner.nextLine();
    System.out.println(chefName + " is cooking " + foodName);
    System.out.println("Meal 2: What would you like to eat?");
    foodName = scanner.nextLine();
    System.out.println(chefName + " is cooking " + foodName);
    scanner.next();
    count++;
  }
}

public class MainProgram1 {

public static void main(String[] args) {
    // Create cooking records
    Cooking cookingByBob = new Cooking();
    cookingByBob.chefName = "Bob";

    Cooking cookingByAnne = new Cooking();
    cookingByAnne.chefName = "Anne";

    // Start delivering
    System.out.println("Hi!");
    cookingByBob.chefName();
    cookingByAnne.foodName();

当我跑步时,安妮是烹饪所有餐点的人。

我如何做到这样安妮做饭1然后鲍勃2和3和安妮4 5?我怎么算上所有的饭菜?

2 个答案:

答案 0 :(得分:2)

你的主要问题是你得到了很多抽象错误;这使得您很难将代码添加到您真正需要的代码中。

示例:类应该代表一些“真实”的概念;与现实相似的东西。你的班级Cooking不是那样的。它首先是这个类向用户询问厨师和食物的名称;但另一方面,您的主程序以编程方式为之后的那些字段分配值。这非常令人困惑。

考虑改变这样的事情:

class Cook {
   private final String name;
   Cook(String name) { this.name = name; }

允许您创建

Cook bob = new Cook("bob");

然后你可以像库克一样添加方法

class Cook { ...
   void prepareFood(String foodName) ...
   List<String> getPreparedFoods() ...

现在你可以做到

bob.prepareFood("1");
anne.prepareFood("2");

以及稍后

System.out.println("food by bob: " + bob.getPreparedFoods());

或类似的东西。问题是:你当前的程序中存在很多“噪音”,这使你很难以正确的方式写下这个简单的序列。

长话短说:在开始编码之前,请考虑一下您要实施的“故事”所需的“事物”和“行为”。然后围绕这些抽象构建您的程序。

答案 1 :(得分:1)

这应该是一个使用您熟悉的工具的有效解决方案,即使阅读您的代码显示您缺乏对一些基本Java概念的理解。不要只是复制粘贴这个,尝试修改自己的代码。首先,您可以删除所有过时的行。

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

    Notification.Builder mNotificationBuilder = new Notification.Builder(context);
    RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);

    mNotificationBuilder.setSmallIcon(R.drawable.ic_small_notification)
            .setDefaults(Notification.DEFAULT_ALL)
            .setContent(contentView)
            .setAutoCancel(true);


    Notification notification = mNotificationBuilder.build();

    mNotificationManager.notify(MANUAL_NOTIFICATION_ID, notification);

主要方法:

public class Kitchen {

    int count = 0;
    String firstChefName;
    String secondChefName;
    String foodName;    
    Scanner scanner = new Scanner(System.in);

    public void setChefName() {
        System.out.println("What is the name of chef 1?");
        firstChefName = scanner.next();
        System.out.println("What is the name of chef 2?");
        secondChefName = scanner.next();
    }

    public void setFoodName() {
        System.out.println("Meal 1: What would you like to eat?");
        foodName = scanner.nextLine();
        System.out.println(firstChefName + " is cooking " + foodName);
        count++;

        System.out.println("Meal 2: What would you like to eat?");
        foodName = scanner.nextLine();
        System.out.println(secondChefName + " is cooking " + foodName);
        count++;

        System.out.println("Meal 3: What would you like to eat?");
        foodName = scanner.nextLine();
        System.out.println(secondChefName + " is cooking " + foodName);
        count++;

        System.out.println("Meal 4: What would you like to eat?");
        foodName = scanner.nextLine();
        System.out.println(firstChefName + " is cooking " + foodName);
        count++;

        System.out.println("Meal 5: What would you like to eat?");
        foodName = scanner.nextLine();
        System.out.println(firstChefName + " is cooking " + foodName);
        count++;
    }

}