类似对象数量

时间:2015-11-09 01:31:43

标签: java oop arraylist

我有一个分配来制作这个餐厅计划。它由订单类,产品类和主类组成。 Order有一个ArrayList来保存产品。我创建了Order的实例,然后通过main方法添加项目。产品有一个名称(字符串)一个条形码(字符串)和一个价格(浮动)。

然后我必须输出收据。但是如果客户订购更多的产品怎么办?我会逐个实例化所有内容吗?第二个啤酒产品是否独立?我应该以某种方式持有数量吗?如果我想添加第二种啤酒,我必须创建一个新产品Beer2等?我事先不知道每个订单会保留多少产品以及每个订单的数量是这样的实例化方式吗?感谢

注意:它仍然不完整,因为我想在继续之前处理这个问题。

import java.util.Date;

public class MyRestaurantTester {

public static void main(String[] args) {
    Date currentDate = new Date();
    Paraggelia order1 = new Paraggelia(currentDate,"11B");
    Product Beer = new Product("Amstel","111222",1.20f);
    Product Beef = new Product("Pork Beef","333444",8.50f);

    order1.add(Beer);
    order1.add(Beef);
    System.out.println(order1.getReceipt(30f));

}

}

订单类(没关系我给它的名字Paraggelia)

import java.util.ArrayList;
import java.util.Date;

/*Notes to self:
 * -Work on Comments
 * -Javadocs maybe? 
 * -try to optimize the rough code.
 */

/*Order class*/
public class Paraggelia {

    private Date orderDate;
    private String tableNumber;
    private int customerCount;
    private ArrayList<Product> listOfItems;

    /*Constructor(s)*/

    Paraggelia(Date orderDate,String tableNumber){
        this.orderDate=orderDate;
        this.tableNumber=tableNumber;
        this.listOfItems = new ArrayList<Product>();
    }


    /*Add && Delete Products from the Order class*/

    public void add(Product p){
        if(p == null)
        {
            throw new IllegalArgumentException();
        }else{
        listOfItems.add(p);
        }
    }

    public void delete(Product p){
        if(p == null)
        {
            throw new IllegalArgumentException();
        }
        else
        {
            listOfItems.remove(p);
        }
    }

    /** Calculates and returns the total price
     * Usually called directly as a parameter of getReceipt function
     * */
    public static float getTotalPrice(){

        return 0;
    }

    /** Creates and returns the final Receipt! 
     * -Display must consist of:
     * Item$ - BarCode# - Item Amount#
     * Total Price#
     * Table Number#
     */
    public  String getReceipt(float totalPrice){
        StringBuilder receipt = new StringBuilder();
        for(int i =0; i<this.listOfItems.size();i++){
        receipt.append(listOfItems.get(i).getName());
        receipt.append("\n");
        }

        return new String(receipt);
    }


    /*Getters && Setters */
    public Date getOrderDate() {
        return orderDate;
    }
    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }
    public String getTableNumber() {
        return tableNumber;
    }
    public void setTableNumber(String tableNumber) {
        this.tableNumber = tableNumber;
    }
    public int getCustomerCount() {
        return customerCount;
    }
    public void setCustomerCount(int customerCount) {
        this.customerCount = customerCount;
    }

}

产品类别:

public class Product {

    private String Name;
    private String barCode;
    private float sellingPrice;

    /*Constructors: */
    Product(){}

    Product(String Name,String barCode,float sellingPrice){
        this.Name=Name;
        this.barCode=barCode;
        this.sellingPrice=sellingPrice;             
    }

    /*Getters & Setters*/
    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getBarCode() {
        return barCode;
    }

    public void setBarCode(String barCode) {
        this.barCode = barCode;
    }

    public float getSellingPrice() {
        return sellingPrice;
    }

    public void setSellingPrice(float sellingPrice) {
        this.sellingPrice = sellingPrice;
    }

}

2 个答案:

答案 0 :(得分:0)

我想到了三种基本方法:

  • 单独实例化每个产品

  • 而不是ArrayList,有另一个可以将项目与数量相关联的结构;或者,

  • 创建一个类Article,该类属于ProductProduct beerProduct = new Product("beer", "0129", 1.37); Article beer = new Article(beerProduct), beer2 = new Article(beerProduct)

第一种解决方案为您提供了很大的灵活性(例如,为了损坏个别文章而对其进行折扣)。第二种解决方案对物体更经济。第三个捕获直觉,所有喜力瓶都是相同的。这完全取决于你想要做什么 - 两种方法都是有效的,出于某种目的。

答案 1 :(得分:0)

您可以使用Map(例如HashMap)

代替ArrayList(List)

<强> MyRestaurantTester

public class MyRestaurantTester {

    public static void main(String[] args) {
        Date currentDate = new Date();
        Paraggelia order1 = new Paraggelia(currentDate,"11B");
        Product Beer = new Product("Amstel","111222",1.20f);
        Product Beef = new Product("Pork Beef","333444",8.50f);

        order1.add(Beer, 1);
        order1.add(Beef, 5);
        System.out.println(order1.getReceipt(30f));

    }

}

<强> Paraggelia

class Paraggelia {

    private Date orderDate;
    private String tableNumber;
    private int customerCount;
    private Map<Product, Integer> listOfItems;

    /*Constructor(s)*/

    Paraggelia(Date orderDate,String tableNumber){
        this.orderDate=orderDate;
        this.tableNumber=tableNumber;
        this.listOfItems = new HashMap<Product, Integer>();
    }


    /*Add && Delete Products from the Order class*/

    public void add(Product p, int quantity){
        if(p == null)
        {
            throw new IllegalArgumentException();
        }else{
            listOfItems.put(p, quantity);
        }
    }

    public void delete(Product p){
        if(p == null)
        {
            throw new IllegalArgumentException();
        }
        else
        {
            listOfItems.remove(p);
        }
    }

    /** Calculates and returns the total price
     * Usually called directly as a parameter of getReceipt function
     * */
    public static float getTotalPrice(){

        return 0;
    }

    /** Creates and returns the final Receipt!
     * -Display must consist of:
     * Item$ - BarCode# - Item Amount#
     * Total Price#
     * Table Number#
     */
    public  String getReceipt(float totalPrice){
        StringBuilder receipt = new StringBuilder();

        for(Map.Entry<Product,Integer> entry : this.listOfItems.entrySet()) {
            Product product = entry.getKey();
            Integer quantity = entry.getValue();

            receipt.append(product.getName() + " " + quantity);
            receipt.append("\n");
        }

        return new String(receipt);
    }


    /*Getters && Setters */
    public Date getOrderDate() {
        return orderDate;
    }
    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }
    public String getTableNumber() {
        return tableNumber;
    }
    public void setTableNumber(String tableNumber) {
        this.tableNumber = tableNumber;
    }
    public int getCustomerCount() {
        return customerCount;
    }
    public void setCustomerCount(int customerCount) {
        this.customerCount = customerCount;
    }

}

<强>输出:

猪肉牛肉5

Amstel 1