Benutzer mit den meisten Antworten
(schleifen und Unterprogramme)(ich habe bis jetzt den automatischen Teil programmiert aber ich weiß nicht wie ich es mit der manuellen Eingabe des Nutzers machen soll )

Frage
-
ÜbBsp18: Geldausgabeautomat
Erstelle ein C#-Konsolprogramm bzw. WPF-Programm zur Simulation eines Geldausgabeautomaten.
Der Automat hat folgende Funktionen:
- Betrag eingeben
- Zwischen automatischer Stückelung und manueller Stückelung wählen
- Die Stückelung erfolgt immer in 20 €-, 10 €- und 5 €-Scheinen
- Die automatische Stückelung erfolgt in möglichst großen Scheinen, gereiht nach Wert: 20, 10, 5
- Für die manuelle Stückelung ist eine Eingabe der gewünschten Scheine erforderlich
- Ausgabe der Scheine
- Die Eingabe des gewünschten Geldbetrags und die Auswahl der Stückelungsart erfolgt im HP bzw. den entsprechenden Click- oder TextChanged-Methoden.
- Alle Geldwerte müssen immer durch 5 ohne Rest teilbar sein. Diese Überprüfung ist durch eine Methode mit Rückgabewert zu lösen. Bei falschen Werten sind diese wiederholt einzulesen.
- Die automatische Stückelung ist als Methode mit out-Parametern (Anzahl20, Anzahl10, Anzahl5) zu lösen. Der Methode wird als E-Parameter der Geldbetrag übergeben.
- Die manuelle Stückelung ist mit Hilfe von mehreren Methoden zu lösen. Zuerst werden im Rahmen einer Methode die gewünschten Scheine eingelesen und als out-Parameter Anz20, Anz10, Anz5 geliefert.
Danach wird mit einer zweiten Methode geprüft, ob diese Stückelung für den Geldbetrag möglich ist. Dafür ist eine Methode zu erstellen, welcher der gewünschte Geldbetrag als E-Parameter und die gewünschten Stückelungen als ref-Parameter Anz20, Anz10 und Anz5
übergeben werden. Die Methode bestimmt, ob die gewünschte Stückelung möglich ist und liefert diese Information über einen boolschen Returnwert. Ist diese Stückelung nicht möglich, bestimmt sie mit Hilfe der Methode für die automatische Stückelung (3.) die
richtige Anz20, Anz10, Anz5 und liefert diese in den ref-Parametern.
Auch für die Ausgabe der Scheine ist eine eigene Methode zu schreiben. Ihr werden die Anzahl der Scheine und die Art des Scheins als E-Parameter übergeben und sie gibt jeden Schein einzeln am Bildschirm (lt. Bsp. unten) aus.
Ich brauche nur den Ansatz für den manuellen Teil
Lg Marc
Ps: ich brauche nur den den Ansatz
Pps : ich verlange und will auch nicht dass mir jemand das Programm schickt
Schonmal danke im Voraus
Teilaufgaben:
Antworten
-
Hi Marc,
hier mal ein Beispiel einer WPF-Oberfläche:XAML:
<Window x:Class="WpfApp1CS.Window50" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1CS" mc:Ignorable="d" Title="Geldautomat" Height="300" Width="300"> <Window.Resources> <local:Window50VM x:Key="vm"/> <Style TargetType="TextBox"> <Setter Property="Margin" Value="5"/> </Style> </Window.Resources> <Grid DataContext="{StaticResource vm}"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="Gewünschter Betrag"/> <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Betrag}"/> <Label Grid.Row="1" Grid.Column="0" Content="Anzahl 20 €"/> <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Anzahl20}"/> <Label Grid.Row="2" Grid.Column="0" Content="Anzahl 10 €"/> <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Anzahl10}"/> <Label Grid.Row="3" Grid.Column="0" Content="Anzahl 5 €"/> <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding Anzahl5}"/> <Button Grid.Row="4" Grid.Column="1" Content="Geld ausgeben" Command="{Binding Cmd}" Margin="5"/> <Label Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" Content="{Binding Result}"/> </Grid> </Window>
Und dazu der Code ohne die Berechnungen, die Du ja vermutlich schon gelöst hast. Deinen Code habe ich nicht geprüft.
using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Input; namespace WpfApp1CS { public class Window50VM : INotifyPropertyChanged { public Window50VM() { geld = new Window50Geld(); } private Window50Geld geld; public decimal Betrag { get; set; } private int _anzahl20; public int Anzahl20 { get { return this._anzahl20; } set { this._anzahl20 = value; } } private int _anzahl10; public int Anzahl10 { get { return this._anzahl10; } set { this._anzahl10 = value; } } private int _anzahl5; public int Anzahl5 { get { return this._anzahl5; } set { this._anzahl5 = value; } } private string _result; public string Result { get { return this._result; } set { this._result = value; OnPropertyChanged(); } } public ICommand Cmd { get { return new RelayCommand(CmdExec); } } private void CmdExec(object obj) { Result = string.Empty; var pruefung1 = geld.Pruefen1(Betrag, this._anzahl20, this._anzahl10, this._anzahl5); if (!pruefung1) { Result = "Falscher Betrag oder nicht durch 5 teilbar"; return; } var pruefung2 = geld.Pruefen2(Betrag, this._anzahl20, this._anzahl10, this._anzahl5); if (!pruefung2) { Result = "Falsche Stückelung, Stückelung neu berechnet"; geld.Stueckelung(Betrag, ref this._anzahl20, ref this._anzahl10, ref this._anzahl5); } OnPropertyChanged(string.Empty); } #region OnPropertyChanged public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged([CallerMemberName] string propName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName)); #endregion } internal class Window50Geld { /// <summary> /// Eingabewerte prüfen /// </summary> internal bool Pruefen1(decimal betrag, int anzahl20, int anzahl10, int anzahl5) => betrag > 0 && betrag % 5 == 0; /// <summary> /// Stückelung prüfen /// </summary> internal bool Pruefen2(decimal betrag, int anzahl20, int anzahl10, int anzahl5) => betrag == anzahl20 * 20 + anzahl10 * 10 + anzahl5 * 5; /// <summary> /// Stückelung ermitteln /// </summary> internal void Stueckelung(decimal betrag, ref int anzahl20, ref int anzahl10, ref int anzahl5) { anzahl20 = (int)(betrag / 20);
anzahl10 = (int)((betrag - anzahl20 * 20) / 10);
anzahl5 = (int)((betrag - anzahl20 * 20 - anzahl10 * 10) / 5); } } }
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks
- Bearbeitet Peter Fleischer Dienstag, 24. April 2018 10:00 Code korrigiert
- Als Antwort vorgeschlagen Ivan DragovMicrosoft contingent staff, Moderator Donnerstag, 26. April 2018 08:49
- Als Antwort markiert Ivan DragovMicrosoft contingent staff, Moderator Dienstag, 8. Mai 2018 06:39
Alle Antworten
-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace richtige__fragezeichen
{
class Program
{
static void Main(string[] args)
{
int eingabe, geldb, s5 = 0, s10 = 0, s20 = 0;
int zw1, zw2, zw3;
int manzwan, manzehn, manfünf;
int manzwan1, manzehn1, manfünf1;
Console.Write(" Geben Sie den gewünschten Geldbetrag ein: ");
geldb = Convert.ToInt32(Console.ReadLine());
if (geldb % 5 != 0)
{
do
{
Console.Write(" Geben Sie den gewünschten Geldbetrag ein: ");
geldb = Convert.ToInt32(Console.ReadLine());
}
while (geldb % 5 != 0);
}
Console.Write(" Geben Sie aut für Automatisch 1 und für manuell 2 ein: ");
eingabe = Convert.ToInt32(Console.ReadLine());
if (eingabe == 1)
{
if (geldb - 20 >= 20)
{
Teilzwanzig(ref geldb, ref s20);
}
if (geldb - 10 >= 5)
{
Teilzehn(ref geldb, ref s10);
}
if (geldb - 5 >= 0)
{
Teilfünf(ref geldb, ref s5);
}
Console.Write(" es sind " + s20 + " 20er scheine" + s10 + " 10er scheine" + s5 + " 5er scheine ");
Console.ReadLine();
}
if (eingabe == 2)
{
Console.Write(" 20er: ");
manzwan = Convert.ToInt32(Console.ReadLine());
Manzwan1rechnung(manzwan, eingabe);
}
}
static void Teilzwanzig(ref int _b1, ref int _b2)
{
do
{
int ze1;
ze1 = _b1 - 20;
_b1 = _b1 - 20;
_b2 = _b2 + 1;
} while (_b1 >= 20);
}
static void Teilzehn(ref int _b1, ref int _b2)
{
do
{
int ze1;
ze1 = _b1 - 10;
_b1 = _b1 - 10;
_b2 = _b2 + 1;
} while (_b1 >= 10);
}
static void Teilfünf(ref int _b1, ref int _b2)
{
do
{
int ze1;
ze1 = _b1 - 5;
_b1 = _b1 - 5;
_b2 = _b2 + 1;
} while (_b1 >= 5);
}
static void Manzwan1rechnung (int _b1, int _b2)
{
int manzwan1;
manzwan1 = _b1 * _b2;
}
}Das ist das Programm bis jetzt
-
Hi Marc,
hier mal ein Beispiel einer WPF-Oberfläche:XAML:
<Window x:Class="WpfApp1CS.Window50" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1CS" mc:Ignorable="d" Title="Geldautomat" Height="300" Width="300"> <Window.Resources> <local:Window50VM x:Key="vm"/> <Style TargetType="TextBox"> <Setter Property="Margin" Value="5"/> </Style> </Window.Resources> <Grid DataContext="{StaticResource vm}"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="Gewünschter Betrag"/> <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Betrag}"/> <Label Grid.Row="1" Grid.Column="0" Content="Anzahl 20 €"/> <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Anzahl20}"/> <Label Grid.Row="2" Grid.Column="0" Content="Anzahl 10 €"/> <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Anzahl10}"/> <Label Grid.Row="3" Grid.Column="0" Content="Anzahl 5 €"/> <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding Anzahl5}"/> <Button Grid.Row="4" Grid.Column="1" Content="Geld ausgeben" Command="{Binding Cmd}" Margin="5"/> <Label Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" Content="{Binding Result}"/> </Grid> </Window>
Und dazu der Code ohne die Berechnungen, die Du ja vermutlich schon gelöst hast. Deinen Code habe ich nicht geprüft.
using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Input; namespace WpfApp1CS { public class Window50VM : INotifyPropertyChanged { public Window50VM() { geld = new Window50Geld(); } private Window50Geld geld; public decimal Betrag { get; set; } private int _anzahl20; public int Anzahl20 { get { return this._anzahl20; } set { this._anzahl20 = value; } } private int _anzahl10; public int Anzahl10 { get { return this._anzahl10; } set { this._anzahl10 = value; } } private int _anzahl5; public int Anzahl5 { get { return this._anzahl5; } set { this._anzahl5 = value; } } private string _result; public string Result { get { return this._result; } set { this._result = value; OnPropertyChanged(); } } public ICommand Cmd { get { return new RelayCommand(CmdExec); } } private void CmdExec(object obj) { Result = string.Empty; var pruefung1 = geld.Pruefen1(Betrag, this._anzahl20, this._anzahl10, this._anzahl5); if (!pruefung1) { Result = "Falscher Betrag oder nicht durch 5 teilbar"; return; } var pruefung2 = geld.Pruefen2(Betrag, this._anzahl20, this._anzahl10, this._anzahl5); if (!pruefung2) { Result = "Falsche Stückelung, Stückelung neu berechnet"; geld.Stueckelung(Betrag, ref this._anzahl20, ref this._anzahl10, ref this._anzahl5); } OnPropertyChanged(string.Empty); } #region OnPropertyChanged public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged([CallerMemberName] string propName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName)); #endregion } internal class Window50Geld { /// <summary> /// Eingabewerte prüfen /// </summary> internal bool Pruefen1(decimal betrag, int anzahl20, int anzahl10, int anzahl5) => betrag > 0 && betrag % 5 == 0; /// <summary> /// Stückelung prüfen /// </summary> internal bool Pruefen2(decimal betrag, int anzahl20, int anzahl10, int anzahl5) => betrag == anzahl20 * 20 + anzahl10 * 10 + anzahl5 * 5; /// <summary> /// Stückelung ermitteln /// </summary> internal void Stueckelung(decimal betrag, ref int anzahl20, ref int anzahl10, ref int anzahl5) { anzahl20 = (int)(betrag / 20);
anzahl10 = (int)((betrag - anzahl20 * 20) / 10);
anzahl5 = (int)((betrag - anzahl20 * 20 - anzahl10 * 10) / 5); } } }
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks
- Bearbeitet Peter Fleischer Dienstag, 24. April 2018 10:00 Code korrigiert
- Als Antwort vorgeschlagen Ivan DragovMicrosoft contingent staff, Moderator Donnerstag, 26. April 2018 08:49
- Als Antwort markiert Ivan DragovMicrosoft contingent staff, Moderator Dienstag, 8. Mai 2018 06:39