In Java, Project Donation For Cheery Charities

import java.util.Scanner;

class Project{

    private double targetDonation;
    private double actualDonation;
    private String name;
    private String sponsor;
    private int nosOfDonations;

    public Project(){
        this(1000, "", "");
    }

    public Project(double targetDonation, String name){
        this(targetDonation, name, "");
    }

    public Project(double targetDonation, String name, String sponsor){
        setTargetDonation(targetDonation);
        setSponsor(sponsor);
        setName(name);
        setNosOfDonations(0);
    }

    public boolean setTargetDonation(double targetDonation){
        if(0 < targetDonation){
            this.targetDonation = targetDonation;
            return true;
        }
        return false;
    }

    public boolean addDonation(double donation){
        boolean added = setActualDonation(getActualDonation() + (donation*2));
        if(added) nosOfDonations ++;
        return added;
    }

    public boolean setActualDonation(double actualDonation){
        boolean added = false;
        if(0 < actualDonation && targetDonation >= actualDonation){
            this.actualDonation = actualDonation;
            added = true;
        }
        return added;
    }

    public double getTargetDonation(){
        return targetDonation;
    }

    public double getActualDonation(){
        return actualDonation;
    }

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

    public String getName(){
        return name;
    }

    public void setSponsor(String sponsor){
        this.sponsor = sponsor;
    }

    public String getSponsor(){
        return sponsor;
    }

    public int getNosOfDonations(){
        return nosOfDonations;
    }

    public void setNosOfDonations(int nosOfDonations){
        if(0 <= nosOfDonations)
            this.nosOfDonations = nosOfDonations;
    }
}

class CheeryCharities{
    public static void main(String[]args){
        Scanner in = new Scanner(System.in);
        Project project = new Project();
        System.out.print("Enter project's name: ");
        project.setName(in.nextLine());
        System.out.print("Use default donation target? [y/n]: ");
        double donation;
        if('y' != in.nextLine().trim().toLowerCase().charAt(0)){
            do{
                System.out.print("Enter donation target: ");
                try{
                    donation = new Double(in.nextLine());
                    if(project.setTargetDonation(donation))
                        break;
                    System.out.println("Error: Invalid donation!");
                }catch(NumberFormatException e){
                    System.out.println("Error: Not a number!");
                }
            }while(true);
        }

        System.out.print("Has a corporate sponsor? [y/n]: ");
        if('y' == in.nextLine().trim().toLowerCase().charAt(0)){
            System.out.print("Sponsor name: ");
            project.setSponsor(in.nextLine());
        }

        do{
            System.out.print("Enter actual donations received (-1 to stop): ");
            try{
                donation = new Double(in.nextLine());
                if(0 > donation) break;
                if(!project.addDonation(donation))
                    System.out.println("Error: Actual donation cannot exceed target donation!");
            }catch(NumberFormatException e){
                System.out.println("Error: Not a number!");
            }
        }while(true);

        System.out.println("**** Summary ****");
        System.out.printf("Project Name: %s\n", project.getName());
        System.out.printf("Target Donation: $%.2f\n", project.getTargetDonation());
        System.out.printf("Actual Donation: $%.2f\n", project.getActualDonation());
        System.out.printf("Nos# of Donations: %d\n", project.getNosOfDonations());
        if(!project.getSponsor().isEmpty())
            System.out.printf("Corporate Sponsor: %s\n", project.getSponsor());
    }
}
Download

Comments

Popular posts from this blog