Maszyna do napojów - switch opcja.

0

Witam wszystkich

Mam maly problem nie wiem co dokladnie zrobic, chodzi mi o zakonczenie aplikacji. Ma wyswietlic sume wszystkich rzeczy ktore kupilem oraz wyswietlic rzeczy ktore kupilem.
I nie wiem czy zrobic to za pomoca IF czy jest jakas inna mozliwosc. Tak w zasadzie to ma byc applet ale tym zajme sie pozniej teraz chcialbym aby wszystko dzialalo.

import java.util.Scanner;

class Assignment2
{
   public static void main(String [] args)
   {
  int product;
  double sum,add;
  double counter;
  Scanner myinput = new Scanner(System.in);
  product = 0;
  counter = 0;


		do
		{
			System.out.println("Press number for : ");
			System.out.println("1. Coffee 2.00 euro ");
			System.out.println("2. Tea 2.00 euro ");
			System.out.println("3. Soup 2.00 euro ");
			System.out.println("4. Water 1.50 euro ");
			System.out.println("5. Finish to pay");
			System.out.print("Option :");
			product = myinput.nextInt();	

			System.out.println("");

 			switch(product) 
  			{
  			case 1: System.out.println("You have chosen Coffee ");
 			product =0;
 			add = 2;
 			counter= counter + add;
 			System.out.println(" ");
 			break;
 			case 2: System.out.println("You have chosen Tea ");
  			product =0;
  			add = 2;
  			counter= counter + add;
  			System.out.println(" ");
  			break;
  			case 3: System.out.println("You have chosen Soup  ");
  			product = 0;
  			add = 2;
  			counter= counter + add;
  			System.out.println(" ");
  			break;
  			case 4: System.out.println("You have chosen Water ");
  			product = 0;
  			add = 1.50;
  			counter= counter + add;
  			System.out.println(" ");
  			break;
			case 5: System.out.println("You have chousen Finish to pay" );
			break;
			case 6: System.out.println("Please re-enter your number");

			
		}

				while (product >= 6)
				{
				product = 0;
				counter -= product;
				System.out.println("Invalid choice, option 1-5 only!");
				System.out.print("Option :");
				product = myinput.nextInt();
				System.out.println(" ");
				}

}
				while (product != 5);
						{
						System.out.println("");
						System.out.println("You have bought: ");
						System.out.println("You have bought: "  +counter ); 
						System.out.println("");
						} 

	}
}

Dzieki z gory.
Pozdrawiam.

0

Lekcje na dziś:

  1. Klasy
  2. Kolekcje obiektów
0

Sam dopiero się uczę więc nie gwarantuje poprawności:

Wersja poprawiona:
http://pastebin.com/jfqMxgH4
http://pastebin.com/iWbDeaxh

0

Ale nadal do konca nie rozumiem jak tego uzyc :/
Głowa na maxa mi zardzewiała.

Przepraszam ale jestem administratorem sieci raczej programista nie zamierzam być.

0

Tak na oko jest ok.
Za dużo tutaj nie pozmieniałem, ale działa tak jak chciałeś.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Main
{
    public static void main(String [] args)
    {
        int product;
        double sum=0;
        List<String> items = new ArrayList<String>();
        Scanner myinput = new Scanner(System.in);


        do
        {
            System.out.println("Press number for : ");
            System.out.println("1. Coffee 2.00 euro ");
            System.out.println("2. Tea 2.00 euro ");
            System.out.println("3. Soup 2.00 euro ");
            System.out.println("4. Water 1.50 euro ");
            System.out.println("5. Finish to pay");
            System.out.print("Option :");
            product = myinput.nextInt();

            System.out.println("");

            switch(product)
            {
                case 1: System.out.println("You have chosen Coffee ");
                    items.add("Coffe");
                    sum += 2;
                    System.out.println(" ");
                    break;
                case 2: System.out.println("You have chosen Tea ");
                    items.add("Tea");
                    sum += 2;
                    System.out.println(" ");
                    break;
                case 3: System.out.println("You have chosen Soup  ");
                    items.add("Soup");
                    sum += 2;
                    System.out.println(" ");
                    break;
                case 4: System.out.println("You have chosen Water ");
                    items.add("Water");
                    sum += 1.5;
                    System.out.println(" ");
                    break;
                case 5: System.out.println("You have chousen Finish to pay" );
                    break;
                case 6: System.out.println("Please re-enter your number");

                default:
                    System.out.println("Invalid choice, option 1-5 only!");


            }

        }
        while (product != 5);
        {
            System.out.println("");
            System.out.println("You have bought: ");
            items.stream().forEach(System.out::println);
            System.out.println("You have bought: "  + items.size() + " items" );
            System.out.println("");
        }

    }
}
0

java:64: error: ')' expected
items.stream().forEach(System.out::println);
nie do konca ogarniam o co chodzi

0

Akurat żeby to działało potrzebujesz javy8, ale możesz zamienić tą linijkę na:

for (String item : items){
                System.out.println(item);
            }

i powinno śmigać na starszych wersjach.

0
List<String> items = new ArrayList<String>();

Czyli to stworzyło tabele items a później dodałeś poszczególne rzeczy do niej

items.add("Soup");

0

Moze ktos pomoc z problemem

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Ass2 extends Applet implements ActionListener
{
//declare label and textfield
Label prompt1, welcomscr;
TextField input1;

//declare variables

int choice =0;
int coffee, tea, soup, water;

	public void init()
	{
		//create label
		welcomscr = new Label("Welcome to my vending machine please choose from fallowing options ");
		add(welcomscr);
		prompt1 = new Label("Please enter number 1 to 5:");
		add(prompt1);

		//create textfield
		input1 = new TextField(10);
		//create textfield
		input1.addActionListener(this);
		add(input1);

	}


/*This paint method is called after the init() method
and is generally used to display info/graphics to the
user*/
	public void paint(Graphics g)
	 {

		g.drawString("", 70,70);


		g.drawString("1. Coffie 2 \u20ac " +coffee, 100,90);


		g.drawString("2. Tea 2 \u20ac " +tea, 100,105);


		g.drawString("3. Soup \u20ac " +soup, 100,120);


		g.drawString("4. Water 1.50 \u20ac " +water, 100,135);


		g.drawString("5. Finish and pay ", 100,150);
		}

/*This  method is called when an action happens at the
user interface.  it contains the response to the events
generated*/

	public void actionPerformed(ActionEvent e)
	{
		repaint();
		//choice = Integer.parseInt(input1.getText());
		//input1.setText("");

			switch(choice)
				{
				case 1: choice = "You have chosen Coffee";
				coffee++;
				break;
				case 2: choice = "You have chosen Tea";
				tea++;
				break;
				case 3: choice = "You have chosen Soup";
				soup++;
				break;
				case 4: choice = "You have chosen Water";
				water++;
				case 5: choice = "You have chosen to finish and pay";
				default: choice = " Please select an option between 1-5";
}



		//repaint();

	}
}

Musze jeszcze dorobic licznik ale mam maly problem.
Wyswietla mi taki blad

incompatible types
case 1: choice = "You have chosen Coffee";
^
required: int
found: String

0

Przypisujesz String do int, to chyba jasne. Nie?

0

jak zadeklaruje choice jako string to ten sam blad tylko odwrotnie wyswietla, dopiero sie ucze

1

Bo tu jakiś błąd projektowy masz straszny. choice to wybór usera - to jasne, może być jako int. W tych linijkach
case 1: choice = "You have chosen Coffee";
powinieneś mieć
case 1: answer = "You have chosen Coffee";
gdzie answer jest stringiem, który poźniej wypiszesz / jakoś dasz użytkownikowi.

0

Tak to jest jak sie o 3 w nocy siedzi na kompie. Dzieki zmienilem i dziala wszystko
Pozdrawiam.

0

pokaz maina

0

Ic3ma4n podziel sie tym co stworzyles w applecie, bo mam podobny problem. Dzieki.

1 użytkowników online, w tym zalogowanych: 0, gości: 1