Java应用程序中的杂货订购逻辑与面向对象编程(OOP)

huangapple go评论76阅读模式
英文:

Java application grocery ordering logic with OOP

问题

我正在进行一个杂货订购系统,并且在订单逻辑方面遇到了问题。

目前,程序会询问顾客想要订购多少杂货,但我发现有一种方式可以在开始时无需指定要进行多少次订单。

有人有任何想法吗?谢谢。

以下是驱动类 Shop.java

package groceries.ordering.system;

import java.util.*;

public class Shop {
    
    public static void main(String[] args){
        
        Scanner myScanner = new Scanner(System.in);
      
        Category[] groceries = {new Category("Apple 1pcs", 1.5, "Fruit"),
                                new Category("Orange 1pcs", 1.4, "Fruit"),
                                new Category("Egg 10pcs", 8, "Dairy"),
                                new Category("Full Cream Milk", 6.5, "Dairy"),
                                new Category("Olive Oil 1L", 30, "Cooking Oil"),
                                new Category("Palm Oil 1L", 20, "Cooking Oil")};
                            

        displayMenu(groceries);

        System.out.print("How many groceries you would like to order: ");
        int totalGroceries = myScanner.nextInt();
        
        OrderItem[] items = new OrderItem[totalGroceries];
        
        for(int i=0; i<items.length; i++){
            System.out.println("Enter your choice of groceries (1 - 6) : ");
            int choice = myScanner.nextInt() - 1;
            System.out.println("Enter quantity ordered: ");
            int quantity = myScanner.nextInt();
            
            items[i] = new OrderItem(groceries[choice], quantity);
        }
        
        Order order = new Order(items, totalGroceries);
        displayOrderItem(items);
        System.out.printf("%65s %9.2f\n", "Grand Total: ", order.getGrandTotalPrice());

    } 

     public static void displayMenu(Groceries[] groceries){
        // ... (略去部分内容)
    }
     
     public static void displayOrderItem(OrderItem[] items){
        // ... (略去部分内容)
    }     
}

这是 Groceries 类(不会为此类的子类(Category)发布内容,因为我认为没有相关内容)

package groceries.ordering.system;

public abstract class Groceries {
    
    private String name;
    private double price;

    public Groceries() {
        this("", 0);
    }
    
    public Groceries(String prodName, double prodPrice){
        super();
        name = prodName;
        price = prodPrice;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    public String displayMenu(){
        return String.format("%-25s %-11.2f ", name, price);
    }
}

这是 OrderItem 类

public class OrderItem {
    
    private Groceries groceries;
    private int quantity;
    private static int prodCount = 1;
    
    public OrderItem(Groceries groceries, int quantity) {
        this.groceries = groceries;
        this.quantity = quantity;
    }
    
    public double getUnitPrice(){
     return groceries.getPrice();
        
    }
    
    public double getSubTotalPrice(){
        return getUnitPrice() * quantity;
    }
    

    public String displayOrder() {
        return String.format("%-10d %-20s %9.2f %13d %19.2f", prodCount++, groceries.getName(),
                              getUnitPrice(), quantity, getSubTotalPrice());
    }
}

最后是 Order 类

public class Order {
    
    private OrderItem[] items;
    private int totalItem;

    
    public Order(OrderItem[] items, int totalItem) {
        this.items = items;
        this.totalItem = totalItem;
    }

    
    public double getGrandTotalPrice() {
        double price = 0.0;
        for (OrderItem item : items) {
            price += item.getSubTotalPrice();
        }
        return price;
    }
}
英文:

I am doing a Groceries Ordering System and have a problem regarding the ordering logic.

Currently, the program will ask customers how many groceries they want to order but I am finding is that a way to ordering without specifying the number of how many orders want to make at the beginning.

Does anyone have any idea? Thank you.

Below is the driver class Shop.java

package groceries.ordering.system;
import java.util.*;
public class Shop {
public static void main(String[] args){
Scanner myScanner = new Scanner(System.in);
Category[] groceries = {new Category(&quot;Apple 1pcs&quot;, 1.5, &quot;Fruit&quot;),
new Category(&quot;Orange 1pcs&quot;, 1.4, &quot;Fruit&quot;),
new Category(&quot;Egg 10pcs&quot;, 8, &quot;Dairy&quot;),
new Category(&quot;Full Cream Milk&quot;, 6.5, &quot;Dairy&quot;),
new Category(&quot;Olive Oil 1L&quot;, 30, &quot;Cooking Oil&quot;),
new Category(&quot;Palm Oil 1L&quot;, 20, &quot;Cooking Oil&quot;)};
displayMenu(groceries);
System.out.print(&quot;How many groceries you would like to order: &quot;);
int totalGroceries = myScanner.nextInt();
OrderItem[] items = new OrderItem[totalGroceries];
for(int i=0;i&lt;items.length;i++){
System.out.println(&quot;Enter your choice of groceries (1 - 6) : &quot;);
int choice = myScanner.nextInt()-1;
System.out.println(&quot;Enter quantity ordered: &quot;);
int quantity = myScanner.nextInt();
items[i] = new OrderItem(groceries[choice],quantity);
}
Order order = new Order(items,totalGroceries);
displayOrderItem(items);
System.out.printf(&quot;%65s %9.2f\n&quot;, &quot;Grand Total: &quot;, order.getGrandTotalPrice());
} 
public static void displayMenu(Groceries[] groceries){
System.out.printf(&quot;\t========================================\n&quot;);
System.out.printf(&quot;\tWelcome to the Groceries Ordering System\n&quot;);
System.out.printf(&quot;\t========================================\n&quot;);
System.out.printf(&quot;%-10s %-25s %-11s %s \n\n&quot;, &quot;No&quot;, &quot;Groceries&quot;, &quot;Price(RM)&quot;, &quot;Category&quot;);
for(Groceries grocery : groceries){
System.out.println(grocery.displayMenu());
}
System.out.println(&quot;\n&quot;);
}
public static void displayOrderItem(OrderItem[] items){
System.out.printf(&quot;\n\t\t\t\t Ordered Item\n&quot;);
System.out.printf(&quot;\t\t\t\t===============\n&quot;);
System.out.printf(&quot;%-10s %-20s %-14s %-13s %-13s\n&quot;, &quot;No&quot;,&quot;Groceries&quot;, &quot;Price(RM)&quot;,&quot;Quantity&quot;,&quot;Total Price(RM)&quot;);
System.out.printf(&quot;--------------------------------------------------------------------------------------\n&quot;);
for(OrderItem item : items){
System.out.println(item.displayOrder());
}
System.out.printf(&quot;--------------------------------------------------------------------------------------\n&quot;);
}     
}

And this the Groceries class (will not posting the subclass(category) for this class as I think do not have any related)

package groceries.ordering.system;
public abstract class Groceries {
private String name;
private double price;
public Groceries() {
this(&quot;&quot;,0);
}
public Groceries(String prodName, double prodPrice){
super();
name = prodName;
price = prodPrice;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public String displayMenu(){
return String.format(&quot;%-25s %-11.2f &quot;, name,price);
}
}

This is the OrderItem class

public class OrderItem {
private Groceries groceries;
private int quantity;
private static int prodCount = 1;
public OrderItem(Groceries groceries, int quantity) {
this.groceries = groceries;
this.quantity = quantity;
}
public double getUnitPrice(){
return groceries.getPrice();
}
public double getSubTotalPrice(){
return getUnitPrice() * quantity;
}
public String displayOrder() {
return String.format(&quot;%-10d %-20s %9.2f %13d %19.2f&quot;,prodCount++, groceries.getName(),
getUnitPrice(),quantity,getSubTotalPrice());
}
}

and lastly the Order class

public class Order {
private OrderItem[] items;
private int totalItem;
public Order(OrderItem[] items, int totalItem) {
this.items = items;
this.totalItem = totalItem;
}
public double getGrandTotalPrice() {
double price = 0.0;
for (OrderItem item : items) {
price += item.getSubTotalPrice();
}
return price;
}
}

答案1

得分: 0

当然。你可以有两种方式实现。

  1. 预先创建一个非常大的数组(不推荐)。
  2. 使用一些ListArrayList或者LinkedList或其他类型),并且持续添加物品直到用户完成。

<br/>

List&lt;OrderItem&gt; items = new ArrayList&lt;&gt;();

do {
    System.out.println("请输入您选择的杂货编号(1 - 6):");
    int choice = myScanner.nextInt() - 1;
    System.out.println("请输入订购数量:");
    int quantity = myScanner.nextInt();

    items.add(new OrderItem(groceries[choice], quantity));

    // 计算 shouldContinue - 类似询问用户输入 -1 以退出。
} while (shouldContinue);
英文:

Sure. You can do it in two ways.

  1. Create a very large array beforehand (Not recommended).
  2. Use some List (ArrayList or LinkedList or something else) and keep adding items till the users are done.

<br/>

List&lt;OrderItem&gt; items = new ArrayList&lt;&gt;();
do {
System.out.println(&quot;Enter your choice of groceries (1 - 6) : &quot;);
int choice = myScanner.nextInt()-1;
System.out.println(&quot;Enter quantity ordered: &quot;);
int quantity = myScanner.nextInt();
items.add(new OrderItem(groceries[choice],quantity));
// compute shouldContinue - something like asking the user to enter -1 to quit.
} while(shouldContinue);

答案2

得分: 0

是的,可以在下面的数组位置使用列表实现,可以使用ArrayList或者LinkedList

用以下代码替换:

OrderItem[] items = new OrderItem[totalGroceries];

使用ArrayList替代:

List<OrderItem> items = new ArrayList<>();

如果你想在多线程环境中运行此应用程序,也可以使用CopyOnWriteArrayList

英文:

Yes , Use List implementation either ArrayList or LinkedList in place below array and use its functions:

Replace this - OrderItem[] items = new OrderItem[totalGroceries];

with ArrayList- List&lt;OrderItem&gt; items = new ArrayList&lt;&gt;();

You can use CopyOnWriteArrayList as well if you want to run this application in multi threaded environment.

huangapple
  • 本文由 发表于 2020年8月15日 21:53:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63426789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定