0% found this document useful (0 votes)
55 views4 pages

Lesson04 - Exercises (PS11-12 Hoh Jungi)

This document contains code for two C# programs that create classes to represent computers and vehicles. The Computer class tracks brand and price, allowing the price to be updated based on the model. The Vehicle class tracks model and price, allowing the price to be updated based on a COE amount or vehicle category. Both programs prompt the user for input, create instances of the classes, and output the updated prices.

Uploaded by

Jungi Hoh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views4 pages

Lesson04 - Exercises (PS11-12 Hoh Jungi)

This document contains code for two C# programs that create classes to represent computers and vehicles. The Computer class tracks brand and price, allowing the price to be updated based on the model. The Vehicle class tracks model and price, allowing the price to be updated based on a COE amount or vehicle category. Both programs prompt the user for input, create instances of the classes, and output the updated prices.

Uploaded by

Jungi Hoh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lesson04Ex1

Filename: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson04Ex1
{
internal class Computer
{
private string strBrand;
private float fltPrice;

public Computer(string b, float p)


{
strBrand = b;
fltPrice = p;
}

public string Brand


{
get { return strBrand; }
}

public float UpdatePrice()


{
return fltPrice;
}

public float UpdatePrice(string model)


{
if (model == "IdeaPad")
fltPrice = fltPrice * 0.90f;
else if (model == "Aspire")
fltPrice = fltPrice * 0.80f;
else if (model == "Zbook")
fltPrice = fltPrice * 0.70f;

return fltPrice;
}
}

internal class Program


{
static void Main(string[] args)
{
Console.WriteLine("Enter the brand: ");
string brand = Console.ReadLine();

Console.WriteLine("Enter the price: ");


float price = float.Parse(Console.ReadLine());

Computer cObj = new Computer(brand, price);

Console.WriteLine("Price of {0} computer = ${1}", cObj.Brand, cObj.UpdatePrice().ToString());

Console.WriteLine("Enter model: ");


string m = Console.ReadLine();

Console.WriteLine("Price of {0} {1} computer = ${2}", cObj.Brand, m, cObj.UpdatePrice(m).ToString());

Console.ReadKey();
}
}
}
Lesson04Ex2

Filename: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson04Ex2
{
class Vehicle
{
private string strModel;
private float fltPrice;

public Vehicle(string m, float p)


{
strModel = m;
fltPrice = p;
}

public string Model


{
get { return strModel; }
}

public float GetPrice(float coe)


{
fltPrice = fltPrice + coe;
return fltPrice;
}

public float GetPrice(string cat)


{
if (cat == "A")
fltPrice = fltPrice + 30000f; //coe is $30000
else if (cat == "B")
fltPrice = fltPrice + 39000f; //coe is $39000
else if (cat == "C")
fltPrice = fltPrice + 31000f; //coe is $31000

return fltPrice;
}
}

internal class Program


{
static void Main(string[] args)
{
Console.WriteLine("Enter the model of vehicle 1: ");
string model = Console.ReadLine();

Console.WriteLine("Enter the price of vehicle 1: ");


float price = float.Parse(Console.ReadLine());

Vehicle v1Obj = new Vehicle(model, price);

Console.WriteLine("Enter the coe of vehicle 1: ");


float co = float.Parse(Console.ReadLine());

Console.WriteLine("Price of {0} vehicle with ${1} COE = ${2}", v1Obj.Model, co,


v1Obj.GetPrice(co).ToString());

Console.WriteLine("Enter the model of vehicle 2: ");


model = Console.ReadLine();

Console.WriteLine("Enter the price of vehicle 2: ");


price = float.Parse(Console.ReadLine());

Vehicle v2Obj = new Vehicle(model, price);

Console.WriteLine("Enter the cat of vehicle 2: ");


string ca = Console.ReadLine();

Console.WriteLine("Price of {0} vehicle with CAT {1} COE = ${2}", v2Obj.Model, ca,
v2Obj.GetPrice(ca).ToString());

Console.ReadKey();

}
}
}

You might also like