Question

In: Computer Science

Goals In this assignment you must use Java language to program the small application for a...

Goals

In this assignment you must use Java language to program the small application for a grocery buyer.

You will create meaningful utility that implements

  1. simple math model for real problem;
  2. design of the application logic;
  3. basic user interface.

The implementation plus coding style and efficiency will be graded.

Description

The supermarket uses three pricing scenarios for the local products –

  1. Monday, Wednesday, and Friday, the product A has fixed regular price of X dollars.
  2. Tuesday and Thursday, the product A has price of X dollars, but you can buy three A-product items for Y dollars with 10% discount (Y = 3 * X * 90%).
  3. On the weekend, the product A has price of X dollars, but you can buy two and get one for free.

Your application must calculate how much you pay for one item depending on a regular price, day of the week, and number of items.

For example, yogurt has regular price of $3. Then, if you buy 3 yogurts on Sunday your spending will be $2 per item.

It is not as simple as it first appears. Consider the following questions before programming:

  1. Are price and how much you pay the same things?
  2. In the scenarios 2 and 3, how much you pay for one item if you buy 4 or 5?
  3. What is the money value of 1000 item if the product is priced using scenario 2?
  4. Does fractional money exist?

Program requirements

  1. The inputs for your program are name of the product, regular price for the product, number of items, and day of the week. You may use information from any local grocery store to create a list of 5-7 products to test your program.
  2. Your program must calculate how much you pay for the one unit of the product.
  3. The output must be in a form “… dollars … cents.”
  4. The program must run until the user decides to quit.
  5. The program must use methods and branches.
  6. Rounding to the cents must be programmed (do not use standard methods).


There is no any sample run, just need a JAVA program to conform the above requirements.

Solutions

Expert Solution

Screenshot

Program

/**
* Program to display price per Item
* Product name,number of items, regular price and day of the week are inputs
* Get each item price in dollar and cent format
*/
import java.util.Scanner;
public class ItemPriceCalculator {
   public static void main(String[] args) {
       //Scanner object to read data from user
       Scanner sc=new Scanner(System.in);
       //Header
       System.out.println("********** PRICE PER ITEM CALCULATOR **********");
       //Loop checker
       System.out.print("Do you want to enter new item (y/n)? ");
       char ch=sc.nextLine().charAt(0);
       //Error check
       while(Character.toUpperCase(ch)!='Y' && Character.toUpperCase(ch)!='N') {
           System.out.println("Error!!!Choice should be y/n");
           System.out.print("Do you want to enter new item (y/n)? ");
           ch=sc.nextLine().charAt(0);
       }
       //Loop until exit
       while(Character.toUpperCase(ch)=='Y') {
           //User input prompts
           System.out.print("\nEnter the name of product: ");
           String productName=sc.nextLine();
           System.out.print("Enter regular price of the product: ");
           float price=sc.nextFloat();
           System.out.print("Enter number of items purchased? ");
           int numPurchased=sc.nextInt();
           sc.nextLine();
           System.out.print("Enter weekday? ");
           String day=sc.nextLine();
           //Price calculator
            if(day.equalsIgnoreCase("Tuesday") || day.equalsIgnoreCase("Thursday")) {
               price=(float)((float)(numPurchased*price*.9))/3;
           }
           else if(day.equalsIgnoreCase("Saturday") || day.equalsIgnoreCase("Sunday")){
               price=(float)((float)((numPurchased-(numPurchased/3))*price))/3;
           }
            //Display result
           int dollar=(int)price;
           float cent=price-dollar;
           System.out.println("Price of "+productName+" per item is "+dollar+" dollars and "+(int)(cent*100)+" cents");
           //Loop repetititon
           System.out.print("\nDo you want to enter new item (y/n)? ");
           ch=sc.nextLine().charAt(0);
           while(Character.toUpperCase(ch)!='Y' && Character.toUpperCase(ch)!='N') {
               System.out.println("Error!!!Choice should be y/n");
               System.out.print("Do you want to enter new item (y/n)? ");
               ch=sc.nextLine().charAt(0);
           }
       }
   }
}

---------------------------------------------------------------------------

Output

********** PRICE PER ITEM CALCULATOR **********
Do you want to enter new item (y/n)? y

Enter the name of product: Yogurt
Enter regular price of the product: 3
Enter number of items purchased? 3
Enter weekday? Monday
Price of Yogurt per item is 3 dollars and 0 cents

Do you want to enter new item (y/n)? y

Enter the name of product: Yogurt
Enter regular price of the product: 3
Enter number of items purchased? 3
Enter weekday? Thursday
Price of Yogurt per item is 2 dollars and 70 cents

Do you want to enter new item (y/n)? y

Enter the name of product: Yogurt
Enter regular price of the product: 3
Enter number of items purchased? 3
Enter weekday? Sunday
Price of Yogurt per item is 2 dollars and 0 cents

Do you want to enter new item (y/n)? n

-----------------------------------------------------------------------

Note:-

I assume that you are expecting this way


Related Solutions

c++ language for this assignment use real world example. Your application must be a unique project...
c++ language for this assignment use real world example. Your application must be a unique project and must incorporate the use of an STL container and/or iterator and at least 7 STL algorithms with an option to exit. Your application must contain at least 8 menu options. You will decide how to implement these menu selections.
**JAVA LANGUAGE** This assignment will use the Employee class that you developed for assignment 6. Design...
**JAVA LANGUAGE** This assignment will use the Employee class that you developed for assignment 6. Design two sub- classes of Employee...FullTimeEmployee and HourlyEmployee. A full-time employee has an annual salary attribute and may elect to receive life insurance. An hourly employee has an hourly pay rate attribute, an hours worked attribute for the current pay period, a total hours worked attribute for the current year, a current earnings attribute (for current pay period), a cumulative earnings attribute (for the current...
Use Visual Basic Language In this assignment you will need to create a program that will...
Use Visual Basic Language In this assignment you will need to create a program that will have both a “for statement” and an “if statement”. Your program will read 2 numbers from the input screen and it will determine which is the larger of the 2 numbers. It will do this 10 times. It will also keep track of both the largest and smallest numbers throughout the entire 10 times through the loop. An example of the program would be...
Language: Java Create a TryIt.java program with a main method. You are going to use this...
Language: Java Create a TryIt.java program with a main method. You are going to use this program to demonstrate some things about how Java works. Make your methods here private, because they are not intended to be called from outside the program. For each test below, make sure that you print to the console: 1) What is being tested 2) The desired output 3) The actual output Question to be answered: Should you use == or the String method equals...
answer in JAVA language please In this assignment you will use: one-dimensional arrays; strings and various...
answer in JAVA language please In this assignment you will use: one-dimensional arrays; strings and various string-handling methods; file input and output; console input and output; loops; and conditional program statements. Instruction You are asked to write a program that translates a Morse code message into English language. The program will ask the user to enter filenames for two input files, one for the code translation table data and the other for the coded message data, as well as an...
Use JAVA language. Using the switch method concept create a program in which you have an...
Use JAVA language. Using the switch method concept create a program in which you have an artist (singer) and the list of his or her songs. Add 18 songs. Then alter the script to achieve the following in each test run: a) Print all the songs. b) Print only song 15. c) Print only song 19.
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Make a Console application Language should be Visual Basic In this assignment, you will be calculating...
Make a Console application Language should be Visual Basic In this assignment, you will be calculating the two parts for each month. you calculate the interest to pay each month and principal you pay every month. The interest you pay every month = loan * monthlyInterest The principal you pay every month = monthlyMortgage -  interest you pay every month ' what is my remaining loan loan = loan - principal you pay every month Problem 1 : Using While Loop,...
Please use Java language! with as much as comment! thanks! Write a program that displays a...
Please use Java language! with as much as comment! thanks! Write a program that displays a frame with a three labels and three textfields. The labels should be "width:", "height:", and "title:" and should each be followed by one textfield. The texfields should be initialized with default values (Example 400, 600, default title), but should be edited by the user. There should be a button (label it whatever you want, I don't care). If you click the button, a new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT