在课堂上,一位老师提出制作一个手机销售系统。该系统在控制台显示,用户需要在控制台输入信息,方可完成手机销售环节。
代码展示 手机类型类class iphone{
String iphoneModel;
int Quantity;
double Price;
public iphone(String iphoneModel) {
this.iphoneModel = iphoneModel;
}
public String getIphoneModel() {
return iphoneModel;
}
public String setIphoneModel(String IphoneModel) {
iphoneModel = IphoneModel;
return iphoneModel;
}
public int getQuantity() {
return Quantity;
}
public int setQuantity(int quantity) {
Quantity = quantity;
return Quantity;
}
public double getPrice() {
return Price;
}
public double setPrice(double price) {
Price = price;
return price;
}
}
发货地址
class DeliveryInfo{
String DeliveryAddress;
String PostalCode;
String Country;
public String getDeliveryAddress() {
return DeliveryAddress;
}
public String setDeliveryAddress(String deliveryAddress) {
DeliveryAddress = deliveryAddress;
return DeliveryAddress;
}
public String getPostalCode() {
return PostalCode;
}
public String setPostalCode(String postalCode) {
PostalCode = postalCode;
return PostalCode;
}
public String getCountry() {
return Country;
}
public String setCountry(String country) {
Country = country;
return Country;
}
}
订单类
class IphoneOrder extends iphone {
double cost;
public IphoneOrder(String iphoneModel) {
super(iphoneModel);
}
public int getQuantityofOrder() {
return this.Quantity;
}
public void setQuantityofOrder(int quantityofOrder) {
this.Quantity = quantityofOrder;
}
public double getCost() {
return cost;
}
public double setCost(double cost1) {
cost = cost1;
return cost;
}
}
界面
//用户登录界面
System.out.println("Welcome to iPhones online Service");
System.out.println("-----------------------------");
System.out.println("Enter three iPhones to be sold");
//手机型号录入
Scanner scanner=new Scanner(System.in);
linkedList linkedList=new linkedList();
for (int i = 1; i <4; i++) {
System.out.print(i+". ");
iphone iphone=new iphone(scanner.nextLine());
linkedList.add(i-1,iphone);
}
System.out.println();
//货源信息
DeliveryInfo deliveryInfo=new DeliveryInfo();
System.out.println("Some other info" );
System.out.println("---------------");
System.out.print("Delivery address: ");
deliveryInfo.setDeliveryAddress(scanner.nextLine());
System.out.print("Postal Code: ");
deliveryInfo.setPostalCode(scanner.nextLine());
System.out.print("Country: ");
deliveryInfo.setCountry(scanner.nextLine());
System.out.println();
//手机价格和数量录入
for (int i = 1; i <4; i++) {
System.out.printf("Enter the quantities and price of %14s:",linkedList.get(i-1).getIphoneModel());
// System.out.print("Enter the quantities and price of"+linkedList.get(i-1).getIphoneModel()+": ");
linkedList.get(i-1).setQuantity(scanner.nextInt());
linkedList.get(i-1).setPrice(scanner.nextDouble());
}
System.out.println();
//手机库存汇总
System.out.println("Summary of iPhones");
System.out.println("----------------");
System.out.println("iPhone Quantity Prices");
System.out.println("-----------------------------------------------------");
for (int i = 1; i <4; i++) {
System.out.printf("%-30s",linkedList.get(i-1).iphoneModel);
System.out.printf("%-15d",linkedList.get(i-1).getQuantity());
System.out.printf("%.2fn",linkedList.get(i-1).getPrice());
}
System.out.println("-----------------------------------------------------");
// 价格表
System.out.println("Summary of iPones after the swaps");
System.out.println("--------------------------------");
swap(0,1,linkedList);
System.out.println("iPhone Quantity Prices");
System.out.println("-----------------------------------------------------");
for (int i = 1; i <4; i++) {
System.out.printf("%-30s",linkedList.get(i-1).iphoneModel);
System.out.printf("%-15d",linkedList.get(i-1).getQuantity());
System.out.printf("%.2fn",linkedList.get(i-1).getPrice());
}
System.out.println("-----------------------------------------------------");
//填写订单
System.out.println("Please place your order");
System.out.println("---------------------");
linkedList linkedList1=new linkedList();
for (int i = 1; i <4; i++) {
System.out.print("No of ");
System.out.print(linkedList.get(i-1).iphoneModel);
System.out.print(": ");
IphoneOrder iphoneOrder=new IphoneOrder(String.valueOf(linkedList.get(i-1)));
iphoneOrder.setQuantityofOrder(scanner.nextInt());
Double cost=iphoneOrder.Quantity*linkedList.get(i-1).getPrice();
iphoneOrder.setCost(cost);
linkedList1.add(i-1,iphoneOrder);
}
//订单汇总
System.out.println("Summary of your order");
System.out.println("---------------------");
System.out.println("iPhone Quantity Cost");
System.out.println("-----------------------------------------------------");
for (int i = 1; i <4; i++) {
System.out.printf("%-30s",linkedList.get(i-1).iphoneModel);
System.out.printf("%-15d",linkedList1.get(i-1).getQuantity());
System.out.printf("%.2fn",linkedList1.get(i-1).getCost());
}
System.out.println("-----------------------------------------------------");
//清理库存
System.out.println("Balance report");
System.out.println("--------------");
System.out.println("iPhone Quantity Sold Balance");
System.out.println("------------------------------------------------------------------------------------------------");
for (int i = 1; i <4; i++) {
System.out.printf("%-45s",linkedList.get(i-1).iphoneModel);
System.out.printf("%-30d",linkedList.get(i-1).getQuantity());
System.out.printf("%-15d",linkedList1.get(i-1).getQuantity());
System.out.printf("%dn",(linkedList.get(i-1).getQuantity()-linkedList1.get(i-1).getQuantity()));
}
System.out.println("------------------------------------------------------------------------------------------------");
}
代码源文件下载
代码下载后,可直接运行



