The problem is
Design a class named Person with fields for holding a person's name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods.
Next design a class named Customer, that extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator and accessor methods for the class's fields. Demonstrate an object of the Customer class in a simple program.
Ok so for this program, I believe that I am doing everything correctly, however, I am really confused with the boolean field set up. Ive tried and it doesnt work. Also in the telephone number, how to write with the dashes in between. I first tried it as an int field but changed to double but when I put in the number 7705642593 the output gives me 7.705642593E9. I dont know how it gets that. The address field is the same. I want to input the number of the city and the name of it but since its a String field, it doesnt let me give the number. I am sorry that post is so long, but any help would be highly aprreciated.
Thank you and here is what I have so far:

public class Person
{
	private String name;
	private String address;
	private double phonenumber;
	
	public Person(String name, String address, double phonenumber)
	{
		this.name = name;
		this.address = address;
		this.phonenumber = phonenumber;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public void setAddress(String address)
	{
		this.address = address;
	}
	public void setNumber(int phonenumber)
	{
		this.phonenumber = phonenumber;
	}
	public String getName()
	{
		return name;
	}
	public String getAddress()
	{
		return address;
	}
	public double getNumber()
	{
		return phonenumber;
	}
}

the customer class

public class Customer extends Person
{
	private int idnumber;
	private boolean onlist = true;
	
	public Customer(int idnumber, boolean onlist, String name, double phonenumber, String address)
	{
		super(name, address, phonenumber);
		this.idnumber = idnumber;
		this.onlist = onlist;
	}
	
	public void setIdNumber(int idnumber)
	{
		this.idnumber = idnumber;
	}
	public void setToBeOnList(boolean onlist)
	{
		if(onlist)
		{
			System.out.println("The customer wants to be on the mailing list.");
		}
	}
	public int getIdNumber()
	{
		return idnumber;
	}
	public boolean getToBeOnList()
	{
		return onlist;
	}
}

the customer demo class

import java.util.Scanner;

public class CustomerDemo
{
	public static void main(String[] args)
	{
		String name;
		String address;
		double phonenumber;
		int idnumber;
		boolean onlist = false;
		String letter;
		
		Scanner kb = new Scanner(System.in);
		
		System.out.println("Name: ");
		name = kb.nextLine();
		
		System.out.println("Address: ");
		address = kb.nextLine();
		
		System.out.println("Phonenumber: ");
		phonenumber = kb.nextDouble();
		
		System.out.println("IdNumber: ");
		idnumber = kb.nextInt();
		
		System.out.println("Would you like to be on the mailing list? y/n: ");
		letter = kb.nextLine();
		
		if(letter.equalsIgnoreCase("Y"))
		{
			onlist = true;
		}
		else
		{
			onlist = false;
		}		
		
		Customer cm = new Customer(idnumber, onlist, name, phonenumber, address);
		
		System.out.println("Here is some information about the customer.");
		
		System.out.println("Name: " + cm.getName());
		System.out.println("Address: " + cm.getAddress());
		System.out.println("Phone Number: " + cm.getNumber());
		System.out.println("IdNumber: " + cm.getIdNumber());
		System.out.println("Would you like to be on the mailing list: " + cm.getToBeOnList());
	}
}

again sorry its going to take a while to read :)

You're taking the telephone number as a double, which means it's treating it as a double-precision floating-point number.
Since you're not planning on doing any math with the phone number, I'd suggest you store it as a String instead. That'll also give you the option of accepting it as a free-form (555.1212 or 282-9268 or what have you)

I'm not sure what's not working with the address - it should be fine with taking any alphanumeric characters you throw at it. What exactly is it telling you when it doesn't take the address?

Edited 6 Years Ago by jon.kiparsky: n/a

I got that but thanks alot! However, I have more questions.:/ Can you help me set up the boolean field indicating whether the customer wants to be on the mailing list or not.Can you give my code a look and see what Im doing wrong? Appreciate it!

What is the behavior you're expecting, and what is the behavior you're seeing with this code? I don't see any obvious flaws, but more information will help me look in the right places.

I started here:

System.out.println("Would you like to be on the mailing list? y/n: ");
	letter = kb.nextLine();
		
	if(letter.equalsIgnoreCase("Y"))
		{
			onlist = true;
		}
	else
		{
			onlist = false;
		}

This looks correct: it should assign onlist the values of true just in the case when the customer enters the single character "Y" or "y", and false otherwise.

By way of an experiment, try putting in this line before the if and see what you get:

System.out.println(letter.equalsIgnoreCase("Y"));

I think this is not where your problem is, but try the experiment to be sure. If it prints "true" for the inputs "Y" and "y", and false otherwise, then your problem is not here, and we'll have to look elsewhere.

(by the way, this test should suggest to you a slightly better way of managing this assignment to onlist - the comparison letter.equalsIgnoreCase("Y") returns a boolean value....)

This article has been dead for over six months. Start a new discussion instead.