In: Computer Science
Coding in Java Assignment
Customer Name: Xxxxxxxxxxxx
Invoice No: XXXXXXXX
Customer Name: Xxxxxxxxxxxx
Invoice No: XXXXXXXX
Average of Items Purchased: $ZZZ,ZZ9.99
2. Write statements that will call each of the methods you coded above. Be conscious of the value-returning methods. Assume the method calls are done in the main() and the main() is in the same class.
a.
b.
c.
d.
e.
f.
g.
1).ANSWER:
GIVEN BELOW:
import java.util.Scanner;
public class CustomerShopping {
Scanner sc=new Scanner(System.in);
String customerName;
String InvoiceNo;
int noItemsPurchased;
double subTotal;
// A method that prompts for the customer’s name and returns it
from the keyboard.
public String setCustomerName() {
System.out.print("Enter customer Name :");
this.customerName = sc.nextLine();
return this.customerName;
}
// A method called shippingInvoice() that prompts for an invoice
number and stores it in a class variable (field) called invoiceNo.
Assume the invoice number is a combination of numbers and
text.
public String shippingInvoice() {
System.out.print("Enter Invoice Number :");
this.InvoiceNo = sc.nextLine();
return this.InvoiceNo;
}
// Overload the method coded in 1b above so that it accepts a
customer’s name through its parameter list, concatenates the name
and invoice number in a header and returns the header. This method
will not prompt for the name or invoice number. The content of the
header will look like this where the Xs represent the actual
customer name and invoice number:
public String CombineNameInvoice(String customerName){
String combinedCode = customerName+this.InvoiceNo;
return combinedCode;
}
// A method that prompts for the number of items purchased and
returns it from the keyboard.
public int setNoItemsPurchased() {
System.out.print("Enter No of items purchased :");
this.noItemsPurchased = sc.nextInt();
return this.noItemsPurchased;
}
// A method that prompts for the subtotal and returns it from the
keyboar
public double setSubTotal() {
System.out.print("Enter Sub Total :");
this.subTotal = sc.nextInt();
return this.subTotal;
}
// A method that accepts the noItemsPurchased and the subtotal
through its parameter list and returns an average dollar amount of
the items purchased. No prompts are needed.
public double calculateAverage(int noItemsPurchased,double
subTotal){
return (double)subTotal/noItemsPurchased;
}
// A method that accepts what is returned by the methods coded in
1c and 1f to print the following:
public void displayBill(String combinedName,double average){
System.out.println("Customer Name: "+combinedName);
System.out.println("Invoice No: "+InvoiceNo);
System.out.println("Average of Items Purchased: "+average);
}
public static void
main(String[] args) {
CustomerShopping customer =new CustomerShopping();
String customerName=customer.setCustomerName();
String InvoiceNo=customer.shippingInvoice();
int noItemsPurchased = customer.setNoItemsPurchased();
double subTotal=customer.setSubTotal();
String CombineNameInvoice =
customer.CombineNameInvoice(customerName);
double average = customer.calculateAverage(noItemsPurchased,
subTotal);
customer.displayBill(CombineNameInvoice, average);
}
}