In: Computer Science
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
The implementation plus coding style and efficiency will be graded.
Description
The supermarket uses three pricing scenarios for the local products –
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:
Program requirements
There is no any sample run, just need a JAVA program to conform the
above requirements.
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