Menu

Import Javautilcalendar Import Javautilscanner Import Javautiltimezone Private Static Fina Q43860656

import java.util.Calendar;
import java.util.Scanner;
import java.util.TimeZone;

private static final String[] LOCATIONS = {

“King’s Cross”,
“Victoria”,
“Waterloo”,
“Euston”,
“Liverpool Street”,
“London Bridge”,
“Paddington”,
“Marylebone”
};
  
   private static final String[] PAYMENT_TYPES = {
           “cash”,
           “creditcard”,
           “debitcard”,
           “bitcoin”
   };
  
private static final String[] TEMPLATE = {
“%n****************************************%n* %s*%n****************************************%n%n”, // +COMPANY_SLOGAN
“Receipt – %s%n%n”, // + receipt reference no
“FixAll – %s%n%n”, // + shop location
“VAT No: %d%n%n”, // + vat no
“Served by: %s%n%n”, // + staff member’s first name (TODO andinitial of surname?)
“———————————————%nYourorder:%n%n”,
“%s%n%n”, // + costs breakdown
“Subtotaltttt£%.2f%n”, // + subtotal (TODO: make it a task tomake the receipts capable of being in euros? [ie make the £/€ inthe receipt configurable] necessary because the Kings Cross branchis seeing lots of European business because of being next to StPancras International)
“Tax (VAT @ 20%%)tttt£%.2f%n%n”, // + tax (TODO: should taxrate also be configurable? VAT may change post Brexit)
“TOTAL : %d item%stttt£%.2f%n%n”, // + number of items, s or nos (single or plural), total amount payable
“PAID (%s) :ttt£%.2f%n%n”, // + payment type (credit card/debitcard/cash/bitcoin/other crypto), amount paid
“TO PAY :tttt£%.2f%n%n”, // + amount left to pay
“———————————————%n%n%n”, //noadditional info
“Exchange and returns policy – %s%n%n”, // + returns policyurl
“All repairs guaranteed for 18 months%nRepair T&Cs – %s%n%n%n”,// + terms & conditions url
“Rate our Service%n%s%n%n%n”, // + 3rd party review site url
“Get 20%% off your order when you refer a friend – see our web pagefor details%n%s%n%n”, // + referral url
“Follow us on %s%n” // + our social media channels
};

public static void create() {
in = new Scanner(System.in);
  
dateAndTime = getDateAndTime();
shopLocation = getShopLocationFromUser();
staffName = getFromUser(“your first name”);
staffName = capitalise(staffName);

itemisedCosts = “”;
numberOfItemsPurchased = 0;
subtotal = 0;
  
boolean keepGoing = true;
//loop so that the user can enter as many items as they needto
while (keepGoing) {
//ask user to enter information about item and charges
String itemDescription = getFromUser(“Item or service user is beingcharged for”);
//TODO client wants the item description in title case
double unitPrice = getDoubleFromUser(“the unit price (without VAT)of ” + itemDescription);
int numberOfUnits = getIntFromUser(“the number of ” +itemDescription);
//TODO make above statements a method

//update receipt information with item and charges foritem
itemisedCosts += addItemisedCostToReceipt(itemDescription,unitPrice, numberOfUnits);
subtotal += (unitPrice * numberOfUnits);
numberOfItemsPurchased += numberOfUnits;
//TODO make above three statements a method

//add more items to the receipt?
String more = getFromUser(“another item or service? Yes/No”);
if (more.trim().toLowerCase().startsWith(“n”)) {
keepGoing = false;
}
//TODO make asking the user if they wish to add more items into amethod
  
}//end of while loop
  
       vat = getVat(subtotal);
//calculate total with vat
total = addVat(subtotal);
  
       paid = getDoubleFromUser(“amountpaid as a deposit by the customer”);
                                    //TODO write a new method to get the depositfrom the user.
                             //enforce that the methodwill (1) give the user the minimum and
                             //the maximum amount of thedeposit (minimum is 20% of the total,
                             //and maximum is the total);(2) The method will enforce that deposit
                             //amounts that are too low ortoo high will be rejected,
                             //and the user asked to enteranother amount. The minimum deposit,
                             //once calculated, should berounded to the nearest int
                             //(normal rounding applies),before being shown to the user.
  
paymentMethod = getFromUser(“deposit payment method”); //TODOvalidate this input. See PAYMENT_TYPES
//all done with user input.

//clean up
in.close();
  
calculateOutstandingBalance();
  
//print the formatted receipt
printReceipt();
}

private static String getDateAndTime() {
Calendar now =Calendar.getInstance(TimeZone.getTimeZone(“Europe/London”));
returnString.format(“%1$te/%<Tb/%<tY::%<tH%<tM%<tz(%<tZ)”,now);
}

private static String getShopLocationFromUser() {
System.out.println(“SHOP LOCATIONS:”);
for (int i = 0; i < LOCATIONS.length; i++) {
System.out.println((i+1) + “) ” + LOCATIONS[i]);
}
int choice = getIntFromUser(“The number that corresponds to yourshop”);
return LOCATIONS[choice-1];
}
  
private static String capitalise(String s) {
       if (s == null) return(“”);
       s = s.trim();
       if (s.length() < 2) returns.toUpperCase();
       returns.substring(0,1).toUpperCase() +s.substring(1).toLowerCase();
   }
  
private static String addItemisedCostToReceipt(StringitemDescription, double unitPrice, int numberOfUnits) {
//format: numberOfUnits x itemDescription @ £unitPrice each:£(unitPrice*NumberOfUnits)
return String.format(“%d x %s @ £%.2f eachtt£%.2f%n”,numberOfUnits, itemDescription, unitPrice, unitPrice *numberOfUnits);
}

private static void calculateOutstandingBalance() {
outstandingBalance = total – paid;
}

private static String getFromUser(String prompt) {
System.out.print(“Enter ” + prompt + “: “);
return in.nextLine();
}

private static int getIntFromUser(String prompt) {
boolean keepGoing = true;
int i = 0;
while (keepGoing) {
System.out.print(“Enter ” + prompt + “: “);
i = in.nextInt();

in.nextLine(); //read the rest of the line that had theint.
//without this, running getIntFromUser() followed bygetFromUser()
//would result in getFromUser() returning the rest of the line thatthe int was part of
//(ie probably just a newline character)

if (i < 1) { //make sure the number is more than 0
System.out.println(“Invalid number. Number must be greater thanzero.”);
} else {
keepGoing = false;
}
}
return i;
}

private static double getDoubleFromUser(String prompt) {
boolean keepGoing = true;
double i = 0;

while (keepGoing) {
System.out.print((“Enter ” + prompt + “: “));
i = in.nextDouble();

in.nextLine(); //read the rest of the line (probably just anewline character)

if (i <= 0) {
System.out.println(“Invalid number. Number must be greater thanzero.”);
} else {
keepGoing = false;
}
}

return i;
}

private static void printReceipt() {
String s = “”;

for (int i = 0; i < TEMPLATE.length; i++) {
s += String.format(TEMPLATE[i], getTemplateData(i));
}

/*
TODO need to fix the tab spacing in the itemised costs
and the other bits between the dashes (the costs breakdownetc)
*/
System.out.println(s);
}

There are three more TODO comments in the user interaction loopin the create() method. These three comments reference turning eachone of the three blocks of code in the loop (separated from eachother by blank lines) into methods. Write these three methods. Youshould write a method to get user input for each item the customeris to pay for, a method to update the receipt with the userinformation about each item, and a method to ask the user if theywish to enter another item (goods or services).
Once you have written your three methods, change the userinteraction loop to have just three statements, each invoking oneof the new methods that you have written. Once you have done this,your user interaction loop should be as follows:
 statement to invoke a method to ask the user for an itemdescription and charging information for the item statement toinvoke a method to update the receipt with the information justentered by the user about an item keepGoing = an invocation of amethod that asks the user if there is another item to be chargedfor, records their response and returns an appropriatevalue

Expert Answer


Answer to import java.util.Calendar; import java.util.Scanner; import java.util.TimeZone; private static final String[] LOCATIONS …

OR