[JAVA]模拟自动售货机

Posted yccy1230

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[JAVA]模拟自动售货机相关的知识,希望对你有一定的参考价值。

package class4_4;

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

import javax.swing.JOptionPane;

public class Machine 

	private static ArrayList<Drinks> drinks = new ArrayList<Drinks>();
	private static double inputMoney;

	private static void adminUI() 
		// TODO Auto-generated method stub
		System.out.println("MENU:");
		System.out.println("【1】向售货机添加一种饮料");
		System.out.println("【2】查看售货机饮料的剩余信息");
		System.out.println("【3】修改某种饮料的产品信息");
		System.out.println("【0】退出管理员系统");
	

	private static void userUI() 
		// TODO Auto-generated method stub
		System.out.println("MENU:");
		System.out.println("【1】查看所有饮料的信息");
		System.out.println("【2】购买某种饮料");
		System.out.println("【0】退出用户系统");
	

	public static void main(String[] args) 
		drinks.clear();
		while (true) 
			System.out.println("请输入你的身份:");
			System.out.println("【1】管理员");
			System.out.println("【2】用户");
			System.out.println("【0】退出系统");
			Scanner in = new Scanner(System.in);
			int identity = Integer.parseInt(in.nextLine());
			while (identity != 1 && identity != 2 && identity != 0) 
				System.out.print("您的输入有误,请重新输入:");
				identity = Integer.parseInt(in.nextLine());
			
			
			/**
			 * 管理员系统
			 */
			if (identity == 1) 
				while (true) 
					adminUI();
					System.out.print("请输入对应选项的编号:");
					int command = Integer.parseInt(in.nextLine());
					while (command != 1 && command != 2 && command != 3 && command != 0) 
						System.out.print("您的输入有误,请重新输入:");
						command = Integer.parseInt(in.nextLine());
					
					switch (command) 
					case 1: 
						Drinks d = new Drinks();
						d.setName(JOptionPane.showInputDialog("请输入需要添加的饮料的名称:"));
						d.setPrice(Double.parseDouble(JOptionPane.showInputDialog("请输入添加该种饮料的价格:")));
						d.setNumber(Integer.parseInt(JOptionPane.showInputDialog("请输入添加该种饮料的数目:")));
						drinks.add(d);
						System.out.println("添加成功!");
						break;
					
					case 2: 
						System.out.println("饮料名称" + '\\t' + "单价" + '\\t' + "剩余量");
						for (int i = 0; i < drinks.size(); i++) 
							Drinks d = drinks.get(i);
							System.out.println(d.getName() + '\\t' + d.getPrice() + '\\t' + d.getNumber());
						
						System.out.println("****************结果如上****************");
						break;
					
					case 3: 
						String name = JOptionPane.showInputDialog("请输入需要修改饮料的名称:");
						int index = -1;
						for (int i = 0; i < drinks.size(); i++) 
							if (drinks.get(i).getName().equals(name))
								index = i;
						
						if (index == -1)
							System.out.println("售货机中不存在" + name);
						else 
							Drinks d = drinks.get(index);
							System.out.println("饮料名称" + '\\t' + "单价" + '\\t' + "剩余量");
							System.out.println(d.getName() + '\\t' + d.getPrice() + '\\t' + d.getNumber());
							d.setPrice(Double.parseDouble(JOptionPane.showInputDialog("请输入修改后该种饮料的价格:")));
							d.setNumber(Integer.parseInt(JOptionPane.showInputDialog("请输入修改后该种饮料的数目:")));
							drinks.remove(index);
							drinks.add(d);
							System.out.println("修改成功!");
						
						break;
					
					
					if (command == 0)
						break;
				
			
			
			/**
			 * 用户系统
			 */
			else if (identity == 2) 
				while(true)
					userUI();
					int command = Integer.parseInt(in.nextLine());
					while (command != 1 && command != 2 && command != 0) 
						System.out.print("您的输入有误,请重新输入:");
						command = Integer.parseInt(in.nextLine());
					
					switch (command) 
					case 1:
						System.out.println("饮料名称" + '\\t' + "单价" + '\\t' + "剩余量");
						for (int i = 0; i < drinks.size(); i++) 
							Drinks d = drinks.get(i);
							System.out.println(d.getName() + '\\t' + d.getPrice() + '\\t' + d.getNumber());
						
						System.out.println("****************结果如上****************");
						break;
					
					case 2:
						String name = JOptionPane.showInputDialog("请输入需要购买饮料的名称:");
						int index = -1;
						for (int i = 0; i < drinks.size(); i++) 
							if (drinks.get(i).getName().equals(name))
								index = i;
						
						if (index == -1)
							System.out.println("售货机中不存在" + name);
						else 
							Drinks d = drinks.get(index);
							System.out.println("饮料名称" + '\\t' + "单价" + '\\t' + "剩余量");
							System.out.println(d.getName() + '\\t' + d.getPrice() + '\\t' + d.getNumber());
							int num = Integer.parseInt(JOptionPane.showInputDialog("请输入需要购买的数量:"));
							while(num>d.getNumber())
								num = Integer.parseInt(JOptionPane.showInputDialog("余量不足!请重新输入需要购买的数量:"));
							
							d.setNumber(d.getNumber()-num);
						
							double money = Double.parseDouble(JOptionPane.showInputDialog("请投币:"));
							double needPay=d.getPrice()*num;
							while(money<needPay)
								money = Double.parseDouble(JOptionPane.showInputDialog("金额不足!请重新投币:"));
							
							drinks.remove(index);
							drinks.add(d);
							System.out.println("购买成功!找零:"+Math.round(money-needPay));
						
						break;
					
					
					if(command==0)break;
				
			
			else break;
		
	


package class4_4;

public class Drinks 
	private String name;
	private double price;
	private int number;
	public String getName() 
		return name;
	
	public void setName(String name) 
		this.name = name;
	
	public double getPrice() 
		return price;
	
	public void setPrice(double price) 
		this.price = price;
	
	public int getNumber() 
		return number;
	
	public void setNumber(int number) 
		this.number = number;
	




可能存在一些问题,不作细调。

以上是关于[JAVA]模拟自动售货机的主要内容,如果未能解决你的问题,请参考以下文章

01模拟ATM机界面程序

python3之模拟商场购物

Tyche 2147 旅行

单据体新增界面,单价与金额字段自动进行合计

最短剩余时间优先:Java多线程

steam一款游戏单价50卖出去250万份 去掉平台抽成 赚多少?