BN108 Programming for Networking: Car Management System Assignment

Verified

Added on  2023/06/11

|6
|1107
|393
Homework Assignment
AI Summary
This assignment solution demonstrates the implementation of a Car management system using object-oriented programming principles. It includes a UML class diagram design and the implementation of a 'Car' class with attributes like ID, model name, year, cost price, and selling price. The 'CarYardMIT162011' class acts as the driver program, allowing users to buy, sell, and display car information. The program features a menu-driven interface for user interaction, enabling them to add new cars, display existing cars, and simulate selling a car based on its ID. The solution also includes sample output demonstrating the program's functionality. Desklib provides access to a wide range of similar solved assignments and past papers to aid students in their studies.
tabler-icon-diamond-filled.svg

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Task1: UML Class Diagram
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;
}
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
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 int carCount=0;
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("\n\tCar ID: "+cars[i].getID());
System.out.println("\n\tModel: "+cars[i].getModel());
System.out.println("\n\tYear: "+cars[i].getYear());
System.out.println("\n\tCost price: $"+cars[i].getCP());
System.out.println("\n\tSelling price:
$"+cars[i].getSP());
System.out.println();
}
}
Document Page
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("\n\tModel:
"+cars[i].getModel());
System.out.println("\n\tYear: "+cars[i].getYear());
System.out.println("\n\tCost price:
$"+cars[i].getCP());
System.out.println("\n\tSelling 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 Car\n2. Sell Car\n3. Display all
cars\n4. Exit\n\nEnter 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!");
}
}
}
}
Document Page
Output
~ Main Menu ~
1. Buy Car
2. Sell Car
3. Display all cars
4. Exit
Enter menu choice... 1
~ Buy Car ~
Enter model name:
Ferrari
Enter year:
2016
Enter price: $
25000
New car added: Ferrari (2016)
~ Main Menu ~
1. Buy Car
2. Sell Car
3. Display all cars
4. 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:
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
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 ~
1. Buy Car
2. Sell Car
3. Display all cars
4. Exit
Enter menu choice... 5
Wrong entry!
~ Main Menu ~
1. Buy Car
2. Sell Car
3. Display all cars
4. Exit
Enter menu choice... 2
~ Sell Car ~
Enter car ID:
323
No match found for car ID: 323
~ Main Menu ~
1. Buy Car
2. Sell Car
3. Display all cars
4. Exit
Enter menu choice... 2
~ Sell Car ~
Enter car ID:
Document Page
393
Model: Suzuki
Year: 2018
Cost price: $7000.0
Selling price: $9100.0
~ Main Menu ~
1. Buy Car
2. Sell Car
3. Display all cars
4. Exit
Enter menu choice... 4
Goodbye! Have a nice day!
chevron_up_icon
1 out of 6
circle_padding
hide_on_mobile
zoom_out_icon
logo.png

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]