-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW1b.java
executable file
·68 lines (51 loc) · 1.97 KB
/
HW1b.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**Qian Yu CS603 HW1b
* This program is to calculate the delivery cost based on weight and delivery method.**/
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;
public class HW1b {
public static void main (String[] srgs) {
Scanner keyboard = new Scanner(System.in);
//enter the number of pounds & ounces
System.out.println("Enter the number of pounds in whole number: ");
int pound = keyboard.nextInt();
System.out.println("Enter the number of ounces in whole number: ");
int oz = keyboard.nextInt();
//select delivery method, express or regular
System.out.println("Enter 1 for express delivery or any other integer for regular delivery.");
int express = keyboard.nextInt();
//coupon?
System.out.println("Enter 1 if you have a coupon or any other integer if not");
int coupon = keyboard.nextInt();
final double FR = 5.50; //constant express flat rate
final double EXPP = 1.20; //constant express per pound
final double RPP = 0.98; //constant regular per pound
final double ED = 0.07; //constant express discount
final double RD = 0.05; //constant regular discount
//compute total cost
double cost;
if (express == 1) {
cost = FR + (EXPP * pound) + (EXPP * oz / 16);
} else {
cost = RPP * pound + RPP * oz / 16;
}
//compute discount
double discount;
if (coupon == 1) {
if (express ==1 ) {
discount = cost * ED;
} else
discount = cost * RD;
} else
discount = 0.00;
//compute total due after discount
double due = cost - discount;
//two digits after decimal
NumberFormat twodigits = new DecimalFormat("#0.00");
//print total cost, discount & total due
System.out.println("Total cost before discount: $" + twodigits.format(cost));
System.out.println("Amount of discount: $" + twodigits.format(discount));
System.out.println("Final cost: $" + twodigits.format(due));
keyboard.close();
} //end of method
} //end of class