Need help JAVA programming!

Discussion in 'Java' started by ngereja, Oct 24, 2007.

  1. ngereja

    ngereja New Member

    Joined:
    Oct 24, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi, i am learning java and have problem, can anyone help to solve. I have done most of the codes but i fail to remove arrors. The codes are in 8 Classes. The problem is with the class ContactExtended and Main which links other classes all together.

    1. Class Company:
    Code:
    package phonebookoscar;
    
    public class Company implements Item {
    	private String name;
    	private String phone;
    	private String address;
    	private String email;
    	private int age;
    	
    	public Company() {
    		
    	}
    	
    	public Company(String name, String phone, String address, String email, int age) {
    		this.name = name;
    		this.phone = phone;
    		this.address = address;
    		this.email = email;
    		this.age = age;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public String getPhone() {
    		return phone;
    	}
    	public String getAddress(){
    		return address;
    	}
    	public String getEmail(){
    		return email;
    	}
    	public int getAge(){
    		return age;
    	}
    
    	public String getSurname() {
    		return "";
    }
    
    2. Class ContactExtended
    Code:
    package phonebookoscar;
    
    public class ContactExtended extends Contact implements ExtendedItem{
    	private String email = null;
    	private int age = 0;
    	
    	public ContactExtended() {
    		super();
    	}
    	
    	public ContactExtended(String name, String surname, String phone, int age, String email){
    		super(name, surname, phone);
    			this.email = email;
    			this.age = age;
    	}
    	
    }
    
    
    3. Class Item

    Code:
    Package phonebookoscar;
    
    public interface Item {
    	public String getName();
    	public String getSurname();
    	public String getPhone();
    	
    	
    }
    
    4. Class Contact
    Code:
    Package phonebookoscar;
    
    public class Contact implements Item {
    	private String name = null;
    	private String surname = null;
    	private String phone = null;
    	private String address = null;
    	private String email = null;
    	private int age = 0;
    
    	public Contact() {};
    
    public Contact(String name, String surname, String phone, String address, String email, int age) {
    		this.name = name;
    		this.surname = surname;
    		this.phone = phone;
    		this.address = address;
    		this.email = email;
    		this.age = age;
    	}
    
    	public String getName() {
    		return name;
    			}
    	
    		public void setName(String name) {
    			this.name = name;
    		}
    
    			public String getSurname() {
    				return surname;
    			}
    	
    				public void setSurname(String surname) {
    					this.surname = surname;
    				}
    
    					public String getPhone() {
    						return phone;
    					}
    	
    						public void setPhone(String phone) {
    							this.phone = phone;
    						}
    							public String getAddress(){
    								return address;
    							}
    								public void setAddress(String address){
    									this.address = address;
    							}
    									public String getEmail(){
    										return email;
     									}
    										public void setEmail(String email){
    											this.email = email;
    											}
    												public int getAge(){
    													return age;
    												}
    									public void setAge(int age){
    											this.age = age;
    													}
    													
    										}
    
    5. Class ExtendedItem

    Code:
    package phonebookoscar;
    
    public interface ExtendedItem extends Item{
    	public String getAddress();
    	public String getEmail();
    	public int getAge();
    }
    
    6. Class Keyboard

    Code:
    Package phonebookoscar;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    
    public class Keyboard {
    	private static final BufferedReader keyboard =
    		new BufferedReader(new InputStreamReader(System.in));
    	
    	public static String readString() {
    		String line = null;
    		
    		try {
    			line = keyboard.readLine();
    		}
    		catch(IOException e) {
    			show("Error reading a string of characters.");
    		}
    		
    		return line;
    	}
    	
    	public static int readInteger() {
    		int integer = 0;
    		String line = readString();
    		
    		if(line != null) integer = Integer.parseInt(line);
    		
    		return integer;
    	}
    	
    	public static float readFloat() {
    		float real = 0.0f;
    		String line = readString();
    		
    		if(line != null) real = Float.parseFloat(line);
    			
    		return real;
    	}
    	
    	public static double readDouble() {
    		double real = 0.0;
    		String line = readString();
    		
    		if(line != null) real = Double.parseDouble(line);
    			
    		return real;
    	}
    	
    	private static void show(String line) {
    		System.err.println(line);
    	}
    }
    
    
    7. Class PhoneBook
    Code:
    package phonebookoscar;
    
    public class PhoneBook {
    	private static final int SIZE = 100;
    	private int nContacts = 0;
    	private Item contacts[] = new Item[SIZE];
    
    	public PhoneBook() {}
    
    	public Item getContactAt(int position) {
    		Item contact = new Contact();
    		if(position >= 0 && position < nContacts)
    			contact = contacts[position];
    		return contact;
    	}
    
    	public int getNContacts() {
    		return nContacts;
    	}
    
    	public Item searchForName(String name) {
    		Item contact = null;
    		int position = 0;
    		while((contact == null) && position < nContacts) {
    			if(contacts[position].getName().equals(name))
    				contact = contacts[position];
    			else position++;
    		}
    		if(contact == null) contact = new Contact();
    		return contact;
    	}
    
    	public void addContact(Item contact) {
    		if(nContacts < SIZE)
    			contacts[nContacts++] = contact;
    	}
    }
    
    8. Class Main
    Code:
    package phonebookoscar;
    
    public class Main {
    	private static final int EXIT = 0;
    	private static final int ADD = 1;
    	private static final int LIST = 2;
    	private static final int SEARCH = 3;
    	private static final int ADD_NEW_COMPANY = 4;
    	private static final int SEARCH_ADDRESS = 5;
    	private static final int SEARCH_EMAIL = 6;
    	private static final int SEARCH_AGE = 7;
    	private PhoneBook phoneBook = new PhoneBook();
    	
    	public void menu() {
    		System.out.println("");
    		System.out.println("1. Add a new contact to the phone book.");
    		System.out.println("2. List all contacts in the phone book.");
    		System.out.println("3. Search a contact for name.");
    		System.out.println("4. Add a new company");
    		System.out.println("5. Searh a contact for address.");
    		System.out.println("6. Search a contact for email.");
    		System.out.println("7. Seach a contact for age.");
    		System.out.println("0. Exit.");
    		System.out.print("Choose an option:");
    	}
    	
    	public void doAdd() {
    		System.out.print("Name: ");
    		String name = Keyboard.readString();
    		System.out.print("Surname: ");
    		String surname = Keyboard.readString();
    		System.out.print("Phone number: ");
    		String phone = Keyboard.readString();
    		System.out.print("Address:");
    		String address = Keyboard.readString();
    		System.out.print("Email:");
    		String email = Keyboard.readString();
    		System.out.print("Age:");
    		String age = Keyboard.readString();
    		phoneBook.addContact(new Contact(name, surname, phone,address,email,age));
    	}
    	
    	public void doAddNewCompany() {
    		System.out.print("Name: ");
    		String name = Keyboard.readString();
    		System.out.print("Phone number: ");
    		String phone = Keyboard.readString();
    		System.out.print("Address");
    		String address = Keyboard.readString();
    		System.out.print("Email");
    		String email = Keyboard.readString();
    		System.out.print("Age");
    		String age = Keyboard.readString();
    		
    		phoneBook.addContact(new Company(name, phone, address, email, age));
    	}
    	
    	public void doList() {
    		int nContacts = phoneBook.getNContacts();
    		Item contact;
    		for(int i = 0; i < nContacts; i++) {
    			contact = phoneBook.getContactAt(i);
    			System.out.println("Name: " + contact.getName());
    			System.out.println("Surname: " + contact.getSurname());
    			System.out.println("Phone: " + contact.getPhone());
    			System.out.println("Address: " + contact.getAddress());
    			System.out.println("Email: " + contact.getEmail());
    			System.out.println("Age: " + contact.getAge());
    			
    		}
    	}
    	
    	public void doSearch() {
    		System.out.print("Name to search for: ");
    		String name = Keyboard.readString();
    		Item contact = phoneBook.searchForName(name);
    		System.out.println("Name: " + contact.getName());
    		System.out.println("Surname: " + contact.getSurname());
    		System.out.println("Phone: " + contact.getPhone());
    		System.out.println("Address: " + contact.getAddress());
    		System.out.println("Email: " + contact.getEmail());
    		System.out.println("Age: " + contact.getAge());
    	}
    	
    	public void doAction(int action) {
    		switch(action) {
    		case ADD :
    			doAdd();
    			break;
    		case LIST :
    			doList();
    			break;
    		case SEARCH :
    			doSearch();
    			break;
    		case ADD_NEW_COMPANY:
    			doAddNewCompany();
    			break;
    		case EXIT :
    			System.out.println("Bye!!");
    			break;
    		default :
    			System.out.println("The option is not valid.");
    			break;
    		}
    	}
    	
    	public void go() {
    		int option;
    		do {
    			menu();
    			option = Keyboard.readInteger();
    			doAction(option);
    		}while(option != EXIT);
    	}
    	
    	public static void main(String args[]) {
    		Main main = new Main();
    		main.go();
    	}
    }
     
    Last edited by a moderator: Oct 24, 2007
  2. rizwan6feb

    rizwan6feb New Member

    Joined:
    Oct 29, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    What is the problem?
     
  3. ngereja

    ngereja New Member

    Joined:
    Oct 24, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    the problem is to link the rest of classes with the Main Class. i get stack here!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice