项目描述:这个小项目类似于大一的语言大作业。一个简单的练习系统。
主要要求:使用集合知识,功能完善,具有异常处理功能。
主要功能:1.查看水果列表。
2.购买水果。
3.查看购物车。(输入水果名称或者水果编号)
4.结账。(清空购物车)
5.退出。
主要思路:
利用循环和switch打出框架。
利用集合进行水果列表和购物车的储存。
购买水果时将水果的花费,并和该水果对象一起添加到购物车中。
结账时将水果的总价增加。并new一个新的购物车地址实现购物车的清空。
package com.pdsu.buyfruit;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TsetBuyfruit {
public static void main(String[] args) {
ArrayList<Fruit> fruitList = new ArrayList<Fruit>();
ArrayList<Fruit> shoppingcarList = new ArrayList<Fruit>();
Fruitadd(fruitList);
boolean go_on = true;
boolean clear = false;
while(go_on) {
//打印主菜单;
Tool.view();
Scanner scan = new Scanner(System.in);
int turn = 0;
try{
turn = scan.nextInt();
}
catch(InputMismatchException e) {
//System.out.println("请按要求选择输入!!");
}
switch(turn)
{
case 1://展示水果列表
Fruit.display(fruitList);
break;
case 2://购买水果
Buyfruit.gofruit(fruitList, shoppingcarList);
break;
case 3://查看购物车
Shoppingcar.display(shoppingcarList);
break;
case 4://结账
Shoppingcar.checkout(fruitList, shoppingcarList, scan);
shoppingcarList = new ArrayList<Fruit>();//清空购物车
break;
case 5://退出
go_on = false;
System.out.println("感谢使用!!");
break;
default:
System.out.println("请按要求选择输入!!");
}
}
}
public static void Fruitadd(ArrayList <Fruit> fruitList){
//水果列表赋值
fruitList.add(new Fruit("1","香蕉",4.5,1235));
fruitList.add(new Fruit("2","红薯",3.5,1324));
fruitList.add(new Fruit("3","黄瓜",8.8,1465));
fruitList.add(new Fruit("4","甘蔗",666,4563));
fruitList.add(new Fruit("5","橘子",2.2,5451));
fruitList.add(new Fruit("6","猕猴桃",7.4,45343));
fruitList.add(new Fruit("7","火龙果",3.3,8345));
fruitList.add(new Fruit("8","橙子",8.8,458));
fruitList.add(new Fruit("9","葡萄",9.9,6145));
fruitList.add(new Fruit("10","山竹",23.9,4345));
fruitList.add(new Fruit("11","榴莲",59.9,1352));
fruitList.add(new Fruit("12","可可果",666.6,666));
//fruitList.add(null);
}
}
package com.pdsu.buyfruit;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Scanner;
public class Buyfruit {
public static void gofruit(ArrayList <Fruit> fruitList,ArrayList <Fruit> shoppingcarList) {
//double sum = 0;
boolean go_on = true;
double number = 0;//购买的数量
Fruit fruitAll = new Fruit();
Fruit fruitShoppingcarAll = new Fruit();
//ArrayList <Fruit> shoppingcarList = null;
while(go_on) {
boolean anything = false;
boolean succful = false;
System.out.println("请输入要购买的水果编号或者名称:");
Scanner scan = new Scanner(System.in);
String str = scan.next();//输入水果的编号或者名字
Iterator<Fruit> iterator = fruitList.iterator();//定义迭代器对水果集合进行遍历;
Iterator<Fruit> iteratorList = shoppingcarList.iterator();
Fruit fruitBeast = new Fruit();
// while (true) {
while(iteratorList.hasNext()) { //找找购物车里有没有相应的水果
Fruit fruitShoppingcar = iteratorList.next();
if(str.equals(fruitShoppingcar.getName())||str.equals(fruitShoppingcar.getId()) ) {
succful = true;//购物车里已经有该水果了
fruitShoppingcarAll = new Fruit(fruitShoppingcar.getId(),fruitShoppingcar.getName(),fruitShoppingcar.getPrice(),0.0);
break;
}
}
if (succful == false) {
while(iterator.hasNext()) {
Fruit fruit = iterator.next();//列表水果
/*判断在这里*/ if(str.equals(fruit.getId())||str.equals(fruit.getName())) {
anything = true;//找到目标水果了
fruitAll = new Fruit(fruit.getId(),fruit.getName(),fruit.getPrice(),0.0);
fruitBeast = fruit;
break;
}
}
}
//
else {
while(iterator.hasNext()) {
Fruit fruit = iterator.next();
/*判断在这里*/ if(str.equals(fruit.getId())||str.equals(fruit.getName())) {
fruitBeast = fruit;
break;
}
}
}
if(succful == true||anything == true) {
System.out.println("请输入购买量(kg)");//输入购买的水果量
//抛个异常
while(true) {
try {
number = scan.nextDouble();
if (number <= fruitBeast.getFruitNumber() && number >= 0){
break;
}else {
System.out.println("超出库存范围");
break;
}
}
catch (InputMismatchException ime) {
System.out.println("请按要求输入数字!!");
}
}
}
if(succful == true) {
Tool.addShoppingcar(shoppingcarList,fruitShoppingcarAll,number,true);//购买重复水果时叠加已存在的水果
shoppingcarList.sort(Comparator.comparing(Fruit::getId));//排序一下
}else if(anything == true){
Tool.addShoppingcar(shoppingcarList,fruitAll,number,false);//在购物车里添加水果
shoppingcarList.sort(Comparator.comparing(Fruit::getId));//排序一下
}
else if(anything == false && succful == false) {
System.out.println("小店没有该水果,请重新选择呀!");
break;
}
//}
//}//判断是否找到水果
//}//遍历循环
// if(succful == true) {
System.out.println("继续购买吗?(Y/N)");
char temp = scan.next().charAt(0);
if(temp == 'N'||temp =='n')
go_on = false;
//}
}//购买水果循环
}
}
package com.pdsu.buyfruit;
import java.util.ArrayList;
import java.util.Iterator;
public class Fruit {
private String id;
private String name;
private double price;
// private int inventory;//水果库存
private double fruitNumber;//购买的水果量(kg)and 水果库存
public Fruit(){
}
public Fruit(String id,String name,double price,double fruitNumber ){
this.id = id;
this.name = name;
this.price = price;
this.fruitNumber = fruitNumber ;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 double getFruitNumber() {
return fruitNumber;
}
public void setFruitNumber(double fruitNumber) {
this.fruitNumber = fruitNumber;
}
public String toString() {
return id + "\t\t\t" + name + "\t\t\t" + price + "\t\t\t" ;
}
public static void display(ArrayList<Fruit> fruitList) {
Iterator<Fruit> iterator = fruitList.iterator();
System.out.println("编号"+ "\t\t\t" + "名称" +"\t\t\t" +"单价" + "\t\t\t" + "库存" );
while(iterator.hasNext()) {
Fruit fruit = iterator.next();
System.out.printf(fruit.toString() + "%.1f", fruit.getFruitNumber());
System.out.println();
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Fruit other = (Fruit) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
package com.pdsu.buyfruit;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Scanner;
//可以查看,需要遍历
public class Shoppingcar {
//展示购物车的方法
public static void display(ArrayList <Fruit> shoppingcarList) {
Iterator<Fruit> iterator = shoppingcarList.iterator();
System.out.println("编号"+ "\t\t\t" + "名称" +"\t\t\t" +"单价" + "\t\t\t" + "购买量(kg)" + "\t\t" + "花费" );
while(iterator.hasNext()) {
//Fruit fruit = iterator.next();
Fruit fruit = iterator.next();
System.out.printf("%s\t\t\t%.1f(元)\n",fruit.toString() + fruit.getFruitNumber(),(fruit.getFruitNumber() * fruit.getPrice()));
// System.out.printf("%s\t\t\t%.1f\n",fruit.toString(),(fruit.getFruitNumber() * fruit.getPrice()));
}
}
public static void checkout(ArrayList <Fruit> fruitList,ArrayList <Fruit> shoppingcarList,Scanner scan) {
Iterator<Fruit> iterator = shoppingcarList.iterator();//购物车便利
Iterator<Fruit> iteratorList = fruitList.iterator();//水果列表遍历
double money = 0;
while(iterator.hasNext()) {
Fruit fruit = iterator.next();//购物车水果
while(iteratorList.hasNext()) {
Fruit Listfruit = iteratorList.next();
if(fruit.getId().equals(Listfruit.getId())) {
// System.out.printf("%s\t\t\t%.1f\n",fruit.toString(),(fruit.getFruitNumber() * fruit.getPrice()));
Listfruit.setFruitNumber(Listfruit.getFruitNumber() - fruit.getFruitNumber());//购买后减少相应的库存
break;
}
}
money = fruit.getFruitNumber() * fruit.getPrice() + money;
}
while(true) {
System.out.printf("你好!一共消费:%.1f" , money);
System.out.println(" 元!\n请输入支付金额:(为您等候)");
double allMoney = 0;
while(true) {
try {
allMoney = scan.nextDouble();
break;
}catch(InputMismatchException e) {
System.out.println("请输入规定的类型数据!!");
}
}
if(allMoney < money) {
System.out.println("不能白嫖!!");
}else {
System.out.printf("找零给你:%.1f",(allMoney-money));
System.out.println("(元)");
break;
}
}
}
}
package com.pdsu.buyfruit;
import java.util.ArrayList;
public class Tool {
public static void view(){
System.out.println("欢迎来到水果超市");
System.out.println("**********精 品 水 果************");
System.out.println("1.查看水果列表");
System.out.println("2.购买水果(输入序号或者水果名称)");
System.out.println("3.查看购物车");
System.out.println("4.结账");
System.out.println("5.退出");
System.out.println("请输入序号:(1-5):");
}
//TemporarilyCollection.BuyList(BuyShoppingList, fruit, num);
public static void addShoppingcar(ArrayList <Fruit> shoppingcarList,Fruit fruit,double number,boolean judge) {
if(judge == false) {
shoppingcarList.add(new Fruit(fruit.getId(),fruit.getName(),fruit.getPrice(),number));
}else {
for(int i=0 ; i < shoppingcarList.size() ; i++) {
if(shoppingcarList.get(i).getId().equals(fruit.getId())) {
fruit.setFruitNumber(number + shoppingcarList.get(i).getFruitNumber());
shoppingcarList.set(i, fruit);
break;
}
}
}
}
}
代码写的很拖沓,用的知识也很基础。希望各位大佬浅喷【弱小】。