Question

In: Computer Science

Write a short, java program that interacts with the user asking them to buy an xbox...

Write a short, java program that interacts with the user asking them to buy an xbox series x or PS5 and does the following:

  • create at least 2 double variables
  • create at least 1 constant
  • get at least 1 string input from the user
  • get at least 1 int/double input from the user
  • include both, incorporating the input you got from the user:
    • 1 multiway if-else with at least 3 "else if"
    • 1 nested if

Your program should combine all of these into one cohesive, interactive program with all the parts working together. In your program description, be sure to describe what your program does.

Solutions

Expert Solution

I have created A TestProgram class with double, int & String variable, taking inputs from user, if-else-if ladder and nested if as well with inline comments to explain the logic.

Do let me know in comments in case of any doubt.

import java.util.Scanner;

public class TestProgram {

    final double fixedXboxPrice = 5000.0, fixedPs5Price = 5100.0;
    String welcomeStr = "Welcome to Play Station & XBOX world";
    int qty;
    double finalPrice;


    public static void main(String[] args) {
        TestProgram obj = new TestProgram();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your name:");
        String name = sc.nextLine();
        System.out.println("Welcome "+name+"!");
        obj.startProgram();
    }

    public void startProgram() {
        Scanner sc = new Scanner(System.in);
        System.out.println(welcomeStr);
        // this loop will run until user selects option 3 from given menu
        while (true) {
            System.out.println("Choose the option from below menu:");
            System.out.println("1. XBOX Series");
            System.out.println("2. PS5");
            System.out.println("3. Exit");
            System.out.println("Enter 1 or 2 or 3:");
            int choice = sc.nextInt();
            if (choice == 1) {
                System.out.println("Enter quantity of XBOX");
                qty = sc.nextInt();
                calcDiscount(qty, fixedXboxPrice, choice);
            } else if (choice == 2) {
                System.out.println("Enter quantity of PS5");
                qty = sc.nextInt();
                calcDiscount(qty, fixedPs5Price, choice);
            } else if(choice == 3){
                System.exit(0);
            }else{
                System.out.println("Enter correct choice.");
            }
        }
    }

    //to demonstrate if-else-if ladder, let's provide discount to user based on below criteria and display final effective price
    // quantity <= 5 then discount of flat 10
    // quantity > 5 then 1% discount
    // quantity > 10 then 5% discount
    // quantity > 15 then 7% discount
    // quantity > 20 then 9 % discount and additional discount of flat 50 in case of ps5
    public void calcDiscount(int qty, double price, int choice) {

        if (qty > 20) {
            finalPrice = price - price * 0.09;
            if (choice == 2) // nested if
                finalPrice = finalPrice - 50;
        } else if (qty > 15) {
            finalPrice = price - price * 0.07;
        } else if (qty > 10) {
            finalPrice = price - price * 0.05;
        } else if (qty > 5) {
            finalPrice = price - price * 0.01;
        } else if (qty > 0) {
            finalPrice = finalPrice - 10;
        } else {
            System.out.println("Quantity entered must be at least 1.");
            return; // return control only. in this case no need to print price because qty is <= 0.
        }
        System.out.println("Final price after discount : "+finalPrice);
    }
}

Screenshot of code:

Output:


Related Solutions

Write a short, cohesive java program in jGrasp that interacts with the user and does the...
Write a short, cohesive java program in jGrasp that interacts with the user and does the following: create at least 2 double variables create at least 1 constant get at least 1 string input from the user get at least 1 int/double input from the user include both, incorporating the input you got from the user: 1 multiway if-else with at least 3 "else if" 1 nested if Your program should combine all of these into one cohesive, interactive program...
Write a java program that asks the user for a number n and gives them the...
Write a java program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n. Example of running this program: Enter an integer number n: __7________ Enter Sum or Product: __Sum__________________________________ Program output: Sum of 1 ... 7 Sum or Product: Sum Sum = 28 Now second sample of second execution Enter an integer number n: __5__________________________________ Enter Sum or Product: __Product__________________________________ Program output:  Product of 1...
First, write a program to loop asking for a number from the user until the user...
First, write a program to loop asking for a number from the user until the user inputs a zero or until the user has input 50 numbers. Store each value in an array except the values that are divisible by 5 and display all stored values. Determine how many of the values stored in the array were divisible by 10 and display result. Next, write a function getMinMax that accepts an array of floats and the size of the array,...
IN JAVA: Write a simple program that takes 5 inputs from the user and save them...
IN JAVA: Write a simple program that takes 5 inputs from the user and save them into a Text File. The inputs are Student Name, Student Address and student Date of Birth. Also write a simple program that reads and display the input from the first program text file.
Write a java program that lets the user to enter any two integers and weave them...
Write a java program that lets the user to enter any two integers and weave them digit by digit and print the result of weaving their digits together to form a single number. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result should be the second-to- the-last digit of...
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
Step by step in python Write a program that will keep asking for a user input...
Step by step in python Write a program that will keep asking for a user input (until a blank line is entered) and will inform me whether what I entered was a valid number or not (without crashing). The program should use at least one try/except loop The program should include at least two custom written functions (a main() function can count as one of the two)
Can you provide java code for a program that prompts the user by asking "How many...
Can you provide java code for a program that prompts the user by asking "How many one mile races have you ran?". After the user inputs how many one mile races have they run it then prompts the user to input how many seconds it took for them to finish each race. So for example, if the user ran 6 races then the user will have to input 6 race times. Is there a way when you prompt the user...
This is Java In this problem we will write a program that asks the user to...
This is Java In this problem we will write a program that asks the user to enter a) The user's first name and b) a series of integers separated by commas. The integers may not include decimal points nor commas. The full string of numbers and commas will not include spaces either, just digits and commas. An example of valid input string is:        7,9,10,2,18,6 The string must be input by the user all at once; do not use a loop...
Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text (for example, "Sara" which is entered by the user)  and String key consisting of integers (for example, 2314) only, such that int rows=(int)Math.ceil(text.length()/key.length())+1; int columns= key.length(); The method fills the 2d array with letters a String entered by the use (column by column). The method then shifts the columns of the array based on key. For example, if the user enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT