Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help on my project Java Project Files provided to you: • Valiator.jar and

ID: 3832416 • Letter: I

Question

I need help on my project

Java Project

Files provided to you:

• Valiator.jar and Validator.html

https://www.dropbox.com/s/laezmiazcypjd4y/Validator.html?dl=0

https://www.dropbox.com/s/p30y2r2wp0uvjcq/Validator.jar?dl=0

The provided Validator.html, similar to the standard Java API Specifications, shows you what methods the class Validator provides. In your NetBeans project, you need to use the provided Validator.jar file to validate user’s input so that you don’t need to write your own validation program.

In order to use the Validator.jar file, you need to add this jar file to your project in NetBeans by the following procedures.

It appears that in order to use the Validator.jar in your NetBeans project, the package for your source code must be default package, refer to the following screenshot.

Assume that in NetBeans your project name is Project234. After you create the project234 project, right click Libraries under Project234, click Add JAR/Folder …, then choose the Validator.jar file from your computer.

• orderline.txt: a sample output file. The provided text file is for your reference, such as to show the format of its content, and your program needs to write output to this file. If this file does not exist yet, your program needs to create it dynamically.

Requirements description:

Assume you work part-time at a sandwich store. As the only employee who knows java programming, you help to write a sandwich ordering application for the store.

The following is a brief requirement description with some sample output.

1. Selecting bread (5 points)

When the program starts, it first shows a list/menu of sandwich breads and their prices, then asks a user to select a bread by entering an integer number. A sample output is as follows.

=== Select Sandwich Bread: ===

1 White Bread $1.5

2 Wheat Bread $1.8

3 French Bread $2.0

4 Organic Bread $2.3 Select a bread [1, 4]:

2

If the user enters a number not between 1 and 4, the user will see an error message and the input verification is done by using the provided Validator class (i.e., Validator.jar). A sample output for invalid number is as follows.

Select a bread [1, 4]: 0

Error! Number must be greater than 0. Select a bread [1, 4]:

In your program, you can hard-code the information for sandwich breads (i.e., bread names and prices) shown above, such as “1 White Bread $1.5” and use the hard-code price, such as 1.5, for calculation of a total price of the order.

2. Selecting vegetables (10 points)

After the user provides a right bread number for bread selection, the program asks the user to select vegetables. A sample output is as follows. Again, input validation is needed and is provided by the Validator class (Validator.jar).

=== Select Sandwich Vegetables: ===

1 red onions $0.10

2 olives $0.10

3 pickles $0.10

4 lettuce $0.20

5 green peppers $0.25

6 tomatoes $0.30

7 cheese $0.49

8 Quit vegetable selection Select vegetables: [1, 8]:

You hard-code the vegetables information as shown above, such as “ 1 red onions $0.10” and use the hard-code price, such as 0.10, for calculation of the total price of the order.

After the user makes a choice for vegetable, such as 2 for olives. The program continues asking for selecting a vegetable so that the user can have multiple vegetables for one sandwich. The user can enter “8” to quit vegetable selection. A sample output is as follows.

=== Select Sandwich Vegetables: ===

1 red onions $0.10

2 olives $0.10

3 pickles $0.10

4 lettuce $0.20

5 green peppers $0.25

6 tomatoes $0.30

7 cheese $0.49

8 Quit vegetable selection

Select vegetables: [1, 8]: 2

=== Select Sandwich Vegetables: ===

1 red onions $0.10

3

2 olives $0.10

3 pickles $0.10

4 lettuce $0.20

5 green peppers $0.25

6 tomatoes $0.30

7 cheese $0.49

8 Quit vegetable selection Select vegetables: [1, 8]:

3. Selecting meat (5 points)

After vegetable selection, the program shows meat selection. A sample output is as follows.

=== Select Sandwich Vegetables: ===

1 red onions $0.10

2 olives $0.10

3 pickles $0.10

4 lettuce $0.20

5 green peppers $0.25

6 tomatoes $0.30

7 cheese $0.49

8 Quit vegetable selection

Select vegetables: [1, 8]: 8

=== Select Sandwich Meat: ===

1 Ham $0.9

2 Roasted Chicken Breast $1.0

3 Turkey Breast $1.1

4 Roast Beef $1.5

5 Quit meat selection Select meat [1, 5]:

Input validation is needed and works as before. Unlike vegetable selection which allows selecting multiple vegetables, meat selection does not repeat after the user enters a valid number between 1 and 5.

You hard-code the information for meat shown above, such as “1 Ham $0.9” and use the hard-code price, such as 0.9, for calculation of the total price of the order.

4. Entering a customer’s name (5 points)

After making a meat selection, the program asks for the user’s name so that the user can enter text like John or John Smith.

=== Select Sandwich Meat: ===

1 Ham $0.9

2 Roasted Chicken Breast $1.0

3 Turkey Breast $1.1

4 Roast Beef $1.5

5 Quit meat selection

4

Select meat [1, 5]: 4

Enter customer's name: John Smith

5. Displaying and writing order line information (15 points)

After entering a name, the program prints details for this order line in computer monitor. The information includes the six fields: date and time, customer name, bread, vegetable(s), meat, and a (formatted) total price, and each field is separated by a tab, /t. A sample output to the monitor is as follows.

Enter customer's name: John Smith

Jul 6, 2016 10:35:43 AM John Smith Organic Bread lettuce, tomatoes, green peppers Roast Beef $4.55

Continue to order more sandwich? (y/n):

For your reference, the date and time is created by the following statements.

Date now = new Date();

DateFormat defaultDate = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);

String time = defaultDate.format(now);

Multiple vegetables are separated by commas.

Besides showing on monitor, the same order line content is written (appended) to a text file named orderline.txt and each order line occupies one line in the text file. Here is a specific example for an order in the orderline.txt.

2016/07/05 20:34:17 John Smith Organic Bread lettuce, tomatoes, green peppers Roast Beef $4.55

Your program needs to create the output file, named orderline.txt, if it doesn’t exist yet, and later appends (not overwrite) order line information to the file. So next time when your program is executed, new order information will be appended to the orderline.txt file. You must use a relative path when creating an output stream writer object. For simplicity, we don’t record any information about the sales clerk and one order includes only one sandwich.

6. Repeating the order process (5 points)

Continue to order more sandwich? (y/n):

If the user enters y or Y in the above prompt, the whole order process repeats by asking the user to select a sandwich bread, vegetable(s), meat, and enter a customer name, etc.

Design requirements: (5 points)

You must have at least the following java classes. You may have additional classes if you want.

[1] a Sandwich class to simulate the sandwich entity in real world, which has attributes of bread, vegetables, meat, and price of the sandwich, and corresponding methods.

[2] an OrderLine class to simulate the orderline. This class has attributes of customer name, sandwich object, and the string value of time stamp (refer to the prior sample code). This class also provides a method to append the content of an orderline’s content to the output text file, orderline.txt.

[3] a java application, named SandwichApp.java, that contains a main method. This class interacts with Sandwich and Orderline classes.

Explanation / Answer

I have created 4 Java Classes

1)SandwichApp.java 2) Sandwich.java 3) OrderLine.java 4) Prices.java

Below is the code:

1)SandwichApp.java

import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class SandwichApp {
   public static void main(String[] args) {
       String bread, meat = null;
       String[] vegetable = new String[10];
       int n;
       String option = "y";
       Prices.initializePrices();
       Scanner in = new Scanner(System.in);
       while(option.equals("y") || option.equals("Y")){
       System.out.println("=== Select Sandwich Bread: ===");
      
       Map<String, Double> breads = Prices.breads;
       Map<String, Double> vegetables = Prices.vegetables;
       Map<String, Double> meats = Prices.meats;
       int i = 0;
       for (Entry<String, Double> ent : breads.entrySet()) {
           System.out.println(++i + " " + ent.getKey() + " " + ent.getValue());
       }
       int num = Validator.getInt(in, "Please enter a number between 1 and 4", 1, 4);
       if (1 == num)
           bread = "White Bread";
       else if (2 == num)
           bread = "Organic Bread";
       else if (3 == num)
           bread = "French Bread";
       else
           bread = "Wheat Bread";
       boolean flag = true;
       i = 0;
       int j = 0;
       while (flag) {
           System.out.println("=== Select Sandwich Vegetables: ===");
           for (Entry<String, Double> ent : vegetables.entrySet()) {
               System.out.println(++i + " " + ent.getKey() + " " + ent.getValue());
           }
           n = Validator.getInt(in, "Please enter a number between 1 and 8", 1, 8);
           if (1 == n)
               vegetable[j++] = "tomatoes";
           else if (2 == n)
               vegetable[j++] = "green peppers";
           else if (3 == n)
               vegetable[j++] = "red onions";
           else if (4 == n)
               vegetable[j++] = "olives";
           else if (5 == n)
               vegetable[j++] = "pickles";
           else if (6 == n)
               vegetable[j++] = "lettuce";
           else if (7 == n)
               vegetable[j++] = "cheese";
           else
               flag = false;
           i = 0;
       }
       System.out.println("=== Select Sandwich Meat: ===");
       for (Entry<String, Double> ent : meats.entrySet()) {
           System.out.println(++i + " " + ent.getKey() + " " + ent.getValue());
       }
       int m = Validator.getInt(in, "Please enter a number between 1 and 5", 1, 5);
       if (1 == m)
           meat = "Roasted Chicken Breast";
       else if (2 == m)
           meat = "Ham";
       else if (3 == m)
           meat = "Roast Beef";
       else if (4 == m)
           meat = "Turkey Breast";
       else if (5 == m)
           meat = null;
       System.out.println("Enter Customer Name");
       String name = in.nextLine();
       Sandwich sandwich = new Sandwich(bread, vegetable, meat);
       Date now = new Date();
       DateFormat defaultDate = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
       String time = defaultDate.format(now);
       OrderLine order = new OrderLine(name, sandwich, time);
       try {
           order.placeOrder();
       } catch (IOException e) {
           e.printStackTrace();
       }
       System.out.println("Continue to order more sandwich?");
       option = in.nextLine();;
       }


   }

}

2) Sandwich.java


public class Sandwich {
   String bread;
   String[] vegetables;
   String meat;
   Float price;

   public Sandwich(String bread, String[] vegetables, String meat) {
       this.bread = bread;
       this.vegetables = vegetables;
       this.meat = meat;
   }

   public String getBread() {
       return bread;
   }

   public void setBread(String bread) {
       this.bread = bread;
   }

   public String[] getVegetables() {
       return vegetables;
   }

   public void setVegetables(String[] vegetables) {
       this.vegetables = vegetables;
   }

   public String getMeat() {
       return meat;
   }

   public void setMeat(String meat) {
       this.meat = meat;
   }

   public Float getPrice() {
       return price;
   }

   public void setPrice(Float price) {
       this.price = price;
   }

}

3) OrderLine.java

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class OrderLine {
   String customerName;
   Sandwich sandwich;
   String timestamp;

   OrderLine(String name, Sandwich sandwich, String time) {
       this.customerName = name;
       this.sandwich = sandwich;
       this.timestamp = time;
   }

   void placeOrder() throws IOException {
       Double price = Prices.getPrice(this.sandwich);
       writeOrder(this.sandwich, this.timestamp, price);
   }

   private void writeOrder(Sandwich sandwich, String timestamp, double price) throws IOException {
       File file = new File("orderline.txt");
       if (!file.exists())
           file.createNewFile();
       try (FileWriter fw = new FileWriter("orderline.txt", true);
               BufferedWriter bw = new BufferedWriter(fw);
               PrintWriter out = new PrintWriter(bw)) {
           out.print(timestamp + " " + this.customerName + " " + sandwich.bread + " ");
           String[] vegs = sandwich.vegetables;
           for (String v : vegs) {
               if (null != v)
                   out.print(v + " ");
           }
           out.print(sandwich.meat + " $" + price + " ");

       } catch (IOException e) {
       }

   }
}

4) Prices.java

import java.util.HashMap;
import java.util.Map;

public class Prices {
   public static Map<String, Double> breads = new HashMap<String, Double>();
   public static Map<String, Double> vegetables = new HashMap<String, Double>();
   public static Map<String, Double> meats = new HashMap<String, Double>();

   public static void initializePrices() {
       breads.put("White Bread", 1.5);
       breads.put("Wheat Bread", 1.8);
       breads.put("French Bread", 2.0);
       breads.put("Organic Bread", 2.3);
       vegetables.put("red onions", 0.10);
       vegetables.put("olives", 0.10);
       vegetables.put("pickles", 0.10);
       vegetables.put("lettuce", 0.20);
       vegetables.put("green peppers", 0.25);
       vegetables.put("tomatoes", 0.30);
       vegetables.put("cheese", 0.49);
       meats.put("Ham", 0.9);
       meats.put("Roasted Chicken Breast", 0.9);
       meats.put("Turkey Breast", 0.9);
       meats.put("Roast Beef", 0.9);
   }

   public static double getPrice(Sandwich sandwich) {
       double bread = breads.get(sandwich.bread);
       double meat = 0.0;
       double veg = 0.0;
       if (null != sandwich.meat)
           meat = meats.get(sandwich.meat);
       String[] vegs = sandwich.vegetables;
       for (String v : vegs) {
           if (null != v && !v.isEmpty())
               veg += vegetables.get(v);
       }
       return (bread + meat + veg);
   }

}

Please let me know if any further help is needed.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote