Skip to content

Instantly share code, notes, and snippets.

@jsuwo
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save jsuwo/8951417 to your computer and use it in GitHub Desktop.

Select an option

Save jsuwo/8951417 to your computer and use it in GitHub Desktop.
Starter code for Lab 7.
package ca.uwo.csd.cs2212.USERNAME;
import java.lang.StringBuilder;
public class Address {
private String street;
private String city;
private String country;
public Address(String street, String city, String country) {
this.street = street;
this.city = city;
this.country = country;
}
public String toString() {
StringBuilder msg = new StringBuilder();
msg.append("Street : ");
msg.append(this.street);
msg.append("\n");
msg.append("City : ");
msg.append(this.city);
msg.append("\n");
msg.append("Country : ");
msg.append(this.country);
msg.append("\n");
return msg.toString();
}
}
package ca.uwo.csd.cs2212.USERNAME;
import java.io.File;
public class App {
private static void storePerson(String[] args) throws Exception {
}
private static void loadPerson(String[] args) throws Exception {
}
private static void showUsageAndExit() {
System.err.println("Usage:");
System.err.println("\tstore filename name street city country phone1 phone2 phone3 ...");
System.err.println("\tload filename");
System.exit(-1);
}
/**
* We expect that the args array will contain either:
*
* store filename name street city country phone1 phone2 phone3 ...
* load filename
*
* You can assume that the arguments will be correct (i.e. you don't have to
* perform validation).
*
* Note that for the 'store' method, you will be passed at least one phone
* number, but may be passed multiple phone numbers.
*/
public static void main(String[] args) throws Exception {
if (args.length == 0)
showUsageAndExit();
switch(args[0]) {
case "store":
if ((args.length < 7) || (new File(args[1]).exists())) {
showUsageAndExit();
}
storePerson(args);
break;
case "load":
if ((args.length != 2) || (! new File(args[1]).exists()) ||
(new File(args[1]).isDirectory())) {
showUsageAndExit();
}
loadPerson(args);
break;
default:
showUsageAndExit();
}
}
}
package ca.uwo.csd.cs2212.USERNAME;
import java.lang.StringBuilder;
import java.util.List;
import java.util.ArrayList;
public class Person {
private String name;
private Address address;
private List<String> phoneNumbers;
public Person() {
this.phoneNumbers = new ArrayList<String>();
}
public Person(String name, Address address) {
this();
this.name = name;
this.address = address;
}
public void addPhoneNumber(String number) {
this.phoneNumbers.add(number);
}
@Override
public String toString() {
StringBuilder msg = new StringBuilder();
msg.append("Name : ");
msg.append(this.name);
msg.append("\n");
if (this.address != null)
msg.append(this.address.toString());
else
msg.append("Address: null");
for (String number : this.phoneNumbers) {
msg.append("Telephone : ");
msg.append(number);
msg.append("\n");
}
return msg.toString();
}
}
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>ca.uwo.csd.cs2212.USERNAME</groupId>
<artifactId>USERNAME-lab7</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>ca.uwo.csd.cs2212.USERNAME.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment