英文:
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("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){
System.out.printf("\t========================================\n");
System.out.printf("\tWelcome to the Groceries Ordering System\n");
System.out.printf("\t========================================\n");
System.out.printf("%-10s %-25s %-11s %s \n\n", "No", "Groceries", "Price(RM)", "Category");
for(Groceries grocery : groceries){
System.out.println(grocery.displayMenu());
}
System.out.println("\n");
}
public static void displayOrderItem(OrderItem[] items){
System.out.printf("\n\t\t\t\t Ordered Item\n");
System.out.printf("\t\t\t\t===============\n");
System.out.printf("%-10s %-20s %-14s %-13s %-13s\n", "No","Groceries", "Price(RM)","Quantity","Total Price(RM)");
System.out.printf("--------------------------------------------------------------------------------------\n");
for(OrderItem item : items){
System.out.println(item.displayOrder());
}
System.out.printf("--------------------------------------------------------------------------------------\n");
}
}
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("",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);
}
}
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("%-10d %-20s %9.2f %13d %19.2f",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
当然。你可以有两种方式实现。
- 预先创建一个非常大的数组(不推荐)。
- 使用一些
List
(ArrayList
或者LinkedList
或其他类型),并且持续添加物品直到用户完成。
<br/>
List<OrderItem> items = new ArrayList<>();
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.
- Create a very large array beforehand (Not recommended).
- Use some
List
(ArrayList
orLinkedList
or something else) and keep adding items till the users are done.
<br/>
List<OrderItem> items = new ArrayList<>();
do {
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.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<OrderItem> items = new ArrayList<>();
You can use CopyOnWriteArrayList
as well if you want to run this application in multi threaded environment.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论