todybear
Goto Top

Ticketautomat

Ich habe mal wieder eine "Hausaufgabe" bekommen und stoße wieder einmal auf meine Grenzen.

Unser Lehrer gab uns folgende Klasse vor:

"Busbahnhof"

public class Busbahnhof										
{
	public static void main (String args)				
	{
		System.out.println("Herzlich Willkomen am Busbahnhof Coesfeld!");  
		System.out.println("Was ist ihr Zielort?");  
		System.out.println("Lette, ");  
		System.out.println("Rorup, ");  
		System.out.println("oder Nottuln");	  
		
		Ticketautomat automat = new Ticketautomat();	
	}	
}

In einer zweiten Klasse sollten wir dann Attribute, Konstruktoren und Methoden bestimmen.
Grob habe ich auch verstanden, was das ist. Attribute -> Variablen festlegen; Konstruktoren -> Variablen Funktion oder Wert geben; Methoden brauch ich glaub ich nicht erklären.

Nun habe ich in der zweiten Klasse

"Ticketautomat"

schoneinmal etwas angefangen, jedoch bereitet mir das Programm noch so einige Kopfschmerzen.

import java.util.Scanner;

public class Ticketautomat																							
{
		private int price;
		// Ticketpreis
		private int amount = 0;
		// eingeworfenes Geld
		private int balance;
		//Restbetrag
		boolean ortsauswahl = false;
		//Eingabe Ortsauswahl un/zulässig
		boolean child = false;
		// Eingabe Kind/Erwachsener un/zulässig
		
		Scanner scanner = new Scanner(System.in);
		String destination = scanner.nextLine();
		
	public Ticketautomat()																							
	{
		/**
		 * @param ortsauswahl: Passende Ortsangabe
		 */
		while (ortsauswahl == true)	
		{
			if (destination.equals("Lette")	||  
				destination.equals("Rorup")	||  
				destination.equals("Nottuln"))  
			{
				ortsauswahl = true;
					break;
			}
			/**
			 * sonst Fehler und erneuter Eingabeversuch
			 */
			else
			{
				System.out.println("Fehler! Bitte erneut versuchen!");  
				ortsauswahl = false;
					continue;
			}
		}
			/**
			 * Preisfestlegung, abhängig von der Ortsauswahl
			 */
			if (destination.equals("Lette"))	  
			{	
				price = 200;
			}					
			else if (destination.equals("Rorup"))	  
			{	
				price = 250;
			}
			else if (destination.equals("Nottuln"))	  
			{	
				price = 300;
			}
			
		System.out.println("Sind Sie ein Kind?");		  
		String ischild = scanner.nextLine();
		
		/**
		 * @param child: Kind oder Erwachsener
		 */
		while (true)				
		{
			if (ischild.equals("ja"))		  
			{
				child = true;
					break;						
			}					
			else if (ischild.equals("nein"))	  
			{		
				child = false;
					break;							
			}
			/**
			 * sonst Fehler und erneuter Eingabeversuch
			 */
			else			
			{
				System.out.println("Falsche Eingabe! Bitte erneut eingeben");  
					continue;	
			}	
		}	
			/**
			 * Preisrabatt für Kind 20%
			 */
			if (child == true)				
			{
				price = price * 80 / 100;	
			}
			else				
			{
				price = price;	
				//kann auch weggelassen werden 
			}
		/**
		 * zu Beginn ist Restbetrag = Preis
		 */
		balance = price;

		System.out.println("Bitte werfen Sie " + price + " Eurocent ein.");	  
		
		/**
		 * @param coin: nur 10, 20, 50, 100, 200 Münzen zulässig
		 */
		while (amount <= price)														
		{
		int coin = scanner.nextInt();
			if (coin == 10	||
				coin == 20	||
				coin == 50	||
				coin == 100	||
				coin == 200)			
			{														
				amount = amount + coin;
				//Geld, das eingeworfen wird, wird auf den Zähler gerechnet
				balance = balance - coin;
				//Geld, das eingeworfen wird, wird vom Restbetrag abgezogen
			}					
			else															
			{
				System.out.println("Bitte nur passende Münzen einwerfen!");	  
			}
			/**
			 * Ist der Betrag bezahlt worden, wird ein Ticket ausgedruckt
			 */
			if (balance <= 0)											
			{
				System.out.println("Der Restbetrag beträgt: 0 Eurocent.");  
				System.out.println("Ihr Restgeld beträgt: " + balance + " Eurocent.");								  
				System.out.println("++++++++++++++++++++");  
				System.out.println("+++++Busbahnhof+++++");  
				System.out.println("++++++Coesfeld++++++");  
					if  (destination.equals("Lette") ||  
						destination.equals("Rorup"))						  
					{
						System.out.println("+++Richtung " + destination + "+++");	  
					}
					else 												
					{
						System.out.println("++Richtung " + destination + "++");	  
					}
					if (child == true)						
					{
						System.out.println("++++++++Kind++++++++");	  
						//Kinderticket
					}
					else if (child == false)				
					{
						System.out.println("+++++Erwachsener++++");	  
					}
						System.out.println("++++" + price + " Eurocent++++");	  
						System.out.println("++++++++++++++++++++");		  
					}
					/**
					 * sonst wird weiterhin Geldeinwurf erwartet.
					 */
					else																			
					{
						System.out.println("Der Restbetrag beträgt: " + balance + " Eurocent.");	  
					}	
			}	
		} 	
}

Ortsauswahl, Kind/Erwachsener und Geldeinwurf geschieht über die Konsole.

Zu den break/continue Schleifen habe ich schon Rat gesucht und es funktionierte auch schon; mittlerweile nicht mehr. :,(

-> Bei jeder Auswahl soll er wieder an den Anfang der Schleife gelangen
-> man soll zu jedem Zeitpunkt auch "abbrechen" eingeben können und das Programm soll wieder an den start gesetzt werden
-> die erste Klasse ist sehr leer; ich wollte Sie als "Oberfläche" nutze - also das, was der Nutzer sehen kann, wenn er den Automaten bedient

Nach Möglichkeit bitte keine vollständigen Codes, da ich Wert auf den Selbstlerneffekt lege. Anregungen und Stichwörter helfen mir deutlich mehr.

Content-Key: 321031

Url: https://administrator.de/contentid/321031

Printed on: April 24, 2024 at 18:04 o'clock

Member: Todybear
Todybear Nov 16, 2016 at 12:32:27 (UTC)
Goto Top
Soweit so gut. Bis hierhin lässt sich das Programm problemlos durchführen. Meine Anliegen bleibt bestehen, ob und wie ich die erste Klasse besser einbinden kann? Bis jetzt besitzt sie gar keine Aufgabe.
Weiterhin will ich noch einen Return-Button haben - Eingabe-Befehl "Stop", um das Programm neu zu starten.

Klasse 1:
public class Busbahnhof										
{
	public static void main (String args)				
	{
		System.out.println("Welcome to the Busstation in Coesfeld!");  
		System.out.println("What is your destination?");  
		System.out.println("Is it Lette, Rorup or Nottuln?");  
		
		Ticketautomat automat = new Ticketautomat();	
	}	
}

Klasse 2:

import java.util.Scanner;

public class Ticketautomat																							
{
	Scanner scanner = new Scanner(System.in);
	
		//the price of a ticket from this machine
		private int price;
		//the amount of money entered so far
		private int amount;
		//the amount of money the customer has yet to pay
		private int balance;
		//the inserted coin
		public int coin;
		//right or wrong input (destination)
		boolean togo;
		//child or adult
		boolean child;
		// destination? (input)
		String destination;
		// child? (input)
		String ischild;

	public Ticketautomat()
	{
		while(true)
		{
			destination = scanner.nextLine();
			/**
			 * price by destination
			 */
			if (destination.equals("Lette"))  
			{
				price = 200;
				togo = true;
					break;
			}
			else if (destination.equals("Rorup"))  
			{
				price = 250;
				togo = true;
					break;
			}
			else if (destination.equals("Nottuln"))  
			{
				price = 300;
				togo = true;
					break;
			}
			else
			{
				togo = false;
				System.out.println("Error! Wrong input!");  
					continue;
			}
		}
		
		System.out.println("Are you a child or an adult?");		  

		while (true)				
		{
			ischild = scanner.nextLine();
			
			if (ischild.equals("child"))		  
			{
				child = true;
				price = price * 80 / 100;
					break;						
			}					
			else if (ischild.equals("adult"))	  
			{		
				child = true;
					break;							
			}		
			else			
			{
				child = false;
				System.out.println("Error! Wrong input!");  
					continue;	
			}	
		}	

		balance = price;

		System.out.println("Please insert " + price + " cents.");	  
		
		while (amount <= price)														
		{
			coin = scanner.nextInt();
			
			if (coin == 10	||
				coin == 20	||
				coin == 50	||
				coin == 100	||
				coin == 200)			
			{														
				amount = amount + coin;
				balance = balance - coin;
				
				if (balance <= 0)	
				{
						System.out.println("0 cents are yet to be paid.");  
						balance = balance - 2 * balance;
						System.out.println("Your back money is " + balance + " cents");								  
						System.out.println("++++++++++++++++++++++++");  
						System.out.println("+      busstation      +");  
						System.out.println("+       Coesfeld       +");  
					if  (destination.equals("Lette") ||  
						destination.equals("Rorup"))	  
					{
						System.out.println("+  destination: " + destination + " +");	  
					}
					else 												
					{
						System.out.println("+ destination: " + destination + " +");	  
					}
					if (child == true)						
					{
						System.out.println("+         child        +");	  
					}
					else if (child == false)				
					{
						System.out.println("+         adult        +");	  
					}
						System.out.println("+       " + price + " cents      +");	  
						System.out.println("++++++++++++++++++++++++");		  
				}
			
				else
				{
						System.out.println(balance + " cents are yet to be paid");  
				}
			}					
			else															
			{
						System.out.println("Please insert valid coins only!");	  
			}					
		}	
	} 	
}
Member: Todybear
Todybear Nov 22, 2016 at 08:28:23 (UTC)
Goto Top
Klasse 1:

public class Busbahnhof										
{
	public static void main (String args)				
	{
		System.out.println("Welcome to the busstation in Coesfeld!");  
		System.out.println("What is your destination?");  
		System.out.println("Is it Lette, Rorup or Nottuln?");  
		
		//Ticketautomat automat = new Ticketautomat();	
	}	
}

Klasse 2:

import java.util.Scanner;

public class Ticketautomat																							
{
	Scanner scanner = new Scanner(System.in);
	
		//the price of a ticket from this machine
		private int price;
		//the amount of money entered so far
		private int amount;
		//the amount of money the customer has yet to pay
		private int balance;
		//the inserted coin
		public int coin;
		//right or wrong input (destination)
		boolean togo;
		//child or adult
		boolean child;
		// destination? (input)
		String destination;
		// child? (input)
		String ischild;

	public Ticketautomat()
	{
		while(true)
		{
			destination = scanner.nextLine();
			/**
			 * price by destination
			 */
			if (destination.equals("Lette"))  
			{
				price = 200;
				togo = true;
					break;
			}
			else if (destination.equals("Rorup"))  
			{
				price = 250;
				togo = true;
					break;
			}
			else if (destination.equals("Nottuln"))  
			{
				price = 300;
				togo = true;
					break;
			}	
			else
			{
				togo = false;
				System.out.println("Error! Wrong input!");  
					continue;
			}
		}
		
		System.out.println("Are you a child or an adult?");		  

		while (true)				
		{
			ischild = scanner.nextLine();
			/**
			 * child or adult
			 * - 20% if "is child" 
			 */
			if (ischild.equals("child"))		  
			{
				child = true;
				price = price * 80 / 100;
					break;						
			}					
			else if (ischild.equals("adult"))	  
			{		
				child = true;
					break;							
			}		
			else			
			{
				child = false;
				System.out.println("Error! Wrong input!");  
					continue;	
			}	
		}	

		balance = price;

		System.out.println("Please insert " + price + " cents.");	  
		
		while (amount <= price)														
		{
			coin = scanner.nextInt();

			/**
			 * valid coins: 10, 20, 50, 100, 200
			 */
			if (coin == 10	||
				coin == 20	||
				coin == 50	||
				coin == 100	||
				coin == 200)			
			{														
				amount = amount + coin;
				balance = balance - coin;
				
				if (balance <= 0)	
				{
						/**
						 * print ticket
						 */
						System.out.println("0 cents are yet to be paid.");  
						balance = balance - 2 * balance;
						System.out.println("Your back money is " + balance + " cents.");								  
						System.out.println("++++++++++++++++++++++++");  
						System.out.println("+ busstation Coesfeld  +");  
					if  (destination.equals("Lette") ||  
						destination.equals("Rorup"))	  
					{
						System.out.println("+ destination: " + destination + "   +");	  
					}
					else 												
					{
						System.out.println("+ destination: " + destination + " +");	  
					}
					if (child == true)						
					{
						System.out.println("+ child                +");	  
					}
					else if (child == false)				
					{
						System.out.println("+ adult                +");	  
					}
						System.out.println("+ price: " + price + " cents     +");	  
						System.out.println("++++++++++++++++++++++++");		  
				}
			
				else
				{
						System.out.println(balance + " cents are yet to be paid.");  
				}
			}					
			else															
			{
						System.out.println("Please insert valid coins only!");	  
			}					
		}	
	} 	
}
Mitglied: 131381
131381 Nov 22, 2016 updated at 09:03:47 (UTC)
Goto Top
So'n Schiet, wie komm ich jetz von hier nach Brenderup, ### Automat *dagegen tret*. face-big-smile Uups jetzt steht da, "unhandled exception error"...

Gruß