Below is the code and the output from the code running. I need help with the Subscription
Question:
Below is the code and the output from the code running. I need help with the Subscription list class. More specifcally the add method. This code created an arraylist of objects and i am not sure how to grab the price from the object creation or the value from the list creation in the main. I am also confused on the file object creation.
import java.util.*;
/*
Use the main() method in this class to test your other classes.
We provide you with minimalist tests to get you started.
Feel free to modify them or add as many as you need.
This part of your code will not be graded but will help
you ensure that your other classes fullfill the requirements.
*/
public class COP2513FinalExam2021 {
public static void main(String[] args) {
// We first create a bunch of subscriptions, both free and paid
Subscription[] mixedFreeAndPaidSubscriptions = {
new PaidSubscription("MeTube", "Subscribing is optional", 17.0),
new PaidSubscription("Microsift Suite", "With OneDrive", 99.0),
new Subscription("Libre Office"),
new PaidSubscription("Mandalorian Channel","Some other stuff too",79.0),
new Subscription("Oogle Drive", "15Gb"),
new PaidSubscription("Emma Zone Prime","It is delivery AND Digiorno",119.0)
};
// Now we are going to add them to a SubscriptionsList that has
// an insanely large budget (so they are all accepted into the list)
display("Displaying list of subscriptions with insane budget");
SubscriptionsList list1 = new SubscriptionsList(900.0);
for(Subscription s : mixedFreeAndPaidSubscriptions)
list1.add(s);
System.out.print(list1);
// We try to add all of the above subscriptions to another SubscriptionsList
// but, this time, it has a limited budget so that some of the subscriptions
// will not be added to the list once we're over-budget
display("Displaying budget-friendly list");
SubscriptionsList list2 = new SubscriptionsList(150.0);
for(Subscription s : mixedFreeAndPaidSubscriptions)
list2.add(s);
System.out.print(list2);
// Last but not least, we save our budget-friendly SubscriptionsList
// to disk
display("Saving budget-friendly list");
try{
list2.save("subscriptions.txt");
} catch(SubscriptionsListException e){
System.out.println("Sorry, I was unable to save the SubscriptionsList to disk...");
System.out.println("Here are the details...");
System.out.println(e);
}
}// end main method
// Simple method to pretty-display what we do in the main.
public static void display(String msg){
String dashes = "";
for(int i=0 ; i < msg.length() ; i++)
dashes += "-";
System.out.println("" + dashes + ""+ msg + "" + dashes + "");
}
}
// ----------------------- CLASS SUBSCRIPTIONLISTEXCEPTION-----------------------
/*
Use the following class as your custom exception class in the code below
*/
class SubscriptionsListException extends Exception {
public SubscriptionsListException(String msg){
super(msg);
}
}
// ----------------------- CLASS SUBSCRIPTIONSLIST ------------------------------
/* Total = 13 points
This class models information about a list of subscriptions.
The code below compiles but is missing critical parts that you need to write.
Look for the TASK tags to tell you what you need to write.
*/
class SubscriptionsList {
protected ArrayList subs;
//protected ArrayList subs;
private double budget;
private double cost;
/* TASK #2 - .5 point
Provide the code for the following no-args constructor so that it
initialize all attributes and sets the budget to $25.0.
Use proper constructor chaining in order to get full credit.
*/
public SubscriptionsList(){
subs = new ArrayList();
this.cost = cost;
this.budget = 25.0;
}// end constructor
/* TASK #3 - 3 points
Provide the code for the following 1-arg constructor so that it
initialize the list attribute appropriately, set the default
value of budget to the parameter (or $15.0 if the parameter is lesser than $0.0),
and set the current cost to $0.0.
*/
public SubscriptionsList(double budget){
subs = new ArrayList();
if(budget < 0){
this.budget = 15.0;
}else{
this.budget = budget;
}
this.cost = 0.0;
}// end constructor
/* TASK #4 - 2 points
Provide the code for the following toString method so that it
returns a String descriping this object in the following format:
"Subscriptions list:
- Subscription to MeTube (Subscribing is optional): $17.0
- Subscription to Microsift Suite: $99.0
- Subscription to Libre Office
- Subscription to Oogle Drive (15Gb)
- Subscription to Mandalorian Channel (Some other stuff too): $79.0
- Subscription to Emma Zone Prime (It is delivery AND Digiorno): $119.0
Total cost is $314.0 out of $900.0 budgetted
"
*/
public String toString(){
int sum = 0;
String result = "Subscription list: t";
for(Subscription i: subs)
{
result += i;
result += "t";
//sum += i.Subscription.getPrice;
}
return result;
}// end method toString
/* TASK #5 - 3 points
Provide the code for the following method so that it:
- Adds to the ArrayList the subscription passed as parameter
if it is NOT of type PaidSubscription (.5 point)
- If it is of type PaidSubscription, then only add it if its price
will not put us over our budget (.5 point).
- If you end up adding it, also update the cost attribute so as to
reflect how much we have spent so far (.5 point)
*/
public void add(Subscription s){
/*for(Subscription i: subs){
if(i instanceof Subscription){
this.subs.add(s);
}if(){
}*/
this.subs.add(s);
}//end method add
/* TASK #6 - 3 points
Provide the code for the following method so that it:
- Creat es a File object corresponding to the parameter filename (0.5 point)
- Uses it to test if the file actually already exists (0.5 point)
- If it does, we will raise the SubscriptionsListException with a message
indicating "File already exists" (0.5 point).
- Creat es a PrintWriter object that will be automatically closed (0.5 point)
- Uses it to write to the file each of the subscriptions contained
in our ArrayList (0.5 point).
- Catches any exceptions that may be thrown and rethrows instead a
SubscriptionsListException with a message corresponding to the toString
of the exception object (0.5 point).
*/
public void save(String filename) throws SubscriptionsListException {
}// end method save
}// end class SubscriptionsList
// ----------------------- CLASS SUBSCRIPTION ----------------------------------
/* Total = 4 points
This class models a basic subscription to a service.
This version does not handle paid services, a subclass will do that for us.
The code below compiles but is missing critical parts that you need to write.
Look for the TASK tags to tell you what you need to write.
*/
class Subscription {
/* TASK #7 - 1 point (.5 per attribute)
Define the following private attributes;
serviceName of data type String
description of data type String
*/
private String serviceName;
private String description;
/* TASK #8 - 1 point
Provide the code for the following 2-args constructor so that it
initialize the attributes appropriately. Do not modify the name
of the parameters.
*/
public Subscription(String serviceName, String description){
this.serviceName = serviceName;
this.description = description;
}// end constructor
/* TASK #9 - 1 point
Provide the code for the following 1-arg constructor so that it
properly chains with the 2-args constructor from this class.
Use an empty string as argument for the description parameter.
*/
public Subscription(String serviceName){
this(serviceName, "");
}// end constructor
/* TASK #10 - 1 point
Provide the code for the following toString method so that it
returns a String descriping this object in the following format:
"Subscription to Libre Office (Open source microsoft suite replacement)"
In the above example, the service name is "Libre Office"
and the description is "Open source microsoft suite replacement"
*/
public String toString(){
String result = "Subscription to " + serviceName + " (" + description + ")";
return result;
}// end method toString
}//end class Subscription
// ----------------------- CLASS PAIDSUBSCRIPTION --------------------------
/* Total = 5 points
This class models information about a paid subscription.
It is a subclass of the Subscription class.
The code below compiles but is missing critical parts that you need to write.
Look for the TASK tags to tell you what you need to write.
*/
class PaidSubscription extends Subscription {
/* TASK #11 - .5 point
Define the following private attributes;
price of data type double
*/
private double price;
/* TASK #12 - 2 points
Provide the code for the following 3-args constructor so that it
initialize the attributes appropriately. Do not modify the name
of the parameters.
Call the superclass' 2-args constructor with the appropriate arguments.
(1 point)
Since a PaidSubscription can not be free, check that the price is greater
than $0.0. If it is not, then assign $1.0 as the default price instead of
using the parameter value.
(1 point)
*/
public PaidSubscription(String serviceName, String description, double price){
super(serviceName, description);
if(price < 0){
this.price = 1.0;
}else{
this.price = price;
}
}// end constructor
/* TASK #13 - 1 point
Provide the code for the following 2-args constructor so that it
calls this class' 3-args constructor with the appropriate arguments
so that we end up with "" as description.
*/
public PaidSubscription(String serviceName, double price){
this(serviceName, "", price);
}// end constructor
/* TASK #14 - .5 point
Provide the code for the following getter method so that it
returns the value of the price attribute.
*/
public double getPrice(){
return price;
}// end method getPrice
/* TASK #15 - 1 point
Provide the code for the following toString method so that it
returns a String describing this object in the following format:
"Subscription to Microsoft Office (with OneDrive): $99.9"
In the above example, the service name is "Microsoft Office"
and the description is "with OneDrive", the price is 99.9.
*/
public String toString(){
String result = super.toString() + ": $" + price;
return result;
}// end method toString
}// end class PaidSubscription
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill