当前位置:编程学习 > JAVA >>

求编程高手帮我写一个JAVA程序

现在有一份菜单给客人察看,并要计算他们的消费。
要求:客人菜时要打印出菜单,而且只需输入菜号即点菜,并要打印出客人所点的菜,最后计算客人应付的菜钱。
编号      菜名      价钱
01      扬州炒饭     5.0
02      辣子鸡丁     9.0
03      羊肉串       5.0
04      鸡汤         15.0
程序是要输入菜号就能知道菜名、菜价,我就是不会这点。。。
如果可以,希望能用一维数组做出来。我是初学者!!!!!
答得好能加分哦,谢谢!!
答案:package ddd;

public class Dish {
private String name;
private String id;
private double unit;
private int number;
private String text;
public Dish(String id,String name,double unit,int number){
this.id=id;
this.name=name;
this.unit=unit;
    this.number=number;
}
public Dish(){

}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getUnit() {
return unit;
}
public void setUnit(double unit) {
this.unit = unit;
}

public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Dish clone(){
Dish d=new Dish();
d.setId(id);
d.setName(name);
d.setUnit(unit);
d.setNumber(number);
return d;
}
}
-----------------------------------------
package ddd;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;

public class Order {

private String user;
public List<Dish> list;
private double sumMoney;
private String date;
public Order(String user){
this.user=user;
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date=new Date(System.currentTimeMillis());
this.date=format.format(date);
list=new LinkedList<Dish>();
}
public List<Dish> getList() {
return list;
}
public void setList(List<Dish> list) {
this.list = list;
}
public double getSumMoney() {
return sumMoney;
}
public void setSumMoney(double sumMoney) {
this.sumMoney = sumMoney;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public void printOrder(){
  System.out.println("--------------------------------------------------------------");
  System.out.println("+用户名:"+user+"+");
  System.out.println("+消费日期:"+date+"+");
  System.out.println("--------------------------------------------------------------");
  System.out.println("编号      \t      名称      \t 价格(元) \t数量     \t 合计(元)");
  for(Dish dish:list){
      System.out.printf("%-5s \t  %-12s  \t %-5s \t        %-2s    \t    %-5s\n",dish.getId(),dish.getName(),dish.getUnit(),dish.getNumber(),dish.getUnit()*dish.getNumber());
  }
  System.out.println("--------------------------------------------------------------");
  System.out.println("+消费合计:"+sumMoney+"+");
}
}
--------------------------------------------
package ddd;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
private Map<String,Dish> map;
private Order order;
public Main(String username){
map=new LinkedHashMap<String,Dish>();
map.put("01",new Dish("01","扬州炒饭",5.0,10));
map.put("02",new Dish("02","辣子鸡丁",9.0,10));
map.put("03",new Dish("03","羊肉串 ",5.0,10));
map.put("04",new Dish("04","鸡汤 ",15.0,10));
map.put("05",new Dish("05","红烧排骨",25.0,10));
map.put("06",new Dish("06","清蒸鲤鱼 ",35.0,10));
map.put("07",new Dish("07","杂酱面 ",8.0,10));
map.put("08",new Dish("08","绿豆粥 ",2.0,10));

order=new Order(username);

}
public void printDishList(){
System.out.println("-------------------菜    谱-------------------");
System.out.println("编号      \t      名称      \t 价格(元) \t数量");
System.out.println("--------------------------------------------");
for(Dish dish:map.values()){
  System.out.printf("%-5s \t  %-12s  \t %-5s \t        %-2s\n",dish.getId(),dish.getName(),dish.getUnit(),dish.getNumber());
}
System.out.println("--------------------------------------------");
}
public static void main(String[] str) throws IOException{
Main main=new Main("佚名");
Scanner s=new Scanner(System.in);
main.printDishList();
while(true){
  String id="";
 
  System.out.print("请输入菜单编号:");
  id=s.nextLine();
 
  if(!main.map.containsKey(id)) {System.out.println("没有该菜名编号!");continue;}
  Dish dish=main.map.get(id).clone();
  System.out.println(dish.getName());
  System.out.print("请输入所要菜名【"+dish.getName()+"】的数量(0表示全部都要;必须小于等于"+dish.getNumber()+"):");
  int number=Integer.parseInt(s.nextLine());
  if(number<0 || number>dish.getNumber()){System.out.println("数量输入错误!");continue;}
  else if(number==0) ;
  else dish.setNumber(number);
  main.order.setSumMoney(main.order.getSumMoney()+dish.getNumber()*dish.getUnit());
  main.order.list.add(dish);
  System.out.println("一种菜品添加成功!继续直接any key;结束输入exit并回车");
  String tag=s.nextLine();
  if("exit".equalsIgnoreCase(tag)) break;
 
}
main.order.printOrder();
}
}

上一个:JAVA 变量配置环境后如何运行
下一个:孙卫琴tomcat与Java web开发技术详解

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,