将超市小票用面向对象的思想写出来。
小票样例:
这次的小票中,我定义了三个类,N,Good,Massage。
import java.text.SimpleDateFormat;
import java.util.Date;//时间函数的包
public class N {
public static void main(String args[]) {
//小票开头信息
Message t=new Message();
t.storeName="杉木文化";
System.out.printf("%33s\n",t.storeName);
t.setDeviceNumber(123);
t.setCashierNumber(1004);
System.out.printf("机号:%d 收银员:%d\n",t.getDeviceNumber(),t.getCashierNumber());
t.setOddNumber(123196275);
System.out.printf("单号:%d\n",t.getOddNumber());
System.out.println("品名\t"+"\t单价"+"\t数量"+"\t金额");
System.out.println("-------------------------------------");
//商品信息
Good[] goods=new Good[2];
goods[0]=new Good("康师傅泡椒牛肉面",2.5,2);
goods[1]=new Good("可口可乐汽水",3,1);
for(int i=0;i<goods.length;i++) {
System.out.printf("%14s%18.2f%8d%10.2f\n",goods[i].getGoodName(),goods[i].getGoodPrice(),
goods[i].getGoodNumber(),goods[i].getTotalPrice());
}
System.out.println("-------------------------------------------");
int totalNumber=0;//总数量
double totalMoney=0.00;//总金额
for(int i=0;i<goods.length;i++) {
totalNumber+=goods[i].getGoodNumber();
totalMoney+=goods[i].getTotalPrice();
}
System.out.println("总数量:"+totalNumber+"\t\t\t"+"总金额:"+totalMoney);
double money=8.00;//实际收的钱
int m=1;
switch(m) {
case 1:
System.out.println("现金:"+money);
System.out.println("实收:"+money+"\t\t\t回找:"+(money-totalMoney));break;
case 2:
System.out.println("\t微信:"+money+"\t\t\t回找:"+(money-totalMoney));break;
case 3:
System.out.println("\t支付宝:"+money+"\t\t\t回找:"+(money-totalMoney));break;
default:
System.out.println("请重新输入:");break;
}
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//可以方便地修改日期格式
String hehe = dateFormat.format( now );
System.out.println("时间:"+hehe);
System.out.println("请保留好小票!以便查询!");
}
}
class Good {
private String goodName;
private double goodPrice;
private int goodNumber;
private double totalPrice=0.00;
public Good(String goodName, double goodPrice, int goodNumber) {
super();
this.goodName = goodName;
this.goodPrice = goodPrice;
this.goodNumber = goodNumber;
}
public String getGoodName() {
return goodName;
}
public double getGoodPrice() {
return goodPrice;
}
public int getGoodNumber() {
return goodNumber;
}
public double getTotalPrice() {
totalPrice=goodPrice*goodNumber;
return totalPrice;
}
}
class Message {
String storeName;
private int deviceNumber;//机号
private int cashierNumber;//收银员
private int oddNumber;//单机
public int getCashierNumber() {
return cashierNumber;
}
public void setCashierNumber(int cashierNumber) {
this.cashierNumber = cashierNumber;
}
public int getDeviceNumber() {
return deviceNumber;
}
public void setDeviceNumber(int deviceNumber) {
this.deviceNumber = deviceNumber;
}
public int getOddNumber() {
return oddNumber;
}
public void setOddNumber(int oddNumber) {
this.oddNumber = oddNumber;
}
}
这个是运行效果