My question is about the below code, when I compile it, everything gets thrown together on one line. How do I make the messages line up on separate lines? I wanted to try the examples in the API documentation, but I did not have success setting it up, do I give examples in the API their own class name, then public static void main ( String args[] )? If you are not sure what I mean, pick an example out of the API documentation (I have 1.4) and show me how to run the code by itself to see how it works (if possible, I tried JOptionPane). Maybe then I can get out of this quicksand....it was so nice today outside...but Nooooooooo, I gotta work on this (wishing for LOL). I want to keep it to the basic concepts, nothing fancy.
Code:
//Assignment3.java
import javax.swing.JOptionPane;//for input/output
public class Assignment3
{
public static void main ( String args[] )
{
int _number_hard_drives; //how many hard drives customer orders
String _parse_number_hard_drives = "empty string"; //how many hard drives customer orders (parse this)
int _number_of_boxes_shipped = 999; //how many boxes will be shipped
int _drives_less_than_ten = 999; //how many is in a box if less than 10
double _drive_price = 1000000; //cost per drive
double _subtotal = 10000000; //to calculate tax
double _tax = 10000000; //tax for order
double _cost = 10000000; //cost of order
String _output_part_a ; //how many drives ordered
String _output_part_b ; //how many drives in a box that is not full
String _output_part_c ; //The price for # hard drives is $9999.99
String _output_part_d ; //Tax $999.99
String _output_part_e ; //Total $999.99
String _message ; //variable greeting based on # of boxes
_number_hard_drives == 1
while ( _number_hard_drives != 0)
{
_parse_number_hard_drives = JOptionPane.showInputDialog
( "Enter the number of hard drives you want to purchase");
_number_hard_drives = Integer.parseInt ( _parse_number_hard_drives );
//calculate number of boxes shipped
if _number_hard_drives >= 10
_number_of_boxes_shipped = (_number_hard_drives / 10);
else _number_of_boxes_shipped = 1
/*If the customer purchased
1 box of drives the_message should be: âPlease visit us again!â
2 boxes â âVisit our web site www.harddriveworld.comâ
3 boxes â âWe offer more than hard drivesâ
4 boxes â âWe can help installing the drivesâ
5 or more â âThank you for your purchase!â*/
switch ( _number_of_boxes_shipped )
{
case 1:
_message = "Please visit us again!";
break;//end this switch
case 2:
_message = "Visit our web site www.harddriveworld.com;"
break;//end this switch
case 3:
_message = "We offer more than hard drives";
break;//end this switch
case 4:
_message = "We can help installing the drives";
break;//end this switch
case 5:
_message = "Thank you for your purchase!";
break;//end this switch
}//END BLOCK OF SWITCH _number_of_boxes_shipped
/*- Calculate the charge, based on followings:
If the purchase is less than 15 drives, each drive would cost $170
If the purchase is between 15 and 90 drives, each drive would cost $150
If the purchase is more than 90 drives, each drive would cost $120
*/
if (_number_hard_drives < 15)
_drive_price = 170;
Else
if ((_number_hard_drives >= 15) & (_number_hard_drives <=90))
_drive_price = 150;
Else
_drive_price = 120;
_subtotal = (_number_hard_drives * _drive_price);
_tax = _subtotal * .0725;
_cost = _subtotal + _tax;
_output_part_a = "Your Order will be shipped in " + _number_of_boxes_shipped + " boxes";
_output_part_b = "One box will not be full since it will have only " + _drives_less_than_ten + " hard drive(s)";
_output_part_c = "The price for " + _number_hard_drives + " hard drives is " + _subtotal;
_output_part_d = "Tax " + _tax;
_output_part_e = "Total " + _cost;
if _drives_less_than_ten = 0 //do not need "One box will not be full when all boxes are full
_output = _output_part_a + _output_part_c + _output_part_d + _output_part_e;
else
_output = _output_part_a + _output_part_b + _output_part_c + _output_part_d + _output_part_e ;
JOptionPane.showMessageDialog( null, _output, "Student Name Hard Drives", JOptionPane.PLAIN_MESSAGE );
}//End Block of While ( _number_hard_drives != 0)
System.exit(0)
}//End Block of Main
}//End Block of Assignment 3
REQUIREMENTS:
Write, compile and execute Java application to calculate the price
and display the charge information for the Hard Drives Warehouse.
This program should do the following:
- Display a screen asking the user for the
number of hard drives the customer wants to purchase.
- Calculate number of boxes that will be shipped to the customer,
assuming that each box will have 10 hard drives.
If the last box wasn't going to be full,
user must be informed how many drives will be in this box.
DIVIDE NUMBER OF HARD DRIVES ORDERED BY 10
INFORM HOW MANY BOXES WILL BE SHIPPED
INDICATE IF A BOX WILL HAVE LESS THAN 10 DRIVES)
- Calculate the charge, based on followings:
If the purchase is less than 15 drives,
each drive would cost $170
If the purchase is between 15 and 90 drives,
each drive would cost $150
If the purchase is more than 90 drives,
each drive would cost $120
- Add the tax of 7.25%
- Display total price formatted with 2 decimal points.
- Display a friendly_message that is different
depending on number of boxes shipped.
If the customer purchased
1 box of drives the_message should be:
"Please visit us again!"
2 boxes -
"Visit our web site www.harddriveworld.com"
3 boxes -
"We offer more than hard drives"
4 boxes -
"We can help installing the drives"
5 or more -
"Thank you for your purchase!"
Use following structures:
switch - to choose the_message to be displayed
do/while - to allow the program to loop and process next customer.
Make sure that there is a way to stop the loop
and terminate the program, and that the user is informed about this.
if, if/else - to determine which price will apply
All variables have to be commented (data dictionary),
and the pseudocode must be included as a part of program comments.
====================================================================
PSEUDOCODE:
DO WHILE -- UNTIL USER ENTERS ZERO (HOW MANY HARD DRIVES DO YOU
WANT TO ORDER?)
DISPLAY BOX 1: (FROM HANDOUT)
GET CUSTOMER'S ORDER
(HOW MANY HARD DRIVES DO YOU WANT TO ORDER?)
INFORM USER THAT PROGRAM WILL STOP IF THEY ENTER 0
DISPLAY BOX 2 (FROM HANDOUT)
CALCULATE HOW MANY BOXES WILL BE SHIPPED (DIVIDE BY 10)
INFORM CUSTOMER HOW MANY BOXES WILL BE SHIPPED
INFORM CUSTOMER IF A BOX WILL CONTAIN LESS THAN TEN HARD DRIVES
CALCULATE COST (SEE REQUIREMENTS)
CALCULATE TAX
CALCULATE COST + TAX AND DISPLAY
DISPLAY "FRIENDLY_message" BASED ON NUMBER ORDERED (SEE REQUIREMENTS)
END DO
EXIT PROGRAM
==================*/
/*
DATA DICTIONARY
Variable Name Type Function
_number_hard_drives int how many hard drives customer orders
_parse_number_hard_drives String how many hard drives customer orders (parse this)
_number_of_boxes_shipped int how many boxes will be shipped
_drives_less_than_ten int how many is in a box if less than 10
_drive_price floating cost per drive
_subtotal floating to calculate tax
_tax floating tax for order
_cost floating cost of order
_output_part_a String tell the customer how many drives they ordered
_output_part_b String tell the customer how many drives will be in
in a box that is not full
_output_part_c String The price for # hard drives is $9999.99
_output_part_d String Tax $999.99
_output_part_e String Total $999.99
_message String variable greeting based on # of boxes
*/