Question

Help Please on JAVA Project: Validating the input is where I need help. Thank you Requirements...

Help Please on JAVA Project: Validating the input is where I need help. Thank you

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

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]:

You are supposed to validate the input.

If the user enters a letter or a number not between 1 and 4, the user will see an error message.

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

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.

=== 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

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

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

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

Select meat [1, 5]: 4

Enter customer's name: John Smith

5. Displaying and writing order line information

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.

2018/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 receipt.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

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:

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

[1] a SandwichOrder class to simulate the SandwichOrder entity in real world, which has fields of bread, vegetables, meat, and price of the sandwich, customer name, the string value of time stamp (refer to the prior sample code) and corresponding methods. This class also provides a method to append the content of an order’s content to the output text file, receipt.txt. (SandwichOrder.java)

[2] a java application, named SandwichApp.java, that contains a main method. This class interacts with SandwichOrder class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE:

import java.util.*;
import java.text.DateFormat;
import java.util.Date;
import java.io.*;
import java.math.RoundingMode;
import java.text.DecimalFormat;
class SandwichOrder    //sandwichorder class
{
   String bread;
   String vegetables="";
   String meat;
   double price;
   String customername;
   String time;
   void appendToFile(String str) throws Exception //method for appending data to text file
   {
       String fileName = "orderline.txt";
        try {
            File file = new File(fileName);
           FileWriter fr = new FileWriter(file, true);
           BufferedWriter br = new BufferedWriter(fr);
           PrintWriter pr = new PrintWriter(br);
           pr.println(str);
           pr.close();
           br.close();
           fr.close();
        }
        catch (IOException e) {
            System.out.println("Exception Occurred" + e);
        }
   }
}
class SandwichApp
{
   public static void main(String[] args) throws Exception
   {
       int opt;
       double p=0;
       Scanner sc=new Scanner(System.in);
       while(true)
       {
           String vegetable="";
           SandwichOrder s=new SandwichOrder();
           while(true)       //logic for selecting sandwich bread
           {
               System.out.println("=== Select Sandwich Bread: ===\n1 White Bread $1.5\n2 Wheat Bread $1.8\n3 French Bread $2.0\n4 Organic Bread $2.3\n");
               System.out.println("Select a bread [1, 4]:");
               opt=sc.nextInt();
               if(opt<1)
               {
                   System.out.println("Error! Number must be greater than "+" "+opt);
               }
               else if(opt>4)
               {
                   System.out.println("Error! Number must be less than "+" "+opt);
               }
               else if(opt==1)
               {
                   s.bread="WhiteBread";
                   p=p+1.50;
                   break;
               }
               else if(opt==2)
               {
                   s.bread="WheatBread";
                   p=p+1.80;
                   break;
               }
               else if(opt==3)
               {
                   s.bread="FrenchBread";
                   p=p+2.00;
                   break;
               }
               else if(opt==4)
               {
                   s.bread="OrganicBread";
                   p=p+2.30;
                   break;
               }
           }
           while(true)   //logic for selecting sandwitch vegetables
           {
               System.out.print("=== Select Sandwich Vegetables: ===\n1 red onions $0.10\n2 olives $0.10\n3 pickles $0.10\n4 lettuce $0.20\n");
               System.out.println("5 green peppers $0.25\n6 tomatoes $0.30\n7 cheese $0.49\n8 Quit vegetable selection");
               System.out.println("Select vegetables: [1, 8]:");
               opt=sc.nextInt();
               if(opt<1)
               {
                   System.out.println("Error! Number must be greater than "+" "+opt);
               }
               else if(opt>8)
               {
                   System.out.println("Error! Number must be less than "+" "+opt);
               }
               if(opt==1)
               {
                   vegetable=vegetable+"red onions"+",";
                   p=p+0.10;
               }
               else if(opt==2)
               {
                   vegetable=vegetable+"olives"+",";
                   p=p+0.10;
               }
               else if(opt==3)
               {
                   vegetable=vegetable+"pickles"+",";
                   p=p+0.10;
               }
               else if(opt==4)
               {
                   vegetable=vegetable+"lettuce"+",";
                   p=p+0.20;
               }
               else if(opt==5)
               {
                   vegetable=vegetable+" green peppers"+",";
                   p=p+0.25;
               }
               else if(opt==6)
               {
                   vegetable=vegetable+"tomatoes"+",";
                   p=p+0.30;
               }
               else if(opt==7)
               {
                   vegetable=vegetable+"cheese"+",";
                   p=p+0.49;
               }
               else if(opt==8)
               {
                   break;
               }  

           }
           s.vegetables=vegetable.substring(0,(vegetable.length()-2));    //removing , at the end of the string
           System.out.println("veg "+s.vegetables);
           while(true) //logic for selecting sandwich meat
           {
               System.out.print("=== Select Sandwich Meat: ===\n1 Ham $0.9\n2 Roasted Chicken Breast $1.0\n3 Turkey Breast $1.1\n");
               System.out.println("4 Roast Beef $1.5\n5 Quit meat selection");
               opt=sc.nextInt();
               if(opt<1)
               {
                   System.out.println("Error! Number must be greater than "+" "+opt);
               }
               else if(opt>5)
               {
                   System.out.println("Error! Number must be less than "+" "+opt);
               }
               else if(opt==1)
               {
                   s.meat="Ham";
                   p=p+0.90;
                   break;
               }
               else if(opt==2)
               {
                   s.meat="Roasted chicken Breast";
                   p=p+1.00;
                   break;
               }
               else if(opt==3)
               {
                   s.meat="Turkey Breast";
                   p=p+1.10;
                   break;
               }
               else if(opt==4)
               {
                   s.meat="Roasted Beef";
                   p=p+1.50;
                   break;
               }
               else if(opt==5)
               {
                   break;
               }
      
           }
           sc.nextLine();
           System.out.print("Enter customer's name:");   //reading customer name
           String name=sc.nextLine();
           s.customername=name;

           Date now = new Date();
           DateFormat defaultDate = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
           String t = defaultDate.format(now);
           s.time=t;
           DecimalFormat twoDForm = new DecimalFormat("#.##");
           p=Double.valueOf(twoDForm.format(p));
           s.price=p;
           System.out.println(s.time+"\t"+s.customername+"\t"+s.bread+"\t"+s.vegetables+"\t"+s.meat+"\t"+"$"+s.price);
           String str=""+s.time+"\t"+s.customername+"\t"+s.bread+"\t"+s.vegetables+"\t"+s.meat+"\t"+"$"+s.price;
           s.appendToFile(str);   //calling appendToFile method
           System.out.println("Continue to order more sandwich? (y/n):");
           char c=sc.next().charAt(0);
           if(c=='y')
           {
               continue;
           }
           else
           {
               break;
           }
       }
      

   }
}

OUTPUT:

ORDERLINE.TXT:

Add a comment
Know the answer?
Add Answer to:
Help Please on JAVA Project: Validating the input is where I need help. Thank you Requirements...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello Guys. I need help with this its in java In this project you will implement...

    Hello Guys. I need help with this its in java In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide...

  • Hello, please help me to answer my assignment, i highly appreciate your effort, kindly explain it...

    Hello, please help me to answer my assignment, i highly appreciate your effort, kindly explain it clearly. Kindly put your codes in (NOTEPAD). Thank you very much. Write a VB application that will facilitate a pizza order. 1.The user interface should have a form containing all components pertaining to pizza order presented in either option buttons or check boxes. 2.Controls must also be grouped in frames according to size, toppings, or crust type. 3.The summary of order must be presented...

  • I need help please... Intro to Programming in C – small program 8 Project objective: To...

    I need help please... Intro to Programming in C – small program 8 Project objective: To compile, build, and execute an interactive program using character arrays and string.h functions **Submit source code (prog8.c) through Canvas • One source code file (unformatted text) will be submitted • The file name must match the assignment • The code should be tested and run on a Microsoft compiler before it is uploaded onto Canvas • The code must be submitted on time in...

  • Can someone please help me with this Python code? Thank you in advance, Zybooks keeps giving me e...

    Can someone please help me with this Python code? Thank you in advance, Zybooks keeps giving me errors. Thanks in advance!! This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. (2 pts) item_description (string) - Set to "none" in default constructor Implement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of...

  • Project overview: Create a java graphics program that displays an order menu and bill from a...

    Project overview: Create a java graphics program that displays an order menu and bill from a Sandwich shop, or any other establishment you prefer. In this program the design is left up to the programmer however good object oriented design is required. Below are two images that should be used to assist in development of your program. Items are selected on the Order Calculator and the Message window that displays the Subtotal, Tax and Total is displayed when the Calculate...

  • Taking this program how can the requirements under it be added in java? import java.util.Scanner; public...

    Taking this program how can the requirements under it be added in java? import java.util.Scanner; public class RetirementSavings {    private static final int MAX_SALARY = 300000;    private static final double MAX_SAVING_RATE = 0.3;    private static final double MAX_INTEREST_RATE = 0.2;    private static final int MAX_YEAR_EMPLOYED = 40;    public static void main(String[] args) {        double savings_rate, interest_rate;        int salary, years_employed;        String name = ""; // name is initialized with an...

  • Help with java . For this project, you will design and implement a program that analyzes...

    Help with java . For this project, you will design and implement a program that analyzes baby name popularities in data provided by the Social Security Administration. Every 10 years, the data gives the 1,000 most popular boy and girl names for kids born in the United States. The data can be boiled down to a single text file as shown below. On each line we have the name, followed by the rank of that name in 1900, 1910, 1920,...

  • This is for a Unix class. Please help me out. I am attaching a skeletal code of the program below, it just needs ti be filled in. Below is a skeletal code of the program. Fork a child process...

    This is for a Unix class. Please help me out. I am attaching a skeletal code of the program below, it just needs ti be filled in. Below is a skeletal code of the program. Fork a child process and then use the parent for reading and the child for writing.  This is just a way of sending and receiving messages asynchronously. /* ************************************************************* * Utility functions * ************************************************************** */ static void usageError(const char * progName, const char *msg) {...

  • I really need help with this, please. This is a java programming assignment. Project 1 The first programming project inv...

    I really need help with this, please. This is a java programming assignment. Project 1 The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of four classes. 1. The first class is the Employee class, which contains the employee's name and monthly salary, which is specified in whole dollars. It should have three methods: a. A constructor that allows the name and monthly salary to be...

  • I really need help with this python programming assignment asap please double check indentations, thank you...

    I really need help with this python programming assignment asap please double check indentations, thank you We were unable to transcribe this imageEnter a drink option: Milk 22 Guest #1: a Please select the number of your entree choice 1) Planked salmon, 2) Prime rib, 3) Sesame tofu with spinach: 3 Please select the number of your dessert choice 27 1) Caramel oat bar, 2) Chocolate decadence, 3) Orange creme brulee: 1 Please select the number of your drink choice...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT