写出答案对于非程序员来说是一项相当复杂的任务。有一个程序应该如何构建以应对的概念,然后是编译和运行。
这个答案是按这个顺序排列的,首先我会解释我认为的关键点是什么(当然我错过了一些,因为其中大部分是第二天性)然后我会给你关于如何运行代码的指示。
步骤1。
想想如果你要在纸上做这件事会涉及什么——你会有一个饮料清单,每个都有一个名字和一个价格(菜单)。一份订单包含菜单中的一种或多种不同数量的饮料。您将每种饮料的价格乘以数量以获得订单成本。
第2步。
现代计算机语言使用一种称为面向对象的技术,简而言之,它涉及用一般术语描述实体以创建所谓的类。当遇到问题时,如第 1 步中的问题,决定类应该是什么的一个好的经验法则是查看名词——在这种情况下,饮料、菜单和订单看起来是不错的候选者。一个类通常具有属性(使实例唯一的数据)和行为(基于该数据的操作),尽管您不必同时拥有这两者,正如您从下面的代码中看到的那样。
步骤 3。
我想对于非程序员来说,第 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);
}
}
这两个类就是你解决问题所需要的。没有菜单是因为 Java 为项目列表提供了一个类。接下来,您需要在程序中使用它们。
第4步。
在 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());
}
}
步骤 5。
这就是我认为你需要的所有代码。但是为了运行它,还有一些进一步的步骤。首先是安装Java Development Kit,可以从Oracle 下载。然后,在 Java 中,每个类通常在一个文本文件中声明,该文本文件与具有.java
扩展名的类具有相同的名称——您最终会得到Beverage.java
,Order.java
和Restaurant.java
. 接下来,您需要编译您的程序 - 基本而言,这是验证您编写的代码并将其转换为 Java 运行时可以理解的内容的过程。我不会试图解释这一点,它在Getting Started Guide中有很好的介绍,它还解释了如何运行 Java 程序 - 最终你会寻找一个看起来像这样的命令行:
java -cp [path to class files] Restaurant