It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
As the title above states - the problem I've encountered is related to the private variable within the class. The private variable does not return the correct value.
My objective is to retrieve private variable named myIncome from income Class which is a parent to these methods.
But when I try to retrieve myIncome using objIncome.getIncome(); in another class - it returns 0.00 value.
The result should be based on the if statements output.
import java.util.Scanner;
import java.text.DecimalFormat;
public class income {
private double income;
private double myIncome;
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("###.##");
public void summery1(double myIncome, double income, double tax, double nic, double personalTAXAllowance, double personalNICAllowance,double taxed, double niced){
this.myIncome = myIncome;
System.out.println("Your income before tax: " + income);
System.out.println("Personal allowance: " + personalTAXAllowance);
System.out.println("NIC allowance: " + personalNICAllowance);
System.out.println("Tax rate: " + tax + " %");
System.out.println("National insurance rate: " + nic + " %");
System.out.println("Your annual income after tax: " + df.format(myIncome));
System.out.println("Your income on monthly basis: " + df.format(myIncome / 12) + "\n");
}
public void summery2(double myIncome, double income, double tax, double nic, double personalTAXAllowance, double personalNICAllowance,double taxed, double niced, double additionalNIC, double resault){
this.myIncome = myIncome;
System.out.println("Your income before tax: " + income);
System.out.println("Personal allowance: " + personalTAXAllowance);
System.out.println("NIC allowance: " + personalNICAllowance);
System.out.println("Tax rate: " + tax + " %");
System.out.println("National insurance rate: " + nic + " %");
System.out.println("Your annual income after tax: " + df.format(myIncome));
System.out.println("Your income on monthly basis: " + df.format(myIncome / 12) + "\n");
}
public void clcSalary(){
System.out.println("Please enter your annual salary before tax");
double income = input.nextDouble();
if (income <= 32010){
double tax = 0.20;
double nic = 0.12;
double personalTAXAllowance = 9440;
double personalNICAllowance = 7748;
double taxed = (income - personalTAXAllowance) * tax;
double niced = (income - personalNICAllowance) * nic;
myIncome = income - (taxed + niced);
summery1(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced);
}
else if (income > 32010 && income < 150000 ){
double tax = 0.40;
double nic = 0.12;
double personalTAXAllowance = 9440;
double personalNICAllowance = 7748;
double taxed = (income - personalTAXAllowance) * tax;
double niced = (income - personalNICAllowance) * nic;
if (income > 41444){
double additionalNIC = income - 41444;
double resault = additionalNIC * 0.02;
this.myIncome = income - (taxed + niced) + resault;
summery2(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced,additionalNIC, resault);
}
else{
this.myIncome = income - (taxed + niced);
summery1(myIncome,income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced);
}
}
else{
double tax = 0.45;
double nic = 0.12;
double personalTAXAllowance = 9440;
double personalNICAllowance = 7748;
double taxed = (income - personalTAXAllowance) * tax;
double niced = (income - personalNICAllowance) * nic;
if (income > 41444){
double additionalNIC = income - 41444;
double resault = additionalNIC * 0.02;
this.myIncome = income - (taxed + niced) + resault;
summery2(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced,additionalNIC, resault);
}
else{
this.myIncome = income - (taxed + niced);
summery1(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced);
}
}
}
public void clcHourlyRate(double income){
System.out.println("Please enter your hourly rate: \n");
double hourlyRate = input.nextDouble();
System.out.println("Please enter the hours you've worked this week \n");
double hoursWeek = input.nextDouble();
income = ((hourlyRate * hoursWeek) * 4) * 12;
if (income <= 32010){
double tax = 0.20;
double nic = 0.12;
double personalTAXAllowance = 9440;
double personalNICAllowance = 7748;
double taxed = (income - personalTAXAllowance) * tax;
double niced = (income - personalNICAllowance) * nic;
this.myIncome = income - (taxed + niced) / 12;
summery1(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced);
}
else if (income > 32010 && income <= 150000 ){
double tax = 0.40;
double nic = 0.12;
double personalTAXAllowance = 9440;
double personalNICAllowance = 7748;
double taxed = (income - personalTAXAllowance) * tax;
double niced = (income - personalNICAllowance) * nic;
if (income > 41444){
double additionalNIC = income - 41444;
double resault = additionalNIC * 0.02;
this.myIncome = ((income - (taxed + niced)) / 12) + resault;
summery2(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced,additionalNIC, resault);
}
else{
myIncome = (income - (taxed + niced)) / 12;
myIncome = income - (taxed + niced) / 12;
summery1(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced);
}
}
else{
double tax = 0.45;
double nic = 0.12;
double personalTAXAllowance = 9440;
double personalNICAllowance = 7748;
double taxed = (income - personalTAXAllowance) * tax;
double niced = (income - personalNICAllowance) * nic;
if (income > 41444){
double additionalNIC = income - 41444;
double resault = additionalNIC * 0.02;
this.myIncome = ((income - (taxed + niced)) / 12) + resault;
summery2(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced,additionalNIC, resault);
}
else{
this.myIncome = (income - (taxed + niced)) / 12;
summery1(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced);
}
}
}
public double getIncome(){
return myIncome;
}
}
Here is the entire code of the whole class.
public class savings {
private double v_sav;
private DecimalFormat df = new DecimalFormat("###.##");
private Scanner input = new Scanner(System.in);
private income myincome = new income();
public void setSavings() {
double income = myincome.getIncome();
System.out.println(income);
System.out.println("Please enter the amount of months: ");
int months = input.nextInt();
df.format(v_sav = income * months);
System.out.println("Your savings in " + months + " months"+ "will be: "+ v_sav);
}
public double getSavings() {
return v_sav;
}
}
This is the class which uses the getIncome method from Class Income object.
public class PFA {
public static void main(String[] args) {
int option;
do{
mainMenu();
option = input.nextInt();
if (option > 5){
System.out.println("Please enter a value between 1 and 5");
}
else{
if (option == 1){
menuIncome(v_income);
}
else if (option == 2){
menuExpenses(n_expenses, c_expenses, v_choice, v_exit);
}
else if (option == 3){
savings mySavings = new savings();
mySavings.setSavings();
System.out.println(mySavings.getSavings());
}
}
}while (option != 5);
Main method.
I'm assuming you have a driver like (which is what you have in your 3rd else-if)
public static void main(String[] args) {
savings s = new savings();
s.setSavings();
double value = s.getSavings();
}
In this case, of course it's going to be 0.0 with the line
double income = myincome.getIncome();
in setSavings(), you haven't called the methods that change the value of myIncome.
In your savings class,
private income myincome = new income();
creates a new income instance, which, because you don't have a constructor, initializes the value of the instance field myIncome to 0. That is the value you are getting back with getIncome().
You aren't calling clcSalary() anywhere. You should do that before calling getIncome().
Are the summery functions being printed? What is the value of myIncome there?
In other words, if getIncome is being called before either clcSalary or clcHourlyRate then your variable will remain 0.
Related
I'm a very new java student and am currently working on a program that I have been stuck on, it's an incremental program and it was working perfect with the first 2 tests and now I'm not sure what's happened. I will post my code below and hopefully someone can help me. Thanks! The scanner takes the input from the computer and is supposed to show something like this to the console:
AGI: $80,500
Deduction: $24,000
Taxable income: $56,500
Federal tax: $6,380
Tax refund: $5,620
it is supposed to test for these 2 first:
AGI: $120,523
Error: Income too high to use this form
which passed until I wrote the rest of the code:
import java.util.Scanner;
import java.io.*;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int wages;
int taxableInterest;
int unemploymentComp;
int status;
int taxWithheld;
int AGI;
int deduction;
int fedTax;
int Income;
wages = scnr.nextInt();
taxableInterest = scnr.nextInt();
unemploymentComp = scnr.nextInt();
AGI = (wages + taxableInterest + unemploymentComp);
status = scnr.nextInt();
Income = (AGI - status);
if (AGI < 120000){
System.out.printf("AGI: $%,d\n", AGI);
}else{
System.out.printf("AGI: $%,d\n", AGI);
System.out.println("Error: Income too high to use this form");
}
Income = scnr.nextInt();
if (Income > 0) {
System.out.printf("Taxable income: $%,d\n", Income);
}
status = scnr.nextInt();
if (status == 1) {
status = 12000;
System.out.printf("Deduction: $%,d\n",status);
if (Income > 0 && Income <= 10000) {
System.out.printf("Federal tax: $%,d\n", (Math.round(Income * .10)));
}
else if (Income > 10001 && Income <= 40000) {
System.out.printf("Federal tax: $%,d\n", (Math.round(Income + 1000 *.20)));
}
else if (Income > 40001 && Income <= 85000) {
System.out.printf("Federal tax:$%,d\n ", (Math.round(Income +4600 *.20)));
}
else if (Income > 85000) {
System.out.printf("Federal tax: $%,d\n", (Math.round(Income + 14500 * .24)));
}
}
if (status == 2){
status = 24000;
System.out.printf("Deduction: $%,d\n",status);
if (Income > 0 && Income <= 20000) {
System.out.printf("Federal tax: $%,d\n", (Math.round(Income * .10)));
}
else if (Income > 20001 && Income < 80000) {
System.out.printf("Federal tax: $%,d\n", (Math.round(Income + 2000 * .12)));
}
else if (Income > 80000) {
System.out.printf("Federal tax: $%,d\n", (Math.round(Income + 9200 * .22)));
}
}
'''
I have a java project that is a Income tax calculator, and I am supposed to add a error message to it so that the program will not respond to a number that is 0 or less in the income prompt. I am just not sure where to put the prompt or what type of error message I need to accomplish this. Code:
import java.util.Scanner;
public class TaxCalculator {
static void calculate() {
// The tax rates for different types of customers.
final double RATE1 = 0.20;
final double RATE2 = 0.25;
final double RATE3 = 0.10;
final double RATE4 = 0.15;
final double RATE5 = 0.30;
final double RATE1_SINGLE_LIMIT = 1;
final double RATE2_MARRIED_LIMIT = 1;
final double RATE3_COHABITATING_LIMIT = 20000;
final double RATE4_COHABITATING_LIMIT = 50000;
double tax = 0;
Scanner in = new Scanner(System.in);
//Prompt for user to enter the customers income
System.out.print("Enter customer's income: ");
double income = in.nextDouble();
in.nextLine();
System.out.print("Please enter 's' for single, 'm' for married, or 'c' for cohabitating: ");
String maritalStatus = in.next();
in.nextLine();
// Where the taxes are calculated
if (maritalStatus.equals("s") && income > RATE1_SINGLE_LIMIT) {
tax = RATE1 * income;
} else if (maritalStatus.equals("m") && income > RATE2_MARRIED_LIMIT) {
tax = RATE2 * income;
} else if (maritalStatus.equals("c") && income <= RATE3_COHABITATING_LIMIT) {
tax = RATE3 * income;
} else if (maritalStatus.equals("c") && income <= RATE4_COHABITATING_LIMIT) {
tax = RATE4 * income;
} else {
tax = RATE5 * income;
}
System.out.print("Your income tax is: $" + tax);
}
// asks user if they would like to process another customer.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String newResponse = "";
do {
calculate();
System.out.println();
System.out.println("Process another response?. Please enter 'y' for yes, or 'n' for no: ");
newResponse = in.next();
in.nextLine();
} while (newResponse.equals("y"));
}
}
You could add the loops after printing appropriate prompts:
System.out.print("Enter customer's income: ");
double income;
while ((income = in.nextDouble()) <= 0) {
System.out.print("Income " + income + " is invalid. Please input correct income which must be greater than 0: ");
in.nextLine();
}
System.out.print("Please enter 's' for single, 'm' for married, or 'c' for cohabitating: ");
String maritalStatus;
while (!(maritalStatus = in.next()).matches("[smc]")) {
System.out.print("Marital status " + maritalStatus + " is invalid. Please input correct value: 's', 'm', or 'c': ");
in.nextLine();
}
Output:
Enter customer's income: -12
Income -12.0 is invalid. Please input correct income which must be greater than 0: -23
Income -23.0 is invalid. Please input correct income which must be greater than 0: 2300
Please enter 's' for single, 'm' for married, or 'c' for cohabitating: 1
Marital status 1 is invalid. Please input correct value: 's', 'm', or 'c': m
I'm having some trouble with a part of an assignment. I have to see how many times an employee is processed in my program, after the loop runs once it asks the user if they would like to process another. If they enter y for yes and then they enter n for end after the second employee calculation. I want it to say "Number of employees processed: 2". How could I do this?
package paytime;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String firstName, lastName, choice;
double hoursWorked, hourlyWage;
boolean processAnotherEmployee = true;
Employee one = new Employee();
while(true)
{
if (processAnotherEmployee)
{
System.out.print("Enter Y to process employee or any other key to end: ");
choice = scn.next();
if (choice.equalsIgnoreCase("Y"))
{
System.out.print("Enter employee number: ");
int number = scn.nextInt();
while (!one.findEmpNumber(number))
{
System.out.print("Invlaid, enter a proper employee number: ");
number = scn.nextInt();
}
System.out.print("Enter first name: ");
firstName = scn.next();
System.out.print("Enter last name: ");
lastName = scn.next();
System.out.print("Enter hours worked: ");
hoursWorked = scn.nextDouble();
while (hoursWorked < 0)
{
System.out.print("Negative hours not allowed. Enter hours worked: ");
hoursWorked = scn.nextDouble();
}
System.out.print("Enter hourly wage: $");
hourlyWage = scn.nextDouble();
while (hourlyWage < 0 || hourlyWage > 100)
{
System.out.print("Negative wage is not allowed or wage entered is to high. Enter hourley wage: $");
hourlyWage = scn.nextDouble();
}
double overtimeHours = hoursWorked - 40;
double overtimeWage = hourlyWage * 1.5;
System.out.println(" ");
if (hoursWorked <= 40.0)
{
System.out.println("Worker " + number + " Paycheck Information: ");
System.out.println("Name is: " + firstName + " " + lastName);
System.out.println("Weekly Pay is: " + one.callWeeklyPay(hoursWorked, hourlyWage));
System.out.println("Income Taxes is: " + one.callIncomeTax());
System.out.println("Net Pay is: " + one.callNetPay());
}
else if (hoursWorked > 40.0)
{
System.out.println("Worker " + number + " Paycheck Information: ");
System.out.println("Name is: " + firstName + " " + lastName);
System.out.println("Weekly Pay is: " + one.callWeeklyPay(hoursWorked, hourlyWage));
System.out.println("Income Taxes is: " + one.callIncomeTax());
System.out.println("Net Pay is: " + one.callNetPay());
System.out.println(" ");
System.out.println("Worker " + number + " Overtime Calculation: ");
System.out.println("Overtime Pay is: " + one.callOvertimePay(overtimeHours, overtimeWage, hourlyWage, hoursWorked));
System.out.println("Overtime Income Tax is: " + one.callOvertimeTax());
System.out.println("Overtime Net Pay is: " + one.callOvertimeNetPay());
System.out.println("Total Net Pay is: " + one.callTotalNetPay());
System.out.println(" ");
}
}
else if (!choice.equalsIgnoreCase("Y"))
{
processAnotherEmployee = false;
System.out.println("Total number of Employees processed: ");
System.out.println(" ");
System.out.println("End of program");
break;
}
}
}
}
}
and
package paytime;
public class Employee {
private int empNumbers [] = {101, 103, 106, 109, 110, 113, 116, 118, 120};
public double weeklyPay, hoursWorked, hourlyWage, incomeTax, netPay,
overtimePay, overtimeHours, overtimeWage, overtimeIncomeTax,
overtimeNetPay, totalNetPay;
public boolean findEmpNumber(int number)
{
boolean found = false;
for (int sub = 0; sub < empNumbers.length; sub++)
{
if (number == empNumbers[sub])
{
found = true;
break;
}
}
return found;
}
private void calculateWeeklyPay(double hoursWorked, double hourlyWage) {
if (hoursWorked > 40)
{
hoursWorked = 40;
weeklyPay = hoursWorked * hourlyWage;
}
else
{
weeklyPay = hoursWorked * hourlyWage;
}
}
public double callWeeklyPay(double hoursWorked, double hourlyWage) {
calculateWeeklyPay(hoursWorked, hourlyWage);
return weeklyPay;
}
private void calculateIncomeTax() {
if (weeklyPay > 0.0 && weeklyPay <= 300.0)
{
incomeTax = weeklyPay * 0.10;
}
else if (weeklyPay > 300.1 && weeklyPay <= 400.0)
{
incomeTax = weeklyPay * 0.12;
}
else if (weeklyPay > 400.1 && weeklyPay <= 500.0)
{
incomeTax = weeklyPay * 0.15;
}
else if (weeklyPay > 500.1)
{
incomeTax = weeklyPay * 0.20;
}
}
public double callIncomeTax() {
calculateIncomeTax();
return incomeTax;
}
private void calculateNetPay() {
netPay = weeklyPay - incomeTax;
}
public double callNetPay() {
calculateNetPay();
return netPay;
}
private void calculateOvertimePay(double overtimeHours, double overtimeWage, double hourlyWage, double hoursWorked) {
overtimePay = overtimeHours * overtimeWage;
}
public double callOvertimePay(double overtimeHours, double overtimeWage, double hourlyWage, double hoursWorked) {
calculateOvertimePay(overtimeHours, overtimeWage, hourlyWage, hoursWorked);
return overtimePay;
}
private void calculateOvertimeTax() {
overtimeIncomeTax = overtimePay * 0.25;
}
public double callOvertimeTax() {
calculateOvertimeTax();
return overtimeIncomeTax;
}
private void calculateOvertimeNetPay() {
overtimeNetPay = overtimePay - overtimeIncomeTax;
}
public double callOvertimeNetPay() {
calculateOvertimeNetPay();
return overtimeNetPay;
}
private void calculateTotalNetPay() {
totalNetPay = netPay + overtimeNetPay;
}
public double callTotalNetPay() {
calculateTotalNetPay();
return totalNetPay;
}
}
You can achieve this by simply having "int employeesProcessed = 0;" outside of you while loop then add "employeesProcessed++;" directly after "if (choice.equalsIgnoreCase("Y"))" so that each time your program is asked to process an employ you add 1 to your int that is keeping track of how many employees you have processed. Then you can add this variable onto the end of your printed string so it says "Total number of Employees processed: " + employeesProcessed.
My code has been an evolving process and the end game is to implement a UI using JOptionPane to my previous iteration. I successfully formatted an output to a 2 decimal place using String.format("Text goes here %.2f", variable) but when I try to carry this method over to my iteration of code using JOptionPane it crashes the program.
Here is my code
import javax.swing.JOptionPane;
public class Ass1d2
{
public static void main(String [] args)
{
final int N = 7;
String taxPayerName;
int taxPayerIncome = 0;
double maxTax = 0.0;
String maxTaxName = "";
JOptionPane.showMessageDialog(null, "Welcome to use Tax Computation System");
for(int i = 0; i < N; i++)
{
taxPayerName = (String)JOptionPane.showInputDialog(null, "Enter tax payers name");
taxPayerIncome = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter income for the tax payer"));
double tax = computeTax(taxPayerIncome);
if (taxPayerIncome > maxTax){
maxTax = tax;
maxTaxName = taxPayerName;
}
JOptionPane.showMessageDialog(null, "The tax that " + taxPayerName + " owes is $" + tax));
}
JOptionPane.showMessageDialog(null, "The maximum tax is $" + maxTax) + " paid by " + maxTaxName);
}
private static double computeTax(int taxPayerIncome)
{
double tax = 0.0;
if (taxPayerIncome < 18200)
tax = 0;
else if (taxPayerIncome < 37000)
tax = (taxPayerIncome - 18200) * 0.19;
else if (taxPayerIncome < 87000)
tax = 3572 + (taxPayerIncome - 37000) * 0.325;
else if (taxPayerIncome < 180000)
tax = 19822 + (taxPayerIncome - 87000) * 0.37;
else
tax = 54232 + (taxPayerIncome - 180000) * 0.47;
return tax;
}
}
How do I format the two showMessageDialog to produce a two decimal floating point result? I've been scouring guides for the past hour and it's just not clicking. Very frustrating that this is the final roadblock. Thanks.
Well I must have done a typo in my other attempts as I have it working now. Awkward. Attached updated code with working floating point result for future reference.
import javax.swing.JOptionPane;
public class Ass1d2
{
public static void main(String [] args)
{
final int N = 3;
String taxPayerName;
int taxPayerIncome = 0;
double maxTax = 0.0;
String maxTaxName = "";
JOptionPane.showMessageDialog(null, "Welcome to use Tax Computation System");
for(int i = 0; i < N; i++)
{
taxPayerName = (String)JOptionPane.showInputDialog(null, "Enter tax payers name");
taxPayerIncome = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter income for the tax payer"));
double tax = computeTax(taxPayerIncome);
if (taxPayerIncome > maxTax){
maxTax = tax;
maxTaxName = taxPayerName;
}
JOptionPane.showMessageDialog(null, "The tax that " + taxPayerName + " owes is $" + String.format("%.2f", tax));
}
JOptionPane.showMessageDialog(null, "The maximum tax is $" + String.format("%.2f", maxTax) + " paid by " + maxTaxName);
}
private static double computeTax(int taxPayerIncome)
{
double tax = 0.0;
if (taxPayerIncome < 18200)
tax = 0;
else if (taxPayerIncome < 37000)
tax = (taxPayerIncome - 18200) * 0.19;
else if (taxPayerIncome < 87000)
tax = 3572 + (taxPayerIncome - 37000) * 0.325;
else if (taxPayerIncome < 180000)
tax = 19822 + (taxPayerIncome - 87000) * 0.37;
else
tax = 54232 + (taxPayerIncome - 180000) * 0.47;
return tax;
}
}
So I have been trying to figure this out for the past 5 hours and the reason being...from a beginner point of view this code looks fine with no errors but the output is still wrong...basically for every 10 kids tickets the user gets one free,so the output is fine if the free tickets is less than the adults but it subtracts the extra adults when its the other way around.
package theatre;
import java.util.*;
public class Theatre {
public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
System.out.println("Enter the number of tickets you would like to buy for each type: ");
System.out.println("Adult: £10.50");
System.out.println("Child: £7.30");
System.out.println("Concessions £8.40");
System.out.println("Adult: ");
int Adult = kybd.nextInt();
System.out.println("Child: ");
int Child = kybd.nextInt();
System.out.println("Concessions: ");
int concessions = kybd.nextInt();
int freeAdult = Child / 10;
if (freeAdult < 9) {
System.out.println("You get " + freeAdult + " adult tickets");
} else {
System.out.println("You don't get any free tickets");
}
System.out.println(freeAdult); //reference out
double adultBill = (Adult * 10.50) - (freeAdult * 10.50);
double childBill = Child * 7.30;
double concessionsBill = concessions * 8.40;
double totalBill = adultBill + childBill + concessionsBill;
double totalBill2 = childBill + concessionsBill;
System.out.println(totalBill2); //reference out
if (freeAdult >= Adult) {
System.out.printf("Total bill is: £%.2f\n", totalBill);
} else if (freeAdult < Adult) {
System.out.printf("Total bill is: £%.2f\n", totalBill2);
} else {
System.out.println("Please enter a corrrect value");
}
}
}
Thanks
I think you have your logic a little confused:
int freeAdult = Child / 10;
if (freeAdult < 9) {
System.out.println("You get " + freeAdult + " adult tickets");
} else {
System.out.println("You don't get any free tickets");
}
appears to be calculating how many free adult tickets the person gets based on the number of child tickets (Child / 10) ...but is then saying they get no tickets if they've qualified for more than 10? I think you might be using the wrong variable here. Then:
double adultBill = (Adult * 10.50) - (freeAdult * 10.50);
double childBill = Child * 7.30;
double concessionsBill = concessions * 8.40;
double totalBill = adultBill + childBill + concessionsBill;
double totalBill2 = childBill + concessionsBill;
System.out.println(totalBill2); //reference out
if (freeAdult >= Adult) {
System.out.printf("Total bill is: £%.2f\n", totalBill);
} else if (freeAdult < Adult) {
System.out.printf("Total bill is: £%.2f\n", totalBill2);
} else {
System.out.println("Please enter a corrrect value");
}
I believe is meant to calculate 2 bills, one including adults and one not and then if the number of free adult tickets is greater than the number of adults, discard the first bill and use the second (which never included the adults calculation), in which case you have your if statement the wrong way around, it should be:
if (freeAdult < Adult) {
System.out.printf("Total bill is: £%.2f\n", totalBill);
} else if (freeAdult >= Adult) {
System.out.printf("Total bill is: £%.2f\n", totalBill2);
...however you should only have to calculate one bill by simply changing the adult calculation to:
double adultBill = freeAdult < Adult ? (Adult * 10.50) - (freeAdult * 10.50) : 0;
Then calculate bill as normal:
double totalBill = adultBill + childBill + concessionsBill;
System.out.printf("Total bill is: £%.2f\n", totalBill);
public class Theatre {
public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
System.out.println("Enter the number of tickets you would like to buy for each type: ");
System.out.println("Adult: £10.50");
System.out.println("Child: £7.30");
System.out.println("Concessions £8.40");
System.out.println("Adult: ");
int adult = kybd.nextInt();
System.out.println("Child: ");
int child = kybd.nextInt();
System.out.println("Concessions: ");
int concessions = kybd.nextInt();
int freeAdult = child / 10;
System.out.println("You get " + freeAdult + " adult tickets free");
if(adult >0){
adult = adult - freeAdult;
}
double adultBill = (adult * 10.50);
double childBill = child * 7.30;
double concessionsBill = concessions * 8.40;
double totalBill = adultBill + childBill + concessionsBill;
System.out.printf("Total bill is: £%.2f\n", totalBill);
}
}