Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Assignment 4 -Intro to programming javascript

MIT 153 – Assignment #4

Note:

This problem could be
solved in a number of ways. For this assignment you must use compound
conditions for both the loop and selection logic. Do not use any programming
techniques that have not been covered in the course yet (such as functions or
arrays).

We have been asked to create a program that will be used to
calculate the totals for a customer’s purchases at a bulk food store. The
customers will have a number of items which are sold by weight. Each item has a
category, a weight or unit count (it could be factional!) and a price per unit.

The program should start by asking for the category of the
first  item. When they have completed
entering all of the items they will enter a sentinel of “STOP” or press the
Cancel button.

For each item the program will ask for the quantity and unit
price for this item.

There are three categories of items numbered 1 through 3.

Items in category 1 are individual items such as canned or
packaged goods. If they buy three or more of the same item they get a 5%
discount on that item.

Items in category 2 are bulk items that are usually
purchased in small quantities. The quantity will represent the weight in
kilograms but the price is per 100 grams. If they buy more than .5 kilograms
then they will get a 10% discount on that item.

Items in category 3 are bulk items that are usually
purchased in larger quantities. In this case both the quantity and price will
be per kilogram. If they purchase more than 2 kilograms of a given item they
will receive a 7% discount on that item.

For each item entered the program will display (in the
document) the item’s category, the quantity, the unit price, and the item total.

After all the items have been processed it will display the total sales (dollar amount) for each category, the total for the bill and the total discount they have received.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<script type="text/javascript">
// Assignment-4

// DECLARATIONS
var cat;
var quantity1=0;
var unitPrice;
var totalCat1,totalCat2,totalCat3;
var total;
var discount=discount1=discount2=discount3=0;
var totalDiscount=totalDiscount1=totalDiscount2=totalDiscount3=0;
var item;
var totalCat1=totalCat2=totalCat3=0;
var weight2,weight3;
var price100Grams;
var totalCatAll1=totalCatAll2=totalCatAll3=0;
var STOP;
var BR="<br />";
// INPUT CATEGORY
cat = prompt("Enter category ","");

// PROCESSING
while (cat != "STOP" ) {
// INPUT DATA FOR CATEGORY 1
	if ( cat == 1 ){
	     item = prompt("Enter Item " , "");
	     quantity1 = parseInt(prompt("Enter quantity " , ""));
	     unitPrice = parseFloat(prompt("Enter unitPrice " , ""));	
         totalCat1=unitPrice*quantity1;
		 document.write(" Category  "+ cat + BR);
		 document.write(" Quantity : "+ quantity1 + BR);
		 document.write(" Unit Price : "+ unitPrice + BR);
		 document.write(" Item Total : $"+ parseFloat(totalCat1).toFixed(2) + BR + BR);
             if (quantity1 >= 3){
                discount1=totalCat1 *.05;
		        totalDiscount1=parseFloat(discount1)+parseFloat(totalDiscount1);
		     }
		 totalCatAll1=parseFloat((totalCat1).toFixed(2))+parseFloat((totalCatAll1).toFixed(2));
     } 
// INPUT DATA FOR CATEGORY 2
	else if ( cat == 2 ){
	     item = prompt("Enter the Item " , "");
	     weight2 = prompt("Enter Total Weight in Kilograms " , "");
         price100Grams = prompt("Enter Price for Each 100 grams " , "");
         totalCat2=10*price100Grams*weight2;	
	     document.write(" Category  "+ cat + BR);
         document.write(" Weight : "+ weight2 + BR);
	     document.write(" Unit Price per 100 gram : " + parseFloat(price100Grams).toFixed(2) + BR);
	     document.write(" Item Total : $"+ parseFloat(totalCat2).toFixed(2) + BR + BR);
             if ( weight2 >= .5){
                discount2=totalCat2 *.1;
		        totalDiscount2=parseFloat(discount2)+parseFloat(totalDiscount2);
		     }
		 totalCatAll2=parseFloat((totalCat2).toFixed(2))+parseFloat((totalCatAll2).toFixed(2));
     } 
// INPUT DATA FOR CATEGORY 3
	else if ( cat == 3 ){
	     item = prompt("Enter the Item " , "");
	     weight3 = prompt("Enter Weight " , "");
	     unitPrice = parseFloat(prompt("Enter Unit Price Per Kilogram " , ""));
	     totalCat3=unitPrice*weight3;
	     document.write(" Category  "+ cat + BR);
         document.write(" Weight : "+ weight3 + " Kilograms" + BR);
	     document.write(" Unit Price : $"+ (unitPrice).toFixed(2)+ "  Per Kilogram" + BR);
	     document.write(" Items Total : $"+ parseFloat(totalCat3).toFixed(2) + BR + BR);
             if ( weight3 > 2){
                discount3=totalCat3 *.07;
		        totalDiscount3=parseFloat(discount3)+parseFloat(totalDiscount3);
		     }
		 totalCatAll3=parseFloat((totalCat3).toFixed(2))+parseFloat((totalCatAll3).toFixed(2));
         } 
    else{
         alert("Category should be 1 , 2 or 3");
    }
	cat = prompt("Enter the category for the item or STOP to Quit","");
}
  
   // OUTPUT 
    total=(parseFloat(totalCatAll1))+(parseFloat(totalCatAll2))+(parseFloat(totalCatAll3));
	discount=parseFloat(totalDiscount1)+parseFloat(totalDiscount2)+parseFloat(totalDiscount3);
    discount=discount.toFixed(2);	
    document.write(" Total for Category 1 $ "+ parseFloat(totalCatAll1).toFixed(2) + BR);
	document.write(" Total for Category 2 $ "+ parseFloat(totalCatAll2).toFixed(2) + BR);
	document.write(" Total for Category 3 $ "+ parseFloat(totalCatAll3).toFixed(2)+ BR);
    document.write(" Total  $ : " + parseFloat(total).toFixed(2) + BR);
	document.write(" Discount  $ : " + discount + BR);
    document.write(" Total after Discount  $ : " + parseFloat(total-discount).toFixed(2) + BR);
</script>
</body>
</html>

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!