IMG-LOGO
×

Tài Liệu Học

Khoá học lập trình Java Core căn bản (OOP)

Hướng Dẫn Cài Đặt Môi Trường

[Video] - Hướng dẫn cài đặt môi trường [Video] - Hướng dẫn tạo dự án Hello World [Video] Hướng dẫn phát hành dự án java - Lập trình Java căn bản

Khai Báo Biến & Mệnh Đề Điều Kiện & Vòng Lặp

[Video] - Khai báo biến & Toán tử trong Java [Video] - Tìm hiểu về Scanner, mệnh đề điều kiền if, else, switch trong java [Video] Tìm hiểu Loop (for, while, do while) - Lập trình Java căn bản

Array

[Video] Tìm hiểu về mảng (array) và List, ArrayList trong Java - Lập trình Java căn bản [Video] Hướng dẫn Collections Java | List & ArrayList Java | Vector Java | Map & Hashmap Java | String Java [Video] Test - Tìm kiếm chuỗi trong chuỗi

OOP - Tích Chất Đóng Gói & Kế Thừa

[Video] Tìm hiểu về Class và Object trong lập trình Java - Lập trình Java căn bản [Video] OOP - Tích chất bao đóng - Kế thừa trong Java - Lập trình Java căn bản [Video] Kế thừa OOP - Lập trình java - Lập trình Java căn bản [Video] OOP - Tính Chất Đa Hình - Tính Chất Trừu Tượng - Lập Trình Java Căn Bản [Video] Chu vi + Diện tích hình tròn và hình chữ nhật - lập trình Java căn bản [Video] Chữa bài tập kế thừa hình tròn + hình chữ nhật - lập trình Java căn bản - Đề 2 [Video] Quản Lí Cán Bộ [Video] Chũa bài tập Employee & StudentMark & Product trong lập trình OOP- lập trình Java căn bản [Video] Quản lý bể bơi bằng Java - Swimming Pool in Java [Video] Quản lý phòng tập GYM - Java basic [Video] Hướng dẫn chữa bài tập quản lý sách - lập trình java [Video] Chữa bài tập quản lý sinh viên - lập trình hướng đối tượng java [Video] Java Basic- OOP - Tổng hợp - Quản lý sinh viên - giáo viên bằng java - Phần 2

OOP - Interface

[Video] Interface - OOP lập trình java - Lập trình Java căn bản [Video] Java Basic- OOP - Tổng hợp - Quản lý sở thú [Video] Java Basic- OOP - Interface - Quản lý mèo (cat) trong java [Video] TEST- Chương trình quản lý rạp chiếu film bằng java [Video] Java basic- Overview - viết chương trình điều khiển TIVI bằng java [Video] Java basic- Overview - Viết chương trình quản lý xe car bằng java [Video] Hướng dẫn cú pháp Lambda Java | Tạo anonymous class Java | Interface Java & Abstract Java [Video] Chuyển DateTime sang String Java|Chuyển String sang DateTime Java | Base64 Java|MD5 Java|Cipher Java [Video] Hướng dẫn tìm hiểu Singleton Design Pattern Java | Singleton Object Java | OOP Java cho người mới

Examination & Test

[Video] Java Basic- Overview - Tổng hợp bài tập ôn luyện java basic - Kiểm tra 60 phút [Video] Exam - Kiểm tra 60 phút lập trình OOP - Quản lý thiết bị máy tính - Lập trình OOP

Java Swing

[Video] Hướng dẫn học Java Swing | Ví du quản lý sinh viên Java | Java Swing cho người mới bắt đầu

Ôn Tập Tổng Quát Java Basic

[Video] Phần mềm quản lý trường học - giảng dạy [Video] Optimize (Tối ưu code) - Phần mềm quản lý trường học - giảng dạy [Video] Java Basic- Test 60 phút lập trình java căn bản - Phần mềm quản lý bệnh nhân COVID-19 (2020 - TOOANG) [Video] bài tập quản lý xe cộ + quản lý bãi đỗ xe - lập trình java [Video] Java Basic - Quản lý ATM - Quản lý tài khoản ngân hàng




Trang Chủ Java Basic Java basic- Overview - Viết chương trình quản lý xe car bằng java

Java basic- Overview - Viết chương trình quản lý xe car bằng java

by GokiSoft.com - 09:58 19/07/2021 5,983 Lượt Xem

1. Create an interface Icar that has the following methods:                                        

      Method “calculateTax()”, returns the type float: calculate the tax of the car.

      Method “calculatePrice()”, returns the type float: calculate the total cost of the car.                                                                      

      Method “getInfor()”, returns the type void: display the information of the car.

 

2. Create a class Car inherit from interface Icar.                                                                                         [6]

Declare fields has access modifier as private use for properties: name, producer, year, seat and rootprice.                                                                          

 

 

        Create two constructors and all method get/set for its attributes

        Implement method calculateTax() to calculate the Tax as follows:            

    If the car has under 7 seats then tax = RootPrice * 60%.

    Else tax = RootPrice * 70%

 

        Implement method calculatePrice() to calculate TotalPrice as follows:      

    TotalPrice=RootPrice  + Tax

 

        Implement method getInfor() to show the information as follows: .... car produced by ... in ... has ... seats with the total price is ....$. Variable includes :name of the car, the name of the manufacturer, year of production, seats and TotalPrice (For example: Ford car produced by Ford in 1997 has 4 seats with the total price is 20000 $).                                                                               

 
  1. Declaring class LuxuryCar inherits from class Car and adding follow attrubtes :                           

private float specialRate;

 

        Create two constructors and all method get/set for its attributes

        Override method calculatePrice to calculate TotalPrice as follows :          

TotalPrice = RootPrice + Tax + RootPrice * specialRate

   

        Overload method calculatePrice with 1 parameter named “transportCost” with type float. It calculates TotalCost as follows:                                                   TotalPrice = RootPrice + Tax + RootPrice * specialRate + transportCost.

   
  1. Create a class named Test.                                                                           

        Declare and initialize 1 instance named “myLuxuryCar” of class LuxuryCar.

    Input Name, Producer, Year, Seat, rootPrice from the keyboard (Catch exception when input value). Display the contents of this LuxuryCar.

    Calculate TotalPrice in case transportCost is $ 20,000, and display it.

Bình luận



Chia sẻ từ lớp học

GokiSoft.com [Teacher]

Ngày viết: 09:00 23/07/2021


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson09;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        LuxuryCar myLuxuryCar = new LuxuryCar();
        myLuxuryCar.input();
        
        myLuxuryCar.getInfor();
        
        float total = myLuxuryCar.calculatePrice(20000);
        System.out.println("\nTotal Price: " + total);
    }
}


#LuxuryCar.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson09;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class LuxuryCar extends Car{
    float specialRate;

    public LuxuryCar() {
    }

    public LuxuryCar(float specialRate, String name, String producer, String year, int seat, float rootPrice) {
        super(name, producer, year, seat, rootPrice);
        this.specialRate = specialRate;
    }

    @Override
    public float calculatePrice() {
        return getRootPrice() + calculateTax() + getRootPrice() * specialRate;
    }
    
    public float calculatePrice(float transportCost) {
        return calculatePrice() + transportCost;
    }

    @Override
    public void input() {
        super.input(); //To change body of generated methods, choose Tools | Templates.
        Scanner scan = new Scanner(System.in);
        
        try {
            System.out.println("Special Rate: ");
            specialRate = Float.parseFloat(scan.nextLine());
        } catch(NumberFormatException e) {
        }
    }
}


#ICar.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson09;

/**
 *
 * @author Diep.Tran
 */
public interface ICar {
    float calculateTax();
    float calculatePrice();
    void getInfor();
}


#Car.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson09;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Car implements ICar{
    String name, producer, year;
    float rootPrice;
    int seat;

    public Car() {
    }

    public Car(String name, String producer, String year, int seat, float rootPrice) {
        this.name = name;
        this.producer = producer;
        this.year = year;
        this.seat = seat;
        this.rootPrice = rootPrice;
    }
    
    @Override
    public float calculateTax() {
        if(seat <= 7) {
            return rootPrice * 0.6f;
        }
        return rootPrice * 0.7f;
    }

    @Override
    public float calculatePrice() {
        return rootPrice + calculateTax();
    }

    @Override
    public void getInfor() {
        System.out.printf("\n%s car produced by %s in %s "
                + "has %d seats with the total price is %f$", 
                name, producer, year, seat, calculatePrice());
    }

    public String getName() {
        return name;
    }

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

    public String getProducer() {
        return producer;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public int getSeat() {
        return seat;
    }

    public void setSeat(int seat) {
        this.seat = seat;
    }

    public float getRootPrice() {
        return rootPrice;
    }

    public void setRootPrice(float rootPrice) {
        this.rootPrice = rootPrice;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Name: ");
        name = scan.nextLine();
        
        System.out.println("Producer: ");
        producer = scan.nextLine();
        
        System.out.println("Year: ");
        year = scan.nextLine();
        
        try {
            System.out.println("Root Price: ");
            rootPrice = Float.parseFloat(scan.nextLine());
        } catch(NumberFormatException e) {
        }
        
        try {
            System.out.println("Seat: ");
            seat = Integer.parseInt(scan.nextLine());
        } catch(NumberFormatException e) {
        }
    }
}


Hoàng Quang Huy [C1907L]

Ngày viết: 21:04 31/03/2020





package Lesson_30_3_2020;

public interface ICar {
	public float calculateTax();
    public float calculatePrice();
    public void getInfor();
}
-------------------------------------------------------------------------------------
package Lesson_30_3_2020;

import java.util.Scanner;

public class Car implements ICar {
	private String name;
	private String producer;
	private int year;
	private int seat;
	private float rootprice;

	public Car() {
		super();
	}

	public Car(String name, String producer, int year, int seat, float rootprice) {
		super();
		this.name = name;
		this.producer = producer;
		this.year = year;
		this.seat = seat;
		this.rootprice = rootprice;
	}

	public String getName() {
		return name;
	}

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

	public String getProducer() {
		return producer;
	}

	public void setProducer(String producer) {
		this.producer = producer;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getSeat() {
		return seat;
	}

	public void setSeat(int seat) {
		this.seat = seat;
	}

	public float getRootprice() {
		return rootprice;
	}

	public void setRootprice(float rootprice) {
		this.rootprice = rootprice;
	}

	@Override
	public float calculateTax() {
		if (this.seat < 7 && this.seat > 0) {
			return (float) (this.rootprice * 0.6);
		} else {
			return (float) (this.rootprice * 0.7);
		}
	}

	@Override
	public float calculatePrice() {
		float totalprice = this.rootprice + calculateTax();
		return totalprice;
	}

	@Override
	public void getInfor() {
		System.out.println(this.name + " car produced by " + this.producer + " in " + this.year + " has " + this.seat
				+ " seats with the total price is " + this.calculatePrice() + "$");

	}

	public void setInfor() {
		Scanner input = new Scanner(System.in);
		System.out.println("Car Name: ");
		this.name = input.nextLine();
		System.out.println("Producer Name: ");
		this.producer = input.nextLine();
		while(true) {
			try {
				System.out.println("Year produced: ");
				this.year = Integer.parseInt(input.nextLine());
				break;
			}catch(NumberFormatException e) {
				System.out.println("Invalid Year");
			}
		}
		while(true) {
			try {
				System.out.println("Number of seats: ");
				this.seat = Integer.parseInt(input.nextLine());
				break;
			}catch(NumberFormatException e) {
				System.out.println("Invalid number of seats");
			}
		}
		while(true) {
			try {
				System.out.println("Root Price: ");
				this.rootprice = Float.parseFloat(input.nextLine());
				break;
			}catch(NumberFormatException e) {
				System.out.println("Invalid Root Price");
			}
		}
	}
}
------------------------------------------------------------------------------------
package Lesson_30_3_2020;

import java.util.Scanner;

public class LuxuryCar extends Car{
	private float specialRate;

	public LuxuryCar(float specialRate) {
		super();
		this.specialRate = specialRate;
	}

	public LuxuryCar() {
		super();
	}
	
	public float getSpecialRate() {
		return specialRate;
	}

	public void setSpecialRate(float specialRate) {
		this.specialRate = specialRate;
	}
	
	@Override
	public float calculatePrice() {
		float totalPrice = super.getRootprice() + super.calculateTax() + super.getRootprice() * this.specialRate;
		return totalPrice;
		
	}
	
	public float calculatePrice(float transportCost) {
		float totalPrice = super.getRootprice() + super.calculateTax() + super.getRootprice() * this.specialRate + transportCost;
		return totalPrice;
	}
	
	@Override
	public void setInfor() {
		Scanner input = new Scanner(System.in);
		super.setInfor();
		while(true) {
			try {
				System.out.println("Special Rate: ");
				this.specialRate = Float.parseFloat(input.nextLine());
				break;
			}catch(NumberFormatException e) {
				System.out.println("Invalid Special Rate");
			}
			
		}
	}
}
-------------------------------------------------------------------------------------
package Lesson_30_3_2020;

public class Test {
	public static void main(String[] args) {
		LuxuryCar myLuxuryCar = new LuxuryCar();
		myLuxuryCar.setInfor();
		myLuxuryCar.getInfor();
		System.out.println("Total price with transportcost: " + myLuxuryCar.calculatePrice(20000) + "$");
	}
}



Nguyễn Hoàng Anh [C1907L]

Ngày viết: 17:03 31/03/2020



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package CarManager;

/**
 *
 * @author Redmibook 14
 */
interface  Icar {
    public float calculateTax();
    public float calculatePrice();
    public void getInfo();
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package CarManager;

/**
 *
 * @author Redmibook 14
 */
public class Car implements Icar {

    private String name, producer;
    
    private int seat,year;
    private float rootprice;

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

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void setSeat(int seat) {
        this.seat = seat;
    }

    public void setRootprice(float rootprice) {
        this.rootprice = rootprice;
    }

    public String getName() {
        return name;
    }

    public String getProducer() {
        return producer;
    }

    public int getYear() {
        return year;
    }

    public int getSeat() {
        return seat;
    }

    public float getRootprice() {
        return rootprice;
    }

    @Override
    public float calculateTax() {
        float tax;
        if (this.seat < 7) {
            tax = (this.rootprice * 60) / 100;
        } else {
            tax = (this.rootprice * 70) / 100;
        }
        return tax;
    }

    @Override
    public float calculatePrice() {
        float TotalPrice = this.rootprice + calculateTax();
        return TotalPrice;
    }

    @Override
    public void getInfo() {
        System.out.println(name + " car produced by " + producer + " in " + year + " has " + seat + " seats with the total price is " + calculatePrice() + "$.");
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package CarManager;

/**
 *
 * @author Redmibook 14
 */
public class LuxuryCar extends Car {

    private float specialRate;

    @Override
    public float calculatePrice() {
        float TotalPrice = getRootprice() + calculateTax() + getRootprice()
                * specialRate;
        return TotalPrice;
    }

    public float calculatePrice(float transportCost) {
        float TotalPrice = getRootprice()
                + calculateTax() + getRootprice() * specialRate + transportCost;
        return TotalPrice;

    }

    @Override
    public void getInfo() {
        System.out.println(getName() + " car produced by " + getProducer() + " in " + getYear() + " has " + getSeat() + " seats with the total price is " + calculatePrice() + "$.");
    }

    public void getInfo(float transportCost) {
        System.out.println(getName() + " car produced by " + getProducer() + " in " + getYear() + " has " + getSeat() + " seats with the total price is " + calculatePrice(transportCost) + "$.");
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package CarManager;

import java.util.*;

/**
 *
 * @author Redmibook 14
 */
public class Test {

    public static void main(String[] args) {
        LuxuryCar myLuxCar = new LuxuryCar();
        Scanner input = new Scanner(System.in);
        System.out.println("1.Enter name : ");
        String name = input.nextLine();
        myLuxCar.setName(name);
        System.out.println("2.Enter Producer : ");
        String producer = input.nextLine();
        myLuxCar.setProducer(producer);
        System.out.println("3.Enter Year : ");
        while (true) {
            try {
                int year = Integer.parseInt(input.nextLine());
                myLuxCar.setYear(year);
                System.out.println("4.Enter Seat : ");
                int seat = Integer.parseInt(input.nextLine());
                myLuxCar.setSeat(seat);
                System.out.println("5.Enter RootPrice  : ");
                float RootPrice = Float.parseFloat(input.nextLine());
                myLuxCar.setRootprice(RootPrice);
                myLuxCar.getInfo();
                System.out.println("in case transportCost is 20,000$  : ");
                myLuxCar.getInfo(20000);
            } catch (NumberFormatException e) {
                 System.err.println("INVALID INPUT");
            }
        }
    }
}


Ngô Quang Huy [C1907L]

Ngày viết: 22:45 30/03/2020



package March30;

public interface Icar {
    float calculateTax();
    float calculatePrice();
    void getInfor();
}



package March30;


import java.util.Scanner;


public class Car implements Icar{
    public String name,producer;
    public int year,seat;
    public float tax,TotalPrice,rootprice;
    
    public Car() {
    }

    public Car(String name, String producer, int year, int seat, int rootprice) {
        this.name = name;
        this.producer = producer;
        this.year = year;
        this.seat = seat;
        this.rootprice = rootprice;
    }

    public String getName() {
        return name;
    }

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

    public String getProducer() {
        return producer;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getSeat() {
        return seat;
    }

    public void setSeat(int seat) {
        this.seat = seat;
    }

    public float getRootprice() {
        return rootprice;
    }

    public void setRootprice(int rootprice) {
        this.rootprice = rootprice;
    }

    public float getTax() {
        return tax;
    }

    public void setTax(float tax) {
        this.tax = tax;
    }

    public float getTotalPrice() {
        return TotalPrice;
    }

    public void setTotalPrice(float TotalPrice) {
        this.TotalPrice = TotalPrice;
    }

    @Override
    public float calculateTax() {
        if(seat<7){
            this.tax = this.rootprice*60/100;
        }else{
            this.tax = this.rootprice*70/100;
        }
        return this.tax;
    }

    @Override
    public float calculatePrice() {
        this.TotalPrice = rootprice + tax;
        return this.TotalPrice;
    }

    @Override
    public void getInfor() {
        System.out.println(name+" car produced by "+ producer +" in "
                +year+" has "+seat+" seats with the total price is "
                +TotalPrice+"$");
    }

    void inputCar(){
        Scanner input = new Scanner(System.in);
        System.out.print("Insert Name: ");
        name=input.nextLine();
        System.out.print("Insert Producer: ");
        producer=input.nextLine();
        while(true){
            try{
                System.out.print("Insert Year: ");
                year=Integer.parseInt(input.nextLine());
                break;
            }catch(NumberFormatException e){
                System.err.println("INVALID INPUT");
            }
        }
        while(true){
            try{
                System.out.print("Insert Seat: ");
                seat=Integer.parseInt(input.nextLine());
                break;
            }catch(NumberFormatException e){
                System.err.println("INVALID INPUT");
            }
        }
        while(true){
            try{
                System.out.print("Insert rootPrice : ");
                rootprice=Float.parseFloat(input.nextLine());
                break;
            }catch(NumberFormatException e){
                System.err.println("INVALID INPUT");
            }
        }
    }
}



package March30;

import java.util.Scanner;

public class LuxuryCar extends Car{
    private float specialRate;

    public LuxuryCar(float specialRate) {
        this.specialRate = specialRate;
    }

    public LuxuryCar(float specialRate, String name, String producer, int year, int seat, int rootprice) {
        super(name, producer, year, seat, rootprice);
        this.specialRate = specialRate;
    }

    public LuxuryCar() {
    }

    @Override
    public float calculatePrice() {
        super.TotalPrice = super.rootprice + super.tax + super.rootprice*specialRate;
        return super.TotalPrice;
    }
    
    public float calculatePrice(float transportCost) {
        super.TotalPrice = super.rootprice + super.tax + super.rootprice*this.specialRate + transportCost;
        return super.TotalPrice;
    }

    @Override
    void inputCar() {
        super.inputCar();
        Scanner input = new Scanner(System.in);
        while(true){
            try{
                System.out.print("Insert specialRate: ");
                this.specialRate=Float.parseFloat(input.nextLine());
                break;
            }catch(NumberFormatException e){
                System.err.println("INVALID INPUT");
            }
        }
        
    }
    
    
    
}



package March30;

public class Test {
    public static void main(String[] args) {
        LuxuryCar car = new LuxuryCar();
        car.inputCar();
        car.calculateTax();
        car.calculatePrice(20000);
        System.out.println("\nCar information:");
        car.getInfor();
        System.out.println("\nTotal Price is "+car.TotalPrice);
    }
}


trung [C1907L]

Ngày viết: 22:20 30/03/2020



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Buoi9;

/**
 *
 * @author prdox
 */
interface Icar {
    //calculate tax of the car
    public float calculateTax();
    //Calculate the total cost of the car
    public float calculatePrice();
    //Display the information of the car
    public void getInfor();
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Buoi9;

import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class Car implements Icar {

    String name, producer;
    int year, seat;
    float rootprice;

    public Car() {
    }

    public Car(String name, String producer, int year, int seat, float rootprice) {
        this.name = name;
        this.producer = producer;
        this.year = year;
        this.seat = seat;
        this.rootprice = rootprice;
    }

    public void inputInfo() {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap vao ten xe");
        name = input.nextLine();
        System.out.println("Nhap vao hang san xuat");
        producer = input.nextLine();
        while(true) {
            try {
                System.out.println("Nhap vao nam san xuat");
                year = Integer.parseInt(input.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Invalid input!");
            }
        }
        while(true) {
            try {
                System.out.println("Nhap vao so ghe");
                seat = Integer.parseInt(input.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Invalid input!");
            }
        }
        while(true) {
            try {
                System.out.println("Nhap vao gia goc");
                rootprice = Float.parseFloat(input.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Invalid input!");
            }
        }

    }

    public String getName() {
        return name;
    }

    public String getProducer() {
        return producer;
    }

    public int getYear() {
        return year;
    }

    public int getSeat() {
        return seat;
    }

    public float getRootprice() {
        return rootprice;
    }

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

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void setSeat(int seat) {
        this.seat = seat;
    }

    public void setRootprice(int rootprice) {
        this.rootprice = rootprice;
    }

    @Override
    public float calculateTax() {
        if (seat < 7) {
            return 0.6f * rootprice;
        }
        return 0.7f * rootprice;
    }

    @Override
    public float calculatePrice() {
        return rootprice + calculateTax();
    }

    @Override
    public void getInfor() {
        System.out.format("%s car produced by %s in %d has %d seats with the total price is %f$", name, producer, year, seat, calculatePrice());
    }

}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Buoi9;

import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class LuxuryCar extends Car{
    private float specialRate;

    public LuxuryCar() {
    }

    public LuxuryCar(String name, String producer, int year, int seat, float rootprice,float specialRate) {
        super(name,producer,year,seat,rootprice);
        this.specialRate = specialRate;
    }

    public float getSpecialRate() {
        return specialRate;
    }
    
    @Override
    public void inputInfo() {
        Scanner input = new Scanner(System.in);
        super.inputInfo();
        while(true) {
            try {
                System.out.println("Nhap vao rate");
                specialRate = Float.parseFloat(input.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Invalid input!");
            }
        }
    }
    
    @Override
    public float calculatePrice(){
        return rootprice + calculateTax() + rootprice*specialRate ;
    }
    
    public float calculatePrice(float transportCost){
        return calculatePrice() + transportCost;
    }
    
    
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Buoi9;

import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        LuxuryCar myLuxuryCar = new LuxuryCar();
        myLuxuryCar.inputInfo();
        myLuxuryCar.getInfor();
        System.out.println("\nTotal price is "+myLuxuryCar.calculatePrice(20000f));

    }
}


Phân Loại Bài Viết

Tài Liệu Tham Khảo

Đã sao chép!!!