java 餐厅的java代码多步骤
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17060477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java codes for a restaurant multiple steps
提问by user9239223
Question 1:
问题 1:
Assuming “OOP Restaurant” owner has engaged you to write a Java program to take the restaurant customer order. The beverage menu of the restaurant is as shown in Table 1. This program must allow customer to decide how many items he/she wish to order. Then, it will let the customer to choose the each item according to his/her preference. Upon completion of the ordering, the program will display the total amount of the order to the customer. Then, the program must ask the customer whether to do another set of ordering.
假设“OOP 餐厅”老板让您编写一个 Java 程序来接受餐厅客户的订单。餐厅的饮料菜单如表 1 所示。该程序必须允许客户决定他/她希望订购多少个项目。然后,它会让客户根据他/她的喜好选择每个项目。完成订购后,程序将向客户显示订购的总金额。然后,程序必须询问客户是否进行另一组订购。
In your code, you must create a method to process the item choice and return the price of the item using the following method signature:
在您的代码中,您必须创建一个方法来处理商品选择并使用以下方法签名返回商品的价格:
double processItem(int input)
The sample out is as shown in Figure 1.
样本输出如图 1 所示。
Beverage | Price
Fried Rice | RM5.50
Chicken Rice | RM5.00
Toast Bread | RM2.00
Mixed Rice | RM3.80
Table 1
How do i even start this? i'm a law student but got forced to do this please do help me out your kindness would be repayed thanks in advance
我该如何开始?我是一名法学院学生,但被迫这样做,请帮助我,您的好意将提前得到回报,谢谢
回答by Nick Holt
Having written the answer out this is quite a complex task for a non-programmer. There's concepts of how a program should be structured to contend with after which comes the compilation and running.
写出答案后,这对于非程序员来说是一项相当复杂的任务。关于程序应该如何构建以应对编译和运行之后的概念。
This answer is in just that order, first I'll explain what I think the key points are (sure I've missed some, as much of this is second nature) then I'll give you pointers on how to run the code.
这个答案就是按照这个顺序,首先我会解释我认为的关键点是什么(当然我错过了一些,因为其中大部分是第二天性)然后我会给你一些关于如何运行代码的指导。
Step 1.
步骤1。
Think about what's involved if you were going to do this on paper - you'd have a list of beverages each with a name and a price (the menu). An order comprises of one or more beverages from the menu in varying quantities. You multiple the price of each beverage by the quantity to get the cost of the order.
想想如果你打算在纸上做这件事会涉及到什么——你会有一个饮料清单,每个饮料都有名字和价格(菜单)。一份订单包括菜单中不同数量的一种或多种饮料。您将每种饮料的价格乘以数量以获得订单成本。
Step 2.
第2步。
Modern computer languages use a technique call Object Orientation, which in a nutshell involves describing the entities in general terms to create what is known as classes. When presented with a problem, like that in step 1, a good rule of thumb in deciding what the classes should be is to look at the nouns - in this case beverage, menu and order look like good candidates. A class typically has attributes (the data that will make instances unique) and behaviour (operations based on that data) though you'd don't neccessarily have to have both as you can see from the code below.
现代计算机语言使用一种称为面向对象的技术,简而言之,它涉及用一般术语描述实体以创建所谓的类。当遇到问题时,例如在步骤 1 中,决定类应该是什么的一个很好的经验法则是查看名词 - 在这种情况下,饮料、菜单和订单看起来是不错的候选者。一个类通常具有属性(使实例唯一的数据)和行为(基于该数据的操作),尽管您不必同时拥有这两者,正如您从下面的代码中看到的那样。
Step 3.
第 3 步。
I imagine to a non-programmer, step 2 doesn't make much sense, so here's some code (which I hope makes it a bit clearer):
我想对于一个非程序员来说,第 2 步没有多大意义,所以这里有一些代码(我希望它能让它更清晰一点):
/**
* This is the way classes are defined in Java, the public bit just says it's visible
* to every other class in the system.
*/
public class Beverage
{
//These are the attributes (fields) of the class. It's good practice to make them
//private so that they can only be accessed from within the class.
private String name;
private BigDecimal cost;
/**
* This is the constructor, which is used to create instances of the class. In
* this case it takes the arguments used to initialize the attributes of the class.
*/
public Beverage(String name, BigDecimal cost)
{
this.name = name;
this.cost = cost;
}
/**
* This is a getter, which provides access to the attributes from outside of the
* class.
*/
public BigDecimal getCost()
{
return this.cost;
}
public String getName()
{
return this.name;
}
}
public class Order
{
//This line is assigning an instance of HashMap (a standard data structure class
//in Java). A map is a bit like a dictionary, you have a key in this case the
//beverage that allows you to look-up another value, the quantity.
private Map<Beverage, Integer> beverages = new HashMap<Beverage, Integer>();
public BigDecimal getTotal()
{
BigDecimal total = BigDecimal.ZERO;
//Loop over all the beverages that have been added to the map summing the cost.
for (Beverage beverage : this.beverages.keySet())
{
//Convert the quantity in the map to a BigDecimal needed for the multiply method.
BigDecimal quantity = new BigDecimal(this.beverages.get(beverage));
total = total.add(beverage.getCost().multiple(quantity));
}
return total;
}
public void add(Beverage beverage, Integer quantity)
{
//Store the quantity against the beverage.
this.beverages.put(beverage, quantity);
}
}
These two classes are all you need to solve the problem. Menu is abscent because Java provides a class for a list of items. Next you need to use them in a program.
这两个类是解决问题所需的全部。Menu 不存在是因为 Java 为项目列表提供了一个类。接下来您需要在程序中使用它们。
Step 4.
步骤4。
In Java any class can be 'run' providing it has a special method called main
. Again, it's probably easier with an example:
在 Java 中,任何类都可以“运行”,只要它有一个名为main
. 同样,举个例子可能更容易:
public class Restaurant
{
/**
* The main method is static meaning it can be accessed without creating an instance
* of the Restaurant class.
*/
public static void main(String[] args)
{
Map<String, Beverage> menu = new HashMap<String, Beverage>();
//Create the instances of Beverage and add them to the menu.
menu.put("Fried Rice", new Beverage("Fried Rice", new BigDecimal(5.50)));
menu.put("Chicken Rice", new Beverage("Chicken Rice", new BigDecimal(5.00)));
menu.put("Toast Bread", new Beverage("Toast Bread", new BigDecimal(2.00)));
menu.put("Mixed Rice", new Beverage("Mixed Rice", new BigDecimal(3.80)));
//Create an order and add items from the menu to it.
Order order1 = new Order();
order1.add(menu.get("Fried Rice"), 2);
order1.add(menu.get("Toast Bread"), 3);
order1.add(menu.get("Mixed Rice"), 1);
System.out.println("Total for order 1: " + order1.getTotal());
//Create another order and add items from the menu to it.
Order order2 = new Order();
order2.add(menu.get("Chicken Rice"), 1);
order2.add(menu.get("Mixed Rice"), 1);
order2.add(menu.get("Toast Bread"), 2);
System.out.println("Total for order 2: " + order2.getTotal());
}
}
Step 5.
第 5 步。
That's all the code I think you'll need. But in order to run it there's a few further steps. First is to install the Java Development Kit, which can be download from Oracle. Then, in Java, each class is typically declared in a text file that has the same name as the class with a .java
extension - you'll end up with Beverage.java
, Order.java
and Restaurant.java
. Next you need to compile your program - in basic terms this is the process of verifying the code you have written and converting it to something the Java Runtime can understand. I won't attempt to explain that, it's pretty well covered in the Getting Started Guide, which also explains how to run a Java program - ultimately you'll be looking for a command line that looks something like:
这就是我认为您需要的所有代码。但是为了运行它,还有一些进一步的步骤。首先是安装Java Development Kit,可以从Oracle 下载。然后,在 Java 中,每个类通常在一个文本文件中声明,该文件与该类同名并带有.java
扩展名 - 最终会以Beverage.java
,Order.java
和Restaurant.java
. 接下来,您需要编译您的程序——从基本意义上讲,这是验证您编写的代码并将其转换为 Java 运行时可以理解的代码的过程。我不会试图解释这一点,它在Getting Started Guide中有很好的介绍,它还解释了如何运行 Java 程序 - 最终您将寻找一个类似于以下内容的命令行:
java -cp [path to class files] Restaurant