中午点外卖,发现有的同事点的多,有的同事点的少,大家合在一起点不仅能减少配送费,达到一定价格还有满减活动,很不错。介于大伙都不是土豪,采用AA点餐,按比例付款的方式。
每个人最后应付费用 = 每个人总费用/折扣前拼单总费用*折扣后拼单的总费用。
下面是我用elipse工具写的简单java代码,解决点外卖AA付款的问题:
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<BillBean> list=new ArrayList<>();
float endPrice=0;
float totalPrice=0;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("Enter name(Over input 'N'):");
String name=br.readLine();
if(name.isEmpty()){
System.out.println("Enter name(Over input 'N'):");
}
if(name.equals("N")){
break;
}
BillBean billBean1=new BillBean();
billBean1.name=name;
while(true){
System.out.println("Enter price(Over input 'N', Clean input C):");
String price=br.readLine();
if(price.isEmpty()){
System.out.println("Enter price(Over input 'N', Clean input C):");
}
if(price.equals("N")){
break;
}
if(price.equals("")){
break;
}
if(price.equals("C")){
billBean1.total -= billBean1.costList.get(billBean1.costList.size()-1);
System.out.println("Clean the last price success");
continue;
}
billBean1.costList.add(Float.parseFloat(price));
billBean1.total += Float.parseFloat(price);
}
list.add(billBean1);
totalPrice+=billBean1.total;
System.out.println("Name:"+name+" Total price:"+billBean1.total);
}
System.out.println("Enter discount total price:");
String endTotal=br.readLine();
if(endTotal.isEmpty()){
System.out.println("Enter name(Over input 'N'):");
}
if(!endTotal.equals("N")){
endPrice=Float.parseFloat(endTotal);
}
System.out.println("------------------start------------------");
for(BillBean b: list){
System.out.println("Name:"+b.name+" Total price:"+b.total+" End total price:"+b.total/totalPrice*endPrice+"\n");
}
System.out.println("------------------end------------------");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class BillBean {
public String name;
public List<Float> costList=new ArrayList<>();
public float total=0;
}
使用步骤按照提示即可,在数字的判别上没做处理,比较脆弱。小心一点还是能用的,放张演示图片。有需要的朋友可以试试看。