Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is a java file that I need your help to check if it can function properly. If there is a problem, please let me

Here is a java file that I need your help to check if it can function properly.
If there is a problem, please let me know how to change it
,
if there is no problem, please let me know, thank you.
import java.util.Scanner;
class DimSum {
private String name;
private float price;
private int quantity;
public DimSum(String name, float price){
this.name = name;
this.price = price;
this.quantity =0;
}
public String getName(){
return name;
}
public float getPrice(){
return price;
}
public int getQuantity(){
return quantity;
}
public void order(int quantity){
this.quantity += quantity;
}
}
public class DimSumOrdering {
// This stores the dim sum dishes in the system
DimSum[] dishes;
// The constructor of DimSumOrdering
public DimSumOrdering(){
// Initialize the dim sum dishes in the system
dishes = new DimSum[4];
dishes[0]= new DimSum("Siu Mai", 30f);
dishes[1]= new DimSum("Barbecued Pork Bun", 25f);
dishes[2]= new DimSum("Shrimp Dumpling", 38f);
dishes[3]= new DimSum("Spring Roll", 25f);
}
// Start the ordering system
public void start(){
Scanner scanner = new Scanner(System.in);
String choice;
do {
// Print the menu
System.out.println("Menu:");
for (int i =0; i < dishes.length; i++){
System.out.println((i +1)+")"+ dishes[i].getName()+"- $"+ dishes[i].getPrice());
}
System.out.println("5) Bill and payment");
// Read the input
choice = scanner.next();
// Process the choice
switch (choice){
case "1":
dishes[0].order(1);
System.out.println("Ordered 1"+ dishes[0].getName());
break;
case "2":
dishes[1].order(1);
System.out.println("Ordered 1"+ dishes[1].getName());
break;
case "3":
dishes[2].order(1);
System.out.println("Ordered 1"+ dishes[2].getName());
break;
case "4":
dishes[3].order(1);
System.out.println("Ordered 1"+ dishes[3].getName());
break;
case "5":
// Show the bill and order summary
System.out.println("Order Summary:");
float total =0;
for (int i =0; i < dishes.length; i++){
int quantity = dishes[i].getQuantity();
if (quantity >0){
float subtotal = quantity * dishes[i].getPrice();
System.out.println(dishes[i].getName()+": "+ quantity +" x $"+ dishes[i].getPrice()+"= $"+ subtotal);
total += subtotal;
}
}
System.out.println("Total: $"+ total);
break;
default:
System.out.println("Invalid choice. Please select again.");
break;
}
} while (!choice.equals("5"));
scanner.close();
}
public static void main(String[] args){
DimSumOrdering orderingSystem = new DimSumOrdering();
orderingSystem.start();
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions