Last active
July 13, 2018 02:11
-
-
Save d630/41b78e0e468dfae1a7f893b13057ca54 to your computer and use it in GitHub Desktop.
Tag 3: Auto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace csharp | |
{ | |
public class Auto | |
{ | |
private int v; | |
private int vMax; | |
private string farbe; | |
private string name; | |
private int tank; | |
private int tankMax; | |
public Auto(string name, int vMax, int tankMax, string farbe) | |
{ | |
this.farbe = farbe; | |
this.name = name; | |
this.vMax = vMax; | |
this.tankMax = tankMax; | |
} | |
private void setFarbe(string farbe) | |
{ | |
this.farbe = farbe; | |
} | |
private void setVMax(int vMax) | |
{ | |
this.vMax = vMax; | |
} | |
private void setV(int v) | |
{ | |
this.v = v; | |
} | |
private void setName(string name) | |
{ | |
this.name = name; | |
} | |
private void setTank(int tank) | |
{ | |
this.tank = tank; | |
} | |
private void setTankMax(int tankMax) | |
{ | |
this.tankMax = tankMax; | |
} | |
private string getFarbe() | |
{ | |
return this.farbe; | |
} | |
private int getVMax() | |
{ | |
return this.vMax; | |
} | |
private int getV() | |
{ | |
return this.v; | |
} | |
private string getName() | |
{ | |
return this.name; | |
} | |
private int getTank() | |
{ | |
return this.tank; | |
} | |
private int getTankMax() | |
{ | |
return this.tankMax; | |
} | |
// -- Methods. | |
public void beschleunigen(int v) | |
{ | |
if (this.getTank() == 0) | |
return; | |
if (this.getV() + v >= this.getVMax()) | |
this.setV(this.getVMax()); | |
else | |
this.setV(this.getV() + v); | |
this.verbrauch(); | |
} | |
public void bremsen(int v) | |
{ | |
if (this.getV() - v <= 0) | |
this.setV(0); | |
else | |
this.setV(this.getV() - v); | |
} | |
public int tacho() | |
{ | |
return this.getV(); | |
} | |
public void tanken(int tank) | |
{ | |
if (this.getTank() + tank >= this.getTankMax()) | |
this.setTank(this.getTankMax()); | |
else | |
this.setTank(this.getTank() + tank); | |
} | |
public void verbrauch() | |
{ | |
// don't care now … | |
if (this.getTank() - 1 <= 0) | |
this.setTank(0); | |
else | |
this.setTank(this.getTank() - 1); | |
} | |
public int sprit() | |
{ | |
return this.getTank(); | |
} | |
public void info() | |
{ | |
Console.WriteLine("name: {0}\nfarbe: {1}\ntankMax: {2}\nvMax: {3}", this.getName(), this.getFarbe(), this.getTankMax(), this.getVMax()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace csharp | |
{ | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Random ran = new Random(); | |
string[] farben = { | |
"rot", | |
"blau", | |
"gelb", | |
"gruen" | |
}; | |
Auto[] autos = new Auto[3]; | |
for (int i = 0; i < autos.Length; i++) | |
{ | |
autos[i] = new Auto("name" + ran.Next(0, 2), ran.Next(180, 261), ran.Next(50, 151), farben[ran.Next(0, autos.Length)]); | |
} | |
while (true) | |
{ | |
Console.Write("Choose your car (0-{0}): ", autos.Length - 1); | |
int n = int.Parse(Console.ReadLine()); | |
Console.Write("Control:\n\t1. Geschwindigkeit anzeigen\n\t2. Tank anzeigen\n\t3. Beschleunigen\n\t4. Bremsen\n\t5. Tanken\n\t6. Info\nChoose: "); | |
switch (int.Parse(Console.ReadLine())) | |
{ | |
case 1: | |
Console.WriteLine(autos[n].tacho()); | |
break; | |
case 2: | |
Console.WriteLine(autos[n].sprit()); | |
break; | |
case 3: | |
Console.Write("Km/h: "); | |
autos[n].beschleunigen(int.Parse(Console.ReadLine())); | |
break; | |
case 4: | |
Console.Write("Km/h: "); | |
autos[n].bremsen(int.Parse(Console.ReadLine())); | |
break; | |
case 5: | |
Console.Write("Liter: "); | |
autos[n].tanken(int.Parse(Console.ReadLine())); | |
break; | |
case 6: | |
autos[n].info(); | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment