64 747
Assignments Done
99,3%
Successfully Done
In September 2018

Answer to Question #67933 in Java | JSP | JSF for Simon Roberts

Question #67933
Write a public instance method for the Garage class called addCustomer(),
which takes four arguments as in the following header:public void
addCustomer(String regNo, String name, String address, int
area)
and returns no value.
The method should create an instance of Customer and then add it to the map
referenced by the instance variable customers with regNo as the key.
Test your method in the OUWorkspace by creating an instance of Garage and
assigning it to a variable called aGarage. Then send addCustomer()
messages to the Garage object to add three customers as shown in the table
above. Inspect aGarage to confirm that the customers were added.
Save the statements from the OUWorkspace that you used to add the
customers (for example in the project’s README.txt file) for use later in this
question.
Expert's answer
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

/**
* Garage has customers.
*/
public class Garage {

/** Customer to reg number. */
private Map<String, Customer> storage = new HashMap<>();

/**
* Test method.
*/
public static void main(String[] args) throws IOException {
Garage aGarage = new Garage();
aGarage.addCustomer("1a", "Piter", "Wall street 11a", 110);
aGarage.addCustomer("2b", "Lois", "Wall street 22b", 220);
aGarage.addCustomer("3c", "Jonh", "Wall street 33c", 330);

Path path = Paths.get("README.txt");
if (!Files.exists(path)) {
Files.createFile(path);
}
try (final BufferedWriter writer = Files.newBufferedWriter(path)) {

aGarage.storage.forEach((k, customer) -> {
System.out.println("key : " + k + " customer : " + customer);
try {
writer.write(customer.regNo + "\n");
writer.write(customer.name + "\n");
writer.write(customer.address + "\n");
writer.write(customer.area + "\n\n");
} catch (IOException e) {
throw new RuntimeException(e);
}
});

}

}

/**
* Add customer.
*
* @param regNo customer reg number.
* @param name customer name.
* @param address customer address.
* @param area customer area.
*/
private void addCustomer(String regNo, String name, String address, int area) {
Customer customer = new Customer(regNo, name, address, area);
storage.put(regNo, customer);
}

/**
* Customer.
*/
private static class Customer {

/** Customer reg number. */
private String regNo;

/** Customer name. */
private String name;

/** Customer address. */
private String address;

/** Customer area. */
private int area;


/**
* Create customer.
*
* @param regNo customer reg number.
* @param name customer name.
* @param address customer address.
* @param area customer area.
*/
Customer(String regNo, String name, String address, int area) {
this.regNo = regNo;
this.name = name;
this.address = address;
this.area = area;
}

@Override
public String toString() {
return "Customer{" + "regNo='" + regNo + '\'' + ", name='" + name + '\'' + ", address='" + address + '\'' + ", area=" + area + '}';
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions