In: Computer Science
Your TASK
Part 1
Write the class Car . Save the file as Car.java.
Part 2
Create a client class ClientCar and save as ClientCar.java.
In the client class, do the following: -
You are required to declare 2 different Cars. Get the input for for manufacturer, the quantity and price. Display the information for each car, such as the manufacturer, price for each car, quantity, and total price after the discount.
Discount: -
Total Price > 300 000 : Discount 20%
Total Price > 50000 – Discount 10%
Code
Car.java
public class Car {
private String manufacturer;
private int quantity;
private double price;
/**
* @param manufacturer
* @param quantity
* @param price
*/
public Car(String manufacturer, int quantity, double
price) {
super();
this.manufacturer =
manufacturer;
this.quantity = quantity;
this.price = price;
}
/**
* @return the manufacturer
*/
public String getManufacturer() {
return
manufacturer.charAt(0)+"";
}
/**
* @return the quantity
*/
public int getQuantity() {
return quantity;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param manufacturer the manufacturer to set
*/
private void setManufacturer(String manufacturer)
{
this.manufacturer =
manufacturer;
}
/**
* @param quantity the quantity to set
*/
private void setQuantity(int quantity) {
this.quantity = quantity;
}
/**
* @param price the price to set
*/
private void setPrice(double price) {
this.price = price;
}
public double calcTotalPrice()
{
double
totalCost=quantity*price;
if(totalCost>300000 )
totalCost-=totalCost*0.2;
else if(totalCost>50000 )
totalCost-=totalCost*0.1;
return totalCost;
}
public String displayInfo ()
{
return "Manufacturer:
"+manufacturer+"\nQuantity: "+quantity+"\nPrice: "+price+"\nTotal
price: "+calcTotalPrice();
}
}
CarClient.java
import java.util.Scanner;
public class ClientCar {
public static void main(String[] args) {
Scanner scnr=new
Scanner(System.in);
String manu;
int qnty;
double price;
System.out.print("Enter the car
manufacturer: ");
manu=scnr.next();
System.out.print("Enter the car
quantity: ");
qnty=scnr.nextInt();
System.out.print("Enter the price:
");
price=scnr.nextDouble();
Car car1=new Car(manu, qnty,
price);
System.out.print("Enter the car
manufacturer: ");
manu=scnr.next();
System.out.print("Enter the car
quantity: ");
qnty=scnr.nextInt();
System.out.print("Enter the price:
");
price=scnr.nextDouble();
Car car2=new Car(manu, qnty,
price);
System.out.println(car1.displayInfo());
System.out.println();
System.out.println(car1.displayInfo());
System.out.println();
}
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.