In: Computer Science
Decision Structures in Java
You are the owner of a company that sells designer desks. Your program will create a user friendly interface (company name, labeled prompts, neat output) that calculates the cost of constructing desks for customers. After the customer enters the appropriate prompted values, the program will calculate the cost of the desk based on the specifications outlined below. The program should allow the user to order a second desk if desired.
Class Name: Desk
Instance Variables: length – float (representing inches)
(Fields) width – float (representing inches)
type – char (valid values P, O, or M for Pine, Oak, or Mahogany)
drawers – integer (representing the number of desk drawers)
Static Variable: count – tracks the number of desks created
Instance Methods: A constructor that accepts no arguments and sets default values as follows: the length to 6, width to 12, Oak type, and 1 drawer
A constructor that accepts 4 arguments, and sets the fields appropriately.
getArea( ) A method to calculate and return the area of the desk.
getType( ) A method to return desk type.
getDrawers( ) A method to return the number of drawers.
toString() Displays the object’s attributes
main(): This method should:
Class methods: createDesk( ) A function to prompt the user for inputs and accept the four (4) input values needed to create an instance of a Desk. It instantiates a Desk object and returns the object to the calling method.
CalculatePrice() This function has a Desk object as its formal argument and returns the price of the desk to be determined as follows:
Additional Notes:
Program: In this program, we implement the Desk class and TestDesk class. TestDesk class is the driver class, which has a main() method, createDesk() method and CalculatePrice() method. We take user inputs for desk specifications and then show the user with the total price, desk properties and also ask if the user wants to buy another desk or not.
Below is the implementation:
Code:
public class Desk {
    // Instance variables
    private float length;
    private float width;
    private char type;
    private int drawer;
    static int count = 0;
    // Default Constructor
    public Desk() {
        length = 6;
        width = 12;
        type = 'O';
        drawer = 1;
        count++;
    }
    // Parameterised Constructor
    public Desk(float length, float width, char type, int drawer) {
        this.length = length;
        this.width = width;
        this.type = type;
        this.drawer = drawer;
        count++;
    }
    // Function to get area
    public double getArea() {
        return length * width;
    }
    // Function to return type
    public char getType() {
        return type;
    }
    // Function to return drawers
    public int getDrawers() {
        return drawer;
    }
    // Function to display desk info
    @Override
    public String toString() {
        String deskType = "";
        if(type == 'O'){
            deskType = "Oak";
        }
        else if(type == 'P'){
            deskType = "Pine";
        }
        else{
            deskType = "Mahogany";
        }
        return "Desk Info: \n" + "Length: " + length + " inches\nWidth: "
                + width + " inches\nType: " + deskType + "\nDrawers: " + drawer;
    }
}


TestDesk.java:
Code:
import java.util.Scanner;
public class TestDesk {
   // Scanner class instance
    private static Scanner scanner;
    // Main()
    public static void main(String[] args) {
        scanner = new Scanner(System.in);
        // Create desk
        Desk desk = createDesk();
        // Store price
        double price = CalculatePrice(desk);
        // Display desk info and price
        System.out.println(desk);
        System.out.println(String.format("Total cost of desk: $%.2f", price));
        // Prompt user to buy another desk
        System.out.println("Do you want to buy another desk? (y/n)");
        String input = scanner.next();
        // If yes, then repeat above steps
        if (input.toLowerCase().equals("y")) {
            desk = createDesk();
            double price2 = CalculatePrice(desk);
            price += price2;
            System.out.println("Total number of desks purchased: " + desk.count);
            System.out.println(String.format("Total cost of desks: $%.2f", price));
            System.out.println(String.format("Average cost of desks: $%.2f",price / 2));
        }
        // End program
        System.out.println("Goodbye!");
    }
    // Method to create a desk
    private static Desk createDesk() {
        scanner = new Scanner(System.in);
        // Prompt user to enter length, width, type, drawers
        System.out.print("Please enter the length of desk: ");
        float length = scanner.nextFloat();
        System.out.print("Please enter the width of desk: ");
        float width = scanner.nextFloat();
        System.out.print("Please enter the type of desk: ");
        char type = scanner.next().charAt(0);
        // convert type to uppercase
        Character.toUpperCase(type);
        // Validate type
        if (type != 'P' && type != 'O' && type != 'M') {
            type = 'O';
        }
        System.out.print("Please enter the number of drawers of desk: ");
        int drawers = scanner.nextInt();
        // Create a Desk class object with all the values
        Desk desk = new Desk(length, width, type, drawers);
        // Return the desk object
        return desk;
    }
    // Method to calculate price of a desk
    public static double CalculatePrice(Desk desk) {
        // Initialise price
        double price = 0;
        // Store and check type of desk, update price accordingly
        char type = desk.getType();
        if (type == 'P') {
            price += 12.50;
        } else if (type == 'O') {
            price += 26;
        } else {
            price += 37.75;
        }
        // Store area of desk
        double area = desk.getArea();
        // If area is greater than 75, update price
        if (area > 75) {
            price += 18.25;
        }
        // If total drawers is greater than 6, add price of 6 drawers
        if (desk.getDrawers() > 6) {
            price += 6 * 9.50;
        }
        //else add price of drawers
        else {
            price += desk.getDrawers() * 9.50;
        }
        // Return price
        return price;
    }
}



Output 1:

Output 2:
