Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
FromBibleC#(OOP)
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { Car carMers = new Mersedes(60, 6, "Mersedes"); carMers.WheelCount = 6; carMers.TechnicalCertificate(); Console.WriteLine(carMers.StepSwitching); Console.WriteLine(carMers.ToString()); carMers.gearShift(-1); carMers.gearShift(1); Person p = new Person(50000000, true, carMers); Person p1 = new Person(40000000, true, carMers); Console.WriteLine($"From Tamplate: {Person.ToString(p.GSCar)} {Person.ToString(p.GSMoney)}"); Console.WriteLine("From Indexer: {0} {1}", p[Person.PersonInfo.Car], p[Person.PersonInfo.Money]); p.GSMoney = Bank.SellCar(ref p); Console.WriteLine($"New balance after car sell: {p.GSMoney}. Car availability? - {p[Person.PersonInfo.AvailabilityCar]}"); Console.WriteLine($"From Car Indexer: {carMers[0]}"); Console.WriteLine($"p > p1 ? = {p > p1}. p < p1 ? = {p < p1}"); Console.WriteLine($"P: {(p + 1000000).GSMoney}"); Console.WriteLine($"P1: {(p1 + 10000000).GSMoney}"); } } interface ICarInfo { int CarPrice{get; set;} string CarNum{get; set;} char this[int index]{get; set;} } struct Person { public enum PersonInfo{Money, AvailabilityCar, Car}; private int money; private bool availabilityCar; private Car car; public Person(int m, bool aC, Car cr) { money = m; availabilityCar = aC; car = cr; } public static string ToString<T>(T val) => val.ToString(); public Car GSCar { get => car; set => car = value; } public int GSMoney { get => money; set => money = value; } public bool AvailabilityCar { get => availabilityCar; } public string this[PersonInfo PI] { get { string temp = "NULL"; switch(PI) { case PersonInfo.Money: temp = money.ToString(); break; case PersonInfo.AvailabilityCar: temp = availabilityCar.ToString(); break; case PersonInfo.Car: temp = car.CarName; break; } return temp; } set { switch(PI) { case PersonInfo.Money: money = Convert.ToInt32(value); break; case PersonInfo.AvailabilityCar: availabilityCar = Convert.ToBoolean(value); break; case PersonInfo.Car: car.CarName = value; break; } } } public static bool operator> (Person p1, Person p2) => (p1.GSMoney > p2.GSMoney); public static bool operator< (Person p1, Person p2) => (p1.GSMoney < p2.GSMoney); public static Person operator+ (Person p, int num) => new Person(p.GSMoney + num, p.AvailabilityCar, p.GSCar); public static Person operator+ (int num, Person p) => p + num; public static Person operator- (Person p, int num) => new Person(p.GSMoney - num, p.AvailabilityCar, p.GSCar); public static Person operator- (int num, Person p) => new Person(num - p.GSMoney, p.AvailabilityCar, p.GSCar); } struct Bank { public static int SellCar(ref Person p) { int cash = p.GSMoney - p.GSCar.CarPrice; p.GSCar = null; p[Person.PersonInfo.AvailabilityCar] = "false"; return cash; } } abstract class Car: ICarInfo { protected int wheelCount = 4; protected int doorCount = 4; protected int headlightCount = 4; protected string carName = "Pego"; protected int maxSpeed = 120; protected int gearsNumber = 6; protected int minSpeed = 20; protected int stepSwitching = 20; protected int currentSpeed = 0; protected int carPrice = 1000000; protected string carNum = "A123BC"; abstract public int WheelCount {get; set;} abstract public int DoorCount {get; set;} abstract public int HeadlightCount {get; set;} abstract public string CarName {get; set;} abstract public int MaxSpeed {get; set;} abstract public int MinSpeed {get; set;} abstract public int GearsNumber {get; set;} abstract public int StepSwitching {get; set;} abstract public int CurrentSpeed {get; set;} public int CarPrice { get => carPrice; set => carPrice = value; } public string CarNum { get => carNum; set => carNum = value; } abstract public void gearShift(int shift); public void TechnicalCertificate() { Console.WriteLine("Car Name: {0} | Wheel Count = {1} | Door Count = {2} | Headlight Count = {3} | Car Num = {4}", CarName, WheelCount, DoorCount, HeadlightCount, CarNum); } /*public new string ToString() //this is working { return this.carName; }*/ public override string ToString() => this.carName; public char this[int index] { get => carName[index % carName.Length]; set => carName.Remove(index % carName.Length, 1).Insert(index % carName.Length, Convert.ToString(value)); } } class Mersedes : Car { public Mersedes(int maxSpeed, int gearsNumber, string carName) { this.maxSpeed = maxSpeed; this.gearsNumber = gearsNumber; this.minSpeed = maxSpeed / gearsNumber; this.stepSwitching = maxSpeed / gearsNumber; this.currentSpeed = 0; this.carName = carName; } public override int WheelCount { set{ this.wheelCount = (value >= 4) ? value : 4; } get{ return this.wheelCount; } } public override int DoorCount { set{ this.doorCount = (value >= 4) ? value : 4; } get{ return this.doorCount; } } public override int HeadlightCount { set{ this.headlightCount = (value >= 4) ? value : 4; } get{ return this.headlightCount; } } public override string CarName {get {return this.carName;} set{this.carName = value;}} public override int MaxSpeed {get {return this.maxSpeed;} set{this.maxSpeed = value;}} public override int MinSpeed {get {return this.minSpeed;} set{this.minSpeed = value;}} public override int GearsNumber {get {return this.gearsNumber;} set{this.gearsNumber = value;}} public override int StepSwitching {get {return this.stepSwitching;} set{this.stepSwitching = value;}} public override int CurrentSpeed {get {return this.currentSpeed;} set{this.currentSpeed = value;}} public override void gearShift(int shift) { Console.WriteLine("Текущая скорость = {0}", this.CurrentSpeed); if (shift == -1) { if (this.currentSpeed > this.minSpeed && this.currentSpeed <= this.maxSpeed) this.currentSpeed -= this.stepSwitching; else this.currentSpeed = this.minSpeed; } if (shift == 1) { if (this.currentSpeed < this.maxSpeed && this.currentSpeed >= this.minSpeed) this.currentSpeed += this.stepSwitching; else this.currentSpeed = this.maxSpeed; } Console.WriteLine("Новая скорость = {0}", this.CurrentSpeed); } } }
run
|
edit
|
history
|
help
0
7. Asynchrony
3
The abstract factory design pattern implemented in c# using generics.
Custom Date Format
String combinations
Bellman Ford algorithm helps to find the shortest path
eryjuyhtgrfesdsadcfg
Student Management System
asdasd
PrecisionTester