Package caryard
Task 2: Car Class
package caryard;
public class Car {
int id;
String modelName;
int year;
double costPrice, sellingPrice;
Car(String name, int year, double costPrice){
setID();
this.modelName = name;
this.year = year;
this.costPrice = costPrice;
sellingPrice = calcSP(); }
public void setID(){
id = (int) ((Math.random() * ((1000 – 100) + 1)) + 100);
}
int getID(){
return id;
}
String getModel(){
return modelName;
}
int getYear(){
return year;
}
double getCP(){
return costPrice;
}
double getSP(){
return sellingPrice;
}
double calcSP(){
return costPrice*(1 + 0.3);
}
}
Task 3: Driver Class
package caryard;
import java.util.Scanner;
public class CarYardMIT162011 {
static Car[] cars = new Car[100];
static Scanner sc = new Scanner(System.in);
private static void buy(){
System.out.println(“n~ Buy Car ~”);
System.out.println(“nEnter model name:”);
String name = sc.nextLine();
int year;
while(true){
System.out.println(“Enter year:”);
year = sc.nextInt();
if(year<2009)
System.out.println(“nError! The car yard does not buy cars manufactured before 2009.”);
else
break; }
System.out.println(“Enter price: $”);
double price = sc.nextDouble();
cars[carCount]=new Car(name,year,price);
System.out.println(“nNew car added: “+name+” (“+year+”)”);
carCount++;
}
private static void display(){
System.out.println(“n~ Car List ~”);
for(int i=0;i<carCount;i++){
System.out.println(“n”+(i+1)+”:”);
System.out.println(“ntCar ID: “+cars[i].getID());
System.out.println(“ntModel: “+cars[i].getModel());
System.out.println(“ntYear: “+cars[i].getYear());
System.out.println(“ntCost price: $”+cars[i].getCP());
System.out.println(“ntSelling price: $”+cars[i].getSP());
System.out.println();
private static int sell(){
System.out.println(“n~ Sell Car ~”);
System.out.println(“nEnter car ID:”);
int id = sc.nextInt();
for(int i=0;i<carCount;i++){
if(cars[i].getID()==id){
System.out.println(“ntModel: “+cars[i].getModel());
System.out.println(“ntYear: “+cars[i].getYear());
System.out.println(“ntCost price: $”+cars[i].getCP());
System.out.println(“ntSelling price: $”+cars[i].getSP());
return 1;
System.out.println(“No match found for car ID: “+id);
return 0;
public static void main(String[] args){
int choice=1;
cars[carCount]=new Car(“Mazda”,2009,15000);
carCount++;
cars[carCount]=new Car(“Suzuki”,2018,7000);
carCount++;
cars[carCount]=new Car(“Royce”,2015,12000);
carCount++;
while(choice!=4){
System.out.println(“n~ Main Menu ~”);
System.out.print(“1. Buy Carn2. Sell Carn3. Display all carsn4. ExitnnEnter menu choice… “);
choice = sc.nextInt();
sc.nextLine();
switch(choice){
case 1: buy();
break;
case 2: sell();
break;
case 3: display();
break;
case 4:
System.out.println(“nGoodbye! Have a nice day!”);
break;
default: System.out.println(“Wrong entry!”);
Output
~ Main Menu ~
- Buy Car
- Sell Car
- Display all cars
- Exit
Enter menu choice… 1~ Buy Car ~
Enter model name:
Ferrari
Enter year:
2016
Enter price: $
25000
New car added: Ferrari (2016)
~ Main Menu ~
- Buy Car
- Sell Car
- Display all cars
- Exit
Enter menu choice… 3~ Car List ~1:
Car ID: 719
Model: Mazda
Year: 2009
Cost price: $15000.0
Selling price: $19500.0
2:
Car ID: 393
Model: Suzuki
Year: 2018
Cost price: $7000.0
Selling price: $9100.0
3: Car ID: 620
Model: Royce
Year: 2015
Cost price: $12000.0
Selling price: $15600.0
4: Car ID: 671
Model: Ferrari
Year: 2016
Cost price: $25000.0
Selling price: $32500.0
~ Main Menu ~
- Buy Car
- Sell Car
- Display all cars
- Exit
Enter menu choice… 5
Wrong entry!
~ Main Menu ~
- Buy Car
- Sell Car
- Display all cars
- Exit
- Enter menu choice… 2
~ Sell Car ~
Enter car ID:
323
No match found for car ID: 323
~ Main Menu ~
- Buy Car
- Sell Car
- Display all cars
- Exit
Enter menu choice… 2
~ Sell Car ~
Enter car ID:
393
Model: Suzuki
Year: 2018
Cost price: $7000.0
Selling price: $9100.0
~ Main Menu ~
- Buy Car
- Sell Car
- Display all cars
- Exit
Enter menu choice… 4
Goodbye! Have a nice day!