It seems to be true that developers get inspiration from the real world, so let's consider a real-world example of the Builder Pattern: Subway sandwiches. A waiter in Subway is your Builder. You can build sandwiches step-by-step adding portions of an ingredient. We offer you to make a simple order: your task is to write some TestDrive code and get a correct output.
Builder
Subway Sandwich
Report a typo
Sample Input 1:
Italian
1
2
0
4Sample Output 1:
Bun : Italian
Salad : 1
Cheese : 2
Cucumber : 0
Ham : 4Write a program in Java 17
import java.util.Scanner;
class Sandwich {
private String bun;
private int salad;
private int cheese;
private int cucumber;
private int ham;
public Sandwich(String bun, int salad, int cheese, int cucumber, int ham) {
this.bun = bun;
this.salad = salad;
this.cheese = cheese;
this.cucumber = cucumber;
this.ham = ham;
}
public static class Builder {
private String bun;
private int salad;
private int cheese;
private int cucumber;
private int ham;
public Builder addBun(String bun) {
this.bun = bun;
return this;
}
public Builder addSalad(int salad) {
this.salad = salad;
return this;
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.