Question

In: Computer Science

Using Eclipse, create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours...

Using Eclipse, create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type double. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type double. For each class, provide its getter and setter functions, a default constructor, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for the Services class is computed as numberOfHours times ratePerHour, and for the Supplies class the cost will be numberOfItems times pricePerItem. Each class should have a function toString() that will return all the required information. Next, create a class called Account with two protected attributes fullName of type String, and accountNum of type integer. Provide appropriate constructors, getter, setters, and toString methods for the class. Make classes Services and Supplies inherit class Account. Write a main() program that uses Java ArrayList to create an array of Account that will store objects of Services and objects of Supplies. Your program should use the loop structure that will ask the user which object to input into the array. The program will then ask the user to enter all the required values. Make up any values when creating each object. The user should have the options to enter one or more objects of Services or Supplies.

Solutions

Expert Solution

Account.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Account {
   protected String fullName;
   protected int accountNum;
   public Account(String fullName, int accountNum) {
       super();
       this.fullName = fullName;
       this.accountNum = accountNum;
   }
   public String getFullName() {
       return fullName;
   }
   public void setFullName(String fullName) {
       this.fullName = fullName;
   }
   public int getAccountNum() {
       return accountNum;
   }
   public void setAccountNum(int accountNum) {
       this.accountNum = accountNum;
   }
   @Override
   public String toString() {
       return "Account [fullName=" + fullName + ", accountNum=" + accountNum + "]";
   }
  
  
   //Main method for testing of the three classes
   public static void main(String[] args) {
       ArrayList<Account> accounts = new ArrayList<Account>();
       String choice="";
       do {
           BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
           try {
               System.out.println("What do you want to add?");
               System.out.println("1. Services.");
               System.out.println("2. Supplies.");
               int option = Integer.parseInt(br.readLine());
               String fullname;
               int ac_no;
               System.out.println("Enter full name: ");
               fullname = br.readLine();
               System.out.println("Enter account number: ");
               ac_no = Integer.parseInt(br.readLine());
               switch(option) {
               case 1:
                   double numHours, rate;
                   System.out.println("Enter number of hours: ");
                   numHours = Double.parseDouble(br.readLine());
                   System.out.println("Enter rate: ");
                   rate = Double.parseDouble(br.readLine());
                   Services s = new Services(fullname, ac_no, numHours, rate);
                   accounts.add(s);
                   break;
               case 2:
                   double numItems, price;
                   System.out.println("Enter number of items: ");
                   numItems = Double.parseDouble(br.readLine());
                   System.out.println("Enter price per item : ");
                   price = Double.parseDouble(br.readLine());
                   Supplies sp = new Supplies(fullname, ac_no, numItems, price);
                   accounts.add(sp);
                   break;
               default:
                   System.exit(-1);
               }
              
               System.out.println("Do you want to add more? yes/no");
               choice = br.readLine();
           }catch(IOException ioe) {
               ioe.printStackTrace();
           }
       }while(choice.contentEquals("yes"));
      
   }
}

Supplies.java

public class Supplies extends Account{
   private double numberOfItems;
   private double pricePerItem;
   public Supplies(String fullName, int accountNum, double numberOfItems, double pricePerItem) {
       super(fullName, accountNum);
       this.numberOfItems = numberOfItems;
       this.pricePerItem = pricePerItem;
   }
   public Supplies(String fullName, int accountNum) {
       super(fullName, accountNum);
   }
   public double getNumberOfItems() {
       return numberOfItems;
   }
   public void setNumberOfItems(double numberOfItems) {
       this.numberOfItems = numberOfItems;
   }
   public double getPricePerItem() {
       return pricePerItem;
   }
   public void setPricePerItem(double pricePerItem) {
       this.pricePerItem = pricePerItem;
   }
   @Override
   public String toString() {
       return "Supplies [numberOfItems=" + numberOfItems + ", pricePerItem=" + pricePerItem + "]";
   }
  
   public double calculateSales() {
       return numberOfItems * pricePerItem;
   }
}

Services.java

public class Services extends Account{
   private double numOfHours;
   private double ratePerHour;
   public Services(String fullName, int accountNum, double numOfHours, double ratePerHour) {
       super(fullName, accountNum);
       this.numOfHours = numOfHours;
       this.ratePerHour = ratePerHour;
   }
   public Services(String fullName, int accountNum) {
       super(fullName, accountNum);
   }
   @Override
   public String toString() {
       return "Services [numOfHours=" + numOfHours + ", ratePerHour=" + ratePerHour + "]";
   }
  
   public double calculateSales() {
       return numOfHours*ratePerHour;
   }
}


Related Solutions

Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this uml as a reference.
Enlistee and Private Classes Write an Enlistee class that keeps data attributes for the following pieces...
Enlistee and Private Classes Write an Enlistee class that keeps data attributes for the following pieces of information: Enlistee name Enlistee number Next, write a class named Private that is a subclass of the Enlistee class. The Private class should keep data attributes for the following information: Platoon number (an integer, such as 1, 2, or 3) Years of service (also an integer) Write the appropriate accessor and mutator methods for each class. Once you have written the classes, write...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make sure the variable names match those #words. size will be a character, either "S", "M", or "L". #extra_shots will be an integer. # #FrapOrder should have a constructor with two required #parameters, one for each of those attributes (size and #extra_shots, in that order). # #FrapOrder should also have a method called get_total. #get_total should calculate the total cost of the order. #If size...
Using JAVA Create a class Client. Your Client class should include the following attributes: Company Name...
Using JAVA Create a class Client. Your Client class should include the following attributes: Company Name (string) Company id (string) Billing address (string) Billing city (string) Billing state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyClient that inherits from the Client class.   HourClient must use the inherited parent class variables and add in hourlyRate and hoursBilled. Your Hourly Client class should contain a constructor that calls the constructor from the Client class to initialize...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create 2 threads that will act as counters. One thread should count down from 5 to 0. Once that thread reaches 0, then a second thread should be used to count up to 20.
Using Python Define a Student class. A Student class needs to have two attributes, the student_name...
Using Python Define a Student class. A Student class needs to have two attributes, the student_name and strudent_grade . It also has three methods as follows: set_grade(grade) that sets the grade of the student. get_grade returns the grade of the student. print_student_info that print the name and grade of student in a formatted string. In the math_utils.py file define a function called average_grade(roster) . This method accepts as input a list of Student Objects and returns the average of the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT