Question

For this problem, we are going to revisit the Online Company exercise from lesson 3. In...

For this problem, we are going to revisit the Online Company exercise from lesson 3. In lesson 3, we created 3 classes, a superclass Company, a subclass OnlineCompany and a runner class CompanyTester. You can take your solutions from lesson 3 for the Company and OnlineCompany, but we are going to redesign the CompanyTester in this exercise.

Your task is to create a loop that will allow users to enter companies that will then get stored in an ArrayList. You should first prompt users for the company name. If the user enters exit, then the program should exit and print the object using the toString.

After prompting for the name, you prompt the user if it is an online company. If so, ask for a website address, otherwise ask for the street address, city, and state. You will then create the Company or OnlineCompany object and insert it into the ArrayList.

Sample output:

Please enter a company name, enter 'exit' to quit: CodeHS
Is this an online company, 'yes' or 'no': yes
Please enter the website address: www.codehs.com
Please enter a company name, enter 'exit' to quit: Uber
Is this an online company, 'yes' or 'no': no
Please enter the street address: 555 Market Street
Please enter the city address: San Fransisco
Please enter the state address: CA
Please enter a company name, enter 'exit' to quit: exit

CodeHS
Website: www.codehs.com

Uber
555 Market Street
San Fransisco, CA

public class Company {
  
private String name;
private String streetAddress;
private String city;
private String state;

// Set missing values to null
public Company(String name){
this.name = name;
this.streetAddress = null;
this.city = null;
this.state = null;
  
}
public Company(String name, String streetAddress, String city, String state){
this.name = name;
this.streetAddress = streetAddress;
this.city = city;
this.state = state;

}

public String getName(){
return name;
}

/**
* Example output:
* 123 Main St
* Springfield, OH
*/
public String address(){
return streetAddress + '\n' + city + ", " + state;
}

/**
* Example output:
* Widget Company
* 123 Main St
* Springfield, OH
*/
public String toString(){
return name + '\n' + streetAddress + '\n' + city + ", " + state;
}
}

public class OnlineCompany extends Company{

private String webAddress;

public OnlineCompany(String name, String webAddress){
super(name, "it", "does not have", "an address");
this.webAddress = webAddress;
}

//Return the website address
@Override
public String address(){
return webAddress;
}

/**
* Remember To get name from superclass, use super.getName()
*
* Example Output:
* CodeHS
* www.codehs.com
*/
@Override
public String toString(){
return super.getName() + '\n' + webAddress;
  
}
}

## the code below is the one I need to complete

import java.util.*;

public class CompanyTester
{
public static void main(String[] args)
{
// Start here!
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please upvote ,comment for any query ,Thanks.

Note : check attached image for output and program .code compiled and tested in netbeans java.

Program : *******************CompanyTester.Java **********************************

import java.util.*;

public class CompanyTester
{
public static void main(String[] args)
{
Scanner scan =new Scanner(System.in); //scanner object
  
List<Object> ListObject=new ArrayList<>(); //create array list
  
while(true)
{
  
String companyName,companyType,webAddress,city,state,street; //attribute for company name
System.out.print("enter a company name or \"exit\": "); //prompt user
companyName=scan.nextLine();
if(companyName.equalsIgnoreCase("exit")) //if user type exit print all company name and attribute
{
for(Object o : ListObject) //first to last object
{
System.out.println(o.toString()); //print attribute and name
}
break; //exit from loop
}
  
System.out.print(companyName+" is an online company, \"yes\" or \"no\" ");
companyType=scan.nextLine(); //get type
if(companyType.equals("yes")) //if company is online add webaddress not location
{
System.out.print("Enter web address of company "+companyName+" :");
webAddress=scan.nextLine();
OnlineCompany object1= new OnlineCompany(companyName, webAddress); //create object
  
  
ListObject.add(object1); //add object to object list
  
  
}
else if(companyType.equalsIgnoreCase("no")) //if company is offline
{
System.out.print("Enter street address of "+companyName+" :"); //get street
street=scan.nextLine();
System.out.print("Enter city name of "+companyName+" :");
city=scan.nextLine();
System.out.print("Enter state name of "+companyName+" :");
state=scan.nextLine();
  
  
Company companyObject = new Company(companyName, street,city,state); //create object of company
  
ListObject.add(companyObject); //add to list
}
  
  
  
}

}
}

Program : ***************************************Company.java**********************

public class Company {
  
private String name;
private String streetAddress;
private String city;
private String state;

// Set missing values to null
public Company(String name){
this.name = name;
this.streetAddress = null;
this.city = null;
this.state = null;

}
public Company(String name, String streetAddress, String city, String state){
this.name = name;
this.streetAddress = streetAddress;
this.city = city;
this.state = state;

}

public String getName(){
return name;
}

/**
* Example output:
* 123 Main St
* Springfield, OH
* @return
*/
public String address(){
return streetAddress + '\n' + city + ", " + state;
}

/**
* Example output:
* Widget Company
* 123 Main St
* Springfield, OH
*/
@Override
public String toString(){
return name + '\n' + streetAddress + '\n' + city + ", " + state;
}
}

Program : *************OnlineCompany.java*********************************

public class OnlineCompany extends Company{

private String webAddress;

public OnlineCompany(String name, String webAddress){
super(name, "it", "does not have", "an address");
this.webAddress = webAddress;
}

//Return the website address
@Override
public String address(){
return webAddress;
}

/**
* Remember To get name from superclass, use super.getName()
*
* Example Output:
* CodeHS
* www.codehs.com
*/
@Override
public String toString(){
return super.getName() + "\nwebsite:" + webAddress;

}
}

Output :

Building Online Company 1.0-SNAPSHOT - --- exec-maven-plugin:1.5.0:exec (default-cli) @ Online Company --- enter a company na

Please upvote

Add a comment
Know the answer?
Add Answer to:
For this problem, we are going to revisit the Online Company exercise from lesson 3. In...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write a parent class that describes a parcel (like a package, as shown below) and a...

    write a parent class that describes a parcel (like a package, as shown below) and a child class that describes an overnight parcel. Use the provided Address class. A parcel is described by: id (which might contain numbers and letters) weight (described as the number of pounds; a parcel could be less than 1 pound) destination address (uses the provided class) An overnight parcel is described by id, weight, destination address and also: whether or not a signature is required...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).

    Ch 7 Program: Online shopping cart (continued) (Java)This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)Public member methodssetDescription() mutator & getDescription() accessor (2 pts)printItemCost() - Outputs the item name followed by the quantity, price, and subtotalprintItemDescription() - Outputs the...

  • The ", delimiter" should now be changed to use the "^ delimiter". There needs to be...

    The ", delimiter" should now be changed to use the "^ delimiter". There needs to be an 2nd address field added to the original code, as well as, a "plus 4" on the zip code (example: 47408-6606). Then the additional prompts must be added. Thank you! Last Real-World problem (in module 04 - 05), you added functionality to allow the user to enter multiple patients until the user indicated done. You are to enhance this functionality. Add the following functionality...

  • Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; //...

    Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; // Scanner class to support user input public class TestPetHierarchy { /* * All the 'work' of the process takes place in the main method. This is actually poor design/structure, * but we will use this (very simple) form to begin the semester... */ public static void main( String[] args ) { /* * Variables required for processing */ Scanner input = new Scanner( System.in...

  • In this assignment, we will be making a program that reads in customers' information, and create...

    In this assignment, we will be making a program that reads in customers' information, and create a movie theatre seating with a number of rows and columns specified by a user. Then it will attempt to assign each customer to a seat in a movie theatre. 1. First, you need to add one additional constructor method into Customer.java file. Method Description of the Method public Customer (String customerInfo) Constructs a Customer object using the string containing customer's info. Use the...

  • In this practice program you are going to practice creating graphical user interface controls and placing...

    In this practice program you are going to practice creating graphical user interface controls and placing them on a form. You are given a working NetBeans project shell to start that works with a given Invoice object that keeps track of a product name, quantity of the product to be ordered and the cost of each item. You will then create the necessary controls to extract the user input and display the results of the invoice. If you have any...

  • Original question: I need a program that simulates a flower pack with the traits listed below:...

    Original question: I need a program that simulates a flower pack with the traits listed below: - You must use a LinkedList for storage (not an Arrray list). - You must be able to display, add, remove, sort, filter, and analyze information from our flower pack. - The flower pack should hold flowers, weeds, fungus, and herbs. All should be a subclass of a plant class. - Each subclass shares certain qualities (ID, Name, and Color) – plant class -...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT