-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson 12.java
146 lines (130 loc) · 3.98 KB
/
Lesson 12.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Student: Griffin Gowdey.
// Instructor: Daniel Goodman.
// Class Number: ITSE1479-86039.
// Class Name: Java Programming.
// Semester: Fall 2020.
// Lesson 12.
/*
Assignment:
Provide a user interface to the invoice program that
allows a user to enter and print an arbitrary invoice.
Do not modify any of the existing classes:
Invoice Program:
// Describes an invoice for a set of purchased products.
public class Invoice
{
// Adds a charge for a product to this invoice.
// @param aProduct the product that the customer ordered.
// @param quantity the quantity of the product.
public void add(Product aProduct, int quantity)
{}
// Formats the invoice.
// @return the formatted invoice.
public String format()
{}
}
// Describes a quantity of an article to purchase.
public class LineItem
{
// Computes the total cost of this line item.
// @return the total price.
public double getTotalPrice()
{}
// Formats this item.
// @return a formatted string of this item.
public String format()
{}
}
// Describes a product with a description and a price.
public class Product
{
// Gets the product description.
// @return the description.
public String getDescription()
{}
// Gets the product price.
// @return the unit price.
public double getPrice()
{}
}
// Describes a mailing address.
public class Address
{
// Formats the address.
// @return the address as a string with three lines.
public String format()
{}
}
*/
import java.util.Scanner;
public class Lesson12
{
public static class InvoicePrinter
{
// Prompt and read a string.
static String read(String prompt, Scanner in)
{
String input;
System.out.print(prompt + ": ");
return in.nextLine();
}
/*
Read an address.
@param in the input stream from which to read the Address.
@return the address.
*/
static Address readAddress(Scanner in)
{
System.out.println("Please enter an address (empty line to terminate)");
String name;
name = read("Name", in);
if (name.equals(""))
return null;
String street;
String city;
String state;
String zip;
street = read("Street", in);
city = read("City", in);
state = read("State", in);
zip = read("Zipcode", in);
return new Address(name, street, city, state, zip);
}
/* Read a Product.
@param in the input stream from which to read the Product. */
static Product readProduct(Scanner in)
{
Product product = null;
System.out.println("Product description (empty line to terminate):");
String description = in.nextLine();
if (description.equals(""))
return null;
System.out.print("Price: ");
double price = in.nextDouble();
in.nextLine();
product = new Product(description, price);
return product;
}
// @param args the command line arguments.
public static void main(String[] args)
{
System.out.println("This program reads an invoice (name, address, products purchased)");
System.out.println("and prints an invoice.\n");
Scanner in = new Scanner(System.in);
Address address;
while ((address = readAddress(in)) != null)
{
Invoice invoice = new Invoice(address);
Product product;
while ((product = readProduct(in)) != null)
{
System.out.println("Quantity: ");
int quantity = in.nextInt();
in.nextLine();
invoice.add(product, quantity);
}
System.out.println(invoice.format());
}
}
}
}