Hello everyone. I am just getting into java and I saw on other forums on the web that an address book would be a good program to code for a beginner - intermediate level coder. I know C++ quite well, and Java is still a little foreign to me.
Could you guys look at my code and try and fix it and/or tell me what is wrong with it. Thank you so much!
package address.book.program;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class AddressBookProgram {
static Contact contactArray[] = new Contact[100];
public static void main(String[] args) {
DisplayCurrentObject(0);
}
public static void DisplayCurrentObject(int index){
String firstName = Contact.getFirstName();
String lastName = Contact.getLastName();
String address1 = Contact.getAddress1();
String address2 = Contact.getAddress2();
String city = Contact.getCity();
String province = Contact.getProvince();
String postal= Contact.getPostal();
System.out.println("|-----------------------------------------------|");
System.out.println("|Contact #: " + index + " |");
System.out.println("|-----------------------------------------------|");
System.out.println("|First Name: " + firstName + " |");
System.out.println("|Last Name: " + lastName + " |");
System.out.println("|Address1: " + address1 + " |");
System.out.println("|Address2: " + address2 + " |");
System.out.println("|City: " + city + " |");
System.out.println("|Province: " + province + " |");
System.out.println("|Postal Code: " + postal + " |");
System.out.println("|-----------------------------------------------|");
System.out.println("| 'P': Previous 'N': Next |");
System.out.println("|'U': Update 'D': Delete 'C': Create |");
System.out.println("|-----------------------------------------------|");
InputCrudCommand(index);
}
public static void ProgramPathGate(char CrudCommandInputValue, int index){
if (CrudCommandInputValue == 'p') { // Previous
if (index == 0) {
DisplayCurrentObject(0);
}
else {
index--;
DisplayCurrentObject(index);
}
}
if (CrudCommandInputValue == 'n') { // Next
if (index == 100) {
DisplayCurrentObject(0);
}
else {
index++;
DisplayCurrentObject(index);
}
}
if (CrudCommandInputValue == 'u') { // Update
UpdateContact(index);
}
if (CrudCommandInputValue == 'd') { // Delete
DeleteContact(index);
}
if (CrudCommandInputValue == 'c') { // Create
CreateContact();
}
}
public static void CreateContact() {
String currentInfo;
int index = 0;
if((currentInfo = NewContactInput()) != null){
Contact contact = new Contact();
String newlyInputedContactInfo[] = currentInfo.split(",");
Contact.setFirstName(newlyInputedContactInfo[0]); // insert this stuff into update class method
Contact.setLastName(newlyInputedContactInfo[1]);
Contact.setAddress1(newlyInputedContactInfo[2]);
Contact.setAddress2(newlyInputedContactInfo[3]);
Contact.setCity(newlyInputedContactInfo[4]);
Contact.setProvince(newlyInputedContactInfo[5]);
Contact.setPostal(newlyInputedContactInfo[6]);
contactArray[index] = contact;
index++;
DisplayCurrentObject(index);
}
if(NewContactInput() == null) {
System.out.println("Maximum Number of Contacts Reached.");
DisplayCurrentObject(0);
}
}
public static void DeleteContact(int index){
}
public static void UpdateContact(int index){
}
public static void InputCrudCommand(int index) {
String CrudCommandInput = null;
char CrudCommandInputValue = 0;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" ");
System.out.print(" Choice: ");
CrudCommandInput = reader.readLine();
CrudCommandInputValue = CrudCommandInput.charAt(0);
reader.close();
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
System.out.println("Input is invalid.");
}
ProgramPathGate(CrudCommandInputValue, index);
}
public static String NewContactInput(){
String userInput = null;
int i = 0;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your: First Name, Last Name, Address1, Address2, City, Province, and Postal Code all separated by a comma (,): ");
userInput = reader.readLine();
reader.close();
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
System.out.println("Input is invalid.");
}
i++;
if (i == 100) {return null;}
return userInput;
}
}
class Contact {
static String firstName, lastName, address1, address2, city, province, postal;
public static void setFirstName (String str) {
firstName = str;
}
public static String getFirstName () {
return firstName;
}
public static void setLastName (String str) {
lastName = str;
}
public static String getLastName () {
return lastName;
}
public static void setAddress1 (String str) {
address1 = str;
}
public static String getAddress1 () {
return address1;
}
public static void setAddress2 (String str) {
address2 = str;
}
public static String getAddress2 () {
return address2;
}
public static void setCity (String str) {
city = str;
}
public static String getCity () {
return city;
}
public static void setProvince (String str) {
province = str;
}
public static String getProvince () {
return province;
}
public static void setPostal (String str) {
postal = str;
}
public static String getPostal () {
return postal;
}
}