lundi 30 janvier 2017

passing an object in an instance method

I am trying to calculate the minutesUsed of a phone call between two people. However I'm pretty sure that in my public void phone(Customer name, int callLength) there is an error but I am not sure where. It may have something to do with the fact that Customer name is never used? Here are the conditions:

  1. If either customer has a "pay-as-you-go" plan and the number of callLength minutes exceeds the minutesRemaining for either "pay-as-you-go" customer, then the call is not allowed ... nothing happens.
  2. After the method is called, both customers' minutesUsed amounts should increase by callLength minutes.
  3. If the call was successful, each customers' number of callsMade should increase by 1.

I don't know if my if statements are faulty but any help would be greatly appreciated. Thanks!

//Cell Phone Class

    public class CellPhone {

    String model;
    String manufacturer;
    int monthsWarranty;
    float price;


    public CellPhone() {

    }

    public CellPhone(String model, String manufacturer, int monthsWarranty, float price) {
        this.model = model;
        this.manufacturer = manufacturer;
        this.monthsWarranty = monthsWarranty;
        this.price = price;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public int getMonthsWarranty() {
        return monthsWarranty;
    }

    public void setMonthsWarranty(int monthsWarranty) {
        this.monthsWarranty = monthsWarranty;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }


    }

//Phone Plan Class

public class PhonePlan {
    public int minutesAllowed;
    public int minutesUsed = 0;
    public int dataAllowed;
    public int dataUsed = 0 ;
    public boolean planType;

    public int dataRemaining;
    public int minutesRemaining;


    public PhonePlan(int minutesAllowed, int dataAllowed, boolean planType) {
        this.minutesAllowed = minutesAllowed;
        this.dataAllowed = dataAllowed;
        this.planType = planType;


    }



    public int getMinutesAllowed() {
        return minutesAllowed;
    }

    public void setMinutesAllowed(int minutesAllowed) {
        this.minutesAllowed = minutesAllowed;
    }

    public int getMinutesUsed() {
        return minutesUsed;
    }

    public void setMinutesUsed(int minutesUsed) {
        this.minutesUsed = minutesUsed;
    }

    public int getDataAllowed() {
        return dataAllowed;
    }

    public void setDataAllowed(int dataAllowed) {
        this.dataAllowed = dataAllowed;
    }

    public int getDataUsed() {
        return dataUsed;
    }

    public void setDataUsed(int dataUsed) {
        this.dataUsed = dataUsed;
    }


    public boolean getPlan() {

      return planType;
    }

    public void setPlanType(boolean planType) {
        this.planType = planType;
    }

    public int getMinutesRemaining() {
        return (minutesAllowed-minutesUsed);
    }

    public int getDataRemaining() {
        return (dataAllowed-dataUsed);
    }

    @Override
    public String toString() {
        if(planType)
        {
            return("Pay-as-you-go Plan with " + minutesAllowed + " minutes and " + dataAllowed+ " KB remaining");
        }
        else
        {
            return("Regular (" +(minutesAllowed +" minute, "+ dataAllowed/1000000f +" GB data)"
                    +") Monthly Plan with "+ minutesAllowed+" minutes and "+ dataAllowed+ " KB remaining"));
        }


    }
}

//Customer Class

public class Customer {

    String name; //Customer Name
    CellPhone cellPhone; // Cell phone object type
    PhonePlan phonePlan; //Phone plan Object type
    int callsMade;
    float balance = 0;

    int internetData;
    int minutesBought;
    int monthlyCharges;
    int voiceOvertimeCharges;
    int dataOverusageCharges;
    double HST;
    double totalDue;



    public Customer(String customerName,CellPhone cellPhone,PhonePlan Plan){
        this.name = customerName;
        this.cellPhone = cellPhone;
        this.phonePlan = Plan;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCallsMade() {
        return callsMade;
    }

    public void setCallsMade(int callsMade) {
        this.callsMade = callsMade;
    }

    public float getBalance() {
        return balance;
    }

    public void setBalance(float balance) {
        this.balance = balance;
    }
    public CellPhone getCellPhone() {
        return cellPhone;
    }


    public void setCellPhone(CellPhone cellPhone) {
        this.cellPhone = cellPhone;
    }

    public PhonePlan getPlan() {
        phonePlan.minutesRemaining = phonePlan.minutesAllowed - phonePlan.minutesUsed;

        return phonePlan;
    }



    public void setPlan(PhonePlan phonePlan) {
        this.phonePlan = phonePlan;
    }

    @Override
    public String toString() {
        if(phonePlan.toString().equals("Regular")){
            return this.getName()+ " with a " + cellPhone.getManufacturer()+ " " +cellPhone.getModel()+" "+
                    "on a"+" "+phonePlan.toString()+" Plan "+"("+" "+phonePlan.getMinutesAllowed()+","+phonePlan.getDataAllowed()+"KB "+")"+"Monthly Plan with "+
                    phonePlan.getMinutesRemaining()+"minuutes remaining and "+" "+phonePlan.dataRemaining+" "+"KB remaining";
        } else {
            return this.getName()+ " with a " +cellPhone.getManufacturer()+" "+cellPhone.getModel()+
                    " on a " + phonePlan.toString()+ " Plan with " +phonePlan.getMinutesAllowed()+" minutes and " +
                    phonePlan.getDataAllowed()+" KB remaining";
        }

    }

    public void phone(Customer name, int callLength){
        if((phonePlan.toString().equals("Pay-as-you go")) && (callLength > phonePlan.minutesRemaining)){

            phonePlan.minutesUsed =  phonePlan.minutesUsed  + callLength;

        } else {
            callsMade = callsMade + 1;
            phonePlan.minutesUsed =  phonePlan.minutesUsed  + callLength;


        }
}

    public int buyMinutes(int minutesBought){
        if((phonePlan.toString().equals("Pay-as-you go"))){
            minutesBought = (int) (minutesBought * 0.40);
            balance  = balance + minutesBought;
            return minutesBought;
        }

        return minutesBought;

    }

    public void accessInternet(int internetData){
        this.internetData = internetData;

    }


    public int Customer(int monthlyCharges){
        if((phonePlan.toString().equals("Regular")) && (phonePlan.minutesAllowed == 100)){
            monthlyCharges = 15 + (phonePlan.dataAllowed * 10);
            return monthlyCharges;
        } else{
            monthlyCharges = 25 + (phonePlan.dataAllowed * 10)+ minutesBought;
            return monthlyCharges;
        }
    }

    public Customer(double voiceOvertimeCharges, double dataOverusageCharges){
        if((phonePlan.toString().equals("Regular")) && (phonePlan.minutesAllowed == 100) && (phonePlan.minutesUsed > 100)){
            voiceOvertimeCharges = (phonePlan.minutesUsed - phonePlan.minutesAllowed)* 0.15;
        } else {
            voiceOvertimeCharges = (phonePlan.minutesUsed - phonePlan.minutesAllowed)* 0.15;

        }
        if((phonePlan.toString().equals("Regular")) && (phonePlan.minutesAllowed == 100) && (phonePlan.minutesUsed > 100)){
            dataOverusageCharges = (phonePlan.dataUsed - phonePlan.dataAllowed)* 0.00005;
        } else {
            dataOverusageCharges = (phonePlan.dataUsed - phonePlan.dataAllowed)* 0.00005;

        }
    }

    public void printMonthlyStatement(){
        if((phonePlan.toString().equals("Regular"))){

        System.out.println(String.format("\n%17s%15s","Name:", name));
        System.out.println(String.format("%19s%15s","Plan Type: ", phonePlan));
        System.out.println(String.format("%19s%10d","Minutes Used: ", phonePlan.minutesUsed));
        System.out.println(String.format("%19s%10d","Calls Made: ", callsMade));
        System.out.println(String.format("%17s%10d","Monthly Charges: ", monthlyCharges));
        System.out.println(String.format("%17s%10.2f","Voice Overtime Charges: ", voiceOvertimeCharges));
        System.out.println(String.format("%17s%10.2f","Data Overusage Charges: ", dataOverusageCharges));
        HST = (monthlyCharges + voiceOvertimeCharges + dataOverusageCharges) * 0.13f;
            System.out.println(String.format("%19s%10.2f","HST: ", HST));
        totalDue = monthlyCharges + voiceOvertimeCharges + dataOverusageCharges + HST;
            System.out.println(String.format("%19s%10.2f","Total Due: ", totalDue));
        }
        else{
            // pay-as-you-go plan
            System.out.println(String.format("\n%17s%15s","Name:", name));
            System.out.println(String.format("%19s%15s","Plan Type: ", phonePlan));
            System.out.println(String.format("%19s%10d","Minutes Used: ", phonePlan.minutesUsed));
            System.out.println(String.format("%17s%10d","Minutes remaining: ", phonePlan.minutesRemaining));
            System.out.println(String.format("%19s%10d","Data Used: ", phonePlan.dataUsed));
            System.out.println(String.format("%19s%10d","Data Reaminging: ", phonePlan.dataRemaining ));
            System.out.println(String.format("%19s%10d","  Calls Made: ", callsMade));
            System.out.println(String.format("%19s%10d","Monthly Charges: ", monthlyCharges));
            HST =  monthlyCharges * 0.13f;
            System.out.println(String.format("%19s%10.2f","HST: ", HST));
            totalDue =  monthlyCharges + HST;
            System.out.println(String.format("%19s%10.2f","Total Due: ", totalDue));
        }

    }


}

The following code is the testing code for the classes above:

//CallStimulationTestProgram

    public class CallStimulationTestProgram {
        public static void main(String args[]) {
        // Create some phone objects
        CellPhone iPhone = new CellPhone("iPhone 6Plus", "Apple", 12, 915.00f);
        CellPhone galaxy = new CellPhone("Galaxy S7", "Samsung", 18, 900.00f);
        CellPhone priv = new CellPhone("PRIV", "BlackBerry", 12, 890.00f);
        // Create some customer objects. Only Tim and April have a "pay-as-you-go" plan
        // (identified by a true Boolean value), the others are on standard monthly plans
        // (identified by a false Boolean value). Realistically, these purchases would
        // occur at different times on different days but we are assuming that all 5
        // Customers purchase at the same time.
        Customer rob = new Customer("Rob Banks", iPhone, new PhonePlan(200, 2500000, false));
        Customer april = new Customer("April Rain", galaxy, new PhonePlan(200, 1000000, true));
        Customer rita = new Customer("Rita Book", priv, new PhonePlan(100, 500000, false));
        Customer sue = new Customer("Sue Permann", iPhone, new PhonePlan(100, 2500000, false));
        Customer tim = new Customer("Tim Bur", iPhone, new PhonePlan(30, 0, true));
        // Show the Customers
        System.out.println("\nHere are the customers:\n");
        System.out.println(rob);
        System.out.println(april);
        System.out.println(rita);
        System.out.println(sue);
        System.out.println(tim);
        // Have the customers make some phone calls to other customers
        rob.phone(sue, 12); // a 12 minute call from Rob's phone to Sue's phone
        rita.phone(april, 27);
        rob.phone(tim, 3);
        tim.phone(rita, 19);
        // This line now assumes that Tim's call was cut off after 8 minutes,
        // because his plan only allows 8 more minutes.
        tim.phone(sue, 8);
        // Output to show how the remaining unused minutes on each account
        // have changed
        System.out.println("\nRob's minutes = " + rob.getPlan().getMinutesRemaining());
        System.out.println("April's minutes = "+ april.getPlan().getMinutesRemaining());
        System.out.println("Rita's minutes = " + rita.getPlan().getMinutesRemaining());
        System.out.println("Sue's minutes = " + sue.getPlan().getMinutesRemaining());
        System.out.println("Tim's minutes = " + tim.getPlan().getMinutesRemaining());
        // Try some more calls
        rob.phone(tim, 1); // Should not connect at all
        rob.phone(sue, 1);
        sue.phone(tim, 1); // Should not connect at all
        // Tim gets his phone working again by paying for more minutes
        tim.buyMinutes(100);
        // Output to show how the remaining unused minutes on Tim's account has changed
        System.out.println("\nTim's minutes = " + tim.getPlan().getMinutesRemaining());
        // Tim lets Rob know that his phone is working again.
        // Then Rob tells Sue who then phones Tim to chat.
        tim.phone(rob, 24); // OK now rob.phone(sue, 15);
        sue.phone(tim, 68); // Sue's limit will exceed and she must pay extra
        rita.phone(sue, 65); // Both customers exceed their minutes and must pay extra
        // Output to show how the remaining unused minutes on each account have changed
        System.out.println("\nRob's minutes = " + rob.getPlan().getMinutesRemaining());
        System.out.println("April's minutes = "+ april.getPlan().getMinutesRemaining());
        System.out.println("Rita's minutes = " + rita.getPlan().getMinutesRemaining());
        System.out.println("Sue's minutes = " + sue.getPlan().getMinutesRemaining());
        System.out.println("Tim's minutes = " + tim.getPlan().getMinutesRemaining());
        // Now simulate internet data access
        rob.accessInternet(45600); // used up 45.6MB
        rita.accessInternet(2700000); // use up 2.7GB
        rob.accessInternet(1200000); // use up 1.2GB
        tim.accessInternet(10000); // attempt to use 10MB ... won't work
        sue.accessInternet(2500000); // used up exactly 2.5GB
        april.accessInternet(1900000); // attempt to use 1.9GB, only 1GB used, then stops
        // Output to show how the remaining unused data on each account have changed
        System.out.println("\nRob's data = " + rob.getPlan().getDataRemaining() + "KB");
        System.out.println("April's data = "+ april.getPlan().getDataRemaining() + "KB");
        System.out.println("Rita's data = " + rita.getPlan().getDataRemaining() + "KB");
        System.out.println("Sue's data = " + sue.getPlan().getDataRemaining() + "KB");
        System.out.println("Tim's data = " + tim.getPlan().getDataRemaining() + "KB");
        // Pretend that the month is over and print out all of the billing statements
        rob.printMonthlyStatement();
        april.printMonthlyStatement();
        rita.printMonthlyStatement();
        sue.printMonthlyStatement();
        tim.printMonthlyStatement();
        }
    }

Aucun commentaire:

Enregistrer un commentaire